Skip to content

Commit

Permalink
fix(networking-feature): profile input not applied after a rotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Nov 12, 2023
1 parent ea428ac commit 18ca20f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ fun MyProfileVM(
userRepository: UserRepository,
onEditInformation: () -> Unit,
modifier: Modifier = Modifier,
viewModel: ProfileInputViewModel = viewModel(
factory = ProfileInputViewModel.Factory.create(userRepository)
viewModel: MyProfileViewModel = viewModel(
factory = MyProfileViewModel.Factory.create(userRepository)
)
) {
val uiState = viewModel.uiState.collectAsState()
when (uiState.value) {
is ProfileInputUiState.Loading -> Text(text = stringResource(id = R.string.text_loading))
is ProfileInputUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is ProfileInputUiState.Success -> {
val profileUi = (uiState.value as ProfileInputUiState.Success).profile
is MyProfileUiState.Loading -> Text(text = stringResource(id = R.string.text_loading))
is MyProfileUiState.Failure -> Text(text = stringResource(id = R.string.text_error))
is MyProfileUiState.Success -> {
val profileUi = (uiState.value as MyProfileUiState.Success).profile
if (profileUi.qrCode == null) {
EmptyNetworking()
} else {
Expand Down
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
}
}
}

0 comments on commit 18ca20f

Please sign in to comment.