Skip to content

Commit b3b7e5a

Browse files
ovitrifclaude
andcommitted
refactor: simplify setup return types to Result<Unit>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a172afb commit b3b7e5a

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

app/src/main/java/to/bitkit/data/backup/VssBackupClient.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import javax.inject.Inject
2121
import javax.inject.Singleton
2222
import kotlin.time.Duration.Companion.seconds
2323

24+
class MnemonicNotAvailableException : Exception("Mnemonic not available")
25+
2426
@Singleton
2527
class VssBackupClient @Inject constructor(
2628
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
@@ -29,14 +31,14 @@ class VssBackupClient @Inject constructor(
2931
) {
3032
private var isSetup = CompletableDeferred<Unit>()
3133

32-
suspend fun setup(walletIndex: Int = 0): Result<Boolean> = withContext(ioDispatcher) {
34+
suspend fun setup(walletIndex: Int = 0): Result<Unit> = withContext(ioDispatcher) {
3335
runCatching {
3436
if (isSetup.isCompleted && !isSetup.isCancelled) {
35-
runCatching { isSetup.await() }.onSuccess { return@runCatching true }
37+
runCatching { isSetup.await() }.onSuccess { return@runCatching }
3638
}
3739

3840
val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)
39-
?: return@runCatching false
41+
?: throw MnemonicNotAvailableException()
4042

4143
withTimeout(30.seconds) {
4244
Logger.debug("VSS client setting up…", context = TAG)
@@ -64,7 +66,6 @@ class VssBackupClient @Inject constructor(
6466
isSetup.complete(Unit)
6567
Logger.info("VSS client setup with server: '$vssUrl'", context = TAG)
6668
}
67-
true
6869
}.onFailure {
6970
isSetup.completeExceptionally(it)
7071
Logger.error("VSS client setup error", it, context = TAG)
@@ -81,16 +82,17 @@ class VssBackupClient @Inject constructor(
8182
maxAttempts: Int = 10,
8283
baseDelayMs: Long = 1000L,
8384
logger: SetupRetryLogger.() -> Unit,
84-
): Result<Boolean> = withContext(ioDispatcher) {
85+
): Result<Unit> = withContext(ioDispatcher) {
8586
val log = SetupRetryLogger().apply(logger)
8687
var attempt = 0
8788
while (attempt < maxAttempts) {
8889
val result = setup()
89-
if (result.getOrDefault(false)) {
90+
if (result.isSuccess) {
9091
log.onSuccess(attempt + 1)
91-
return@withContext Result.success(true)
92+
return@withContext Result.success(Unit)
9293
}
93-
if (result.isFailure) {
94+
val exception = result.exceptionOrNull()
95+
if (exception != null && exception !is MnemonicNotAvailableException) {
9496
return@withContext result
9597
}
9698
attempt++
@@ -101,7 +103,7 @@ class VssBackupClient @Inject constructor(
101103
}
102104
}
103105
log.onExhausted(maxAttempts)
104-
Result.success(false)
106+
Result.failure(MnemonicNotAvailableException())
105107
}
106108

107109
fun reset() {

app/src/test/java/to/bitkit/data/backup/VssBackupClientTest.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import org.mockito.kotlin.verify
1010
import org.mockito.kotlin.whenever
1111
import to.bitkit.data.keychain.Keychain
1212
import to.bitkit.test.BaseUnitTest
13-
import kotlin.test.assertFalse
13+
import kotlin.test.assertIs
14+
import kotlin.test.assertTrue
1415

1516
class VssBackupClientTest : BaseUnitTest() {
1617

@@ -29,12 +30,13 @@ class VssBackupClientTest : BaseUnitTest() {
2930
}
3031

3132
@Test
32-
fun `setup returns false when mnemonic is not available`() = test {
33+
fun `setup fails with MnemonicNotAvailableException when mnemonic is not available`() = test {
3334
whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(null)
3435

3536
val result = sut.setup()
3637

37-
assertFalse(result.getOrThrow())
38+
assertTrue(result.isFailure)
39+
assertIs<MnemonicNotAvailableException>(result.exceptionOrNull())
3840
}
3941

4042
@Test
@@ -63,9 +65,9 @@ class VssBackupClientTest : BaseUnitTest() {
6365
fun `setup can be called multiple times when mnemonic not available`() = test {
6466
whenever(keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name)).thenReturn(null)
6567

66-
// Multiple calls should all return false without crashing
67-
assertFalse(sut.setup().getOrThrow())
68-
assertFalse(sut.setup().getOrThrow())
69-
assertFalse(sut.setup().getOrThrow())
68+
// Multiple calls should all fail with MnemonicNotAvailableException without crashing
69+
assertIs<MnemonicNotAvailableException>(sut.setup().exceptionOrNull())
70+
assertIs<MnemonicNotAvailableException>(sut.setup().exceptionOrNull())
71+
assertIs<MnemonicNotAvailableException>(sut.setup().exceptionOrNull())
7072
}
7173
}

0 commit comments

Comments
 (0)