From 6333a5e2d15c21f5e0f534ade784c60f320e30ee Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 7 Dec 2023 02:38:01 +0100 Subject: [PATCH] Create the skeleton of the RoomActivity --- .../eu/steffo/twom/create/AvatarSelector.kt | 4 +- .../twom/matrix/avatar/AvatarFromURL.kt | 4 +- .../java/eu/steffo/twom/room/LocalRoom.kt | 6 ++ .../java/eu/steffo/twom/room/RoomActivity.kt | 37 ++++++++++ .../steffo/twom/room/RoomActivityContent.kt | 13 ++++ .../twom/room/RoomActivityRoomIconButton.kt | 73 +++++++++++++++++++ .../steffo/twom/room/RoomActivityScaffold.kt | 39 ++++++++++ .../eu/steffo/twom/room/RoomActivityTopBar.kt | 43 +++++++++++ app/src/main/res/values/strings.xml | 1 + 9 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/eu/steffo/twom/room/LocalRoom.kt create mode 100644 app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt create mode 100644 app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt create mode 100644 app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt create mode 100644 app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt diff --git a/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt b/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt index 3c57a80..2bfb5f4 100644 --- a/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt +++ b/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt @@ -9,7 +9,7 @@ import androidx.compose.foundation.layout.Box 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.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap @@ -26,7 +26,7 @@ fun AvatarSelector( val context = LocalContext.current val resolver = context.contentResolver - var selection by rememberSaveable { mutableStateOf(null) } + var selection by remember { mutableStateOf(null) } val launcher = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) ImageSelect@{ diff --git a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt index 4a89c68..d9dd6bd 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt @@ -7,7 +7,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap @@ -23,7 +23,7 @@ fun AvatarFromURL( contentDescription: String = "", ) { val session = LocalSession.current - var bitmap by rememberSaveable { mutableStateOf(null) } + var bitmap by remember { mutableStateOf(null) } LaunchedEffect(session, url) GetAvatar@{ if (session == null) { diff --git a/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt new file mode 100644 index 0000000..3dc9ac3 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt @@ -0,0 +1,6 @@ +package eu.steffo.twom.room + +import androidx.compose.runtime.staticCompositionLocalOf +import org.matrix.android.sdk.api.session.room.model.RoomSummary + +val LocalRoom = staticCompositionLocalOf { null } diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivity.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivity.kt index 62d42ac..cbedcd3 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivity.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivity.kt @@ -1,9 +1,46 @@ package eu.steffo.twom.room +import android.os.Bundle +import android.util.Log import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import eu.steffo.twom.matrix.TwoMMatrix +import org.matrix.android.sdk.api.session.Session class RoomActivity : ComponentActivity() { companion object { const val ROOM_ID_EXTRA = "roomId" } + + private lateinit var session: Session + + private fun fetchLastSession() { + Log.d("Main", "Fetching the last successfully authenticated session...") + // FIXME: If this is null, it means that something launched this while no session was authenticated... + session = TwoMMatrix.matrix.authenticationService().getLastAuthenticatedSession()!! + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // FIXME: Hopefully, if this succeeds, the session is also open... hopefully. + fetchLastSession() + } + + override fun onStart() { + super.onStart() + + val roomId = intent.getStringExtra(ROOM_ID_EXTRA) + + setContent { + RoomActivityScaffold( + session = session, + roomId = roomId!!, // FIXME: Again, this should be set. Should. + onBack = { + setResult(RESULT_CANCELED) + finish() + }, + ) + } + } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt new file mode 100644 index 0000000..0f274d6 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt @@ -0,0 +1,13 @@ +package eu.steffo.twom.room + +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview + +@Composable +@Preview(showBackground = true) +fun RoomActivityContent( + modifier: Modifier = Modifier, +) { + +} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt new file mode 100644 index 0000000..18cc30c --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt @@ -0,0 +1,73 @@ +package eu.steffo.twom.room + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.size +import androidx.compose.material.ripple.rememberRipple +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.minimumInteractiveComponentSize +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import eu.steffo.twom.R +import eu.steffo.twom.matrix.avatar.AvatarFromURL + +@Composable +@Preview(showBackground = true) +fun RoomActivityRoomIconButton( + modifier: Modifier = Modifier, + interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, +) { + var expanded by remember { mutableStateOf(false) } + + Box(modifier) { + // Mostly copied from IconButton's source + // TODO: Make sure accessibility works right + // FIXME: This will need changes when Material 4 is released + Box( + modifier = Modifier + .minimumInteractiveComponentSize() + .size(40.dp) + .clip(MaterialTheme.shapes.medium) + .clickable( + role = Role.Button, + interactionSource = interactionSource, + indication = rememberRipple( + bounded = false, + radius = 28.dp + ) + ) { expanded = true }, + ) { + AvatarFromURL( + url = LocalRoom.current!!.avatarUrl, + contentDescription = LocalContext.current.getString(R.string.room_options_label), + ) + } + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false }, + ) { + DropdownMenuItem( + text = { + Text("garasauto") + }, + onClick = { + expanded = false + } + ) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt new file mode 100644 index 0000000..b2183dd --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt @@ -0,0 +1,39 @@ +package eu.steffo.twom.room + +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Scaffold +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +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 org.matrix.android.sdk.api.session.getRoomSummary + +@Composable +fun RoomActivityScaffold( + session: Session, + roomId: String, + onBack: () -> Unit = {}, +) { + val roomSummary = session.getRoomSummary(roomId) + + TwoMTheme { + CompositionLocalProvider(LocalSession provides session) { + CompositionLocalProvider(LocalRoom provides roomSummary) { + Scaffold( + topBar = { + RoomActivityTopBar( + onBack = onBack, + ) + }, + content = { + RoomActivityContent( + modifier = Modifier.padding(it), + ) + }, + ) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt new file mode 100644 index 0000000..f88b576 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt @@ -0,0 +1,43 @@ +package eu.steffo.twom.room + +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.ArrowBack +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.tooling.preview.Preview +import eu.steffo.twom.R + + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +@Preview +fun RoomActivityTopBar( + modifier: Modifier = Modifier, + onBack: () -> Unit = {}, + roomName: String = "{Room name}", + roomAvatarURL: String? = null, +) { + TopAppBar( + modifier = modifier, + navigationIcon = { + IconButton(onClick = onBack) { + Icon( + imageVector = Icons.Filled.ArrowBack, + contentDescription = LocalContext.current.getString(R.string.back) + ) + } + }, + title = { + Text(LocalRoom.current!!.name) + }, + actions = { + RoomActivityRoomIconButton() + }, + ) +} \ 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 fb51129..36e9ff8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -32,4 +32,5 @@ Select happening avatar Description Create + Room options \ No newline at end of file