-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(networking-feature): profile input not applied after a rotation.
- Loading branch information
1 parent
ea428ac
commit 18ca20f
Showing
2 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
...ain/kotlin/org/gdglille/devfest/android/theme/m3/networking/feature/MyProfileViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.gdglille.devfest.android.theme.m3.networking.feature | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.firebase.crashlytics.ktx.crashlytics | ||
import com.google.firebase.ktx.Firebase | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.stateIn | ||
import org.gdglille.devfest.models.ui.UserProfileUi | ||
import org.gdglille.devfest.repositories.UserRepository | ||
|
||
sealed class MyProfileUiState { | ||
data object Loading : MyProfileUiState() | ||
data class Success(val profile: UserProfileUi) : MyProfileUiState() | ||
data class Failure(val throwable: Throwable) : MyProfileUiState() | ||
} | ||
|
||
class MyProfileViewModel( | ||
userRepository: UserRepository | ||
) : ViewModel() { | ||
val uiState: StateFlow<MyProfileUiState> = userRepository.fetchProfile() | ||
.map { | ||
MyProfileUiState.Success( | ||
profile = it ?: UserProfileUi( | ||
email = "", | ||
firstName = "", | ||
lastName = "", | ||
company = "", | ||
qrCode = null | ||
) | ||
) | ||
} | ||
.catch { | ||
Firebase.crashlytics.recordException(it) | ||
MyProfileUiState.Failure(it) | ||
} | ||
.stateIn( | ||
scope = viewModelScope, | ||
initialValue = MyProfileUiState.Loading, | ||
started = SharingStarted.WhileSubscribed() | ||
) | ||
|
||
object Factory { | ||
fun create(repository: UserRepository) = object : ViewModelProvider.Factory { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T = | ||
MyProfileViewModel(userRepository = repository) as T | ||
} | ||
} | ||
} |