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 efd16fa..f239fd2 100644 --- a/app/src/main/java/eu/steffo/twom/main/MainActivity.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivity.kt @@ -50,13 +50,22 @@ class MainActivity : ComponentActivity() { } private fun openSession() { - Log.d("Main", "If possible, opening session: $session") - session?.open() + val currentSession = session + Log.d("Main", "If possible, opening session: $currentSession") + if (currentSession != null) { + Log.d("Main", "Opening session: $currentSession") + currentSession.open() + currentSession.syncService().startSync(true) + } } private fun closeSession() { - Log.d("Main", "If possible, closing session: $session") - session?.close() + val currentSession = session + Log.d("Main", "If possible, closing session: $currentSession") + if (currentSession != null) { + Log.d("Main", "Closing session: $currentSession") + currentSession.close() + } } private fun onClickLogin() { diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt deleted file mode 100644 index 7a9c21b..0000000 --- a/app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt +++ /dev/null @@ -1,23 +0,0 @@ -package eu.steffo.twom.main - -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.platform.LocalContext -import eu.steffo.twom.R -import eu.steffo.twom.theme.TwoMPadding -import org.matrix.android.sdk.api.session.Session - -@Composable -fun MainActivityLoggedInControl( - session: Session, - modifier: Modifier = Modifier, -) { - Column(modifier) { - Row(TwoMPadding.base) { - Text(LocalContext.current.getString(R.string.loggedin_text)) - } - } -} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivityRoomListControl.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityRoomListControl.kt new file mode 100644 index 0000000..a30c6dc --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityRoomListControl.kt @@ -0,0 +1,69 @@ +package eu.steffo.twom.main + +import android.util.Log +import androidx.compose.foundation.layout.Column +import androidx.compose.material3.Divider +import androidx.compose.material3.ListItem +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +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.res.stringResource +import eu.steffo.twom.R +import eu.steffo.twom.matrix.Avatar +import eu.steffo.twom.theme.TwoMPadding +import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams + +@Composable +fun MainActivityRoomListControl( + session: Session, + modifier: Modifier = Modifier, +) { + var roomSummaries by remember { mutableStateOf?>(null) } + + LaunchedEffect(session) GetRoomSummaries@{ + Log.d("RoomList", "Getting room summaries...") + val queryParamsBuilder = roomSummaryQueryParams() + roomSummaries = session.roomService().getRoomSummaries(queryParamsBuilder) + Log.d("RoomList", "Obtained room summaries: $roomSummaries") + + val sanityCheck = session.roomService().getRoom("!MdYm4p7umKo4DYX71m:candy.steffo.eu") + Log.d("RoomList", "Sanity check: $sanityCheck") + } + + Column(modifier) { + if (roomSummaries == null) { + Text( + modifier = TwoMPadding.base, + text = stringResource(R.string.loading) + ) + } else if (roomSummaries!!.isEmpty()) { + Text( + modifier = TwoMPadding.base, + text = stringResource(R.string.room_list_empty_text) + ) + } else { + roomSummaries!!.forEach { + ListItem( + headlineContent = { + Text(it.name) + }, + leadingContent = { + Avatar( + session = session, + url = it.avatarUrl, + contentDescription = "" // TODO: Is this correct? + ) + } + ) + Divider() + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt index f24de96..448f441 100644 --- a/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt @@ -37,16 +37,16 @@ fun MatrixActivityScaffold( ) } ) { - if (session != null) { - MainActivityLoggedInControl( - modifier = Modifier.padding(it), - session = session, - ) - } else { + if (session == null) { MatrixActivityNotLoggedInControl( modifier = Modifier.padding(it), onClickLogin = onClickLogin, ) + } else { + MainActivityRoomListControl( + modifier = Modifier.padding(it), + session = session, + ) } } } diff --git a/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt b/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt index 41ad958..322aa17 100644 --- a/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt +++ b/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt @@ -18,7 +18,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R -import eu.steffo.twom.matrix.Avatar +import eu.steffo.twom.matrix.UserAvatar import org.matrix.android.sdk.api.session.Session @Composable @@ -41,7 +41,7 @@ fun ProfileIconButton( contentDescription = LocalContext.current.getString(R.string.account_label), ) } else { - Avatar( + UserAvatar( session = session, userId = session.myUserId, contentDescription = LocalContext.current.getString(R.string.account_label), diff --git a/app/src/main/java/eu/steffo/twom/matrix/Avatar.kt b/app/src/main/java/eu/steffo/twom/matrix/Avatar.kt index 1e72cb7..db86aa6 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/Avatar.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/Avatar.kt @@ -16,32 +16,28 @@ import org.matrix.android.sdk.api.session.Session @Composable fun Avatar( session: Session, - userId: String, + url: String, contentDescription: String, ) { var avatar by remember { mutableStateOf(null) } - var fetched by remember { mutableStateOf(false) } - LaunchedEffect(session, userId) GetAvatar@{ - Log.d("Avatar", "Trying to retrieve the avatar url for $userId...") - val avatarUrl = session.profileService().getAvatarUrl(userId) - Log.d("Avatar", "Avatar URL for $userId is: $avatarUrl") + LaunchedEffect(session, url) GetAvatar@{ + Log.d("Avatar", "Downloading avatar at: $url") val avatarFile = session.fileService().downloadFile( fileName = "avatar", - url = avatarUrl.getOrNull(), + url = url, mimeType = null, elementToDecrypt = null, ) // TODO: Should I check the MIME type? And the size of the image? - Log.d("Avatar", "Avatar file for $userId is: $avatarFile") + Log.d("Avatar", "File for $url is: $avatarFile") val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath) - Log.d("Avatar", "Avatar bitmap for $userId is: $avatarBitmap") + Log.d("Avatar", "Bitmap for $url is: $avatarBitmap") avatar = avatarBitmap.asImageBitmap() - fetched = true } Image( - bitmap = if (fetched && avatar != null) avatar!! else TwoMMatrix.defaultAvatar, + bitmap = if (avatar != null) avatar!! else TwoMMatrix.defaultAvatar, contentDescription = contentDescription ) } \ 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 70a2bf3..5b07ab8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,4 +24,6 @@ You are logged in. My account Logout + There are no rooms. + Loading... \ No newline at end of file