Last active
February 16, 2021 14:42
-
-
Save DarkAbhi/fad37c15cd6c33f85f80bfc10ebfc584 to your computer and use it in GitHub Desktop.
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
@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