Skip to content

Commit

Permalink
Remember last filter combinations on relaunch
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam committed Feb 4, 2024
1 parent 548df94 commit 3dc4300
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,37 @@ class HomeViewModel @Inject constructor(

private val _filterFlowData: MutableState<FilterFlowData> = 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> = _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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/starry/greenstash/widget/GoalWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
Expand Down

0 comments on commit 3dc4300

Please sign in to comment.