Skip to content

Commit 32b73f7

Browse files
authored
Migrate node sort prefs to datastore (#3241)
1 parent e3dd432 commit 32b73f7

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

app/src/main/java/com/geeksville/mesh/ui/node/NodesViewModel.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.meshtastic.core.data.repository.NodeRepository
4040
import org.meshtastic.core.data.repository.RadioConfigRepository
4141
import org.meshtastic.core.database.model.Node
4242
import org.meshtastic.core.database.model.NodeSortOption
43+
import org.meshtastic.core.datastore.UiPreferencesDataSource
4344
import org.meshtastic.core.prefs.ui.UiPrefs
4445
import timber.log.Timber
4546
import javax.inject.Inject
@@ -52,6 +53,7 @@ constructor(
5253
radioConfigRepository: RadioConfigRepository,
5354
private val serviceRepository: ServiceRepository,
5455
private val uiPrefs: UiPrefs,
56+
private val uiPreferencesDataSource: UiPreferencesDataSource,
5557
) : ViewModel() {
5658

5759
val ourNodeInfo: StateFlow<Node?> = nodeRepository.ourNodeInfo
@@ -76,14 +78,13 @@ constructor(
7678
val sharedContactRequested = _sharedContactRequested.asStateFlow()
7779

7880
private val nodeSortOption =
79-
MutableStateFlow(NodeSortOption.entries.getOrElse(uiPrefs.nodeSortOption) { NodeSortOption.VIA_FAVORITE })
81+
uiPreferencesDataSource.nodeSort.map { NodeSortOption.entries.getOrElse(it) { NodeSortOption.VIA_FAVORITE } }
8082

8183
private val nodeFilterText = MutableStateFlow("")
82-
private val includeUnknown = MutableStateFlow(uiPrefs.includeUnknown)
83-
private val onlyOnline = MutableStateFlow(uiPrefs.onlyOnline)
84-
private val onlyDirect = MutableStateFlow(uiPrefs.onlyDirect)
85-
private val _showIgnored = MutableStateFlow(uiPrefs.showIgnored)
86-
val showIgnored: StateFlow<Boolean> = _showIgnored
84+
private val includeUnknown = uiPreferencesDataSource.includeUnknown
85+
private val onlyOnline = uiPreferencesDataSource.onlyOnline
86+
private val onlyDirect = uiPreferencesDataSource.onlyDirect
87+
private val showIgnored = uiPreferencesDataSource.showIgnored
8788

8889
private val nodeFilter: Flow<NodeFilterState> =
8990
combine(nodeFilterText, includeUnknown, onlyOnline, onlyDirect, showIgnored) {
@@ -151,17 +152,24 @@ constructor(
151152
nodeFilterText.value = text
152153
}
153154

154-
fun toggleIncludeUnknown() = toggle(includeUnknown) { uiPrefs.includeUnknown = it }
155+
fun toggleIncludeUnknown() {
156+
uiPreferencesDataSource.setIncludeUnknown(!includeUnknown.value)
157+
}
155158

156-
fun toggleOnlyOnline() = toggle(onlyOnline) { uiPrefs.onlyOnline = it }
159+
fun toggleOnlyOnline() {
160+
uiPreferencesDataSource.setOnlyOnline(!onlyOnline.value)
161+
}
157162

158-
fun toggleOnlyDirect() = toggle(onlyDirect) { uiPrefs.onlyDirect = it }
163+
fun toggleOnlyDirect() {
164+
uiPreferencesDataSource.setOnlyDirect(!onlyDirect.value)
165+
}
159166

160-
fun toggleShowIgnored() = toggle(_showIgnored) { uiPrefs.showIgnored = it }
167+
fun toggleShowIgnored() {
168+
uiPreferencesDataSource.setShowIgnored(!showIgnored.value)
169+
}
161170

162171
fun setSortOption(sort: NodeSortOption) {
163-
nodeSortOption.value = sort
164-
uiPrefs.nodeSortOption = sort.ordinal
172+
uiPreferencesDataSource.setNodeSort(sort.ordinal)
165173
}
166174

167175
fun toggleShowDetails() = toggle(showDetails) { uiPrefs.showDetails = it }

core/datastore/src/main/kotlin/org/meshtastic/core/datastore/UiPreferencesDataSource.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ import javax.inject.Singleton
3636
internal const val KEY_APP_INTRO_COMPLETED = "app_intro_completed"
3737
internal const val KEY_THEME = "theme"
3838

39+
// Node list filters/sort
40+
internal const val KEY_NODE_SORT = "node-sort-option"
41+
internal const val KEY_INCLUDE_UNKNOWN = "include-unknown"
42+
internal const val KEY_ONLY_ONLINE = "only-online"
43+
internal const val KEY_ONLY_DIRECT = "only-direct"
44+
internal const val KEY_SHOW_IGNORED = "show-ignored"
45+
3946
@Singleton
4047
class UiPreferencesDataSource @Inject constructor(private val dataStore: DataStore<Preferences>) {
4148

@@ -46,6 +53,12 @@ class UiPreferencesDataSource @Inject constructor(private val dataStore: DataSto
4653
// Default value for AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
4754
val theme: StateFlow<Int> = dataStore.prefStateFlow(key = THEME, default = -1)
4855

56+
val nodeSort: StateFlow<Int> = dataStore.prefStateFlow(key = NODE_SORT, default = -1)
57+
val includeUnknown: StateFlow<Boolean> = dataStore.prefStateFlow(key = INCLUDE_UNKNOWN, default = false)
58+
val onlyOnline: StateFlow<Boolean> = dataStore.prefStateFlow(key = ONLY_ONLINE, default = false)
59+
val onlyDirect: StateFlow<Boolean> = dataStore.prefStateFlow(key = ONLY_DIRECT, default = false)
60+
val showIgnored: StateFlow<Boolean> = dataStore.prefStateFlow(key = SHOW_IGNORED, default = false)
61+
4962
fun setAppIntroCompleted(completed: Boolean) {
5063
dataStore.setPref(key = APP_INTRO_COMPLETED, value = completed)
5164
}
@@ -54,6 +67,26 @@ class UiPreferencesDataSource @Inject constructor(private val dataStore: DataSto
5467
dataStore.setPref(key = THEME, value = value)
5568
}
5669

70+
fun setNodeSort(value: Int) {
71+
dataStore.setPref(key = NODE_SORT, value = value)
72+
}
73+
74+
fun setIncludeUnknown(value: Boolean) {
75+
dataStore.setPref(key = INCLUDE_UNKNOWN, value = value)
76+
}
77+
78+
fun setOnlyOnline(value: Boolean) {
79+
dataStore.setPref(key = ONLY_ONLINE, value = value)
80+
}
81+
82+
fun setOnlyDirect(value: Boolean) {
83+
dataStore.setPref(key = ONLY_DIRECT, value = value)
84+
}
85+
86+
fun setShowIgnored(value: Boolean) {
87+
dataStore.setPref(key = SHOW_IGNORED, value = value)
88+
}
89+
5790
private fun <T : Any> DataStore<Preferences>.prefStateFlow(key: Preferences.Key<T>, default: T): StateFlow<T> =
5891
data.map { it[key] ?: default }.stateIn(scope = scope, started = SharingStarted.Lazily, initialValue = default)
5992

@@ -64,5 +97,10 @@ class UiPreferencesDataSource @Inject constructor(private val dataStore: DataSto
6497
private companion object {
6598
val APP_INTRO_COMPLETED = booleanPreferencesKey(KEY_APP_INTRO_COMPLETED)
6699
val THEME = intPreferencesKey(KEY_THEME)
100+
val NODE_SORT = intPreferencesKey(KEY_NODE_SORT)
101+
val INCLUDE_UNKNOWN = booleanPreferencesKey(KEY_INCLUDE_UNKNOWN)
102+
val ONLY_ONLINE = booleanPreferencesKey(KEY_ONLY_ONLINE)
103+
val ONLY_DIRECT = booleanPreferencesKey(KEY_ONLY_DIRECT)
104+
val SHOW_IGNORED = booleanPreferencesKey(KEY_SHOW_IGNORED)
67105
}
68106
}

core/datastore/src/main/kotlin/org/meshtastic/core/datastore/di/DataStoreModule.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ import kotlinx.coroutines.CoroutineScope
3939
import kotlinx.coroutines.Dispatchers
4040
import kotlinx.coroutines.SupervisorJob
4141
import org.meshtastic.core.datastore.KEY_APP_INTRO_COMPLETED
42+
import org.meshtastic.core.datastore.KEY_INCLUDE_UNKNOWN
43+
import org.meshtastic.core.datastore.KEY_NODE_SORT
44+
import org.meshtastic.core.datastore.KEY_ONLY_DIRECT
45+
import org.meshtastic.core.datastore.KEY_ONLY_ONLINE
46+
import org.meshtastic.core.datastore.KEY_SHOW_IGNORED
4247
import org.meshtastic.core.datastore.KEY_THEME
4348
import org.meshtastic.core.datastore.serializer.ChannelSetSerializer
4449
import org.meshtastic.core.datastore.serializer.LocalConfigSerializer
@@ -61,7 +66,16 @@ object DataStoreModule {
6166
SharedPreferencesMigration(
6267
context = appContext,
6368
sharedPreferencesName = "ui-prefs",
64-
keysToMigrate = setOf(KEY_APP_INTRO_COMPLETED, KEY_THEME),
69+
keysToMigrate =
70+
setOf(
71+
KEY_APP_INTRO_COMPLETED,
72+
KEY_THEME,
73+
KEY_NODE_SORT,
74+
KEY_INCLUDE_UNKNOWN,
75+
KEY_ONLY_ONLINE,
76+
KEY_ONLY_DIRECT,
77+
KEY_SHOW_IGNORED,
78+
),
6579
),
6680
),
6781
scope = CoroutineScope(Dispatchers.IO + SupervisorJob()),

core/prefs/src/main/kotlin/org/meshtastic/core/prefs/ui/UiPrefs.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ import javax.inject.Singleton
3030

3131
interface UiPrefs {
3232
var hasShownNotPairedWarning: Boolean
33-
var nodeSortOption: Int
34-
var includeUnknown: Boolean
3533
var showDetails: Boolean
36-
var onlyOnline: Boolean
37-
var onlyDirect: Boolean
38-
var showIgnored: Boolean
3934
var showQuickChat: Boolean
4035

4136
fun shouldProvideNodeLocation(nodeNum: Int): StateFlow<Boolean>
@@ -68,12 +63,7 @@ class UiPrefsImpl @Inject constructor(@UiSharedPreferences private val prefs: Sh
6863
}
6964

7065
override var hasShownNotPairedWarning: Boolean by PrefDelegate(prefs, "has_shown_not_paired_warning", false)
71-
override var nodeSortOption: Int by PrefDelegate(prefs, "node-sort-option", -1)
72-
override var includeUnknown: Boolean by PrefDelegate(prefs, "include-unknown", false)
7366
override var showDetails: Boolean by PrefDelegate(prefs, "show-details", false)
74-
override var onlyOnline: Boolean by PrefDelegate(prefs, "only-online", false)
75-
override var onlyDirect: Boolean by PrefDelegate(prefs, "only-direct", false)
76-
override var showIgnored: Boolean by PrefDelegate(prefs, "show-ignored", false)
7767
override var showQuickChat: Boolean by PrefDelegate(prefs, "show-quick-chat", false)
7868

7969
override fun shouldProvideNodeLocation(nodeNum: Int): StateFlow<Boolean> = provideNodeLocationFlows

0 commit comments

Comments
 (0)