1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-22 08:04:26 +00:00

Add basic room leave functionality

This commit is contained in:
Steffo 2024-01-09 08:34:06 +01:00
parent cca38d0026
commit 2ef4186968
Signed by: steffo
GPG key ID: 2A24051445686895
3 changed files with 56 additions and 6 deletions

View file

@ -1,16 +1,19 @@
package eu.steffo.twom.main package eu.steffo.twom.main
import android.util.Log
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
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.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.rememberCoroutineScope
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.matrix.TwoMMatrix import eu.steffo.twom.matrix.TwoMMatrix
import eu.steffo.twom.theme.TwoMPadding 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.model.Membership
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
@ -19,6 +22,8 @@ fun MainActivityRoomList(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
onClickRoom: (roomId: String) -> Unit = {}, onClickRoom: (roomId: String) -> Unit = {},
) { ) {
val scope = rememberCoroutineScope()
val session = LocalSession.current val session = LocalSession.current
val roomSummaries by session!!.roomService().getRoomSummariesLive( val roomSummaries by session!!.roomService().getRoomSummariesLive(
roomSummaryQueryParams { roomSummaryQueryParams {
@ -41,8 +46,21 @@ fun MainActivityRoomList(
} else { } else {
roomSummaries!!.forEach { roomSummaries!!.forEach {
RoomListItem( RoomListItem(
onClickRoom = onClickRoom,
roomSummary = it, 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`!")
}
}
) )
} }
} }

View file

@ -1,28 +1,42 @@
package eu.steffo.twom.main 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.Box
import androidx.compose.foundation.layout.size 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.ListItem
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.mutableStateOf
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.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import eu.steffo.twom.R
import eu.steffo.twom.matrix.avatar.AvatarFromURL import eu.steffo.twom.matrix.avatar.AvatarFromURL
import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.model.RoomSummary
@OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun RoomListItem( fun RoomListItem(
roomSummary: RoomSummary, roomSummary: RoomSummary,
onClickRoom: (roomId: String) -> Unit, onClickRoom: (roomId: String) -> Unit = {},
onLeaveRoom: (roomId: String) -> Unit = {},
) { ) {
var expanded by rememberSaveable { mutableStateOf(false) }
ListItem( ListItem(
modifier = Modifier.clickable { modifier = Modifier.combinedClickable(
onClickRoom(roomSummary.roomId) onClick = { onClickRoom(roomSummary.roomId) },
}, onLongClick = { expanded = true }
),
headlineContent = { headlineContent = {
Text(roomSummary.displayName) Text(roomSummary.displayName)
}, },
@ -39,10 +53,27 @@ fun RoomListItem(
} }
}, },
supportingContent = { supportingContent = {
// TODO: Display rsvpComment instead of alias
val canonicalAlias = roomSummary.canonicalAlias val canonicalAlias = roomSummary.canonicalAlias
if (canonicalAlias != null) { if (canonicalAlias != null) {
Text(canonicalAlias) 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)
}
)
}
} }

View file

@ -68,4 +68,5 @@
<string name="room_invite_title">Send an invite</string> <string name="room_invite_title">Send an invite</string>
<string name="room_rsvp_invited_label">Not opened</string> <string name="room_rsvp_invited_label">Not opened</string>
<string name="room_rsvp_invited_response">Hasn\'t opened the invite yet</string> <string name="room_rsvp_invited_response">Hasn\'t opened the invite yet</string>
<string name="main_room_leave_label">Leave party</string>
</resources> </resources>