Skip to content

Commit 042b5fa

Browse files
Add setting to have favorites page be the default one (#215)
* Initial plan * Add setting to have favorites page be the default one Co-authored-by: yogeshpaliyal <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: yogeshpaliyal <[email protected]> Co-authored-by: Yogesh Choudhary Paliyal <[email protected]>
1 parent af0790a commit 042b5fa

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

app/src/main/java/com/yogeshpaliyal/deepr/preference/AppPreferenceDataStore.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AppPreferenceDataStore(
2828
private val AUTO_BACKUP_LOCATION = stringPreferencesKey("auto_backup_location")
2929
private val AUTO_BACKUP_INTERVAL = longPreferencesKey("auto_backup_interval")
3030
private val LAST_BACKUP_TIME = longPreferencesKey("last_backup_time")
31+
private val DEFAULT_PAGE_FAVOURITES = booleanPreferencesKey("default_page_favourites")
3132
}
3233

3334
val getSortingOrder: Flow<@SortType String> =
@@ -75,6 +76,11 @@ class AppPreferenceDataStore(
7576
preferences[LAST_BACKUP_TIME] ?: 0L // Default to 0 (never backed up)
7677
}
7778

79+
val getDefaultPageFavourites: Flow<Boolean> =
80+
context.appDataStore.data.map { preferences ->
81+
preferences[DEFAULT_PAGE_FAVOURITES] ?: false // Default to All (-1)
82+
}
83+
7884
suspend fun setSortingOrder(order: @SortType String) {
7985
context.appDataStore.edit { prefs ->
8086
prefs[SORTING_ORDER] = order
@@ -134,4 +140,10 @@ class AppPreferenceDataStore(
134140
prefs[LAST_BACKUP_TIME] = timestamp
135141
}
136142
}
143+
144+
suspend fun setDefaultPageFavourites(favourites: Boolean) {
145+
context.appDataStore.edit { prefs ->
146+
prefs[DEFAULT_PAGE_FAVOURITES] = favourites
147+
}
148+
}
137149
}

app/src/main/java/com/yogeshpaliyal/deepr/ui/screens/Settings.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import compose.icons.tablericons.Language
6767
import compose.icons.tablericons.Refresh
6868
import compose.icons.tablericons.Server
6969
import compose.icons.tablericons.Settings
70+
import compose.icons.tablericons.Star
7071
import compose.icons.tablericons.Upload
7172
import kotlinx.coroutines.flow.collectLatest
7273
import org.koin.androidx.compose.koinViewModel
@@ -110,6 +111,9 @@ fun SettingsScreen(
110111
val languageCode by viewModel.languageCode.collectAsStateWithLifecycle()
111112
var showLanguageDialog by remember { mutableStateOf(false) }
112113

114+
// Collect default page preference state
115+
val defaultPageFavourites by viewModel.defaultPageFavouritesEnabled.collectAsStateWithLifecycle()
116+
113117
// Collect sync preference states
114118
val syncEnabled by viewModel.syncEnabled.collectAsStateWithLifecycle()
115119
val syncFilePath by viewModel.syncFilePath.collectAsStateWithLifecycle()
@@ -426,6 +430,26 @@ fun SettingsScreen(
426430
},
427431
)
428432

433+
SettingsItem(
434+
TablerIcons.Star,
435+
title = stringResource(R.string.default_page),
436+
description =
437+
if (defaultPageFavourites) {
438+
stringResource(R.string.default_page_favourites)
439+
} else {
440+
stringResource(R.string.default_page_all)
441+
},
442+
onClick = {
443+
viewModel.setDefaultPageFavourites(!defaultPageFavourites)
444+
},
445+
trailing = {
446+
Switch(
447+
checked = defaultPageFavourites,
448+
onCheckedChange = { viewModel.setDefaultPageFavourites(it) },
449+
)
450+
},
451+
)
452+
429453
SettingsItem(
430454
TablerIcons.InfoCircle,
431455
title = stringResource(R.string.about_us),

app/src/main/java/com/yogeshpaliyal/deepr/viewmodel/AccountViewModel.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,18 @@ class AccountViewModel(
131131
val selectedTagFilter: StateFlow<List<Tags>> = _selectedTagFilter
132132

133133
// State for favourite filter (-1 = All, 0 = Not Favourite, 1 = Favourite)
134+
private val defaultPageFavourites: Flow<Boolean> = preferenceDataStore.getDefaultPageFavourites
134135
private val _favouriteFilter = MutableStateFlow(-1)
135136
val favouriteFilter: StateFlow<Int> = _favouriteFilter
136137

138+
init {
139+
viewModelScope.launch(Dispatchers.IO) {
140+
defaultPageFavourites.collect { isFavouritesDefault ->
141+
_favouriteFilter.update { if (isFavouritesDefault) 1 else -1 }
142+
}
143+
}
144+
}
145+
137146
// Set tag filter - toggle tag in the list
138147
fun setTagFilter(tag: Tags?) {
139148
if (tag == null) {
@@ -425,6 +434,17 @@ class AccountViewModel(
425434
}
426435
}
427436

437+
// Default page preference methods
438+
val defaultPageFavouritesEnabled =
439+
preferenceDataStore.getDefaultPageFavourites
440+
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)
441+
442+
fun setDefaultPageFavourites(favourites: Boolean) {
443+
viewModelScope.launch(Dispatchers.IO) {
444+
preferenceDataStore.setDefaultPageFavourites(favourites)
445+
}
446+
}
447+
428448
// Auto backup preference methods
429449
val autoBackupEnabled =
430450
preferenceDataStore.getAutoBackupEnabled

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<string name="french">Français</string>
9797
<string name="german">Deutsch</string>
9898
<string name="urdu">اردو</string>
99+
<string name="default_page">Default Page</string>
100+
<string name="default_page_all">Start with All links</string>
101+
<string name="default_page_favourites">Start with Favourites</string>
99102
<string name="app_version">App Version: %s</string>
100103
<string name="made_with_love">Made with ❤️ in India</string>
101104

0 commit comments

Comments
 (0)