-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArkFilePickerViewModel.kt
229 lines (197 loc) · 7.31 KB
/
ArkFilePickerViewModel.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package dev.arkbuilders.components.filepicker
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import dev.arkbuilders.arklib.arkGlobal
import kotlinx.coroutines.launch
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
import org.orbitmvi.orbit.syntax.simple.reduce
import org.orbitmvi.orbit.viewmodel.container
import dev.arkbuilders.arklib.data.folders.FoldersRepo
import dev.arkbuilders.arklib.utils.DeviceStorageUtils
import dev.arkbuilders.arklib.utils.listChildren
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
enum class ArkFilePickerMode {
FILE, FOLDER, ANY
}
internal data class FilePickerState(
val devices: List<Path>,
val selectedDevicePos: Int,
val currentPath: Path,
val files: List<Path>,
val rootsWithFavs: Map<Path, List<Path>>
) {
val currentDevice get() = devices[selectedDevicePos]
}
internal sealed class FilePickerSideEffect {
object DismissDialog : FilePickerSideEffect()
object ToastAccessDenied : FilePickerSideEffect()
class NotifyPathPicked(val path: Path) : FilePickerSideEffect()
data object PinAsRoot : FilePickerSideEffect()
data object AlreadyRoot : FilePickerSideEffect()
data object PinAsFavorite : FilePickerSideEffect()
data object AlreadyFavorite : FilePickerSideEffect()
data object PinAsFirstRoot : FilePickerSideEffect()
}
internal class ArkFilePickerViewModel(
private val deviceStorageUtils: DeviceStorageUtils,
private val mode: ArkFilePickerMode,
private val initialPath: Path?
): ViewModel(), ContainerHost<FilePickerState, FilePickerSideEffect> {
private val foldersRepo = FoldersRepo.instance
override val container: Container<FilePickerState, FilePickerSideEffect> =
container(initialState())
init {
viewModelScope.launch {
refreshRootsWithFavs()
}
}
private suspend fun addRoot(root:Path) = withContext(Dispatchers.IO) {
foldersRepo.addRoot(root)
refreshRootsWithFavs()
}
private fun addFavorite(favorite: Path) {
viewModelScope.launch(Dispatchers.IO) {
val favoritePath = favorite.toRealPath()
val folders = foldersRepo.provideFolders()
val root = folders.keys.find { favoritePath.startsWith(it) }
?: throw IllegalStateException(
"Can't add favorite if it's root is not added"
)
val favoriteRelativePath = root.relativize(favoritePath)
if (folders[root]?.contains(favoriteRelativePath) == true) {
throw AssertionError("Path must be checked in RootPicker")
}
foldersRepo.addFavorite(root, favoriteRelativePath)
refreshRootsWithFavs()
}
}
private suspend fun refreshRootsWithFavs() {
val rootsWithFavs = foldersRepo.provideFolders()
intent {
reduce {
state.copy(rootsWithFavs = rootsWithFavs)
}
}
}
fun onItemClick(path: Path) = intent {
if (path.isDirectory()) {
try {
reduce {
state.copy(
currentPath = path,
files = formatChildren(path)
)
}
} catch (e: Exception) {
postSideEffect(FilePickerSideEffect.ToastAccessDenied)
}
return@intent
}
if (mode != ArkFilePickerMode.FOLDER)
onPathPicked(path)
}
fun onPickBtnClick() = intent {
onPathPicked(state.currentPath)
}
fun onDeviceSelected(selectedDevicePos: Int) = intent {
val selectedDevice = state.devices[selectedDevicePos]
reduce {
state.copy(
selectedDevicePos = selectedDevicePos,
currentPath = selectedDevice,
files = formatChildren(selectedDevice)
)
}
}
fun onBackClick() = intent {
val isDevice = state.devices.any { device -> device == state.currentPath }
if (isDevice) {
postSideEffect(FilePickerSideEffect.DismissDialog)
return@intent
}
val parent = state.currentPath.parent
reduce {
state.copy(
currentPath = parent,
files = formatChildren(parent)
)
}
}
private fun onPathPicked(path: Path) = intent {
postSideEffect(FilePickerSideEffect.NotifyPathPicked(path))
postSideEffect(FilePickerSideEffect.DismissDialog)
}
private fun initialState(): FilePickerState {
val devices = deviceStorageUtils.listStorages()
val currentPath = initialPath ?: devices[0]
val selectedDevice =
devices.find { currentPath.startsWith(it) } ?: devices[0]
val selectedDevicePos = devices.indexOf(selectedDevice)
return FilePickerState(
devices,
selectedDevicePos,
currentPath,
formatChildren(currentPath),
emptyMap()
)
}
private fun formatChildren(path: Path): List<Path> {
val (dirs, files) = listChildren(path)
val children = mutableListOf<Path>()
children.addAll(dirs.sorted())
children.addAll(files.sorted())
return children
}
fun haveRoot(): Boolean {
val arkGlobal = deviceStorageUtils.listStorages().firstOrNull()?.arkGlobal()
return arkGlobal?.exists() == true
}
fun pinFile(file: Path) = intent {
val rootsWithFavorites = container.stateFlow.value.rootsWithFavs
val roots = rootsWithFavorites.keys
val root = roots.find { root -> file.startsWith(root) }
val favorites = rootsWithFavorites[root]?.flatten()
val haveRoot = haveRoot()
root?.let {
//Make sure file isn't inside a root folder
if (root != file) {
val foundAsFavorite = favorites?.any { file.endsWith(it) } ?: false
if (!foundAsFavorite) {
addFavorite(file)
postSideEffect(FilePickerSideEffect.PinAsFavorite)
} else {
postSideEffect(FilePickerSideEffect.AlreadyFavorite)
}
} else {
postSideEffect(FilePickerSideEffect.AlreadyRoot)
}
} ?: let {
if (rootsWithFavorites.keys.contains(file)) {
postSideEffect(FilePickerSideEffect.AlreadyRoot)
} else {
addRoot(file)
if (!haveRoot) {
postSideEffect(FilePickerSideEffect.PinAsFirstRoot)
} else {
postSideEffect(FilePickerSideEffect.PinAsRoot)
}
}
}
}
}
internal class ArkFilePickerViewModelFactory(
private val deviceStorageUtils: DeviceStorageUtils,
private val mode: ArkFilePickerMode,
private val initialPath: Path?
): ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T =
ArkFilePickerViewModel(deviceStorageUtils, mode, initialPath) as T
}