From 2ef4186968af020cf7036bd27e7adbe904899f3a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 9 Jan 2024 08:34:06 +0100 Subject: [PATCH] Add basic room leave functionality --- .../steffo/twom/main/MainActivityRoomList.kt | 20 ++++++++- .../java/eu/steffo/twom/main/RoomListItem.kt | 41 ++++++++++++++++--- app/src/main/res/values/strings.xml | 1 + 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt index c8cbbfc..1fba34c 100644 --- a/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt @@ -1,16 +1,19 @@ package eu.steffo.twom.main +import android.util.Log import androidx.compose.foundation.layout.Column import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue 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.matrix.TwoMMatrix import eu.steffo.twom.theme.TwoMPadding +import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams @@ -19,6 +22,8 @@ fun MainActivityRoomList( modifier: Modifier = Modifier, onClickRoom: (roomId: String) -> Unit = {}, ) { + val scope = rememberCoroutineScope() + val session = LocalSession.current val roomSummaries by session!!.roomService().getRoomSummariesLive( roomSummaryQueryParams { @@ -41,8 +46,21 @@ fun MainActivityRoomList( } else { roomSummaries!!.forEach { RoomListItem( - onClickRoom = onClickRoom, roomSummary = it, + onClickRoom = onClickRoom, + onLeaveRoom = { + scope.launch LeaveRoom@{ + Log.i("Main", "Leaving room `$it`...") + try { + session!!.roomService().leaveRoom(it, "Decided to leave the room") + } catch (error: Throwable) { + Log.e("Main", "Failed to leave room `$it`: $error") + // TODO: Display an error somewhere + return@LeaveRoom + } + Log.d("Main", "Successfully left room `$it`!") + } + } ) } } diff --git a/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt b/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt index 89493571..57fa17b 100644 --- a/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt +++ b/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt @@ -1,28 +1,42 @@ package eu.steffo.twom.main -import androidx.compose.foundation.clickable +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Box 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.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.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import eu.steffo.twom.R import eu.steffo.twom.matrix.avatar.AvatarFromURL import org.matrix.android.sdk.api.session.room.model.RoomSummary +@OptIn(ExperimentalFoundationApi::class) @Composable fun RoomListItem( roomSummary: RoomSummary, - onClickRoom: (roomId: String) -> Unit, + onClickRoom: (roomId: String) -> Unit = {}, + onLeaveRoom: (roomId: String) -> Unit = {}, ) { + var expanded by rememberSaveable { mutableStateOf(false) } + ListItem( - modifier = Modifier.clickable { - onClickRoom(roomSummary.roomId) - }, + modifier = Modifier.combinedClickable( + onClick = { onClickRoom(roomSummary.roomId) }, + onLongClick = { expanded = true } + ), headlineContent = { Text(roomSummary.displayName) }, @@ -39,10 +53,27 @@ fun RoomListItem( } }, supportingContent = { + // TODO: Display rsvpComment instead of alias val canonicalAlias = roomSummary.canonicalAlias if (canonicalAlias != null) { Text(canonicalAlias) } }, ) + + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false }, + ) { + // TODO: Align me to the right + DropdownMenuItem( + text = { + Text(stringResource(id = R.string.main_room_leave_label)) + }, + onClick = { + expanded = false + onLeaveRoom(roomSummary.roomId) + } + ) + } } \ 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 f59cc93..ab214e0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -68,4 +68,5 @@ Send an invite Not opened Hasn\'t opened the invite yet + Leave party \ No newline at end of file