2022-03-08 11:05:03 -08:00
|
|
|
package co.electriccoin.zcash.ui
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
import android.os.Bundle
|
2021-12-03 05:19:15 -08:00
|
|
|
import android.os.SystemClock
|
2021-09-14 14:02:06 -07:00
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
import androidx.activity.compose.setContent
|
2023-10-12 10:04:23 -07:00
|
|
|
import androidx.activity.enableEdgeToEdge
|
2021-10-09 07:37:03 -07:00
|
|
|
import androidx.activity.viewModels
|
2021-12-03 05:19:15 -08:00
|
|
|
import androidx.annotation.VisibleForTesting
|
2021-12-09 12:18:18 -08:00
|
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
2022-10-03 06:59:09 -07:00
|
|
|
import androidx.compose.runtime.Composable
|
2023-02-20 08:07:26 -08:00
|
|
|
import androidx.compose.runtime.CompositionLocalProvider
|
2022-10-03 06:59:09 -07:00
|
|
|
import androidx.compose.runtime.getValue
|
2021-12-09 12:18:18 -08:00
|
|
|
import androidx.compose.ui.Modifier
|
2021-12-03 05:19:15 -08:00
|
|
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
2023-02-17 08:39:15 -08:00
|
|
|
import androidx.lifecycle.Lifecycle
|
2022-12-22 00:38:02 -08:00
|
|
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
2023-02-17 08:39:15 -08:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
|
import androidx.lifecycle.repeatOnLifecycle
|
2022-01-31 13:42:22 -08:00
|
|
|
import androidx.navigation.NavHostController
|
2023-10-10 03:41:17 -07:00
|
|
|
import cash.z.ecc.android.sdk.fixture.WalletFixture
|
|
|
|
import cash.z.ecc.android.sdk.model.SeedPhrase
|
|
|
|
import cash.z.ecc.android.sdk.model.ZcashNetwork
|
|
|
|
import cash.z.ecc.sdk.type.fromResources
|
|
|
|
import co.electriccoin.zcash.spackle.FirebaseTestLabUtil
|
2022-10-13 06:13:41 -07:00
|
|
|
import co.electriccoin.zcash.ui.common.BindCompLocalProvider
|
2023-02-20 08:07:26 -08:00
|
|
|
import co.electriccoin.zcash.ui.configuration.RemoteConfig
|
2022-06-08 07:22:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.design.component.ConfigurationOverride
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.design.component.GradientSurface
|
2022-06-08 07:22:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.design.component.Override
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.design.theme.ZcashTheme
|
2023-03-03 05:06:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.screen.backup.WrapNewWallet
|
2023-10-10 03:41:17 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.home.model.OnboardingState
|
2023-02-17 08:39:15 -08:00
|
|
|
import co.electriccoin.zcash.ui.screen.home.viewmodel.HomeViewModel
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.screen.home.viewmodel.SecretState
|
|
|
|
import co.electriccoin.zcash.ui.screen.home.viewmodel.WalletViewModel
|
2022-06-09 10:17:58 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.onboarding.WrapOnboarding
|
2023-10-10 03:41:17 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.onboarding.persistExistingWalletWithSeedPhrase
|
|
|
|
import co.electriccoin.zcash.ui.screen.securitywarning.WrapSecurityWarning
|
2022-10-03 06:59:09 -07:00
|
|
|
import co.electriccoin.zcash.ui.screen.warning.WrapNotEnoughSpace
|
|
|
|
import co.electriccoin.zcash.ui.screen.warning.viewmodel.StorageCheckViewModel
|
2023-02-17 08:39:15 -08:00
|
|
|
import co.electriccoin.zcash.work.WorkIds
|
2022-06-08 07:22:09 -07:00
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
2023-02-17 08:39:15 -08:00
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
import kotlinx.coroutines.flow.filterNotNull
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
import kotlinx.coroutines.launch
|
2021-12-03 05:19:15 -08:00
|
|
|
import kotlin.time.Duration
|
|
|
|
import kotlin.time.Duration.Companion.milliseconds
|
|
|
|
import kotlin.time.Duration.Companion.seconds
|
2021-09-14 14:02:06 -07:00
|
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
2021-10-09 07:37:03 -07:00
|
|
|
|
2023-02-20 08:07:26 -08:00
|
|
|
val homeViewModel by viewModels<HomeViewModel>()
|
|
|
|
|
2022-01-31 13:42:22 -08:00
|
|
|
val walletViewModel by viewModels<WalletViewModel>()
|
|
|
|
|
2022-10-03 06:59:09 -07:00
|
|
|
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
|
|
|
val storageCheckViewModel by viewModels<StorageCheckViewModel>()
|
|
|
|
|
2022-01-31 13:42:22 -08:00
|
|
|
lateinit var navControllerForTesting: NavHostController
|
2021-11-12 04:09:30 -08:00
|
|
|
|
2022-06-08 07:22:09 -07:00
|
|
|
val configurationOverrideFlow = MutableStateFlow<ConfigurationOverride?>(null)
|
|
|
|
|
2021-09-14 14:02:06 -07:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-12-03 05:19:15 -08:00
|
|
|
|
|
|
|
setupSplashScreen()
|
|
|
|
|
2023-02-09 03:47:04 -08:00
|
|
|
setupUiContent()
|
2023-02-17 08:39:15 -08:00
|
|
|
|
|
|
|
monitorForBackgroundSync()
|
2021-09-14 14:02:06 -07:00
|
|
|
}
|
2021-11-17 12:19:49 -08:00
|
|
|
|
2021-12-03 05:19:15 -08:00
|
|
|
private fun setupSplashScreen() {
|
2021-12-09 12:18:18 -08:00
|
|
|
val splashScreen = installSplashScreen()
|
|
|
|
val start = SystemClock.elapsedRealtime().milliseconds
|
|
|
|
|
2022-04-04 05:48:47 -07:00
|
|
|
splashScreen.setKeepOnScreenCondition {
|
2021-12-09 12:18:18 -08:00
|
|
|
if (SPLASH_SCREEN_DELAY > Duration.ZERO) {
|
|
|
|
val now = SystemClock.elapsedRealtime().milliseconds
|
2021-12-03 05:19:15 -08:00
|
|
|
|
2021-12-09 12:18:18 -08:00
|
|
|
// This delay is for debug purposes only; do not enable for production usage.
|
|
|
|
if (now - start < SPLASH_SCREEN_DELAY) {
|
2022-04-04 05:48:47 -07:00
|
|
|
return@setKeepOnScreenCondition true
|
2021-12-09 12:18:18 -08:00
|
|
|
}
|
2021-12-03 05:19:15 -08:00
|
|
|
}
|
2021-12-09 12:18:18 -08:00
|
|
|
|
2023-02-20 08:07:26 -08:00
|
|
|
// Note this condition needs to be kept in sync with the condition in MainContent()
|
|
|
|
homeViewModel.configurationFlow.value == null || SecretState.Loading == walletViewModel.secretState.value
|
2021-12-03 05:19:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:31:39 -08:00
|
|
|
private fun setupUiContent() {
|
2023-10-12 10:04:23 -07:00
|
|
|
// Turn off the decor fitting system windows, which allows us to handle insets,
|
|
|
|
// including IME animations, and go edge-to-edge.
|
|
|
|
// This also sets up the initial system bar style based on the platform theme
|
|
|
|
enableEdgeToEdge()
|
|
|
|
|
2021-12-06 12:31:39 -08:00
|
|
|
setContent {
|
2022-06-08 07:22:09 -07:00
|
|
|
Override(configurationOverrideFlow) {
|
|
|
|
ZcashTheme {
|
|
|
|
GradientSurface(
|
|
|
|
Modifier
|
|
|
|
.fillMaxWidth()
|
|
|
|
.fillMaxHeight()
|
|
|
|
) {
|
2022-10-13 06:13:41 -07:00
|
|
|
BindCompLocalProvider {
|
2022-12-22 00:38:02 -08:00
|
|
|
val isEnoughSpace by storageCheckViewModel.isEnoughSpace.collectAsStateWithLifecycle()
|
2022-10-03 06:59:09 -07:00
|
|
|
if (isEnoughSpace == false) {
|
|
|
|
WrapNotEnoughSpace()
|
|
|
|
} else {
|
|
|
|
MainContent()
|
2022-08-09 12:23:38 -07:00
|
|
|
}
|
2021-12-09 12:18:18 -08:00
|
|
|
}
|
2021-12-06 12:31:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-31 13:42:22 -08:00
|
|
|
|
2022-12-22 00:38:02 -08:00
|
|
|
// Force collection to improve performance; sync can start happening while
|
|
|
|
// the user is going through the backup flow.
|
|
|
|
walletViewModel.synchronizer.collectAsStateWithLifecycle()
|
2022-09-29 06:13:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 06:59:09 -07:00
|
|
|
@Composable
|
|
|
|
private fun MainContent() {
|
2023-02-20 08:07:26 -08:00
|
|
|
val configuration = homeViewModel.configurationFlow.collectAsStateWithLifecycle().value
|
|
|
|
val secretState = walletViewModel.secretState.collectAsStateWithLifecycle().value
|
|
|
|
|
|
|
|
// Note this condition needs to be kept in sync with the condition in setupSplashScreen()
|
|
|
|
if (null == configuration || secretState == SecretState.Loading) {
|
|
|
|
// For now, keep displaying splash screen using condition above.
|
|
|
|
// In the future, we might consider displaying something different here.
|
|
|
|
} else {
|
|
|
|
// Note that the deeply nested child views will probably receive arguments derived from
|
|
|
|
// the configuration. The CompositionLocalProvider is helpful for passing the configuration
|
|
|
|
// to the "platform" layer, which is where the arguments will be derived from.
|
|
|
|
CompositionLocalProvider(RemoteConfig provides configuration) {
|
|
|
|
when (secretState) {
|
|
|
|
SecretState.None -> {
|
|
|
|
WrapOnboarding()
|
|
|
|
}
|
2023-10-10 03:41:17 -07:00
|
|
|
is SecretState.NeedsWarning -> {
|
|
|
|
WrapSecurityWarning(
|
|
|
|
onBack = { walletViewModel.persistOnboardingState(OnboardingState.NONE) },
|
|
|
|
onConfirm = {
|
|
|
|
walletViewModel.persistOnboardingState(OnboardingState.NEEDS_BACKUP)
|
|
|
|
|
|
|
|
if (FirebaseTestLabUtil.isFirebaseTestLab(applicationContext)) {
|
|
|
|
persistExistingWalletWithSeedPhrase(
|
|
|
|
applicationContext,
|
|
|
|
walletViewModel,
|
|
|
|
SeedPhrase.new(WalletFixture.Alice.seedPhrase),
|
|
|
|
WalletFixture.Alice.getBirthday(ZcashNetwork.fromResources(applicationContext))
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
walletViewModel.persistNewWallet()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2023-02-20 08:07:26 -08:00
|
|
|
is SecretState.NeedsBackup -> {
|
2023-03-03 05:06:03 -08:00
|
|
|
WrapNewWallet(
|
2023-02-20 08:07:26 -08:00
|
|
|
secretState.persistableWallet,
|
2023-10-10 03:41:17 -07:00
|
|
|
onBackupComplete = { walletViewModel.persistOnboardingState(OnboardingState.READY) }
|
2023-02-20 08:07:26 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
is SecretState.Ready -> {
|
|
|
|
Navigation()
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
error("Unhandled secret state: $secretState")
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 06:59:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 08:39:15 -08:00
|
|
|
private fun monitorForBackgroundSync() {
|
|
|
|
val isEnableBackgroundSyncFlow = run {
|
|
|
|
val homeViewModel by viewModels<HomeViewModel>()
|
|
|
|
val isSecretReadyFlow = walletViewModel.secretState.map { it is SecretState.Ready }
|
|
|
|
val isBackgroundSyncEnabledFlow = homeViewModel.isBackgroundSyncEnabled.filterNotNull()
|
|
|
|
|
|
|
|
isSecretReadyFlow.combine(isBackgroundSyncEnabledFlow) { isSecretReady, isBackgroundSyncEnabled ->
|
|
|
|
isSecretReady && isBackgroundSyncEnabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lifecycleScope.launch {
|
|
|
|
repeatOnLifecycle(Lifecycle.State.RESUMED) {
|
|
|
|
isEnableBackgroundSyncFlow.collect { isEnableBackgroundSync ->
|
|
|
|
if (isEnableBackgroundSync) {
|
|
|
|
WorkIds.enableBackgroundSynchronization(application)
|
|
|
|
} else {
|
|
|
|
WorkIds.disableBackgroundSynchronization(application)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 05:19:15 -08:00
|
|
|
companion object {
|
|
|
|
@VisibleForTesting
|
|
|
|
internal val SPLASH_SCREEN_DELAY = 0.seconds
|
2022-06-14 23:53:54 -07:00
|
|
|
}
|
|
|
|
}
|