Skip to content

Instantly share code, notes, and snippets.

Avatar
😎
Dumping my head

Abhishek AN DarkAbhi

😎
Dumping my head
View GitHub Profile
View PostActivity.kt
CoroutineScope(Dispatchers.Main).launch {
loadPost()
}
private suspend fun loadPost() {
viewModel.getPost().collect {
when (it) {
is State.Loading -> {
Timber.i("LOADING")
}
View PostViewModel.kt
@ExperimentalCoroutinesApi
class PostViewModel(private val repo: PostRepository) : ViewModel() {
fun getPost() = repo.getPostData()
}
View PostRepository.kt
@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 {
View State.kt
sealed class State<out T> {
class Loading<out T> : State<T>()
data class Success<out T>(val data: T) : State<T>()
data class Failed<out T>(val message: String) : State<T>()
}