Last active
February 8, 2022 05:26
-
-
Save ShinichiroFunatsu/8c525527a55fcaec2e6483f588715e89 to your computer and use it in GitHub Desktop.
Android "Guide to app architecture : UI events" ViewModel Live Template code for Android dev. see: "https://developer.android.com/jetpack/guide/ui-layer/events"
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 UserMessage(val id: Long, val title: String, val message: String) | |
data class $name$UiState( | |
val userMessages: List<UserMessage> = kotlin.collections.emptyList(), | |
) | |
class $name$ViewModel : androidx.lifecycle.ViewModel() { | |
private val _uiState = kotlinx.coroutines.flow.MutableStateFlow($name$UiState()) | |
val uiState: kotlinx.coroutines.flow.StateFlow<$name$UiState> = _uiState | |
private fun showMessage(title: String, message: String) { | |
_uiState.update { currentUiState -> | |
currentUiState.copy( | |
userMessages = currentUiState.userMessages + userMessage(title, message) | |
) | |
} | |
} | |
fun userMessageShown(messageId: Long) { | |
_uiState.update { currentUiState -> | |
val messages = currentUiState.userMessages.filterNot { it.id == messageId } | |
currentUiState.copy(userMessages = messages) | |
} | |
} | |
} | |
private fun userMessage(title: String, message: String) = UserMessage( | |
java.util.UUID.randomUUID().mostSignificantBits, | |
title, | |
message, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment