|
1 | 1 | package com.androidvip.sysctlgui.ui.params.browse |
2 | 2 |
|
3 | | -import android.app.Activity |
4 | | -import android.view.View |
5 | | -import androidx.core.app.ActivityOptionsCompat |
6 | | -import androidx.lifecycle.LiveData |
7 | | -import androidx.lifecycle.MutableLiveData |
8 | | -import androidx.lifecycle.ViewModel |
9 | 3 | import androidx.lifecycle.viewModelScope |
10 | 4 | import com.androidvip.sysctlgui.R |
11 | 5 | import com.androidvip.sysctlgui.data.mapper.DomainParamMapper |
12 | | -import com.androidvip.sysctlgui.data.models.KernelParam |
13 | 6 | import com.androidvip.sysctlgui.domain.repository.AppPrefs |
14 | 7 | import com.androidvip.sysctlgui.domain.usecase.GetParamsFromFilesUseCase |
15 | | -import com.androidvip.sysctlgui.ui.params.edit.EditKernelParamActivity |
| 8 | +import com.androidvip.sysctlgui.utils.BaseViewModel |
16 | 9 | import com.androidvip.sysctlgui.utils.Consts |
17 | | -import com.hadilq.liveevent.LiveEvent |
18 | | -import com.hadilq.liveevent.LiveEventConfig |
19 | 10 | import kotlinx.coroutines.CoroutineDispatcher |
20 | 11 | import kotlinx.coroutines.Dispatchers |
21 | 12 | import kotlinx.coroutines.launch |
22 | 13 | import kotlinx.coroutines.withContext |
23 | 14 | import java.io.File |
24 | | -import androidx.core.util.Pair as PairUtil |
25 | 15 |
|
26 | 16 | class BrowseParamsViewModel( |
27 | 17 | private val getParamsFromFilesUseCase: GetParamsFromFilesUseCase, |
28 | | - private val dispatcher: CoroutineDispatcher = Dispatchers.IO, |
29 | | - appPrefs: AppPrefs |
30 | | -) : ViewModel() { |
31 | | - private val _viewState = MutableLiveData<ParamBrowserViewState>() |
32 | | - val viewState: LiveData<ParamBrowserViewState> = _viewState |
33 | | - val viewEffect = LiveEvent<ParamBrowserViewEffect>(config = LiveEventConfig.PreferFirstObserver) |
34 | | - |
35 | | - var listFoldersFirst = true |
| 18 | + appPrefs: AppPrefs, |
| 19 | + private val dispatcher: CoroutineDispatcher = Dispatchers.IO |
| 20 | +) : BaseViewModel<ParamBrowserViewEvent, ParamBrowserViewState, ParamBrowserViewEffect>() { |
| 21 | + private var listFoldersFirst = true |
| 22 | + private var searchExpression = "" |
36 | 23 |
|
37 | 24 | init { |
38 | 25 | listFoldersFirst = appPrefs.listFoldersFirst |
39 | 26 | } |
40 | 27 |
|
41 | | - fun setPath(path: String) { |
42 | | - viewModelScope.launch { |
43 | | - loadBrowsableParamFiles(path) |
44 | | - } |
45 | | - } |
| 28 | + override fun createInitialState(): ParamBrowserViewState = ParamBrowserViewState() |
46 | 29 |
|
47 | | - fun setSearchExpression(expression: String) = updateState { |
48 | | - searchExpression = expression |
| 30 | + override fun processEvent(event: ParamBrowserViewEvent) { |
| 31 | + when (event) { |
| 32 | + ParamBrowserViewEvent.RefreshRequested -> setPath(currentState.currentPath) |
| 33 | + is ParamBrowserViewEvent.DirectoryChanged -> onDirectoryChanged(event.dir) |
| 34 | + is ParamBrowserViewEvent.SearchExpressionChanged -> searchExpression = event.data |
| 35 | + is ParamBrowserViewEvent.ParamClicked -> setEffect { |
| 36 | + ParamBrowserViewEffect.NavigateToParamDetails(DomainParamMapper.map(event.param)) |
| 37 | + } |
| 38 | + ParamBrowserViewEvent.DocumentationMenuClicked -> setEffect { |
| 39 | + ParamBrowserViewEffect.OpenDocumentationUrl(currentState.docUrl) |
| 40 | + } |
| 41 | + ParamBrowserViewEvent.FavoritesMenuClicked -> setEffect { |
| 42 | + ParamBrowserViewEffect.NavigateToFavorite |
| 43 | + } |
| 44 | + } |
49 | 45 | } |
50 | 46 |
|
51 | | - fun doWhenParamItemClicked(param: KernelParam, itemLayout: View, activity: Activity) { |
52 | | - val sharedElements = arrayOf<PairUtil<View, String>>( |
53 | | - PairUtil( |
54 | | - itemLayout.findViewById(R.id.name), |
55 | | - EditKernelParamActivity.NAME_TRANSITION_NAME |
56 | | - ) |
57 | | - ) |
58 | | - val options: ActivityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation( |
59 | | - activity, |
60 | | - *sharedElements |
61 | | - ) |
62 | | - |
63 | | - viewEffect.postValue(ParamBrowserViewEffect.NavigateToParamDetails(param, options)) |
| 47 | + private fun setPath(path: String) { |
| 48 | + viewModelScope.launch { |
| 49 | + loadBrowsableParamFiles(path) |
| 50 | + } |
64 | 51 | } |
65 | 52 |
|
66 | | - fun doWhenDirectoryChanges(newDir: File) { |
| 53 | + private fun onDirectoryChanged(newDir: File) { |
67 | 54 | val newPath = newDir.absolutePath |
68 | 55 | if (newPath.isEmpty() || !newPath.startsWith(Consts.PROC_SYS)) { |
69 | | - viewEffect.postValue(ParamBrowserViewEffect.ShowToast(R.string.invalid_path)) |
| 56 | + setEffect { ParamBrowserViewEffect.ShowToast(R.string.invalid_path) } |
70 | 57 | return |
71 | 58 | } |
72 | 59 |
|
73 | 60 | setPath(newPath) |
74 | 61 |
|
75 | 62 | when { |
76 | | - newPath.startsWith("/proc/sys/abi") -> updateState { |
77 | | - docUrl = "https://www.kernel.org/doc/Documentation/sysctl/abi.txt" |
78 | | - showDocumentationMenu = true |
| 63 | + newPath.startsWith("/proc/sys/abi") -> setState { |
| 64 | + copy( |
| 65 | + docUrl = "https://www.kernel.org/doc/Documentation/sysctl/abi.txt", |
| 66 | + showDocumentationMenu = true |
| 67 | + ) |
79 | 68 | } |
80 | 69 |
|
81 | | - newPath.startsWith("/proc/sys/fs") -> updateState { |
82 | | - docUrl = "https://www.kernel.org/doc/Documentation/sysctl/fs.txt" |
83 | | - showDocumentationMenu = true |
| 70 | + newPath.startsWith("/proc/sys/fs") -> setState { |
| 71 | + copy( |
| 72 | + docUrl = "https://www.kernel.org/doc/Documentation/sysctl/fs.txt", |
| 73 | + showDocumentationMenu = true |
| 74 | + ) |
84 | 75 | } |
85 | 76 |
|
86 | | - newPath.startsWith("/proc/sys/kernel") -> updateState { |
87 | | - docUrl = "https://www.kernel.org/doc/Documentation/sysctl/kernel.txt" |
88 | | - showDocumentationMenu = true |
| 77 | + newPath.startsWith("/proc/sys/kernel") -> setState { |
| 78 | + copy( |
| 79 | + docUrl = "https://www.kernel.org/doc/Documentation/sysctl/kernel.txt", |
| 80 | + showDocumentationMenu = true |
| 81 | + ) |
89 | 82 | } |
90 | 83 |
|
91 | | - newPath.startsWith("/proc/sys/net") -> updateState { |
92 | | - docUrl = "https://www.kernel.org/doc/Documentation/sysctl/net.txt" |
93 | | - showDocumentationMenu = true |
| 84 | + newPath.startsWith("/proc/sys/net") -> setState { |
| 85 | + copy( |
| 86 | + docUrl = "https://www.kernel.org/doc/Documentation/sysctl/net.txt", |
| 87 | + showDocumentationMenu = true |
| 88 | + ) |
94 | 89 | } |
95 | 90 |
|
96 | | - newPath.startsWith("/proc/sys/vm") -> updateState { |
97 | | - docUrl = "https://www.kernel.org/doc/Documentation/sysctl/vm.txt" |
98 | | - showDocumentationMenu = true |
| 91 | + newPath.startsWith("/proc/sys/vm") -> setState { |
| 92 | + copy( |
| 93 | + docUrl = "https://www.kernel.org/doc/Documentation/sysctl/vm.txt", |
| 94 | + showDocumentationMenu = true |
| 95 | + ) |
99 | 96 | } |
100 | 97 |
|
101 | | - else -> updateState { |
102 | | - showDocumentationMenu = false |
103 | | - } |
| 98 | + else -> setState { copy(showDocumentationMenu = false) } |
104 | 99 | } |
105 | 100 | } |
106 | 101 |
|
107 | | - fun doWhenDocumentationMenuClicked() { |
108 | | - val url = viewState.value?.docUrl.orEmpty() |
109 | | - viewEffect.postValue(ParamBrowserViewEffect.OpenDocumentationUrl(url)) |
110 | | - } |
111 | | - |
112 | | - fun doWhenFavoritesMenuClicked() { |
113 | | - viewEffect.postValue(ParamBrowserViewEffect.NavigateToFavorite) |
114 | | - } |
115 | | - |
116 | 102 | private suspend fun getCurrentPathFiles(path: String) = withContext(dispatcher) { |
117 | 103 | runCatching { |
118 | 104 | File(path).listFiles()?.toList() |
119 | 105 | }.getOrDefault(emptyList()) |
120 | 106 | } |
121 | 107 |
|
122 | 108 | private suspend fun loadBrowsableParamFiles(path: String) { |
123 | | - updateState { isLoading = true } |
| 109 | + setState { copy(isLoading = true) } |
124 | 110 | val files = getCurrentPathFiles(path).maybeDirectorySorted().maybeFiltered() |
125 | 111 | val params = getParamsFromFilesUseCase(files).map { |
126 | 112 | DomainParamMapper.map(it) |
127 | 113 | } |
128 | 114 |
|
129 | | - updateState { |
130 | | - currentPath = path |
131 | | - isLoading = false |
132 | | - data = params |
| 115 | + setState { |
| 116 | + copy(currentPath = path, isLoading = false, data = params) |
133 | 117 | } |
134 | 118 | } |
| 119 | + |
135 | 120 | private suspend fun List<File>?.maybeDirectorySorted() = withContext(dispatcher) { |
136 | 121 | return@withContext this@maybeDirectorySorted?.run { |
137 | 122 | if (listFoldersFirst) { |
138 | 123 | sortedByDescending { it.isDirectory } |
139 | | - } else this |
| 124 | + } else { |
| 125 | + this |
| 126 | + } |
140 | 127 | }?.toList().orEmpty() |
141 | 128 | } |
142 | 129 |
|
143 | 130 | private suspend fun List<File>?.maybeFiltered() = withContext(dispatcher) { |
144 | | - val searchExpression = viewState.value?.searchExpression.orEmpty() |
145 | 131 | return@withContext this@maybeFiltered?.run { |
146 | 132 | if (searchExpression.isNotEmpty()) { |
147 | 133 | filter { param -> |
148 | 134 | param.name.lowercase() |
149 | 135 | .replace(".", "") |
150 | 136 | .contains(searchExpression.lowercase()) |
151 | 137 | } |
152 | | - } else this |
| 138 | + } else { |
| 139 | + this |
| 140 | + } |
153 | 141 | }?.toList().orEmpty() |
154 | 142 | } |
155 | | - |
156 | | - private fun updateState(state: ParamBrowserViewState.() -> Unit) { |
157 | | - _viewState.value = currentViewState.apply(state) |
158 | | - } |
159 | | - |
160 | | - private val currentViewState: ParamBrowserViewState |
161 | | - get() = viewState.value ?: ParamBrowserViewState() |
162 | 143 | } |
0 commit comments