-
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 4 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 MainState( | ||
|
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. name of this data class shouble be MainActivityScreenState but MainState is inConsistent and not the convention for naming screen state |
||
| val isDarkTheme: Boolean = false, | ||
| val hasOnBoardingShown: Boolean? = null, | ||
|
kareem-01 marked this conversation as resolved.
Outdated
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 don't see any need of nullable value for hasOnBoardingShow, because it must be initialize when the app start
Collaborator
Author
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 agree |
||
| 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( | ||
|
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 suggest to rename this to MainActivityViewModel
kareem-01 marked this conversation as resolved.
|
||
| private val appPreferencesService: AppPreferencesService | ||
| ) : BaseViewModel<MainState>(MainState()) { | ||
|
|
||
| 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,26 +1,30 @@ | ||
| package com.washingtondcsquad.tudee.di | ||
|
|
||
| import com.washingtondcsquad.tudee.presentation.features.home.HomeViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.task_details.BottomSheetTaskViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.onBoarding.OnboardingViewModel | ||
| import com.washingtondcsquad.tudee.MainViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.add_task.AddTaskViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.categories.CategoriesViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.edit_task.EditTaskViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.home.HomeViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.onBoarding.OnboardingViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.task_details.BottomSheetTaskViewModel | ||
| import com.washingtondcsquad.tudee.presentation.features.tasks_screen.TasksViewModel | ||
| import org.koin.core.module.dsl.viewModel | ||
| import org.koin.core.module.dsl.viewModelOf | ||
| import org.koin.dsl.module | ||
| import java.time.LocalDate | ||
| import com.washingtondcsquad.tudee.presentation.features.categories.CategoriesViewModel | ||
|
|
||
|
|
||
| val viewModelModule = module { | ||
| viewModelOf(::CategoriesViewModel) | ||
| viewModelOf(::OnboardingViewModel) | ||
| viewModelOf(::HomeViewModel) | ||
| viewModel{ TasksViewModel( | ||
| get(), | ||
| get() | ||
| ) } | ||
| viewModel { | ||
| TasksViewModel( | ||
| get(), | ||
| get() | ||
| ) | ||
| } | ||
| viewModelOf(::MainViewModel) | ||
| viewModel { BottomSheetTaskViewModel(get()) } | ||
| viewModel { (taskDate: LocalDate, onCancel: () -> Unit, onActionResult: (success: Boolean, message: String) -> Unit) -> | ||
| AddTaskViewModel( | ||
|
|
@@ -32,7 +36,7 @@ val viewModelModule = module { | |
| ) | ||
| } | ||
|
|
||
| viewModel { (taskId:Int , onCancel: () -> Unit , onActionResult: (success: Boolean, message: String) -> Unit) -> | ||
| viewModel { (taskId: Int, onCancel: () -> Unit, onActionResult: (success: Boolean, message: String) -> Unit) -> | ||
|
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. we changed type of task id to be TaskId alise name not int
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. check domain layer
Collaborator
Author
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. will do
Collaborator
Author
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. this concerns the EditTask feature which is out of scope for this pr so I will not be modifying it, as to not affect other's work |
||
| EditTaskViewModel( | ||
| tasksService = get(), | ||
| categoryService = get(), | ||
|
|
||
| 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) | ||
| ) | ||
|
|
||
| } | ||
| ) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.