@@ -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,
0 commit comments