From 3dc4300918bc14a7fb04a566d07445a97e9dfce3 Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Sun, 4 Feb 2024 11:54:30 +0530 Subject: [PATCH] Remember last filter combinations on relaunch Signed-off-by: starry-shivam --- .../screens/home/viewmodels/HomeViewModel.kt | 26 ++++++++++++++----- .../starry/greenstash/utils/PreferenceUtil.kt | 4 +++ .../starry/greenstash/widget/GoalWidget.kt | 4 +-- build.gradle | 2 +- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/starry/greenstash/ui/screens/home/viewmodels/HomeViewModel.kt b/app/src/main/java/com/starry/greenstash/ui/screens/home/viewmodels/HomeViewModel.kt index 53fc2341..95ecb00b 100644 --- a/app/src/main/java/com/starry/greenstash/ui/screens/home/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/starry/greenstash/ui/screens/home/viewmodels/HomeViewModel.kt @@ -78,25 +78,37 @@ class HomeViewModel @Inject constructor( private val _filterFlowData: MutableState = mutableStateOf( FilterFlowData( - FilterField.Title, - FilterSortType.Ascending + FilterField.entries[preferenceUtil.getInt( + PreferenceUtil.GOAL_FILTER_FIELD_INT, + FilterField.Title.ordinal + )], + FilterSortType.entries[preferenceUtil.getInt( + PreferenceUtil.GOAL_FILTER_SORT_TYPE_INT, + FilterSortType.Ascending.ordinal + )] ) ) val filterFlowData: State = _filterFlowData private val filterFlow = MutableStateFlow(filterFlowData.value) - private val goalsListFlow = filterFlow.flatMapLatest { - when (it.filterField) { + private val goalsListFlow = filterFlow.flatMapLatest { ffData -> + + // Save the current filter combination in shared preferences + preferenceUtil.putInt(PreferenceUtil.GOAL_FILTER_FIELD_INT, ffData.filterField.ordinal) + preferenceUtil.putInt(PreferenceUtil.GOAL_FILTER_SORT_TYPE_INT, ffData.sortType.ordinal) + + // Fetch the goals list based on the current filter combination + when (ffData.filterField) { FilterField.Title -> { - goalDao.getAllGoalsByTitle(it.sortType.value) + goalDao.getAllGoalsByTitle(ffData.sortType.value) } FilterField.Amount -> { - goalDao.getAllGoalsByAmount(it.sortType.value) + goalDao.getAllGoalsByAmount(ffData.sortType.value) } FilterField.Priority -> { - goalDao.getAllGoalsByPriority(it.sortType.value) + goalDao.getAllGoalsByPriority(ffData.sortType.value) } } } diff --git a/app/src/main/java/com/starry/greenstash/utils/PreferenceUtil.kt b/app/src/main/java/com/starry/greenstash/utils/PreferenceUtil.kt index 9333303c..2b3683ef 100644 --- a/app/src/main/java/com/starry/greenstash/utils/PreferenceUtil.kt +++ b/app/src/main/java/com/starry/greenstash/utils/PreferenceUtil.kt @@ -40,6 +40,10 @@ class PreferenceUtil(context: Context) { const val DEFAULT_CURRENCY_STR = "default_currency_code" const val DATE_FORMAT_STR = "date_format" const val APP_LOCK_BOOL = "app_lock" + + // Goal filter preferences + const val GOAL_FILTER_FIELD_INT = "goal_filter_field" + const val GOAL_FILTER_SORT_TYPE_INT = "goal_filter_sort_type" } private var prefs: SharedPreferences diff --git a/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt b/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt index f19126ee..9d98600c 100644 --- a/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt +++ b/app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt @@ -113,9 +113,7 @@ class GoalWidget : AppWidgetProvider() { goalItem.getCurrentlySavedAmount(), defCurrency ) - } / $defCurrency${ - Utils.formatCurrency(goalItem.goal.targetAmount, defCurrency) - }" + } / ${Utils.formatCurrency(goalItem.goal.targetAmount, defCurrency)}" ) views.setCharSequence(R.id.widgetDesc, "setText", widgetDesc) diff --git a/build.gradle b/build.gradle index fa81f155..b9fad866 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ buildscript { ext { kotlin_version = '1.9.21' - gradle_version = '8.2.1' + gradle_version = '8.2.2' hilt_version = '2.49' room_version = '2.6.1' }