From 06f7ddf62bd9d70afce4acccb889fe64375f3fd3 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 7 Dec 2023 03:22:20 +0100 Subject: [PATCH] Fix rooms not getting displayed upon first login --- .../java/eu/steffo/twom/main/MainActivity.kt | 22 +++++++++++----- .../java/eu/steffo/twom/main/RoomListItem.kt | 2 +- .../java/eu/steffo/twom/room/LocalRoom.kt | 3 ++- .../steffo/twom/room/RoomActivityContent.kt | 18 ++++++++++--- .../twom/room/RoomActivityRoomIconButton.kt | 5 ++-- .../steffo/twom/room/RoomActivityScaffold.kt | 5 ++-- .../eu/steffo/twom/room/RoomActivityTopBar.kt | 25 ++++++++++++++----- app/src/main/res/values/strings.xml | 1 + 8 files changed, 59 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivity.kt b/app/src/main/java/eu/steffo/twom/main/MainActivity.kt index 0e47720..da4c9b2 100644 --- a/app/src/main/java/eu/steffo/twom/main/MainActivity.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivity.kt @@ -75,6 +75,13 @@ class MainActivity : ComponentActivity() { Log.d("Main", "Opening session: $currentSession") currentSession.open() currentSession.syncService().startSync(true) + currentSession.addListener(OpenSessionListener(this::resetContent)) + } + } + + private class OpenSessionListener(private val resetContent: () -> Unit) : Session.Listener { + override fun onSessionStarted(session: Session) { + resetContent() } } @@ -102,10 +109,8 @@ class MainActivity : ComponentActivity() { "Login activity returned a successful result, trying to get session..." ) fetchLastSession() - session?.open() - resetContent() + openSession() } - else -> { Log.d("Main", "Login activity was cancelled.") } @@ -114,11 +119,16 @@ class MainActivity : ComponentActivity() { private fun onClickLogout() { lifecycleScope.launch { - Log.d("Main", "Clicked logout, signing out...") - session!!.signOutService().signOut(true) + val signedOutSession = session!! + + Log.d("Main", "Clicked logout, recomposing...") session = null - Log.d("Main", "Done logging out, recomposing...") resetContent() + + Log.d("Main", "Done recomposing, now signing out...") + signedOutSession.signOutService().signOut(true) + + Log.d("Main", "Done logging out!") } } 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 e27df66..9ace6b5 100644 --- a/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt +++ b/app/src/main/java/eu/steffo/twom/main/RoomListItem.kt @@ -24,7 +24,7 @@ fun RoomListItem( onClickRoom(roomSummary.roomId) }, headlineContent = { - Text(roomSummary.name) + Text(roomSummary.displayName) }, leadingContent = { Box( diff --git a/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt index 3dc9ac3..34d4373 100644 --- a/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt +++ b/app/src/main/java/eu/steffo/twom/room/LocalRoom.kt @@ -2,5 +2,6 @@ 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 LocalRoom = staticCompositionLocalOf { null } +val LocalRoom = staticCompositionLocalOf?> { null } diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt index 0f274d6..ffac140 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityContent.kt @@ -1,13 +1,25 @@ package eu.steffo.twom.room +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier -import androidx.compose.ui.tooling.preview.Preview +import eu.steffo.twom.theme.TwoMPadding @Composable -@Preview(showBackground = true) fun RoomActivityContent( modifier: Modifier = Modifier, ) { + val isLoading = (LocalRoom.current == null) + val roomSummary = LocalRoom.current?.getOrNull() + val isError = (!isLoading && roomSummary == null) -} \ No newline at end of file + Column(modifier) { + Row(TwoMPadding.base) { + if (roomSummary != null) { + Text(roomSummary.topic) + } + } + } +} diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt index 18cc30c..285d41f 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityRoomIconButton.kt @@ -19,16 +19,15 @@ 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() }, + avatarUrl: String, ) { var expanded by remember { mutableStateOf(false) } @@ -51,7 +50,7 @@ fun RoomActivityRoomIconButton( ) { expanded = true }, ) { AvatarFromURL( - url = LocalRoom.current!!.avatarUrl, + url = avatarUrl, contentDescription = LocalContext.current.getString(R.string.room_options_label), ) } diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt index b2183dd..4be244b 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityScaffold.kt @@ -4,11 +4,12 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.getValue +import androidx.compose.runtime.livedata.observeAsState 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( @@ -16,7 +17,7 @@ fun RoomActivityScaffold( roomId: String, onBack: () -> Unit = {}, ) { - val roomSummary = session.getRoomSummary(roomId) + val roomSummary by session.roomService().getRoomSummaryLive(roomId).observeAsState() TwoMTheme { CompositionLocalProvider(LocalSession provides session) { diff --git a/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt index f88b576..d6be7d6 100644 --- a/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt +++ b/app/src/main/java/eu/steffo/twom/room/RoomActivityTopBar.kt @@ -2,6 +2,8 @@ package eu.steffo.twom.room import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack +import androidx.compose.material.icons.filled.Warning +import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -10,19 +12,20 @@ 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 androidx.compose.ui.res.stringResource import eu.steffo.twom.R @OptIn(ExperimentalMaterial3Api::class) @Composable -@Preview fun RoomActivityTopBar( modifier: Modifier = Modifier, onBack: () -> Unit = {}, - roomName: String = "{Room name}", - roomAvatarURL: String? = null, ) { + val isLoading = (LocalRoom.current == null) + val roomSummary = LocalRoom.current?.getOrNull() + val isError = (!isLoading && roomSummary == null) + TopAppBar( modifier = modifier, navigationIcon = { @@ -34,10 +37,20 @@ fun RoomActivityTopBar( } }, title = { - Text(LocalRoom.current!!.name) + if (roomSummary != null) { + Text(roomSummary.displayName) + } }, actions = { - RoomActivityRoomIconButton() + if (isLoading) { + CircularProgressIndicator() + } else if (isError) { + Icon(Icons.Filled.Warning, stringResource(R.string.error)) + } else { + RoomActivityRoomIconButton( + avatarUrl = roomSummary!!.avatarUrl, + ) + } }, ) } \ 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 36e9ff8..ec6da06 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -33,4 +33,5 @@ Description Create Room options + Error \ No newline at end of file