From 30e68fc81b74f576caefc9633d52518b9c39ef9c Mon Sep 17 00:00:00 2001 From: revanthkumarJ Date: Mon, 23 Dec 2024 10:01:10 +0530 Subject: [PATCH] Moved Preference Helper Initialization to Viewmodel --- .../kotlin/org/mifos/mobile/HomeActivity.kt | 22 +++++++++---------- .../org/mifos/mobile/HomeActivityViewModel.kt | 13 ++++++++--- .../core/datastore/PreferencesHelper.kt | 5 +++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt b/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt index ea9e5afc6..d6c99cf30 100644 --- a/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt +++ b/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt @@ -17,7 +17,6 @@ import androidx.activity.viewModels import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.DisposableEffect -import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -33,7 +32,6 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import org.mifos.mobile.HomeActivityUiState.Success import org.mifos.mobile.core.data.utils.NetworkMonitor -import org.mifos.mobile.core.datastore.PreferencesHelper import org.mifos.mobile.core.designsystem.theme.MifosMobileTheme import org.mifos.mobile.core.designsystem.theme.darkScrim import org.mifos.mobile.core.designsystem.theme.lightScrim @@ -50,9 +48,6 @@ class HomeActivity : ComponentActivity() { @Inject lateinit var networkMonitor: NetworkMonitor - @Inject - lateinit var preferenceHelper: PreferencesHelper - private val viewModel: HomeActivityViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { @@ -60,7 +55,6 @@ class HomeActivity : ComponentActivity() { super.onCreate(savedInstanceState) var uiState: HomeActivityUiState by mutableStateOf(HomeActivityUiState.Loading) - // Update the uiState lifecycleScope.launch { lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.uiState @@ -76,6 +70,7 @@ class HomeActivity : ComponentActivity() { } } + enableEdgeToEdge() setContent { @@ -99,12 +94,15 @@ class HomeActivity : ComponentActivity() { onDispose {} } - val currentTheme by preferenceHelper.themeFlow.collectAsState() - val isDarkMode = when (currentTheme) { - AppTheme.DARK -> true - AppTheme.LIGHT -> false - AppTheme.SYSTEM -> isSystemInDarkMode + val isDarkMode = when (uiState) { + is Success -> when ((uiState as Success).themeState) { + AppTheme.DARK -> true + AppTheme.LIGHT -> false + AppTheme.SYSTEM -> isSystemInDarkMode + } + else -> isSystemInDarkMode } + CompositionLocalProvider { MifosMobileTheme(isDarkMode) { RootNavGraph( @@ -124,4 +122,4 @@ class HomeActivity : ComponentActivity() { } } } -} +} \ No newline at end of file diff --git a/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivityViewModel.kt b/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivityViewModel.kt index fdfc7e533..bd88118ae 100644 --- a/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivityViewModel.kt +++ b/androidApp/src/main/kotlin/org/mifos/mobile/HomeActivityViewModel.kt @@ -14,22 +14,29 @@ import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import org.mifos.library.passcode.data.PasscodeManager import org.mifos.mobile.core.data.repository.UserDataRepository +import org.mifos.mobile.core.datastore.PreferencesHelper import org.mifos.mobile.core.model.UserData +import org.mifos.mobile.core.model.enums.AppTheme import javax.inject.Inject @HiltViewModel class HomeActivityViewModel @Inject constructor( private val userDataRepository: UserDataRepository, private val passcodeManager: PasscodeManager, + private val preferencesHelper: PreferencesHelper ) : ViewModel() { - val uiState: StateFlow = userDataRepository.userData.map { - HomeActivityUiState.Success(it) + val uiState: StateFlow = combine( + userDataRepository.userData, + preferencesHelper.themeFlow + ) { userData, themeState -> + HomeActivityUiState.Success(userData, themeState) }.stateIn( scope = viewModelScope, initialValue = HomeActivityUiState.Loading, @@ -46,5 +53,5 @@ class HomeActivityViewModel @Inject constructor( sealed interface HomeActivityUiState { data object Loading : HomeActivityUiState - data class Success(val userData: UserData) : HomeActivityUiState + data class Success(val userData: UserData,val themeState: AppTheme) : HomeActivityUiState } diff --git a/core/datastore/src/main/java/org/mifos/mobile/core/datastore/PreferencesHelper.kt b/core/datastore/src/main/java/org/mifos/mobile/core/datastore/PreferencesHelper.kt index 44b846078..8c1fea1d8 100644 --- a/core/datastore/src/main/java/org/mifos/mobile/core/datastore/PreferencesHelper.kt +++ b/core/datastore/src/main/java/org/mifos/mobile/core/datastore/PreferencesHelper.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.callbackFlow import org.mifos.mobile.core.model.enums.AppTheme @@ -31,8 +32,8 @@ import javax.inject.Singleton */ @Singleton class PreferencesHelper @Inject constructor(@ApplicationContext context: Context?) { - val themeFlowState: MutableStateFlow - val themeFlow: StateFlow get() = themeFlowState + private val themeFlowState: MutableStateFlow + val themeFlow: StateFlow get() = themeFlowState.asStateFlow() private val sharedPreferences: SharedPreferences? = PreferenceManager.getDefaultSharedPreferences(context)