From b66cb24a7fbf1c3c54a815413138047f5f1bde0e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 21 Jan 2024 16:23:58 +0100 Subject: [PATCH] Add a mechanism for accepting invites --- .../composables/main/MainContentLoggedIn.kt | 2 +- .../twom/composables/main/RoomListItem.kt | 55 +++++++++++++++---- app/src/main/res/values/strings.xml | 4 +- 3 files changed, 49 insertions(+), 12 deletions(-) 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 97a3a2f..9605cbc 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 @@ -29,7 +29,7 @@ fun MainContentLoggedIn( val roomSummaries by session.roomService().getRoomSummariesLive( roomSummaryQueryParams { - this.memberships = listOf(Membership.JOIN) + this.memberships = listOf(Membership.JOIN, Membership.INVITE) this.includeType = listOf(TwoMGlobals.ROOM_TYPE) } ).observeAsState() 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 47ce9a6..d90841f 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 @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.size import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ListItem +import androidx.compose.material3.LocalContentColor import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -29,6 +30,7 @@ import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.matrix.LocalSession import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.RoomSummary @@ -52,9 +54,23 @@ fun RoomListItem( var expanded by rememberSaveable { mutableStateOf(false) } val error by remember { mutableStateOf(LocalizableError()) } + // TODO: Display a running indicator + val viewRoomActivityLauncher = rememberLauncherForActivityResult(ViewRoomActivity.Contract()) {} - fun openRoom() { + suspend fun openRoom() { + if (roomSummary.membership == Membership.INVITE) { + Log.i("Main", "Opening invite `$roomId`...") + try { + session.roomService().joinRoom(roomId, "Opened the invite") + } catch (e: Throwable) { + Log.e("Main", "Failed to open invite to room `$roomId`: $error") + error.set(R.string.main_error_join_generic, e) + return + } + Log.d("Main", "Successfully opened invite to room `$roomId`!") + } + Log.i("Main", "Opening room `$roomId`...") viewRoomActivityLauncher.launch(roomId) } @@ -71,15 +87,20 @@ fun RoomListItem( Log.d("Main", "Successfully left room `$roomId`!") } + val alpha = if (roomSummary.membership == Membership.INVITE) 0.4f else 1.0f + Box { ListItem( modifier = Modifier.combinedClickable( - onClick = { openRoom() }, + onClick = { scope.launch { openRoom() } }, onLongClick = { expanded = true } ), headlineContent = { - Text(roomSummary.displayName) + Text( + text = roomSummary.displayName, + color = LocalContentColor.current.copy(alpha) + ) }, leadingContent = { Box( @@ -90,17 +111,24 @@ fun RoomListItem( AvatarURL( // FIXME: URL can appearently be set before the image is available on the homeserver url = roomSummary.avatarUrl, + alpha = alpha, ) } }, supportingContent = { val count = roomSummary.joinedMembersCount if (count != null) { - if (count != 1) { - Text(stringResource(R.string.main_partecipants, count)) - } else { - Text(stringResource(R.string.main_partecipant, count)) - } + Text( + text = stringResource( + id = if (count != 1) { + R.string.main_partecipants + } else { + R.string.main_partecipant + }, + count, + ), + color = LocalContentColor.current.copy(alpha) + ) } }, ) @@ -112,7 +140,15 @@ fun RoomListItem( // TODO: Align me to the right DropdownMenuItem( text = { - Text(stringResource(id = R.string.main_room_leave_label)) + Text( + stringResource( + id = if (roomSummary.membership == Membership.INVITE) { + R.string.main_room_reject_label + } else { + R.string.main_room_leave_label + } + ) + ) }, onClick = { expanded = false @@ -120,6 +156,5 @@ fun RoomListItem( } ) } - } } \ 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 0a4cd8e..f0bb1ee 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,6 +1,6 @@ TwoM - Partying with myself + A brand new party Partying with %1$s Partying with %1$s and %2$s Partying with %1$s, %2$s, and %3$s @@ -87,4 +87,6 @@ Editing %1$s %1$d partecipants %1$d partecipant + Reject invite + Something went wrong while opening the invite: %1$s \ No newline at end of file