Skip to content

Commit 71e73f6

Browse files
authored
Merge pull request #51 from MMRLApp/copilot/fix-sorting-issue
Fix sorting bug - Modules not re-sorting when changing sort options
2 parents d22a7f0 + e8afbdf commit 71e73f6

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

app/src/main/java/com/dergoogler/mmrl/wx/viewmodel/ModulesViewModel.kt

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ModulesViewModel @Inject constructor(
3939
platform
4040
}
4141

42+
private val sourceFlow = MutableStateFlow(listOf<LocalModule>())
4243
private val cacheFlow = MutableStateFlow(listOf<LocalModule>())
4344
private val localFlow = MutableStateFlow(listOf<LocalModule>())
4445
val local get() = localFlow.asStateFlow()
@@ -81,26 +82,27 @@ class ModulesViewModel @Inject constructor(
8182
}
8283

8384
private fun dataObserver() {
84-
getLocalAllAsFlow()
85+
sourceFlow
8586
.combine(modulesMenu) { list, menu ->
86-
cacheFlow.value = if (list.isEmpty()) {
87-
emptyList()
88-
} else {
89-
list.sortedWith(
90-
comparator(menu.option, menu.descending)
91-
).let { v ->
92-
val a = if (menu.pinEnabled) {
93-
v.sortedByDescending { it.state == State.ENABLE }
94-
} else v
95-
96-
val b = if (menu.pinAction) {
97-
a.sortedByDescending { it.hasAction }
98-
} else a
99-
100-
if (menu.pinWebUI) {
101-
b.sortedByDescending { it.hasWebUI }
102-
} else b
103-
}
87+
if (list.isEmpty()) {
88+
isLoadingFlow.update { false }
89+
return@combine
90+
}
91+
92+
cacheFlow.value = list.sortedWith(
93+
comparator(menu.option, menu.descending)
94+
).let { v ->
95+
val a = if (menu.pinEnabled) {
96+
v.sortedByDescending { it.state == State.ENABLE }
97+
} else v
98+
99+
val b = if (menu.pinAction) {
100+
a.sortedByDescending { it.hasAction }
101+
} else a
102+
103+
if (menu.pinWebUI) {
104+
b.sortedByDescending { it.hasWebUI }
105+
} else b
104106
}
105107

106108
isLoadingFlow.update { false }
@@ -203,27 +205,15 @@ class ModulesViewModel @Inject constructor(
203205
refreshing {
204206
try {
205207
val modules = getModules()
206-
cacheFlow.value = modules.await()
208+
sourceFlow.value = modules.await()
207209
} catch (e: Exception) {
208210
Log.e(TAG, "Error fetching modules", e)
209211
}
210212
}
211213
}
212214

213215
private fun getLocalAllAsFlow(): StateFlow<List<LocalModule>> {
214-
return flow {
215-
try {
216-
val modules = getModules()
217-
emit(modules.await())
218-
} catch (e: Exception) {
219-
Log.e(TAG, "Failed to load modules", e)
220-
emit(emptyList())
221-
}
222-
}.stateIn(
223-
viewModelScope,
224-
SharingStarted.Eagerly,
225-
emptyList()
226-
)
216+
return sourceFlow
227217
}
228218

229219
val screenState: StateFlow<ModulesScreenState> = getLocalAllAsFlow()

0 commit comments

Comments
 (0)