Skip to content

Instantly share code, notes, and snippets.

@luks91
Created April 29, 2018 17:26
Show Gist options
  • Save luks91/d006c60b21589a9dd68d66be9c2b4ecc to your computer and use it in GitHub Desktop.
Save luks91/d006c60b21589a9dd68d66be9c2b4ecc to your computer and use it in GitHub Desktop.
private fun contacts(): Flowable<SimpleContact> =
Flowable.generate<SimpleContact, Cursor>(
Callable<Cursor> {
contentResolver.query(Contacts.CONTENT_URI,
arrayOf(Contacts.DISPLAY_NAME, Contacts.STARRED),
null, null,
"${Contacts.DISPLAY_NAME} ASC")
},
BiFunction<Cursor, Emitter<SimpleContact>, Cursor> { cursor, emitter ->
if (cursor.moveToNext()) {
emitter.onNext(SimpleContact.from(cursor))
} else {
emitter.onComplete()
}
return@BiFunction cursor
},
Consumer<Cursor> {
cursor -> cursor.close()
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment