-
Notifications
You must be signed in to change notification settings - Fork 3
feature/91-add-main-view-model #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
b74e33e
56c9fb0
6c8e5fd
e32552a
b7054c4
a5a0bdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.washingtondcsquad.tudee | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| data class MainActivityUiState( | ||
| val isDarkTheme: Boolean = false, | ||
| val hasOnBoardingShown: Boolean = false, | ||
| val currentAppLocale: Locale = Locale.ENGLISH | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.washingtondcsquad.tudee | ||
|
|
||
| import androidx.lifecycle.viewModelScope | ||
| import com.washingtondcsquad.tudee.domain.services.AppPreferencesService | ||
| import com.washingtondcsquad.tudee.presentation.base.BaseViewModel | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.collectLatest | ||
| import kotlinx.coroutines.launch | ||
| import java.util.Locale | ||
|
|
||
| class MainViewModel( | ||
|
kareem-01 marked this conversation as resolved.
|
||
| private val appPreferencesService: AppPreferencesService | ||
| ) : BaseViewModel<MainActivityUiState>(MainActivityUiState()) { | ||
|
|
||
| init { | ||
| getIsDarkTheme() | ||
| getOnBoardingState() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the above method |
||
| getAppLocale() | ||
| } | ||
|
|
||
| private fun getIsDarkTheme() { | ||
|
kareem-01 marked this conversation as resolved.
|
||
| tryToExecute( | ||
| request = { | ||
| appPreferencesService.isDarkModeEnabled() | ||
|
kareem-01 marked this conversation as resolved.
|
||
| }, | ||
| onSuccess = ::onDarkThemeEnabled, | ||
| onError = {} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it always a good practice to handle error state, help in debugging and improve user experience |
||
| ) | ||
| } | ||
|
|
||
| private fun onDarkThemeEnabled(isDarkThemeEnabled: Flow<Boolean>) = viewModelScope.launch { | ||
| isDarkThemeEnabled.collectLatest { | ||
| updateState { | ||
| copy(isDarkTheme = it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun getOnBoardingState() { | ||
| tryToExecute( | ||
| request = { | ||
| appPreferencesService.hasOnboardingBeenShown() | ||
| }, | ||
| onSuccess = ::onOnBoardingState, | ||
| onError = {} | ||
| ) | ||
| } | ||
|
|
||
| private fun onOnBoardingState(hasOnboardingBeenShown: Flow<Boolean>) = viewModelScope.launch { | ||
| hasOnboardingBeenShown.collectLatest { | ||
| updateState { | ||
| copy(hasOnBoardingShown = it) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun getAppLocale() { | ||
| tryToCollect( | ||
| request = { | ||
| appPreferencesService.getCurrentLocale() | ||
| }, | ||
| onChange = ::onAppLanguageChanged, | ||
| onError = {}, | ||
| ) | ||
| } | ||
|
|
||
| private fun onAppLanguageChanged(newLocale: Locale) { | ||
| updateState { | ||
| copy(currentAppLocale = newLocale) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,37 @@ | ||
| package com.washingtondcsquad.tudee.data.services | ||
|
|
||
| import androidx.appcompat.app.AppCompatDelegate | ||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.core.booleanPreferencesKey | ||
| import androidx.datastore.preferences.core.edit | ||
| import com.washingtondcsquad.tudee.domain.services.AppPreferencesService | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.distinctUntilChanged | ||
| import kotlinx.coroutines.flow.map | ||
| import java.util.Locale | ||
|
|
||
| class AppPreferencesServiceImpl( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where isDarkModelEnabled function ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it exists in this file down below |
||
| private val dataStore: DataStore<Preferences> | ||
| ) : AppPreferencesService { | ||
|
|
||
| private val _currentLocale = MutableStateFlow(getCurrentAppLocale()) | ||
| val currentLocale: Flow<Locale> = _currentLocale.asStateFlow() | ||
|
|
||
| override suspend fun getCurrentLocale(): Flow<Locale> = currentLocale.distinctUntilChanged() | ||
|
|
||
|
|
||
| private fun getCurrentAppLocale(): Locale { | ||
| val androidLocale = AppCompatDelegate.getApplicationLocales()[0] | ||
| ?: Locale.getDefault() | ||
| return Locale.Builder() | ||
| .setLocale(androidLocale) | ||
| .setLanguage(androidLocale.language) | ||
| .build() | ||
| } | ||
|
|
||
| companion object PreferencesKeys { | ||
| val HAS_ONBOARDING_BEEN_SHOWN = booleanPreferencesKey("has_onboarding_been_shown") | ||
| val DARK_MODE_ENABLED = booleanPreferencesKey("dark_mode_enabled") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| package com.washingtondcsquad.tudee.domain.services | ||
|
|
||
| import kotlinx.coroutines.flow.Flow | ||
| import java.util.Locale | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to search for kotlin version . it's not recommended to use java libraries in domain layer. |
||
|
|
||
| interface AppPreferencesService { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on multiple lines |
||
| suspend fun getCurrentLocale(): Flow<Locale> | ||
| fun hasOnboardingBeenShown(): Flow<Boolean> | ||
| suspend fun setOnboardingShown() | ||
| fun isDarkModeEnabled(): Flow<Boolean> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.washingtondcsquad.tudee.presentation.utils | ||
|
|
||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.material3.SnackbarDuration | ||
| import androidx.compose.material3.SnackbarHost | ||
| import androidx.compose.material3.SnackbarHostState | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.unit.dp | ||
| import com.washingtondcsquad.tudee.R | ||
| import com.washingtondcsquad.tudee.presentation.components.SnackBarCard | ||
| import com.washingtondcsquad.tudee.presentation.components.snack_bar.ObserveAsEvent | ||
| import com.washingtondcsquad.tudee.presentation.components.snack_bar.SnackbarController | ||
| import com.washingtondcsquad.tudee.presentation.design.AppTheme | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| @Composable | ||
| fun SnackBarHandler() { | ||
| val snackBarHostState = remember { SnackbarHostState() } | ||
| val coroutineScope = rememberCoroutineScope() | ||
| ObserveAsEvent( | ||
| flow = SnackbarController.event, | ||
| onEvent = { event -> | ||
| coroutineScope.launch { | ||
| snackBarHostState.currentSnackbarData?.dismiss() | ||
| snackBarHostState.showSnackbar( | ||
| message = event.message, | ||
| duration = SnackbarDuration.Short | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| SnackbarHost( | ||
| hostState = snackBarHostState, | ||
| snackbar = { snackbarData -> | ||
| val (iconRes, iconTint) = when { | ||
| snackbarData.visuals.message.contains("success", ignoreCase = true) -> | ||
| Pair(R.drawable.checkmark, AppTheme.colors.greenAccent) | ||
|
|
||
| snackbarData.visuals.message.contains("error", ignoreCase = true) -> | ||
| Pair(R.drawable.checkmark, AppTheme.colors.error) | ||
|
|
||
| else -> | ||
| Pair(R.drawable.information_diamond, AppTheme.colors.error) | ||
| } | ||
|
|
||
| SnackBarCard( | ||
| message = snackbarData.visuals.message, | ||
| icon = painterResource(id = iconRes), | ||
| iconTint = iconTint, | ||
| iconBackgroundColor = AppTheme.colors.surface, | ||
| modifier = Modifier.padding(horizontal = 8.dp, vertical = 48.dp) | ||
| ) | ||
|
|
||
| } | ||
| ) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i suggest to rename this to MainActivityViewModel