-
Notifications
You must be signed in to change notification settings - Fork 26
/
ExtensionsScreenViewModel.kt
179 lines (160 loc) · 6.15 KB
/
ExtensionsScreenViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.ui.extensions
import androidx.compose.runtime.Immutable
import androidx.compose.ui.text.intl.Locale
import ca.gosyer.jui.core.lang.displayName
import ca.gosyer.jui.domain.extension.interactor.GetExtensionList
import ca.gosyer.jui.domain.extension.interactor.InstallExtension
import ca.gosyer.jui.domain.extension.interactor.UninstallExtension
import ca.gosyer.jui.domain.extension.interactor.UpdateExtension
import ca.gosyer.jui.domain.extension.model.Extension
import ca.gosyer.jui.domain.extension.service.ExtensionPreferences
import ca.gosyer.jui.i18n.MR
import ca.gosyer.jui.uicore.vm.ContextWrapper
import ca.gosyer.jui.uicore.vm.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import me.tatarka.inject.annotations.Inject
import org.lighthousegames.logging.logging
class ExtensionsScreenViewModel @Inject constructor(
private val getExtensionList: GetExtensionList,
private val installExtension: InstallExtension,
private val updateExtension: UpdateExtension,
private val uninstallExtension: UninstallExtension,
extensionPreferences: ExtensionPreferences,
contextWrapper: ContextWrapper
) : ViewModel(contextWrapper) {
private val extensionList = MutableStateFlow<List<Extension>?>(null)
private val _enabledLangs = extensionPreferences.languages().asStateFlow()
val enabledLangs = _enabledLangs.asStateFlow()
private val _searchQuery = MutableStateFlow<String?>(null)
val searchQuery = _searchQuery.asStateFlow()
val extensions = combine(
searchQuery,
extensionList,
enabledLangs
) { searchQuery, extensions, enabledLangs ->
search(searchQuery, extensions, enabledLangs)
}.stateIn(scope, SharingStarted.Eagerly, emptyList())
val availableLangs = extensionList.filterNotNull().map { langs ->
langs.map { it.lang }.distinct()
}.stateIn(scope, SharingStarted.Eagerly, emptyList())
private val _isLoading = MutableStateFlow(true)
val isLoading = _isLoading.asStateFlow()
init {
scope.launch {
getExtensions()
}
}
private suspend fun getExtensions() {
extensionList.value = getExtensionList.await().orEmpty()
_isLoading.value = false
}
fun install(extension: Extension) {
log.info { "Install clicked" }
scope.launch {
installExtension.await(extension)
getExtensions()
}
}
fun update(extension: Extension) {
log.info { "Update clicked" }
scope.launch {
updateExtension.await(extension)
getExtensions()
}
}
fun uninstall(extension: Extension) {
log.info { "Uninstall clicked" }
scope.launch {
uninstallExtension.await(extension)
getExtensions()
}
}
fun setEnabledLanguages(langs: Set<String>) {
_enabledLangs.value = langs
}
fun setQuery(query: String) {
_searchQuery.value = query
}
private fun search(searchQuery: String?, extensionList: List<Extension>?, enabledLangs: Set<String>): List<ExtensionUI> {
val extensions = extensionList?.filter { it.lang in enabledLangs }
.orEmpty()
return if (searchQuery.isNullOrBlank()) {
extensions.splitSort()
} else {
val queries = searchQuery.split(" ")
val filteredExtensions = extensions.toMutableList()
queries.forEach { query ->
filteredExtensions.removeAll { !it.name.contains(query, true) }
}
filteredExtensions.toList().splitSort()
}
}
private fun List<Extension>.splitSort(): List<ExtensionUI> {
val all = MR.strings.all.toPlatformString()
return this
.filter(Extension::installed)
.sortedWith(
compareBy<Extension> {
when {
it.obsolete -> 1
it.hasUpdate -> 2
else -> 3
}
}
.thenBy(Extension::lang)
.thenBy(String.CASE_INSENSITIVE_ORDER, Extension::name)
)
.map(ExtensionUI::ExtensionItem)
.let {
if (it.isNotEmpty()) {
listOf(ExtensionUI.Header(MR.strings.installed.toPlatformString())) + it
} else it
}.plus(
filterNot(Extension::installed)
.groupBy(Extension::lang)
.mapKeys { (key) ->
when (key) {
"all" -> all
else -> Locale(key).displayName
}
}
.mapValues {
it.value
.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, Extension::name))
.map(ExtensionUI::ExtensionItem)
}
.toList()
.sortedWith(
compareBy<Pair<String, *>> { (key) ->
when (key) {
all -> 1
else -> 2
}
}.thenBy(String.CASE_INSENSITIVE_ORDER, Pair<String, *>::first)
)
.flatMap { (key, value) ->
listOf(ExtensionUI.Header(key)) + value
}
)
}
private companion object {
private val log = logging()
}
}
@Immutable
sealed class ExtensionUI {
data class Header(val header: String) : ExtensionUI()
data class ExtensionItem(val extension: Extension) : ExtensionUI()
}