From a6e562c14bd892733a27f90b15fccae0374df329 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 8 Jan 2024 19:43:28 +0100 Subject: [PATCH] Checkpoint --- .../java/eu/steffo/twom/room/LocalRoom.kt | 6 +- .../eu/steffo/twom/room/LocalRoomSummary.kt | 7 ++ .../java/eu/steffo/twom/room/RSVPAnswer.kt | 3 +- .../steffo/twom/room/RoomActivityContent.kt | 66 ++++++++++++++----- .../steffo/twom/room/RoomActivityScaffold.kt | 30 +++++---- .../eu/steffo/twom/room/RoomActivityTopBar.kt | 4 +- .../java/eu/steffo/twom/theme/ErrorText.kt | 15 +++++ app/src/main/res/values/strings.xml | 3 + 8 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/eu/steffo/twom/room/LocalRoomSummary.kt create mode 100644 app/src/main/java/eu/steffo/twom/theme/ErrorText.kt diff --git a/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt index 34d4373..e0fa8e8 100644 --- a/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt +++ b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt @@ -1,7 +1,7 @@ package eu.steffo.twom.room import androidx.compose.runtime.staticCompositionLocalOf -import org.matrix.android.sdk.api.session.room.model.RoomSummary -import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.api.session.room.Room +import java.util.Optional -val LocalRoom = staticCompositionLocalOf?> { null } +val LocalRoom = staticCompositionLocalOf?> { null } diff --git a/app/src/main/java/eu/steffo/twom/room/LocalRoomSummary.kt b/app/src/main/java/eu/steffo/twom/room/LocalRoomSummary.kt new file mode 100644 index 0000000..0775145 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/LocalRoomSummary.kt @@ -0,0 +1,7 @@ +package eu.steffo.twom.room + +import androidx.compose.runtime.staticCompositionLocalOf +import org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.util.Optional + +val LocalRoomSummary = staticCompositionLocalOf?> { null } diff --git a/app/src/main/java/eu/steffo/twom/room/RSVPAnswer.kt b/app/src/main/java/eu/steffo/twom/room/RSVPAnswer.kt index 25776e0..a590971 100644 --- a/app/src/main/java/eu/steffo/twom/room/RSVPAnswer.kt +++ b/app/src/main/java/eu/steffo/twom/room/RSVPAnswer.kt @@ -14,8 +14,9 @@ import eu.steffo.twom.theme.iconMaybe import eu.steffo.twom.theme.iconNoway import eu.steffo.twom.theme.iconSure import eu.steffo.twom.theme.iconUnknown +import org.matrix.android.sdk.api.util.JsonDict -enum class RSVPAnswer { +enum class RSVPAnswer : JsonDict { SURE, LATER, MAYBE, diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt index d18aa8f..5dcdc20 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt @@ -5,30 +5,49 @@ import androidx.compose.foundation.layout.Row import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.saveable.rememberSaveable -import androidx.compose.runtime.setValue +import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import eu.steffo.twom.R import eu.steffo.twom.matrix.LocalSession +import eu.steffo.twom.theme.ErrorText import eu.steffo.twom.theme.TwoMPadding +import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.query.QueryStringValue +import kotlin.jvm.optionals.getOrNull @Composable fun RoomActivityContent( modifier: Modifier = Modifier, ) { - val isLoading = (LocalRoom.current == null) - val roomSummary = LocalRoom.current?.getOrNull() - val isError = (!isLoading && roomSummary == null) + val scope = rememberCoroutineScope() - var rsvpAnswer by rememberSaveable { mutableStateOf(null) } - var rsvpComment by rememberSaveable { mutableStateOf("") } + val session = LocalSession.current + val roomRequest = LocalRoom.current + val room = roomRequest?.getOrNull() + val roomSummaryRequest = LocalRoomSummary.current + val roomSummary = roomSummaryRequest?.getOrNull() + + val myRsvp = room?.stateService()?.getStateEventLive( + eventType = "eu.steffo.twom.rsvp", + stateKey = QueryStringValue.Equals(session!!.myUserId), + )?.observeAsState() + // TODO: I stopped here; how to retrieve the RSVP from this? Column(modifier) { - if (roomSummary != null) { + if (session == null) { + ErrorText(stringResource(R.string.room_error_session_missing)) + } else if (roomRequest == null) { + ErrorText(stringResource(R.string.room_error_room_missing)) + } else if (!roomRequest.isPresent) { + ErrorText(stringResource(R.string.room_error_room_notfound)) + } else if (roomSummaryRequest == null) { + // Loading + } else if (!roomSummaryRequest.hasValue()) { + ErrorText(stringResource(R.string.room_error_room_notfound)) + } else if (roomSummary != null) { Row(TwoMPadding.base) { Text( text = stringResource(R.string.room_topic_title), @@ -47,11 +66,21 @@ fun RoomActivityContent( } RoomActivityAnswerForm( - currentRsvpAnswer = rsvpAnswer, - currentRsvpComment = rsvpComment, + currentRsvpAnswer = myRsvp, + currentRsvpComment = myRsvp, onUpdate = { answer, comment -> - rsvpAnswer = answer - rsvpComment = comment + scope.launch SendStateEvent@{ + room!!.stateService().sendStateEvent( + eventType = "eu.steffo.twom.rsvp", + stateKey = session.myUserId, + body = mapOf( + pairs = arrayOf( + "answer" to answer.toString(), + "comment" to comment, + ) + ), + ) + } } ) @@ -62,7 +91,6 @@ fun RoomActivityContent( ) } - // TODO: Risky assertion? Column(TwoMPadding.base) { MemberListItem( memberId = LocalSession.current!!.myUserId, @@ -78,6 +106,14 @@ fun RoomActivityContent( ) } } + } else if (isError) { + Row(TwoMPadding.base) { + Text( + // TODO: Maybe add a better error string + text = stringResource(R.string.error), + color = MaterialTheme.colorScheme.error, + ) + } } } } diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt index 4be244b..cffed8c 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt @@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier import eu.steffo.twom.matrix.LocalSession import eu.steffo.twom.theme.TwoMTheme import org.matrix.android.sdk.api.session.Session +import java.util.Optional @Composable fun RoomActivityScaffold( @@ -17,23 +18,26 @@ fun RoomActivityScaffold( roomId: String, onBack: () -> Unit = {}, ) { + val room = Optional.ofNullable(session.roomService().getRoom(roomId)) val roomSummary by session.roomService().getRoomSummaryLive(roomId).observeAsState() TwoMTheme { CompositionLocalProvider(LocalSession provides session) { - CompositionLocalProvider(LocalRoom provides roomSummary) { - Scaffold( - topBar = { - RoomActivityTopBar( - onBack = onBack, - ) - }, - content = { - RoomActivityContent( - modifier = Modifier.padding(it), - ) - }, - ) + CompositionLocalProvider(LocalRoom provides room) { + CompositionLocalProvider(LocalRoomSummary provides roomSummary) { + Scaffold( + topBar = { + RoomActivityTopBar( + onBack = onBack, + ) + }, + content = { + RoomActivityContent( + modifier = Modifier.padding(it), + ) + }, + ) + } } } } diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt index 8e4acc5..b053ea0 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt @@ -23,8 +23,8 @@ fun RoomActivityTopBar( modifier: Modifier = Modifier, onBack: () -> Unit = {}, ) { - val isLoading = (LocalRoom.current == null) - val roomSummary = LocalRoom.current?.getOrNull() + val isLoading = (LocalRoomSummary.current == null) + val roomSummary = LocalRoomSummary.current?.getOrNull() val isError = (!isLoading && roomSummary == null) TopAppBar( diff --git a/app/src/main/java/eu/steffo/twom/theme/ErrorText.kt b/app/src/main/java/eu/steffo/twom/theme/ErrorText.kt new file mode 100644 index 0000000..772c536 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/theme/ErrorText.kt @@ -0,0 +1,15 @@ +package eu.steffo.twom.theme + +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable + +@Composable +fun ErrorText( + text: String +) { + Text( + text = text, + color = MaterialTheme.colorScheme.error, + ) +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a938fb..2c18465 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -55,4 +55,7 @@ Hasn\'t answered yet Leave a comment… Update + The Matrix session context has not been initialized. + Could not find the requested Matrix room. + The Matrix room context has not been initialized. \ No newline at end of file