Skip to content

Instantly share code, notes, and snippets.

View euri16's full-sized avatar

Eury Perez euri16

View GitHub Profile
@Composable
fun UserProfileScreen(viewModel: UserProfileViewModel) {
val lifecycleOwner = LocalLifecycleOwner.current
LaunchedEffect(Unit) {
viewModel.events.flowWithLifecycle(lifecycleOwner.lifecycle)
.onEach { event ->
when (event) {
UIEvent.NavigateToLogin -> {
// This might never execute if config change happens
// This sequence can lose events during configuration changes
fun deleteAccount() {
viewModelScope.launch {
try {
userRepository.deleteAccount()
// 1. Event is sent
_events.send(UIEvent.ShowToast("Account deleted successfully"))
// 2. UI receives and schedules event
// 3. Configuration change cancels collection
// 4. Event is lost - user never sees confirmation!
@Composable
fun UserProfileScreen(
viewModel: UserProfileViewModel = hiltViewModel()
) {
val context = LocalContext.current
val navController = LocalNavController.current
// Collecting events from Channel
val lifecycleOwner = LocalLifecycleOwner.current
sealed class UIEvent {
data class ShowToast(val message: String) : UIEvent()
data class ShowError(val error: String) : UIEvent()
data object NavigateToLogin : UIEvent()
}
class UserProfileViewModel(
private val userRepository: UserRepository
) : ViewModel() {
@euri16
euri16 / App.kt
Last active March 13, 2024 18:57
package dev.euryperez.loginsample
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import org.jetbrains.compose.ui.tooling.preview.Preview
@Composable
@Preview
fun App() {
implementation(project.dependencies.platform(libs.firebase.bom))
implementation(libs.firebase.crashlyticsKtx)
alias(libs.plugins.google.playServices)
alias(libs.plugins.firebase.crashlytics)
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
version = "1.0"
ios.deploymentTarget = "16.0"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "composeApp"
isStatic = true
}
sourceSets {
commonMain.dependencies {
implementation(compose.runtime)
implementation(compose.foundation)
implementation(compose.material)
implementation(compose.ui)
implementation(compose.components.resources)
implementation(compose.components.uiToolingPreview)
}
commonTest.dependencies {
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinCocoapods)
alias(libs.plugins.androidLibrary)
alias(libs.plugins.jetbrainsCompose)
}
[...]