This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val contentResolver :ContentResolver = mock { | |
on { | |
query( | |
same(ContactsContract.CommonDataKinds.Phone.CONTENT_URI), | |
anyOrNull(), | |
anyOrNull(), | |
anyOrNull(), | |
anyOrNull()) | |
} doReturn phoneNumbersRoboCursor | |
on { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `get full contacts list`() { | |
allContactsRoboCursor.setResults( | |
arrayOf( | |
CONTACT_1_FULL, | |
CONTACT_2_FULL | |
) | |
) | |
phoneNumbersRoboCursor.setResults( | |
arrayOf( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApplication : Application(){ | |
override fun onCreate() { | |
super.onCreate() | |
if (BuildConfig.DEBUG) { | |
Timber.plant(DebugTree()) | |
} else { | |
Timber.plant(CrashReportingTree()) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} | |
} |
OlderNewer