Skip to content

Instantly share code, notes, and snippets.

@luks91
Created November 23, 2017 15:28
Show Gist options
  • Select an option

  • Save luks91/4b8662c95940cadb37f7d7e206711875 to your computer and use it in GitHub Desktop.

Select an option

Save luks91/4b8662c95940cadb37f7d7e206711875 to your computer and use it in GitHub Desktop.
data class SimpleContact(val displayName: String, val isStarred: Boolean) {
companion object {
fun from(cursor: Cursor) =
SimpleContact(
cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)),
cursor.getInt(cursor.getColumnIndex(Contacts.STARRED)) == 1)
}
}
fun contacts(contentResolver: ContentResolver): Flowable<SimpleContact> =
contentResolver.uriChangesOf(
{
itemsFor(
{ query(Contacts.CONTENT_URI,
arrayOf(Contacts.DISPLAY_NAME, Contacts.STARRED),
null, null, "${Contacts.DISPLAY_NAME} ASC") },
{ SimpleContact.from(it) }
)
}, Contacts.CONTENT_URI)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
.switchMap { obs -> obs }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment