From 32dc397e0010ceb3cd986a8965481a87edc1341f Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Thu, 2 Jul 2026 00:49:41 +0200 Subject: [PATCH] This some trash code. --- .../ui/composable/navigation/TorchNavHost.kt | 27 ++-- .../ui/view/model/NavigationViewModel.kt | 133 ++++++++++-------- .../compose/ui/view/model/NotaryViewModel.kt | 1 + 3 files changed, 95 insertions(+), 66 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/composable/navigation/TorchNavHost.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/composable/navigation/TorchNavHost.kt index 8e030718..f2e97f03 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/composable/navigation/TorchNavHost.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/composable/navigation/TorchNavHost.kt @@ -22,12 +22,16 @@ import at.torch.compose.ui.composable.ShareProfileScreen import at.torch.compose.ui.composable.UnsyncedProfileScreen import at.torch.compose.ui.composable.navigation.routes.ActiveProfileRoute import at.torch.compose.ui.composable.navigation.routes.ChatRoomCreationRoute +import at.torch.compose.ui.composable.navigation.routes.LandingRoute import at.torch.compose.ui.composable.navigation.routes.LoadingRoute import at.torch.compose.ui.composable.navigation.routes.NostrEventDetailRoute import at.torch.compose.ui.composable.navigation.routes.ShareProfileRoute import at.torch.compose.ui.composable.navigation.routes.SocialPreconditionRoute import at.torch.compose.ui.composable.navigation.routes.SovereignWalletStartupRoute import at.torch.compose.ui.composable.navigation.routes.WriteNewNoteRoute +import at.torch.compose.ui.view.model.NavigationViewModel +import at.torch.compose.ui.view.model.NotaryViewModel +import at.torch.compose.ui.view.model.SynchronizationViewModel import at.torch.compose.ui.view.state.NavigationUIState import co.touchlab.kermit.Logger import fr.acinq.phoenix.PhoenixGlobal @@ -84,8 +88,8 @@ fun TorchNavHost( phoenixGlobal = phoenixGlobal )) - val navigationViewModel: at.torch.compose.ui.view.model.NavigationViewModel = viewModel ( - factory = at.torch.compose.ui.view.model.NavigationViewModel.factory( + val navigationViewModel: NavigationViewModel = viewModel ( + factory = NavigationViewModel.factory( activeWallet = sovereignWalletViewModel.activeWalletInUI, initialNavigationUIState = NavigationUIState.Loading(), nostrRepository = databaseNostrRepository, @@ -94,8 +98,8 @@ fun TorchNavHost( ) - val notaryViewModel: at.torch.compose.ui.view.model.NotaryViewModel = viewModel( - factory = at.torch.compose.ui.view.model.NotaryViewModel.factory( + val notaryViewModel: NotaryViewModel = viewModel( + factory = NotaryViewModel.factory( activeWalletStateFlow = sovereignWalletViewModel.activeWalletInUI, nostrRepository = databaseNostrRepository, chatRepository = databaseChatRepository, @@ -104,8 +108,8 @@ fun TorchNavHost( ) // TODO: Produce a notary UI Element... - val synchronizationViewModel: at.torch.compose.ui.view.model.SynchronizationViewModel = viewModel( - factory = at.torch.compose.ui.view.model.SynchronizationViewModel.factory( + val synchronizationViewModel: SynchronizationViewModel = viewModel( + factory = SynchronizationViewModel.factory( activeWalletStateFlow = sovereignWalletViewModel.activeWalletInUI, nostrRepository = databaseNostrRepository, chatRepository = databaseChatRepository, @@ -229,17 +233,22 @@ fun TorchNavHost( navController = navController, startDestination = LoadingRoute() ) { - composable { + composable { backStackEntry -> + val route = backStackEntry.toRoute() + at.torch.compose.ui.composable.SovereignWalletStartupScreen( sovereignWalletViewModel = sovereignWalletViewModel, onNavigateToWalletLandingPage = { navController.navigate( route = LoadingRoute( - text = "Loading Aux" + text = "Landing... Aux" ) ) }, onNavigateToWalletIntroPage = { + applicationIOScope.launch { + navigationViewModel.loadInitialNostrProfile(route) + } navController.navigate( route = LoadingRoute( text = "Introducing... Aux" @@ -248,7 +257,7 @@ fun TorchNavHost( }, onSuccessfulStartup = { applicationIOScope.launch { - navigationViewModel.loadNostrProfile() + navigationViewModel.loadInitialNostrProfile(route) } }, forceWalletId = null, diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NavigationViewModel.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NavigationViewModel.kt index 39e47c17..2f8abee1 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NavigationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NavigationViewModel.kt @@ -4,6 +4,8 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory +import at.torch.compose.database.model.intermdiate.LocalAccount +import at.torch.compose.ui.composable.navigation.routes.SovereignWalletStartupRoute import at.torch.compose.ui.view.state.NavigationUIState import co.touchlab.kermit.Logger import fr.acinq.phoenix.data.ActiveWallet @@ -17,6 +19,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.getAndUpdate import kotlinx.coroutines.launch import kotlin.time.Duration.Companion.milliseconds @@ -59,19 +62,84 @@ class NavigationViewModel( observeProfile() } - suspend fun loadNostrProfile() { + suspend fun loadInitialNostrProfile(startupRoute: SovereignWalletStartupRoute? = null) { + logger.d("loadInitialNostrProfile: $startupRoute") val activeUserPublicKey = nostrRepository.getLocalAccounts().firstOrNull()?.profile?.publicKey if (activeUserPublicKey == null) { - // TODO: We can just go directly to startup witout fear... _navigationUIState.getAndUpdate { - NavigationUIState.Landing + if (startupRoute != null) { + NavigationUIState.Landing + } else { + NavigationUIState.StartupPhoenix + } } } else { -// _navigationUIState.getAndUpdate { -// NavigationUIState.Loading(text = "Getting profile ready") -// } - loadNostrProfile(activeUserPublicKey) + if (startupRoute != null) { + val localAccount = nostrRepository.observeProfile( + publicKey = activeUserPublicKey + ).firstOrNull() + + processLocalAccount(localAccount) + } else { + _navigationUIState.getAndUpdate { + NavigationUIState.StartupPhoenix + } + } + } + } + + private fun processLocalAccount(localAccount: LocalAccount?) { + logger.i("processLocalAccount: $localAccount") + + _navigationUIState.getAndUpdate { + if (localAccount == null) { + logger.i("We need to be on the landing screen so user creates account") + NavigationUIState.Landing + } else if (localAccount.unsignedNostrEvent == null) { + logger.i("We need to be on the landing screen so user creates account") + NavigationUIState.Landing + } else if (localAccount.broadcastNostrEventReceipt != null) { + logger.i("We have a broadcast receipt: ${localAccount.profile}") + NavigationUIState.ProfileLoaded( + publicKey = localAccount.unsignedNostrEvent.pubKey + ) + } else if (localAccount.unsignedNostrEvent.signedAt != null && localAccount.profile != null) { + logger.i("We have successfully synced a profile: ${localAccount.profile}") + NavigationUIState.ProfileLoaded( + publicKey = localAccount.unsignedNostrEvent.pubKey + ) + } else if (localAccount.broadcastNostrEventRequest != null) { + logger.i("We have a broadcast request: ${localAccount.broadcastNostrEventRequest}") + NavigationUIState.UnannouncedProfile( + broadcastNostrEventRequest = localAccount.broadcastNostrEventRequest + ) + } else if (localAccount.profile != null) { + logger.i("We have a profile that needs to queued for broadcast: ${localAccount.profile}") + NavigationUIState.UnqueuedProfile( + profile = localAccount.profile + ) + } else if (localAccount.nostrEvent != null) { + logger.i("Nostr event with the profile needs to be indexed: ${localAccount.nostrEvent}") + NavigationUIState.UnindexedProfile( + nostrEvent = localAccount.nostrEvent + ) + } else if (localAccount.unsignedNostrEvent.signedAt != null && localAccount.synchronizeNostrEventRequests.isEmpty()) { + logger.i("We should request to sync profile: ${localAccount.unsignedNostrEvent}") + NavigationUIState.UnqueuedProfileSynchronization( + unsignedNostrEvent = localAccount.unsignedNostrEvent + ) + } else if (localAccount.unsignedNostrEvent.signedAt != null && localAccount.synchronizeNostrEventRequests.isEmpty()) { + logger.i("We should be syncing the profile: ${localAccount.unsignedNostrEvent}") + NavigationUIState.UnsyncedProfile( + unsignedNostrEvent = localAccount.unsignedNostrEvent + ) + } else { + logger.i("We have an unsigned profile: ${localAccount.unsignedNostrEvent}") + NavigationUIState.UnsignedProfile( + unsignedNostrEvent = localAccount.unsignedNostrEvent + ) + } } } @@ -79,56 +147,7 @@ class NavigationViewModel( nostrRepository.observeProfile( publicKey = activeUserPublicKey ).distinctUntilChanged().collect { localProfile -> - logger.i("Local Profile: $localProfile") - _navigationUIState.getAndUpdate { - if (localProfile == null) { - logger.i("We need to be on the landing screen so user creates account") - NavigationUIState.Landing - } else if (localProfile.unsignedNostrEvent == null) { - logger.i("We need to be on the landing screen so user creates account") - NavigationUIState.Landing - } else if (localProfile.broadcastNostrEventReceipt != null) { - logger.i("We have a broadcast receipt: ${localProfile.profile}") - NavigationUIState.ProfileLoaded( - publicKey = localProfile.unsignedNostrEvent.pubKey - ) - } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.profile != null) { - logger.i("We have successfully synced a profile: ${localProfile.profile}") - NavigationUIState.ProfileLoaded( - publicKey = localProfile.unsignedNostrEvent.pubKey - ) - } else if (localProfile.broadcastNostrEventRequest != null) { - logger.i("We have a broadcast request: ${localProfile.broadcastNostrEventRequest}") - NavigationUIState.UnannouncedProfile( - broadcastNostrEventRequest = localProfile.broadcastNostrEventRequest - ) - } else if (localProfile.profile != null) { - logger.i("We have a profile that needs to queued for broadcast: ${localProfile.profile}") - NavigationUIState.UnqueuedProfile( - profile = localProfile.profile - ) - } else if (localProfile.nostrEvent != null) { - logger.i("Nostr event with the profile needs to be indexed: ${localProfile.nostrEvent}") - NavigationUIState.UnindexedProfile( - nostrEvent = localProfile.nostrEvent - ) - } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.synchronizeNostrEventRequests.isEmpty()) { - logger.i("We should request to sync profile: ${localProfile.unsignedNostrEvent}") - NavigationUIState.UnqueuedProfileSynchronization( - unsignedNostrEvent = localProfile.unsignedNostrEvent - ) - } else if (localProfile.unsignedNostrEvent.signedAt != null && localProfile.synchronizeNostrEventRequests.isEmpty()) { - logger.i("We should be syncing the profile: ${localProfile.unsignedNostrEvent}") - NavigationUIState.UnsyncedProfile( - unsignedNostrEvent = localProfile.unsignedNostrEvent - ) - } else { - logger.i("We have an unsigned profile: ${localProfile.unsignedNostrEvent}") - NavigationUIState.UnsignedProfile( - unsignedNostrEvent = localProfile.unsignedNostrEvent - ) - } - } + processLocalAccount(localProfile) } } diff --git a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NotaryViewModel.kt b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NotaryViewModel.kt index 281efd4f..2d344a4c 100644 --- a/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NotaryViewModel.kt +++ b/composeApp/src/commonMain/kotlin/at/torch/compose/ui/view/model/NotaryViewModel.kt @@ -52,6 +52,7 @@ class NotaryViewModel( private val logger = Logger.withTag(TAG) init { + logger.d("NotaryViewModel.init") scope.launch { activeWalletStateFlow.collectLatest { activeWallet -> logger.d("Notary ActiveWallet: $activeWallet")