adding error views to profile, messenger and settings screens

This commit is contained in:
Adam Brown 2022-10-09 15:53:26 +01:00
parent 7e0d4d6013
commit ec5c58a95c
7 changed files with 43 additions and 41 deletions

View File

@ -10,22 +10,27 @@ fun <T : Any> Spider(currentPage: SpiderPage<T>, onNavigate: (SpiderPage<out T>?
val pageCache = remember { mutableMapOf<Route<*>, SpiderPage<out T>>() } val pageCache = remember { mutableMapOf<Route<*>, SpiderPage<out T>>() }
pageCache[currentPage.route] = currentPage pageCache[currentPage.route] = currentPage
val navigateAndPopStack = {
pageCache.remove(currentPage.route)
onNavigate(pageCache[currentPage.parent])
}
val itemScope = object : SpiderItemScope {
override fun goBack() {
navigateAndPopStack()
}
}
val computedWeb = remember(true) { val computedWeb = remember(true) {
mutableMapOf<Route<*>, @Composable (T) -> Unit>().also { computedWeb -> mutableMapOf<Route<*>, @Composable (T) -> Unit>().also { computedWeb ->
val scope = object : SpiderScope { val scope = object : SpiderScope {
override fun <T> item(route: Route<T>, content: @Composable (T) -> Unit) { override fun <T> item(route: Route<T>, content: @Composable SpiderItemScope.(T) -> Unit) {
computedWeb[route] = { content(it as T) } computedWeb[route] = { content(itemScope, it as T) }
} }
} }
graph.invoke(scope) graph.invoke(scope)
} }
} }
val navigateAndPopStack = {
pageCache.remove(currentPage.route)
onNavigate(pageCache[currentPage.parent])
}
Column { Column {
if (currentPage.hasToolbar) { if (currentPage.hasToolbar) {
Toolbar( Toolbar(
@ -40,7 +45,11 @@ fun <T : Any> Spider(currentPage: SpiderPage<T>, onNavigate: (SpiderPage<out T>?
interface SpiderScope { interface SpiderScope {
fun <T> item(route: Route<T>, content: @Composable (T) -> Unit) fun <T> item(route: Route<T>, content: @Composable SpiderItemScope.(T) -> Unit)
}
interface SpiderItemScope {
fun goBack()
} }
data class SpiderPage<T>( data class SpiderPage<T>(

View File

@ -43,10 +43,7 @@ import app.dapk.st.core.LifecycleEffect
import app.dapk.st.core.StartObserving import app.dapk.st.core.StartObserving
import app.dapk.st.core.components.CenteredLoading import app.dapk.st.core.components.CenteredLoading
import app.dapk.st.core.extensions.takeIfContent import app.dapk.st.core.extensions.takeIfContent
import app.dapk.st.design.components.MessengerUrlIcon import app.dapk.st.design.components.*
import app.dapk.st.design.components.MissingAvatarIcon
import app.dapk.st.design.components.SmallTalkTheme
import app.dapk.st.design.components.Toolbar
import app.dapk.st.matrix.common.RoomId import app.dapk.st.matrix.common.RoomId
import app.dapk.st.matrix.common.UserId import app.dapk.st.matrix.common.UserId
import app.dapk.st.matrix.sync.MessageMeta import app.dapk.st.matrix.sync.MessageMeta
@ -95,7 +92,7 @@ internal fun MessengerScreen(
}) })
when (state.composerState) { when (state.composerState) {
is ComposerState.Text -> { is ComposerState.Text -> {
Room(state.roomState, replyActions) Room(state.roomState, replyActions, onRetry = { viewModel.post(MessengerAction.OnMessengerVisible(roomId, attachments)) })
TextComposer( TextComposer(
state.composerState, state.composerState,
onTextChange = { viewModel.post(MessengerAction.ComposerTextUpdate(it)) }, onTextChange = { viewModel.post(MessengerAction.ComposerTextUpdate(it)) },
@ -132,7 +129,7 @@ private fun MessengerViewModel.ObserveEvents(galleryLauncher: ActivityResultLaun
} }
@Composable @Composable
private fun ColumnScope.Room(roomStateLce: Lce<MessengerState>, replyActions: ReplyActions) { private fun ColumnScope.Room(roomStateLce: Lce<MessengerState>, replyActions: ReplyActions, onRetry: () -> Unit) {
when (val state = roomStateLce) { when (val state = roomStateLce) {
is Lce.Loading -> CenteredLoading() is Lce.Loading -> CenteredLoading()
is Lce.Content -> { is Lce.Content -> {
@ -165,16 +162,7 @@ private fun ColumnScope.Room(roomStateLce: Lce<MessengerState>, replyActions: Re
} }
} }
is Lce.Error -> { is Lce.Error -> GenericError(cause = state.cause, action = onRetry)
Box(contentAlignment = Alignment.Center) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Text("Something went wrong...")
Button(onClick = {}) {
Text("Retry")
}
}
}
}
} }
} }

View File

@ -15,6 +15,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import app.dapk.st.core.* import app.dapk.st.core.*
import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.core.extensions.unsafeLazy
import app.dapk.st.design.components.GenericError
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
@ -59,7 +60,10 @@ fun Activity.PermissionGuard(state: State<Lce<PermissionResult>>, onGranted: @Co
PermissionResult.ShowRational -> finish() PermissionResult.ShowRational -> finish()
} }
is Lce.Error -> finish() is Lce.Error -> GenericError(message = "Store permission required", label = "Close") {
finish()
}
is Lce.Loading -> { is Lce.Loading -> {
// loading should be quick, let's avoid displaying anything // loading should be quick, let's avoid displaying anything
} }

View File

@ -51,7 +51,6 @@ fun ImageGalleryScreen(viewModel: ImageGalleryViewModel, onTopLevelBack: () -> U
} }
@Composable @Composable
fun ImageGalleryFolders(state: ImageGalleryPage.Folders, onClick: (Folder) -> Unit, onRetry: () -> Unit) { fun ImageGalleryFolders(state: ImageGalleryPage.Folders, onClick: (Folder) -> Unit, onRetry: () -> Unit) {
val screenWidth = LocalConfiguration.current.screenWidthDp val screenWidth = LocalConfiguration.current.screenWidthDp

View File

@ -119,7 +119,7 @@ private fun ProfilePage(context: Context, viewModel: ProfileViewModel, profile:
} }
@Composable @Composable
private fun Invitations(viewModel: ProfileViewModel, invitations: Page.Invitations) { private fun SpiderItemScope.Invitations(viewModel: ProfileViewModel, invitations: Page.Invitations) {
when (val state = invitations.content) { when (val state = invitations.content) {
is Lce.Loading -> CenteredLoading() is Lce.Loading -> CenteredLoading()
is Lce.Content -> { is Lce.Content -> {
@ -147,7 +147,7 @@ private fun Invitations(viewModel: ProfileViewModel, invitations: Page.Invitatio
} }
} }
is Lce.Error -> TODO() is Lce.Error -> GenericError(label = "Go back", cause = state.cause) { goBack() }
} }
} }

View File

@ -63,7 +63,7 @@ internal fun SettingsScreen(viewModel: SettingsViewModel, onSignOut: () -> Unit,
} }
Spider(currentPage = viewModel.state.page, onNavigate = onNavigate) { Spider(currentPage = viewModel.state.page, onNavigate = onNavigate) {
item(Page.Routes.root) { item(Page.Routes.root) {
RootSettings(it) { viewModel.onClick(it) } RootSettings(it, onClick = { viewModel.onClick(it) }, onRetry = { viewModel.start() })
} }
item(Page.Routes.encryption) { item(Page.Routes.encryption) {
Encryption(viewModel, it) Encryption(viewModel, it)
@ -180,7 +180,7 @@ internal fun SettingsScreen(viewModel: SettingsViewModel, onSignOut: () -> Unit,
} }
@Composable @Composable
private fun RootSettings(page: Page.Root, onClick: (SettingItem) -> Unit) { private fun RootSettings(page: Page.Root, onClick: (SettingItem) -> Unit, onRetry: () -> Unit) {
when (val content = page.content) { when (val content = page.content) {
is Lce.Content -> { is Lce.Content -> {
LazyColumn( LazyColumn(
@ -226,12 +226,10 @@ private fun RootSettings(page: Page.Root, onClick: (SettingItem) -> Unit) {
} }
} }
is Lce.Error -> { is Lce.Error -> GenericError(cause = content.cause, action = onRetry)
// TODO
}
is Lce.Loading -> { is Lce.Loading -> {
// TODO // Should be quick enough to avoid needing a loading state
} }
} }
} }
@ -264,7 +262,7 @@ private fun PushProviders(viewModel: SettingsViewModel, state: Page.PushProvider
} }
} }
is Lce.Error -> TODO() is Lce.Error -> GenericError(cause = lce.cause) { viewModel.fetchPushProviders() }
} }
} }

View File

@ -14,6 +14,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import app.dapk.st.core.AppLogTag import app.dapk.st.core.AppLogTag
import app.dapk.st.core.Lce import app.dapk.st.core.Lce
import app.dapk.st.core.components.CenteredLoading
import app.dapk.st.design.components.GenericError
import app.dapk.st.matrix.common.MatrixLogTag import app.dapk.st.matrix.common.MatrixLogTag
private val filterItems = listOf<String?>(null) + (MatrixLogTag.values().map { it.key } + AppLogTag.values().map { it.key }).distinct() private val filterItems = listOf<String?>(null) + (MatrixLogTag.values().map { it.key } + AppLogTag.values().map { it.key }).distinct()
@ -33,11 +35,13 @@ fun EventLogScreen(viewModel: EventLoggerViewModel) {
viewModel.selectLog(it, filter = null) viewModel.selectLog(it, filter = null)
} }
} }
else -> { else -> {
Events( Events(
selectedPageContent = state.selectedState, selectedPageContent = state.selectedState,
onExit = { viewModel.exitLog() }, onExit = { viewModel.exitLog() },
onSelectTag = { viewModel.selectLog(state.selectedState.selectedPage, it) } onSelectTag = { viewModel.selectLog(state.selectedState.selectedPage, it) },
onRetry = { viewModel.start() },
) )
} }
} }
@ -46,6 +50,7 @@ fun EventLogScreen(viewModel: EventLoggerViewModel) {
is Lce.Error -> { is Lce.Error -> {
// TODO // TODO
} }
is Lce.Loading -> { is Lce.Loading -> {
// TODO // TODO
} }
@ -69,7 +74,7 @@ private fun LogKeysList(keys: List<String>, onSelected: (String) -> Unit) {
} }
@Composable @Composable
private fun Events(selectedPageContent: SelectedState, onExit: () -> Unit, onSelectTag: (String?) -> Unit) { private fun Events(selectedPageContent: SelectedState, onExit: () -> Unit, onSelectTag: (String?) -> Unit, onRetry: () -> Unit) {
BackHandler(onBack = onExit) BackHandler(onBack = onExit)
when (val content = selectedPageContent.content) { when (val content = selectedPageContent.content) {
is Lce.Content -> { is Lce.Content -> {
@ -112,9 +117,8 @@ private fun Events(selectedPageContent: SelectedState, onExit: () -> Unit, onSel
} }
} }
} }
is Lce.Error -> TODO()
is Lce.Loading -> { is Lce.Error -> GenericError(cause = content.cause, action = onRetry)
// TODO is Lce.Loading -> CenteredLoading()
}
} }
} }