This file contains hidden or 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
fun getAllPosts() = flow<State<List<Post>>> { | |
// Emit loading state | |
emit(State.loading()) | |
val snapshot = mPostsCollection.get().await() | |
val posts = snapshot.toObjects(Post::class.java) | |
// Emit success state with data | |
emit(State.success(posts)) |
This file contains hidden or 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
private suspend fun addPost(post: Post) { | |
viewModel.addPost(post).collect { state -> | |
when (state) { | |
is State.Loading -> { | |
showToast("Loading") | |
binding.buttonAdd.isEnabled = false | |
} | |
is State.Success -> { | |
showToast("Posted") |
This file contains hidden or 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
private suspend fun loadPosts() { | |
viewModel.getAllPosts().collect { state -> | |
when (state) { | |
is State.Loading -> { | |
showToast("Loading") | |
} | |
is State.Success -> { | |
val postText = state.data.joinToString("\n") { | |
"${it.postContent} ~ ${it.postAuthor}" |
This file contains hidden or 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
class MainViewModel(private val repository: PostsRepository) : ViewModel() { | |
fun getAllPosts() = repository.getAllPosts() | |
fun addPost(post: Post) = repository.addPost(post) | |
} |
This file contains hidden or 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
fun addPost(post: Post) = flow<State<DocumentReference>> { | |
// Emit loading state | |
emit(State.loading()) | |
val postRef = mPostsCollection.add(post).await() | |
// Emit success state with post reference | |
emit(State.success(postRef)) |
This file contains hidden or 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
sealed class State<T> { | |
class Loading<T> : State<T>() | |
data class Success<T>(val data: T) : State<T>() | |
data class Failed<T>(val message: String) : State<T>() | |
companion object { | |
fun <T> loading() = Loading<T>() | |
fun <T> success(data: T) = Success(data) | |
fun <T> failed(message: String) = Failed<T>(message) | |
} |
This file contains hidden or 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
data class Post( | |
val postContent: String? = null, | |
val postAuthor: String? = null | |
) |
This file contains hidden or 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
dependencies { | |
// Kotlin | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | |
// Kotlin Coroutines | |
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5" | |
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5" | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.5' |
This file contains hidden or 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
public class Main { | |
public static void main(String[] args) { | |
Life life = DaggerLifeComponent.create().getLife(); | |
life.enjoy(); | |
} | |
} |
This file contains hidden or 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
@Singleton | |
@Component | |
public interface LifeComponent { | |
Life getLife(); | |
} |