Skip to content

Commit e07b58e

Browse files
committed
fix: handle node stop when service not initialized
1 parent f6093ed commit e07b58e

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,21 @@ fun ContentView(
186186
val context = LocalContext.current
187187
val lifecycle = LocalLifecycleOwner.current.lifecycle
188188

189+
val walletUiState by walletViewModel.walletState.collectAsStateWithLifecycle()
190+
val lightningState by walletViewModel.lightningState.collectAsStateWithLifecycle()
191+
val nodeLifecycleState = lightningState.nodeLifecycleState
192+
193+
val isRecoveryMode by walletViewModel.isRecoveryMode.collectAsStateWithLifecycle()
194+
val notificationsGranted by settingsViewModel.notificationsGranted.collectAsStateWithLifecycle()
195+
val walletExists = walletUiState.walletExists
196+
189197
// Effects on app entering fg (ON_START) / bg (ON_STOP)
190198
DisposableEffect(lifecycle) {
191-
// TODO ADAPT THIS LOGIC TO WORK WITH LightningNodeService
192199
val observer = LifecycleEventObserver { _, event ->
193200
when (event) {
194201
Lifecycle.Event.ON_START -> {
195-
try {
202+
if (walletExists && !isRecoveryMode) {
196203
walletViewModel.start()
197-
} catch (e: Throwable) {
198-
Logger.error("Failed to start wallet", e)
199204
}
200205

201206
val pendingTransaction = NewTransactionSheetDetails.load(context)
@@ -208,6 +213,14 @@ fun ContentView(
208213
blocktankViewModel.refreshOrders()
209214
}
210215

216+
Lifecycle.Event.ON_STOP -> {
217+
if (walletExists && !isRecoveryMode && !notificationsGranted) {
218+
// App backgrounded without notification permission - stop node
219+
walletViewModel.stop()
220+
}
221+
// If notificationsGranted=true, service keeps node running
222+
}
223+
211224
else -> Unit
212225
}
213226
}
@@ -242,9 +255,6 @@ fun ContentView(
242255
}
243256
}
244257

245-
val walletUiState by walletViewModel.uiState.collectAsStateWithLifecycle()
246-
val nodeLifecycleState = walletUiState.nodeLifecycleState
247-
248258
var walletIsInitializing by remember { mutableStateOf(nodeLifecycleState == NodeLifecycleState.Initializing) }
249259
var walletInitShouldFinish by remember { mutableStateOf(false) }
250260

@@ -271,7 +281,6 @@ fun ContentView(
271281
var restoreRetryCount by remember { mutableIntStateOf(0) }
272282

273283
if (walletIsInitializing) {
274-
// TODO ADAPT THIS LOGIC TO WORK WITH LightningNodeService
275284
if (nodeLifecycleState is NodeLifecycleState.ErrorStarting) {
276285
WalletRestoreErrorView(
277286
retryCount = restoreRetryCount,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ class WalletViewModel @Inject constructor(
153153
}
154154
}
155155

156+
fun stop() {
157+
if (!walletExists) return
158+
159+
viewModelScope.launch(bgDispatcher) {
160+
lightningRepo.stop()
161+
.onFailure { error ->
162+
Logger.error("Node stop error", error)
163+
ToastEventBus.send(error)
164+
}
165+
}
166+
}
167+
156168
suspend fun observeLdkWallet() {
157169
walletRepo.observeLdkWallet()
158170
}

0 commit comments

Comments
 (0)