Skip to content

Commit 918f111

Browse files
committed
refactor: encapsulate wallet init logic
1 parent bc53574 commit 918f111

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed

app/src/main/java/to/bitkit/ui/MainActivity.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ private fun OnboardingNav(
242242
scope.launch {
243243
runCatching {
244244
appViewModel.resetIsAuthenticatedState()
245-
walletViewModel.setInitNodeLifecycleState()
246-
walletViewModel.setRestoringWalletState()
247245
walletViewModel.restoreWallet(mnemonic, passphrase)
248246
}.onFailure {
249247
appViewModel.toast(it)
@@ -259,7 +257,6 @@ private fun OnboardingNav(
259257
scope.launch {
260258
runCatching {
261259
appViewModel.resetIsAuthenticatedState()
262-
walletViewModel.setInitNodeLifecycleState()
263260
walletViewModel.createWallet(bip39Passphrase = passphrase)
264261
}.onFailure {
265262
appViewModel.toast(it)

app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ class WalletViewModel @Inject constructor(
118118
restoreState = RestoreState.BackupRestoreCompleted
119119
}
120120

121-
fun setRestoringWalletState() {
122-
restoreState = RestoreState.RestoringWallet
123-
}
124121

125122
fun onRestoreContinue() {
126123
restoreState = RestoreState.NotRestoring
@@ -136,9 +133,7 @@ class WalletViewModel @Inject constructor(
136133
}
137134
}
138135

139-
fun setInitNodeLifecycleState() {
140-
lightningRepo.setInitNodeLifecycleState()
141-
}
136+
fun setInitNodeLifecycleState() = lightningRepo.setInitNodeLifecycleState()
142137

143138
fun start(walletIndex: Int = 0) {
144139
if (!walletExists) return
@@ -245,6 +240,7 @@ class WalletViewModel @Inject constructor(
245240
}
246241

247242
suspend fun createWallet(bip39Passphrase: String?) {
243+
setInitNodeLifecycleState()
248244
walletRepo.createWallet(bip39Passphrase)
249245
.onSuccess {
250246
backupRepo.scheduleFullBackup()
@@ -255,9 +251,12 @@ class WalletViewModel @Inject constructor(
255251
}
256252

257253
suspend fun restoreWallet(mnemonic: String, bip39Passphrase: String?) {
254+
setInitNodeLifecycleState()
255+
restoreState = RestoreState.RestoringWallet
256+
258257
walletRepo.restoreWallet(
259258
mnemonic = mnemonic,
260-
bip39Passphrase = bip39Passphrase
259+
bip39Passphrase = bip39Passphrase,
261260
).onFailure { error ->
262261
ToastEventBus.send(error)
263262
}

app/src/test/java/to/bitkit/ui/WalletViewModelTest.kt

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,57 +82,58 @@ class WalletViewModelTest : BaseUnitTest() {
8282
}
8383

8484
@Test
85-
fun `disconnectPeer should call lightningRepo disconnectPeer and send success toast`() = test {
85+
fun `disconnectPeer should call lightningRepo disconnectPeer`() = test {
8686
val testPeer = PeerDetails.from("nodeId", "host", "9735")
87-
whenever(lightningRepo.disconnectPeer(testPeer)).thenReturn(Result.success(Unit))
87+
val testError = Exception("Test error")
88+
whenever(lightningRepo.disconnectPeer(testPeer)).thenReturn(Result.failure(testError))
8889

8990
sut.disconnectPeer(testPeer)
9091

9192
verify(lightningRepo).disconnectPeer(testPeer)
92-
// Add verification for ToastEventBus.send if you have a way to capture those events
9393
}
9494

9595
@Test
96-
fun `disconnectPeer should call lightningRepo disconnectPeer and send failure toast`() = test {
97-
val testPeer = PeerDetails.from("nodeId", "host", "9735")
98-
val testError = Exception("Test error")
99-
whenever(lightningRepo.disconnectPeer(testPeer)).thenReturn(Result.failure(testError))
100-
101-
sut.disconnectPeer(testPeer)
96+
fun `wipeWallet should call walletRepo wipeWallet`() = test {
97+
whenever(walletRepo.wipeWallet(walletIndex = 0)).thenReturn(Result.success(Unit))
98+
sut.wipeWallet()
10299

103-
verify(lightningRepo).disconnectPeer(testPeer)
104-
// Add verification for ToastEventBus.send if you have a way to capture those events
100+
verify(walletRepo).wipeWallet(walletIndex = 0)
105101
}
106102

107103
@Test
108-
fun `wipeWallet should call walletRepo wipeWallet`() =
109-
test {
110-
whenever(walletRepo.wipeWallet(walletIndex = 0)).thenReturn(Result.success(Unit))
111-
sut.wipeWallet()
104+
fun `createWallet should call walletRepo createWallet`() = test {
105+
whenever(walletRepo.createWallet(anyOrNull())).thenReturn(Result.success(Unit))
106+
107+
sut.createWallet(null)
112108

113-
verify(walletRepo).wipeWallet(walletIndex = 0)
114-
}
109+
verify(walletRepo).createWallet(anyOrNull())
110+
}
115111

116112
@Test
117-
fun `createWallet should call walletRepo createWallet and send failure toast`() = test {
118-
val testError = Exception("Test error")
119-
whenever(walletRepo.createWallet(anyOrNull())).thenReturn(Result.failure(testError))
113+
fun `createWallet should call setInitNodeLifecycleState`() = test {
114+
whenever(walletRepo.createWallet(anyOrNull())).thenReturn(Result.success(Unit))
120115

121116
sut.createWallet(null)
122117

123-
verify(walletRepo).createWallet(anyOrNull())
124-
// Add verification for ToastEventBus.send
118+
verify(lightningRepo).setInitNodeLifecycleState()
125119
}
126120

127121
@Test
128-
fun `restoreWallet should call walletRepo restoreWallet and send failure toast`() = test {
129-
val testError = Exception("Test error")
130-
whenever(walletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.failure(testError))
122+
fun `restoreWallet should call walletRepo restoreWallet`() = test {
123+
whenever(walletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.success(Unit))
131124

132125
sut.restoreWallet("test_mnemonic", null)
133126

134127
verify(walletRepo).restoreWallet(any(), anyOrNull())
135-
// Add verification for ToastEventBus.send
128+
}
129+
130+
@Test
131+
fun `restoreWallet should call setInitNodeLifecycleState`() = test {
132+
whenever(walletRepo.restoreWallet(any(), anyOrNull())).thenReturn(Result.success(Unit))
133+
134+
sut.restoreWallet("test_mnemonic", null)
135+
136+
verify(lightningRepo).setInitNodeLifecycleState()
136137
}
137138

138139
@Test
@@ -169,7 +170,7 @@ class WalletViewModelTest : BaseUnitTest() {
169170
fun `onBackupRestoreSuccess should reset restoreState`() = test {
170171
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.success(Unit))
171172
mockWalletState.value = mockWalletState.value.copy(walletExists = true)
172-
sut.setRestoringWalletState()
173+
sut.restoreWallet("mnemonic", "passphrase")
173174
assertEquals(RestoreState.RestoringWallet, sut.restoreState)
174175

175176
sut.onRestoreContinue()
@@ -182,7 +183,7 @@ class WalletViewModelTest : BaseUnitTest() {
182183
fun `proceedWithoutRestore should exit restore flow`() = test {
183184
val testError = Exception("Test error")
184185
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.failure(testError))
185-
sut.setRestoringWalletState()
186+
sut.restoreWallet("mnemonic", "passphrase")
186187
mockWalletState.value = mockWalletState.value.copy(walletExists = true)
187188
assertEquals(RestoreState.BackupRestoreCompleted, sut.restoreState)
188189

@@ -196,7 +197,7 @@ class WalletViewModelTest : BaseUnitTest() {
196197
whenever(backupRepo.performFullRestoreFromLatestBackup()).thenReturn(Result.success(Unit))
197198
assertEquals(RestoreState.NotRestoring, sut.restoreState)
198199

199-
sut.setRestoringWalletState()
200+
sut.restoreWallet("mnemonic", "passphrase")
200201
assertEquals(RestoreState.RestoringWallet, sut.restoreState)
201202

202203
mockWalletState.value = mockWalletState.value.copy(walletExists = true)

0 commit comments

Comments
 (0)