improve error handling

This commit is contained in:
2026-05-06 14:25:04 +07:00
parent eb2f543f53
commit 8b5a8b0e48
3 changed files with 156 additions and 101 deletions
@@ -3,6 +3,7 @@ package su.reya.coop
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.MaterialExpressiveTheme
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
@@ -32,6 +33,10 @@ val LocalNostrViewModel = staticCompositionLocalOf<NostrViewModel> {
error("No NostrViewModel provided")
}
val LocalSnackbarHostState = staticCompositionLocalOf<SnackbarHostState> {
error("No SnackbarHostState provided")
}
@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun App(dbPath: String) {
@@ -53,24 +58,32 @@ fun App(dbPath: String) {
else -> expressiveLightColorScheme()
}
// Snackbar
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(Unit) {
viewModel.initAndConnect(dbPath)
viewModel.startNotificationHandler()
viewModel.getChatRooms()
viewModel.errorEvents.collect { message ->
snackbarHostState.showSnackbar(message)
}
}
MaterialExpressiveTheme(
colorScheme = colorScheme,
) {
CompositionLocalProvider(LocalNostrViewModel provides viewModel) {
CompositionLocalProvider(
LocalNostrViewModel provides viewModel,
LocalSnackbarHostState provides snackbarHostState,
) {
rememberCoroutineScope()
val navController = rememberNavController()
val hasSecret by viewModel.hasSecret.collectAsState(initial = null)
val emptySecret by viewModel.emptySecret.collectAsState(initial = null)
LaunchedEffect(hasSecret) {
LaunchedEffect(emptySecret) {
// Navigate to the home screen if the secret is already set
if (hasSecret == true) {
// Start a background notification handler
viewModel.startNotificationHandler()
if (emptySecret == false) {
// Get chat rooms
viewModel.getChatRooms()
// Navigate to the home screen
@@ -81,11 +94,11 @@ fun App(dbPath: String) {
}
// Show loading screen while initializing
if (hasSecret == null) return@CompositionLocalProvider
if (emptySecret == null) return@CompositionLocalProvider
NavHost(
navController = navController,
startDestination = if (hasSecret == true) Screen.Home else Screen.Onboarding
startDestination = if (emptySecret == false) Screen.Home else Screen.Onboarding
) {
composable<Screen.Onboarding> { backStackEntry ->
OnboardingScreen(