1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-28 19:14:24 +00:00

Checkpoint

This commit is contained in:
Steffo 2024-01-08 19:43:28 +01:00
parent 4ab3fa168c
commit a6e562c14b
Signed by: steffo
GPG key ID: 2A24051445686895
8 changed files with 100 additions and 34 deletions

View file

@ -1,7 +1,7 @@
package eu.steffo.twom.room package eu.steffo.twom.room
import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.runtime.staticCompositionLocalOf
import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.util.Optional import java.util.Optional
val LocalRoom = staticCompositionLocalOf<Optional<RoomSummary>?> { null } val LocalRoom = staticCompositionLocalOf<Optional<Room>?> { null }

View file

@ -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<Optional<RoomSummary>?> { null }

View file

@ -14,8 +14,9 @@ import eu.steffo.twom.theme.iconMaybe
import eu.steffo.twom.theme.iconNoway import eu.steffo.twom.theme.iconNoway
import eu.steffo.twom.theme.iconSure import eu.steffo.twom.theme.iconSure
import eu.steffo.twom.theme.iconUnknown import eu.steffo.twom.theme.iconUnknown
import org.matrix.android.sdk.api.util.JsonDict
enum class RSVPAnswer { enum class RSVPAnswer : JsonDict {
SURE, SURE,
LATER, LATER,
MAYBE, MAYBE,

View file

@ -5,30 +5,49 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.matrix.LocalSession import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.theme.ErrorText
import eu.steffo.twom.theme.TwoMPadding import eu.steffo.twom.theme.TwoMPadding
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.query.QueryStringValue
import kotlin.jvm.optionals.getOrNull
@Composable @Composable
fun RoomActivityContent( fun RoomActivityContent(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val isLoading = (LocalRoom.current == null) val scope = rememberCoroutineScope()
val roomSummary = LocalRoom.current?.getOrNull()
val isError = (!isLoading && roomSummary == null)
var rsvpAnswer by rememberSaveable { mutableStateOf<RSVPAnswer?>(null) } val session = LocalSession.current
var rsvpComment by rememberSaveable { mutableStateOf("") } 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) { 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) { Row(TwoMPadding.base) {
Text( Text(
text = stringResource(R.string.room_topic_title), text = stringResource(R.string.room_topic_title),
@ -47,11 +66,21 @@ fun RoomActivityContent(
} }
RoomActivityAnswerForm( RoomActivityAnswerForm(
currentRsvpAnswer = rsvpAnswer, currentRsvpAnswer = myRsvp,
currentRsvpComment = rsvpComment, currentRsvpComment = myRsvp,
onUpdate = { answer, comment -> onUpdate = { answer, comment ->
rsvpAnswer = answer scope.launch SendStateEvent@{
rsvpComment = comment 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) { Column(TwoMPadding.base) {
MemberListItem( MemberListItem(
memberId = LocalSession.current!!.myUserId, 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,
)
}
} }
} }
} }

View file

@ -10,6 +10,7 @@ import androidx.compose.ui.Modifier
import eu.steffo.twom.matrix.LocalSession import eu.steffo.twom.matrix.LocalSession
import eu.steffo.twom.theme.TwoMTheme import eu.steffo.twom.theme.TwoMTheme
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import java.util.Optional
@Composable @Composable
fun RoomActivityScaffold( fun RoomActivityScaffold(
@ -17,11 +18,13 @@ fun RoomActivityScaffold(
roomId: String, roomId: String,
onBack: () -> Unit = {}, onBack: () -> Unit = {},
) { ) {
val room = Optional.ofNullable(session.roomService().getRoom(roomId))
val roomSummary by session.roomService().getRoomSummaryLive(roomId).observeAsState() val roomSummary by session.roomService().getRoomSummaryLive(roomId).observeAsState()
TwoMTheme { TwoMTheme {
CompositionLocalProvider(LocalSession provides session) { CompositionLocalProvider(LocalSession provides session) {
CompositionLocalProvider(LocalRoom provides roomSummary) { CompositionLocalProvider(LocalRoom provides room) {
CompositionLocalProvider(LocalRoomSummary provides roomSummary) {
Scaffold( Scaffold(
topBar = { topBar = {
RoomActivityTopBar( RoomActivityTopBar(
@ -37,4 +40,5 @@ fun RoomActivityScaffold(
} }
} }
} }
}
} }

View file

@ -23,8 +23,8 @@ fun RoomActivityTopBar(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onBack: () -> Unit = {}, onBack: () -> Unit = {},
) { ) {
val isLoading = (LocalRoom.current == null) val isLoading = (LocalRoomSummary.current == null)
val roomSummary = LocalRoom.current?.getOrNull() val roomSummary = LocalRoomSummary.current?.getOrNull()
val isError = (!isLoading && roomSummary == null) val isError = (!isLoading && roomSummary == null)
TopAppBar( TopAppBar(

View file

@ -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,
)
}

View file

@ -55,4 +55,7 @@
<string name="room_rsvp_unknown_response">Hasn\'t answered yet</string> <string name="room_rsvp_unknown_response">Hasn\'t answered yet</string>
<string name="room_rsvp_unknown_placeholder">Leave a comment…</string> <string name="room_rsvp_unknown_placeholder">Leave a comment…</string>
<string name="room_update_label">Update</string> <string name="room_update_label">Update</string>
<string name="room_error_session_missing">The Matrix session context has not been initialized.</string>
<string name="room_error_room_notfound">Could not find the requested Matrix room.</string>
<string name="room_error_room_missing">The Matrix room context has not been initialized.</string>
</resources> </resources>