From 5083f8dfcfcdb6d235ebd804b30f261ef1509342 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 17 Jan 2024 12:52:36 +0100 Subject: [PATCH] Improve error and loading display --- .../twom/composables/login/LoginForm.kt | 4 +- .../composables/main/MainContentLoggedIn.kt | 4 +- .../twom/composables/main/RoomListItem.kt | 4 +- .../composables/viewroom/MemberListItem.kt | 12 +++-- .../twom/composables/viewroom/RSVPForm.kt | 10 ++++ .../composables/viewroom/ViewRoomContent.kt | 7 ++- .../twom/composables/viewroom/ViewRoomForm.kt | 44 +++++++++++----- .../composables/viewroom/ViewRoomMembers.kt | 52 +++++++++++-------- .../composables/viewroom/ViewRoomTopBar.kt | 38 +++++--------- .../composables/viewroom/ViewRoomTopic.kt | 28 +++++----- .../twom/composables/viewroom/observeRSVP.kt | 12 +++++ app/src/main/res/values/strings.xml | 1 + 12 files changed, 136 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/eu/steffo/twom/composables/login/LoginForm.kt b/app/src/main/java/eu/steffo/twom/composables/login/LoginForm.kt index a8b5be5..46099d3 100644 --- a/app/src/main/java/eu/steffo/twom/composables/login/LoginForm.kt +++ b/app/src/main/java/eu/steffo/twom/composables/login/LoginForm.kt @@ -224,7 +224,9 @@ fun LoginForm( } error.Show { Row(Modifier.basePadding()) { - ErrorText(it) + ErrorText( + text = it + ) } } } diff --git a/app/src/main/java/eu/steffo/twom/composables/main/MainContentLoggedIn.kt b/app/src/main/java/eu/steffo/twom/composables/main/MainContentLoggedIn.kt index 48b65e8..97a3a2f 100644 --- a/app/src/main/java/eu/steffo/twom/composables/main/MainContentLoggedIn.kt +++ b/app/src/main/java/eu/steffo/twom/composables/main/MainContentLoggedIn.kt @@ -21,7 +21,9 @@ fun MainContentLoggedIn( ) { val session = LocalSession.current if (session == null) { - ErrorText(stringResource(R.string.error_session_missing)) + ErrorText( + text = stringResource(R.string.error_session_missing) + ) return } diff --git a/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt b/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt index ddf8f12..aa42521 100644 --- a/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt +++ b/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt @@ -43,7 +43,9 @@ fun RoomListItem( val session = LocalSession.current if (session == null) { - ErrorText(stringResource(R.string.error_session_missing)) + ErrorText( + text = stringResource(R.string.error_session_missing) + ) return } diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt index 569f5cb..5499371 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt @@ -39,19 +39,25 @@ fun MemberListItem( ) { val session = LocalSession.current if (session == null) { - ErrorText(stringResource(R.string.error_session_missing)) + ErrorText( + text = stringResource(R.string.error_session_missing) + ) return } val roomRequest = LocalRoom.current if (roomRequest == null) { - ErrorText(stringResource(R.string.room_error_room_missing)) + ErrorText( + text = stringResource(R.string.room_error_room_missing) + ) return } val room = roomRequest.getOrNull() if (room == null) { - ErrorText(stringResource(R.string.room_error_room_notfound)) + ErrorText( + text = stringResource(R.string.room_error_room_notfound) + ) return } diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/RSVPForm.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/RSVPForm.kt index 81583e6..10833a1 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/RSVPForm.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/RSVPForm.kt @@ -1,5 +1,6 @@ package eu.steffo.twom.composables.viewroom +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable @@ -9,6 +10,8 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp +import eu.steffo.twom.composables.errorhandling.LoadingText +import eu.steffo.twom.composables.theme.basePadding import eu.steffo.twom.utils.RSVP import eu.steffo.twom.utils.RSVPAnswer @@ -18,6 +21,13 @@ fun RSVPForm( onRequestPublish: (newAnswer: RSVPAnswer, newComment: String) -> Unit = { _, _ -> }, isPublishRunning: Boolean = false, ) { + if (published.answer == RSVPAnswer.LOADING) { + Row(Modifier.basePadding()) { + LoadingText() + } + return + } + var currentAnswer by rememberSaveable { mutableStateOf(published.answer) } var currentComment by rememberSaveable { mutableStateOf(published.comment) } diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomContent.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomContent.kt index 6230893..8632b25 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomContent.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomContent.kt @@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import eu.steffo.twom.R @@ -18,11 +17,11 @@ import eu.steffo.twom.composables.matrix.LocalSession fun ViewRoomContent( modifier: Modifier = Modifier, ) { - val scope = rememberCoroutineScope() - val session = LocalSession.current if (session == null) { - ErrorText(stringResource(R.string.error_session_missing)) + ErrorText( + text = stringResource(R.string.error_session_missing) + ) return } diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomForm.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomForm.kt index c2465ac..9405a0c 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomForm.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomForm.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import eu.steffo.twom.R import eu.steffo.twom.composables.errorhandling.ErrorText +import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.matrix.LocalSession import eu.steffo.twom.composables.theme.basePadding @@ -23,30 +24,50 @@ import kotlin.jvm.optionals.getOrNull @Composable fun ViewRoomForm() { + Row(Modifier.basePadding()) { + Text( + text = stringResource(R.string.room_rsvp_title), + style = MaterialTheme.typography.labelLarge, + ) + } + val scope = rememberCoroutineScope() val session = LocalSession.current if (session == null) { - ErrorText(stringResource(R.string.error_session_missing)) + Row(Modifier.basePadding()) { + ErrorText( + text = stringResource(R.string.error_session_missing) + ) + } return } val roomRequest = LocalRoom.current if (roomRequest == null) { - ErrorText(stringResource(R.string.room_error_room_missing)) + Row(Modifier.basePadding()) { + LoadingText() + } return } val room = roomRequest.getOrNull() if (room == null) { - ErrorText(stringResource(R.string.room_error_room_notfound)) + Row(Modifier.basePadding()) { + ErrorText( + text = stringResource(R.string.room_error_room_notfound) + ) + } return } - // FIXME: This breaks if the member is kicked from the chat val member = room.membershipService().getRoomMember(session.myUserId) if (member == null) { - ErrorText(stringResource(R.string.room_error_members_notfound)) + Row(Modifier.basePadding()) { + ErrorText( + text = stringResource(R.string.room_error_self_notfound) + ) + } return } @@ -55,12 +76,6 @@ fun ViewRoomForm() { var isPublishRunning by rememberSaveable { mutableStateOf(false) } val publishError by remember { mutableStateOf(LocalizableError()) } - Row(Modifier.basePadding()) { - Text( - text = stringResource(R.string.room_rsvp_title), - style = MaterialTheme.typography.labelLarge, - ) - } RSVPForm( published = published, onRequestPublish = { answer, comment -> @@ -121,7 +136,12 @@ fun ViewRoomForm() { }, isPublishRunning = isPublishRunning, ) + publishError.Show { - ErrorText(it) + Row(Modifier.basePadding()) { + ErrorText( + text = it + ) + } } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomMembers.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomMembers.kt index acf044d..be21602 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomMembers.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomMembers.kt @@ -10,39 +10,49 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import eu.steffo.twom.R import eu.steffo.twom.composables.errorhandling.ErrorText +import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.theme.basePadding import org.matrix.android.sdk.api.session.room.members.RoomMemberQueryParams import kotlin.jvm.optionals.getOrNull @Composable fun ViewRoomMembers() { - val roomRequest = LocalRoom.current - if (roomRequest == null) { - ErrorText(stringResource(R.string.room_error_room_missing)) - return - } - - val room = roomRequest.getOrNull() - if (room == null) { - ErrorText(stringResource(R.string.room_error_room_notfound)) - return - } - - val roomMembers by room.membershipService().getRoomMembersLive( - RoomMemberQueryParams.Builder().build() - ).observeAsState() - if (roomMembers == null) { - ErrorText(stringResource(R.string.room_error_members_notfound)) - return - } - Row(Modifier.basePadding()) { Text( text = stringResource(R.string.room_invitees_title), style = MaterialTheme.typography.labelLarge, ) } - roomMembers!!.forEach { + + val roomRequest = LocalRoom.current + if (roomRequest == null) { + Row(Modifier.basePadding()) { + LoadingText() + } + return + } + + val room = roomRequest.getOrNull() + if (room == null) { + Row(Modifier.basePadding()) { + ErrorText( + text = stringResource(R.string.room_error_room_notfound) + ) + } + return + } + + val roomMembersRequest by room.membershipService().getRoomMembersLive( + RoomMemberQueryParams.Builder().build() + ).observeAsState() + if (roomMembersRequest == null) { + Row(Modifier.basePadding()) { + LoadingText() + } + return + } + + roomMembersRequest!!.forEach { MemberListItem(member = it) } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopBar.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopBar.kt index 556f6da..0fa7ed0 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopBar.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopBar.kt @@ -2,7 +2,6 @@ package eu.steffo.twom.composables.viewroom import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar @@ -13,7 +12,7 @@ import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R import eu.steffo.twom.composables.errorhandling.ErrorIconButton import eu.steffo.twom.composables.errorhandling.ErrorText -import eu.steffo.twom.composables.errorhandling.LocalizableError +import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.navigation.BackIconButton @@ -22,20 +21,9 @@ import eu.steffo.twom.composables.navigation.BackIconButton @Preview fun ViewRoomTopBar( modifier: Modifier = Modifier, - roomName: String? = null, - roomAvatarUrl: String? = null, - isLoading: Boolean = false, - error: LocalizableError? = null, ) { - val roomSummaryRequest = LocalRoomSummary.current - val isLoading = (roomSummaryRequest == null) - - val roomSummary = roomSummaryRequest.getOrNull() - if (roomSummary == null) { - ErrorText(stringResource(R.string.room_error_roomsummary_notfound)) - return - } + val roomSummary = roomSummaryRequest?.getOrNull() TopAppBar( modifier = modifier, @@ -43,31 +31,33 @@ fun ViewRoomTopBar( BackIconButton() }, title = { - if (roomName != null) { - Text( - text = roomName, + if (roomSummaryRequest == null) { + LoadingText( + style = MaterialTheme.typography.titleLarge, + ) + } else if (roomSummary == null) { + ErrorText( style = MaterialTheme.typography.titleLarge, ) } else { Text( - text = stringResource(R.string.loading), + text = roomSummary.displayName, style = MaterialTheme.typography.titleLarge, - color = LocalContentColor.current.copy(0.4f) ) } }, actions = { - if (isLoading) { + if (roomSummaryRequest == null) { CircularProgressIndicator() - } else if (error != null && error.occurred()) { + } else if (roomSummary == null) { ErrorIconButton( - message = error.renderString()!! + message = stringResource(R.string.room_error_roomsummary_notfound) ) } else { RoomIconButton( - avatarUrl = roomAvatarUrl, + avatarUrl = roomSummary.avatarUrl, ) } }, ) -} \ No newline at end of file +} diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopic.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopic.kt index 1e098c0..af4ad59 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopic.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/ViewRoomTopic.kt @@ -9,23 +9,12 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R import eu.steffo.twom.composables.errorhandling.ErrorText +import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.theme.basePadding @Composable @Preview fun ViewRoomTopic() { - val roomSummaryRequest = LocalRoomSummary.current - if (roomSummaryRequest == null) { - ErrorText(stringResource(R.string.room_error_roomsummary_missing)) - return - } - - val roomSummary = roomSummaryRequest.getOrNull() - if (roomSummary == null) { - ErrorText(stringResource(R.string.room_error_roomsummary_notfound)) - return - } - Row(Modifier.basePadding()) { Text( text = stringResource(R.string.room_topic_title), @@ -33,7 +22,20 @@ fun ViewRoomTopic() { ) } + val roomSummaryRequest = LocalRoomSummary.current + val roomSummary = roomSummaryRequest?.getOrNull() + Row(Modifier.basePadding()) { - Text(roomSummary.topic) + if (roomSummaryRequest == null) { + LoadingText() + } else if (roomSummary == null) { + ErrorText( + text = stringResource(R.string.room_error_roomsummary_notfound) + ) + } else { + Text( + text = roomSummary.topic, + ) + } } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/observeRSVP.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/observeRSVP.kt index 8f1aa3f..b4422fc 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/observeRSVP.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/observeRSVP.kt @@ -1,5 +1,6 @@ package eu.steffo.twom.composables.viewroom +import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState @@ -26,6 +27,8 @@ fun observeRSVP(room: Room, member: RoomMemberSummary): RSVP { stateKey = QueryStringValue.Equals(member.userId), ).observeAsState() + Log.v("observeRSVP", "$request") + if (request == null) { return RSVP( event = null, @@ -48,6 +51,15 @@ fun observeRSVP(room: Room, member: RoomMemberSummary): RSVP { comment = "", ) + // Redactions + if (content.isEmpty()) { + return RSVP( + event = event, + answer = RSVPAnswer.NONE, + comment = "", + ) + } + val commentField = content[TwoMGlobals.RSVP_STATE_COMMENT_FIELD] ?: return RSVP( event = event, diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 59a11ee..8264d11 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,4 +82,5 @@ Uninvite Something went wrong while updating your RSVP: %1$s Your response has been updated, but something went wrong while attempting to remove your previous one: %1$s + You have been removed from the room. \ No newline at end of file