-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainActivity.kt
More file actions
364 lines (335 loc) · 14.4 KB
/
Copy pathMainActivity.kt
File metadata and controls
364 lines (335 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package to.bitkit.ui
import android.app.NotificationManager
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.haze.hazeSource
import dev.chrisbanes.haze.rememberHazeState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import to.bitkit.R
import to.bitkit.androidServices.LightningNodeService
import to.bitkit.androidServices.LightningNodeService.Companion.CHANNEL_ID_NODE
import to.bitkit.models.NewTransactionSheetDetails
import to.bitkit.ui.components.AuthCheckView
import to.bitkit.ui.components.IsOnlineTracker
import to.bitkit.ui.components.ToastOverlay
import to.bitkit.ui.onboarding.CreateWalletWithPassphraseScreen
import to.bitkit.ui.onboarding.IntroScreen
import to.bitkit.ui.onboarding.OnboardingSlidesScreen
import to.bitkit.ui.onboarding.RestoreWalletScreen
import to.bitkit.ui.onboarding.TermsOfUseScreen
import to.bitkit.ui.onboarding.WarningMultipleDevicesScreen
import to.bitkit.ui.screens.MigrationLoadingScreen
import to.bitkit.ui.screens.SplashScreen
import to.bitkit.ui.sheets.ForgotPinSheet
import to.bitkit.ui.sheets.NewTransactionSheet
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.utils.composableWithDefaultTransitions
import to.bitkit.ui.utils.enableAppEdgeToEdge
import to.bitkit.utils.Logger
import to.bitkit.viewmodels.ActivityListViewModel
import to.bitkit.viewmodels.AppViewModel
import to.bitkit.viewmodels.BackupsViewModel
import to.bitkit.viewmodels.BlocktankViewModel
import to.bitkit.viewmodels.CurrencyViewModel
import to.bitkit.viewmodels.MainScreenEffect
import to.bitkit.viewmodels.SettingsViewModel
import to.bitkit.viewmodels.TransferViewModel
import to.bitkit.viewmodels.WalletViewModel
@AndroidEntryPoint
class MainActivity : FragmentActivity() {
private companion object {
const val KEY_CONSUMED_LAUNCH_INTENT = "consumed_launch_intent"
}
private val appViewModel by viewModels<AppViewModel>()
private val walletViewModel by viewModels<WalletViewModel>()
private val blocktankViewModel by viewModels<BlocktankViewModel>()
private val currencyViewModel by viewModels<CurrencyViewModel>()
private val activityListViewModel by viewModels<ActivityListViewModel>()
private val transferViewModel by viewModels<TransferViewModel>()
private val settingsViewModel by viewModels<SettingsViewModel>()
private val backupsViewModel by viewModels<BackupsViewModel>()
@Suppress("LongMethod", "CyclomaticComplexMethod")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initNotificationChannel()
initNotificationChannel(
id = CHANNEL_ID_NODE,
name = getString(R.string.notification__channel_node__name),
desc = getString(R.string.notification__channel_node__body),
importance = NotificationManager.IMPORTANCE_LOW
)
val consumedLaunchIntent = savedInstanceState?.getString(KEY_CONSUMED_LAUNCH_INTENT)
val currentLaunchIntent = intent.launchKey()
if (currentLaunchIntent == null || currentLaunchIntent != consumedLaunchIntent) {
appViewModel.handleDeeplinkIntent(intent)
}
installSplashScreen()
enableAppEdgeToEdge()
setContent {
AppThemeSurface(
modifier = Modifier.semantics {
testTagsAsResourceId = true // see https://github.com/appium/appium/issues/15138
}
) {
val scope = rememberCoroutineScope()
val isRecoveryMode by walletViewModel.isRecoveryMode.collectAsStateWithLifecycle()
val notificationsGranted by settingsViewModel.notificationsGranted.collectAsStateWithLifecycle()
val walletExists by walletViewModel.walletState
.map { it.walletExists }
.collectAsStateWithLifecycle(initialValue = walletViewModel.walletExists)
val isShowingMigrationLoading by walletViewModel.isShowingMigrationLoading.collectAsStateWithLifecycle()
val restoreState by walletViewModel.restoreState.collectAsStateWithLifecycle()
val hazeState = rememberHazeState(blurEnabled = true)
LaunchedEffect(
walletExists,
isRecoveryMode,
notificationsGranted,
restoreState,
) {
val canStartService = walletExists && notificationsGranted && restoreState.isIdle()
if (canStartService && !isRecoveryMode) {
tryStartForegroundService()
}
}
if (isShowingMigrationLoading && !isRecoveryMode) {
MigrationLoadingScreen(isVisible = true)
} else if (!walletViewModel.walletExists && !isRecoveryMode) {
OnboardingNav(
startupNavController = rememberNavController(),
scope = scope,
appViewModel = appViewModel,
walletViewModel = walletViewModel,
)
} else {
val isAuthenticated by appViewModel.isAuthenticated.collectAsStateWithLifecycle()
IsOnlineTracker(appViewModel)
ContentView(
appViewModel = appViewModel,
walletViewModel = walletViewModel,
blocktankViewModel = blocktankViewModel,
currencyViewModel = currencyViewModel,
activityListViewModel = activityListViewModel,
transferViewModel = transferViewModel,
settingsViewModel = settingsViewModel,
backupsViewModel = backupsViewModel,
hazeState = hazeState,
modifier = Modifier.hazeSource(hazeState, zIndex = 0f),
)
AnimatedVisibility(
visible = !isAuthenticated,
enter = fadeIn(),
exit = fadeOut(),
) {
AuthCheckView(
showLogoOnPin = true,
appViewModel = appViewModel,
settingsViewModel = settingsViewModel,
onSuccess = { appViewModel.setIsAuthenticated(true) },
)
}
val showForgotPinSheet by appViewModel.showForgotPinSheet.collectAsStateWithLifecycle()
if (showForgotPinSheet) {
ForgotPinSheet(
onDismiss = { appViewModel.setShowForgotPin(false) },
onResetClick = { walletViewModel.wipeWallet() },
)
}
LaunchedEffect(appViewModel) {
appViewModel.mainScreenEffect.collect {
when (it) {
MainScreenEffect.WipeWallet -> walletViewModel.wipeWallet()
else -> Unit
}
}
}
}
val currentToast by appViewModel.currentToast.collectAsStateWithLifecycle()
ToastOverlay(
toast = currentToast,
hazeState = hazeState,
onDismiss = { appViewModel.hideToast() },
onDragStart = { appViewModel.pauseToast() },
onDragEnd = { appViewModel.resumeToast() }
)
val transactionSheetDetails by appViewModel.transactionSheet.collectAsStateWithLifecycle()
if (transactionSheetDetails != NewTransactionSheetDetails.EMPTY) {
NewTransactionSheet(
appViewModel = appViewModel,
currencyViewModel = currencyViewModel,
settingsViewModel = settingsViewModel,
)
}
SplashScreen(appViewModel.splashVisible)
}
}
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
appViewModel.handleDeeplinkIntent(intent)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
intent.launchKey()?.let { outState.putString(KEY_CONSUMED_LAUNCH_INTENT, it) }
}
override fun onDestroy() {
super.onDestroy()
if (!settingsViewModel.notificationsGranted.value) {
runCatching {
stopService(Intent(this, LightningNodeService::class.java))
}
}
}
/**
* Attempts to start the LightningNodeService if it's not already running.
*/
private fun tryStartForegroundService() {
runCatching {
Logger.debug("Attempting to start LightningNodeService", context = "MainActivity")
startForegroundService(Intent(this, LightningNodeService::class.java))
}.onFailure { error ->
Logger.error("Failed to start LightningNodeService", error, context = "MainActivity")
}
}
}
private fun Intent?.launchKey(): String? {
this ?: return null
return when (action) {
Intent.ACTION_VIEW -> data?.toString()
else -> null
}
}
@Composable
private fun OnboardingNav(
startupNavController: NavHostController,
scope: CoroutineScope,
appViewModel: AppViewModel,
walletViewModel: WalletViewModel,
) {
NavHost(
navController = startupNavController,
startDestination = StartupRoutes.Terms,
) {
composable<StartupRoutes.Terms> {
TermsOfUseScreen(
onNavigateToIntro = {
startupNavController.navigateTo(StartupRoutes.Intro)
}
)
}
composableWithDefaultTransitions<StartupRoutes.Intro> {
IntroScreen(
onStartClick = {
startupNavController.navigateTo(StartupRoutes.Slides())
},
onSkipClick = {
startupNavController.navigateTo(StartupRoutes.Slides(StartupRoutes.LAST_SLIDE_INDEX))
},
)
}
composableWithDefaultTransitions<StartupRoutes.Slides> { navBackEntry ->
val route = navBackEntry.toRoute<StartupRoutes.Slides>()
val isGeoBlocked by appViewModel.isGeoBlocked.collectAsStateWithLifecycle()
OnboardingSlidesScreen(
currentTab = route.tab,
isGeoBlocked = isGeoBlocked,
onAdvancedSetupClick = { startupNavController.navigateTo(StartupRoutes.Advanced) },
onCreateClick = {
scope.launch {
runCatching {
appViewModel.resetIsAuthenticatedState()
walletViewModel.setInitNodeLifecycleState()
walletViewModel.createWallet(bip39Passphrase = null)
}.onFailure {
appViewModel.toast(it)
}
}
},
onRestoreClick = {
startupNavController.navigateTo(
StartupRoutes.WarningMultipleDevices
)
},
)
}
composableWithDefaultTransitions<StartupRoutes.WarningMultipleDevices> {
WarningMultipleDevicesScreen(
onBackClick = {
startupNavController.popBackStack()
},
onConfirmClick = {
startupNavController.navigateTo(StartupRoutes.Restore)
}
)
}
composableWithDefaultTransitions<StartupRoutes.Restore> {
RestoreWalletScreen(
onBackClick = { startupNavController.popBackStack() },
onRestoreClick = { mnemonic, passphrase ->
scope.launch {
runCatching {
appViewModel.resetIsAuthenticatedState()
walletViewModel.restoreWallet(mnemonic, passphrase)
}.onFailure {
appViewModel.toast(it)
}
}
}
)
}
composableWithDefaultTransitions<StartupRoutes.Advanced> {
CreateWalletWithPassphraseScreen(
onBackClick = { startupNavController.popBackStack() },
onCreateClick = { passphrase ->
scope.launch {
runCatching {
appViewModel.resetIsAuthenticatedState()
walletViewModel.createWallet(bip39Passphrase = passphrase)
}.onFailure {
appViewModel.toast(it)
}
}
},
)
}
}
}
private object StartupRoutes {
const val LAST_SLIDE_INDEX = 4
@Serializable
data object Terms
@Serializable
data object Intro
@Serializable
data class Slides(val tab: Int = 0)
@Serializable
data object Restore
@Serializable
data object Advanced
@Serializable
data object WarningMultipleDevices
}