Skip to content

Commit c05b471

Browse files
committed
fix: debounce
1 parent 6d2c9ff commit c05b471

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

app/src/main/java/to/bitkit/ui/settings/advanced/RgsServerViewModel.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import dagger.hilt.android.lifecycle.HiltViewModel
77
import kotlinx.coroutines.CoroutineDispatcher
8+
import kotlinx.coroutines.Job
9+
import kotlinx.coroutines.delay
810
import kotlinx.coroutines.flow.MutableStateFlow
911
import kotlinx.coroutines.flow.StateFlow
1012
import kotlinx.coroutines.flow.asStateFlow
@@ -18,6 +20,7 @@ import to.bitkit.env.Env
1820
import to.bitkit.repositories.LightningRepo
1921
import java.net.URI
2022
import javax.inject.Inject
23+
import kotlin.time.Duration.Companion.seconds
2124

2225
@HiltViewModel
2326
class RgsServerViewModel @Inject constructor(
@@ -32,11 +35,14 @@ class RgsServerViewModel @Inject constructor(
3235
RegexOption.IGNORE_CASE,
3336
)
3437
private val PATH_PATTERN = Regex("^(/[a-zA-Z\\d_.~%+-]*)*$")
38+
private val VALIDATION_DEBOUNCE = 1.seconds
3539
}
3640

3741
private val _uiState = MutableStateFlow(RgsServerUiState())
3842
val uiState: StateFlow<RgsServerUiState> = _uiState.asStateFlow()
3943

44+
private var validationJob: Job? = null
45+
4046
init {
4147
observeState()
4248
}
@@ -57,17 +63,20 @@ class RgsServerViewModel @Inject constructor(
5763
}
5864

5965
fun setRgsUrl(url: String) {
60-
_uiState.update {
61-
val newState = it.copy(rgsUrl = url.trim())
62-
computeState(newState)
63-
}
66+
_uiState.update { it.copy(rgsUrl = url.trim()) }
67+
debounceValidation()
6468
}
6569

6670
fun resetToDefault() {
67-
val defaultUrl = Env.ldkRgsServerUrl ?: ""
68-
_uiState.update {
69-
val newState = it.copy(rgsUrl = defaultUrl)
70-
computeState(newState)
71+
_uiState.update { it.copy(rgsUrl = Env.ldkRgsServerUrl ?: "") }
72+
debounceValidation()
73+
}
74+
75+
private fun debounceValidation() {
76+
validationJob?.cancel()
77+
validationJob = viewModelScope.launch(bgDispatcher) {
78+
delay(VALIDATION_DEBOUNCE)
79+
_uiState.update { computeState(it) }
7180
}
7281
}
7382

app/src/test/java/to/bitkit/ui/settings/advanced/RgsServerViewModelTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import app.cash.turbine.test
44
import kotlinx.coroutines.ExperimentalCoroutinesApi
55
import kotlinx.coroutines.flow.flowOf
66
import kotlinx.coroutines.test.advanceUntilIdle
7+
import kotlinx.coroutines.withTimeout
78
import org.junit.Before
89
import org.junit.Test
910
import org.mockito.kotlin.mock
@@ -14,7 +15,6 @@ import to.bitkit.data.SettingsData
1415
import to.bitkit.data.SettingsStore
1516
import to.bitkit.repositories.LightningRepo
1617
import to.bitkit.test.BaseUnitTest
17-
import kotlinx.coroutines.withTimeout
1818
import kotlin.test.assertEquals
1919
import kotlin.test.assertFalse
2020
import kotlin.test.assertNotNull
@@ -64,6 +64,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
6464
advanceUntilIdle()
6565

6666
sut.setRgsUrl("https://other.server.com/snapshot")
67+
advanceUntilIdle()
6768

6869
val state = sut.uiState.value
6970
assertEquals("https://other.server.com/snapshot", state.rgsUrl)
@@ -226,6 +227,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
226227
sut.setRgsUrl("https://custom.server.com/snapshot")
227228

228229
sut.resetToDefault()
230+
advanceUntilIdle()
229231

230232
val state = sut.uiState.value
231233
assertFalse(state.canReset)
@@ -239,6 +241,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
239241
sut = createSut()
240242
advanceUntilIdle()
241243
sut.setRgsUrl(newUrl)
244+
advanceUntilIdle()
242245

243246
sut.uiState.test {
244247
skipItems(1)
@@ -256,6 +259,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
256259

257260
withTimeout(2.seconds) {
258261
sut.setRgsUrl("https://rapidsync.lightningdevkit/snapshot/" + "a".repeat(100) + "!")
262+
advanceUntilIdle()
259263
}
260264

261265
assertFalse(sut.uiState.value.canConnect)
@@ -268,6 +272,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
268272

269273
withTimeout(2.seconds) {
270274
sut.setRgsUrl("https://rapidsync.lightningdevkit/snapshot")
275+
advanceUntilIdle()
271276
}
272277

273278
assertTrue(sut.uiState.value.canConnect)
@@ -289,6 +294,7 @@ class RgsServerViewModelTest : BaseUnitTest() {
289294
advanceUntilIdle()
290295

291296
sut.setRgsUrl("https://192.168.1.1:8080/snapshot")
297+
advanceUntilIdle()
292298

293299
assertTrue(sut.uiState.value.canConnect)
294300
}

0 commit comments

Comments
 (0)