Skip to content

Commit

Permalink
Moved Preference Helper Initialization to Viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
revanthkumarJ committed Dec 23, 2024
1 parent d4553ea commit 30e68fc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
22 changes: 10 additions & 12 deletions androidApp/src/main/kotlin/org/mifos/mobile/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -50,17 +48,13 @@ class HomeActivity : ComponentActivity() {
@Inject
lateinit var networkMonitor: NetworkMonitor

@Inject
lateinit var preferenceHelper: PreferencesHelper

private val viewModel: HomeActivityViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
var uiState: HomeActivityUiState by mutableStateOf(HomeActivityUiState.Loading)

// Update the uiState
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
Expand All @@ -76,6 +70,7 @@ class HomeActivity : ComponentActivity() {
}
}


enableEdgeToEdge()

setContent {
Expand All @@ -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(
Expand All @@ -124,4 +122,4 @@ class HomeActivity : ComponentActivity() {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HomeActivityUiState> = userDataRepository.userData.map {
HomeActivityUiState.Success(it)
val uiState: StateFlow<HomeActivityUiState> = combine(
userDataRepository.userData,
preferencesHelper.themeFlow
) { userData, themeState ->
HomeActivityUiState.Success(userData, themeState)
}.stateIn(
scope = viewModelScope,
initialValue = HomeActivityUiState.Loading,
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,8 +32,8 @@ import javax.inject.Singleton
*/
@Singleton
class PreferencesHelper @Inject constructor(@ApplicationContext context: Context?) {
val themeFlowState: MutableStateFlow<AppTheme>
val themeFlow: StateFlow<AppTheme> get() = themeFlowState
private val themeFlowState: MutableStateFlow<AppTheme>
val themeFlow: StateFlow<AppTheme> get() = themeFlowState.asStateFlow()

private val sharedPreferences: SharedPreferences? =
PreferenceManager.getDefaultSharedPreferences(context)
Expand Down

0 comments on commit 30e68fc

Please sign in to comment.