Skip to content

Instantly share code, notes, and snippets.

data class ContactModel(
val id: Long,
val contactId: Long,
val photoUri: String?,
val firstName: String?,
val surname: String?,
val fullName: String?,
var phoneNumbers: Set<String> = emptySet()
)
private fun loadAllContacts(): Map<Long, ContactModel> {
val uri = ContactsContract.Data.CONTENT_URI
val selection = "${ContactsContract.Data.MIMETYPE} = ? "
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
val itemsMap = mutableMapOf<Long, ContactModel>()
contentResolver.query(
uri,
getContactProjection(),
selection,
private fun getContactProjection() = arrayOf(
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI
)
fun getAllContacts(): Single<Map<Long, ContactModel>> {
return Single.fromCallable { loadAllContacts() } // here we load all contacts
.map { addPhoneNumbers(it) } // here we add phone numbers
.map { it.filter { !it.value.phoneNumbers.isNullOrEmpty() } } // here we filter empty contacts
.map { it.values.toList().groupBy { it.contactId }.mapValues { mergePhoneNumbers(it.value) } } //here additional merging happens
}
private val CONTACTS_COLUMNS = listOf(
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME
)
val contentResolver :ContentResolver = mock {
on {
query(
same(ContactsContract.CommonDataKinds.Phone.CONTENT_URI),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull())
} doReturn phoneNumbersRoboCursor
on {
@Test
fun `get full contacts list`() {
allContactsRoboCursor.setResults(
arrayOf(
CONTACT_1_FULL,
CONTACT_2_FULL
)
)
phoneNumbersRoboCursor.setResults(
arrayOf(
companion object MockData {
private val CONTACT_1_ID = 1L
private val CONTACT_2_ID = 2L
private val CONTACT_1_FULL = arrayOf(
CONTACT_1_ID,
CONTACT_1_ID,
null,
"display name",
class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
} else {
Timber.plant(CrashReportingTree())
}
}
}
class CrashReportingTree : Timber.Tree() {
@SuppressLint("LogNotTimber")
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority == Log.DEBUG || priority == Log.ERROR) {
Log.e("ho", "Log was secured")
}
}
}