Fix initial load

This commit is contained in:
Kgothatso Ngako
2026-03-26 03:57:58 +02:00
parent f73a46ae5c
commit 1f2ffd1a0b
2 changed files with 18 additions and 11 deletions

View File

@@ -44,7 +44,6 @@ fun AuxNavHost(
navController: NavHostController
) {
val lifecycleOwner = LocalLifecycleOwner.current
val scope = rememberCoroutineScope()
val databaseManager = DatabaseManager(auxGlobal)
@@ -85,7 +84,9 @@ fun AuxNavHost(
}
is NavigationUIState.UnsignedProfile -> {
navController.navigate(
route = UnsignedProfileRoute
route = UnsignedProfileRoute(
state.unsignedNostrEvent.id
)
) {
popUpTo(0)
}

View File

@@ -40,6 +40,8 @@ class NavigationViewModel(
}
}
private val logger = Logger.withTag(TAG)
private val _navigationUIState = MutableStateFlow(
initialNavigationUIState
)
@@ -49,42 +51,46 @@ class NavigationViewModel(
observeProfile()
}
private val logger = Logger.withTag(TAG)
private fun observeProfile() {
logger.i("observeProfile")
viewModelScope.launch {
delay(2_100) // Looking busy...
_navigationUIState.getAndUpdate {
NavigationUIState.Landing
}
logger.i("Navigation UI State is Landing")
val publicKey = SeedManager.activePublicKey().toHexKey()
logger.i("Observing: $publicKey")
nostrRepository.observeProfile(
publicKey = SeedManager.activePublicKey().toHexKey()
).map { localProfile ->
publicKey = publicKey
).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.profile != null) {
logger.i("We have a profile: ${localProfile.profile}")
NavigationUIState.ProfileLoaded(
profile = localProfile.profile
)
} else if (localProfile.nostrEvent != null) {
logger.i("Nostr event with the profile needs to be announced")
NavigationUIState.UnannouncedProfile(
nostrEvent = localProfile.nostrEvent
)
} else {
logger.i("We have an unsigned profile: ${localProfile.unsignedNostrEvent}")
NavigationUIState.UnsignedProfile(
unsignedNostrEvent = localProfile.unsignedNostrEvent
)
}
}
}.catch { e ->
logger.e("Error observing profile:", e)
_navigationUIState.getAndUpdate {
NavigationUIState.Error
}
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), NavigationUIState.Landing)
}
}
}