-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRgsServerViewModel.kt
More file actions
158 lines (134 loc) · 5.04 KB
/
RgsServerViewModel.kt
File metadata and controls
158 lines (134 loc) · 5.04 KB
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
package to.bitkit.ui.settings.advanced
import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import to.bitkit.data.SettingsStore
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Env
import to.bitkit.repositories.LightningRepo
import java.net.URI
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds
@HiltViewModel
class RgsServerViewModel @Inject constructor(
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
private val settingsStore: SettingsStore,
private val lightningRepo: LightningRepo,
) : ViewModel() {
companion object {
private val HOSTNAME_PATTERN = Regex(
"^([a-z\\d]([a-z\\d-]*[a-z\\d])*\\.)+[a-z]{2,}|(\\d{1,3}\\.){3}\\d{1,3}$",
RegexOption.IGNORE_CASE,
)
private val PATH_PATTERN = Regex("^(/[a-zA-Z\\d_.~%+-]*)*$")
private val VALIDATION_DEBOUNCE = 1.seconds
}
private val _uiState = MutableStateFlow(RgsServerUiState())
val uiState: StateFlow<RgsServerUiState> = _uiState.asStateFlow()
private var validationJob: Job? = null
init {
observeState()
}
private fun observeState() {
viewModelScope.launch(bgDispatcher) {
settingsStore.data.map { it.rgsServerUrl }.distinctUntilChanged()
.collect { rgsServerUrl ->
_uiState.update {
val newState = it.copy(
connectedRgsUrl = rgsServerUrl,
rgsUrl = rgsServerUrl.orEmpty(),
)
computeState(newState)
}
}
}
}
fun setRgsUrl(url: String) {
_uiState.update { it.copy(rgsUrl = url.trim()) }
debounceValidation()
}
fun resetToDefault() {
_uiState.update { it.copy(rgsUrl = Env.ldkRgsServerUrl ?: "") }
debounceValidation()
}
private fun debounceValidation() {
validationJob?.cancel()
validationJob = viewModelScope.launch(bgDispatcher) {
delay(VALIDATION_DEBOUNCE)
_uiState.update { computeState(it) }
}
}
fun onClickConnect() {
val currentState = _uiState.value
val url = currentState.rgsUrl
if (url.isBlank() || !isValidURL(url)) {
return
}
_uiState.update { it.copy(isLoading = true) }
viewModelScope.launch(bgDispatcher) {
lightningRepo.restartWithRgsServer(normalizeUrl(url))
.onSuccess {
_uiState.update {
val newState = it.copy(
isLoading = false,
connectionResult = Result.success(Unit),
)
computeState(newState)
}
}
.onFailure { error ->
_uiState.update {
it.copy(
isLoading = false,
connectionResult = Result.failure(error),
)
}
}
}
}
fun onScan(data: String) = setRgsUrl(data)
fun clearConnectionResult() = _uiState.update { it.copy(connectionResult = null) }
private fun computeState(state: RgsServerUiState): RgsServerUiState {
val hasEdited = state.rgsUrl != state.connectedRgsUrl.orEmpty()
val canConnect = hasEdited && state.rgsUrl.isNotBlank() && isValidURL(state.rgsUrl)
val canReset = state.rgsUrl != Env.ldkRgsServerUrl.orEmpty()
return state.copy(
hasEdited = hasEdited,
canConnect = canConnect,
canReset = canReset,
)
}
private fun normalizeUrl(url: String): String =
if (!url.startsWith("http://") && !url.startsWith("https://")) "https://$url" else url
private fun isValidURL(data: String): Boolean {
return runCatching {
val uri = URI(normalizeUrl(data))
val hostname = uri.host ?: return false
if (Env.isDebug && hostname == "localhost") return true
if (!HOSTNAME_PATTERN.matches(hostname)) return false
val path = uri.path.orEmpty()
path.isEmpty() || PATH_PATTERN.matches(path)
}.getOrDefault(false)
}
}
@Stable
data class RgsServerUiState(
val connectedRgsUrl: String? = null,
val rgsUrl: String = "",
val isLoading: Boolean = false,
val connectionResult: Result<Unit>? = null,
val hasEdited: Boolean = false,
val canConnect: Boolean = false,
val canReset: Boolean = false,
)