Skip to content

Instantly share code, notes, and snippets.

@DarkAbhi
Last active February 16, 2021 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkAbhi/fad37c15cd6c33f85f80bfc10ebfc584 to your computer and use it in GitHub Desktop.
Save DarkAbhi/fad37c15cd6c33f85f80bfc10ebfc584 to your computer and use it in GitHub Desktop.
@ExperimentalCoroutinesApi
class PostRepository {
fun getPostData(): Flow<State<PostModel>> = callbackFlow {
offer(State.Loading())
val postDocument = Firebase.firestore
.collection(AppConfig.POSTS_COLLECTION)
.document("post1")
val subscription = postDocument.addSnapshotListener { snapshot, exception ->
exception?.let {
offer(State.Failed(it.message.toString()))
cancel(it.message.toString())
}
if (snapshot!!.exists()) {
offer(State.Success(snapshot.toObject(PostModel::class.java)!!))
}
}
awaitClose { subscription.remove() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment