From 293de92ccbb66796182a2c1a2284922c34a3255a Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 1 Dec 2023 03:39:57 +0100 Subject: [PATCH] Use LiveData for the rooms list --- app/build.gradle.kts | 1 + .../steffo/twom/main/MainActivityRoomList.kt | 25 ++++++------------- .../java/eu/steffo/twom/matrix/TwoMMatrix.kt | 2 ++ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 01a6982..c4f6cbc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -69,6 +69,7 @@ dependencies { implementation("androidx.compose.ui:ui") implementation("androidx.compose.ui:ui-graphics") implementation("androidx.compose.ui:ui-tooling-preview") + implementation("androidx.compose.runtime:runtime-livedata:1.5.4") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") 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 81b897c..c8cbbfc 100644 --- a/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityRoomList.kt @@ -1,20 +1,17 @@ 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.LaunchedEffect import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue +import androidx.compose.runtime.livedata.observeAsState 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 org.matrix.android.sdk.api.session.room.model.RoomSummary +import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams @Composable @@ -23,18 +20,12 @@ fun MainActivityRoomList( onClickRoom: (roomId: String) -> Unit = {}, ) { val session = LocalSession.current - var roomSummaries by remember { mutableStateOf?>(null) } - - LaunchedEffect(session) GetRoomSummaries@{ - if (session == null) { - Log.d("RoomList", "Not doing anything, session is null.") - return@GetRoomSummaries + val roomSummaries by session!!.roomService().getRoomSummariesLive( + roomSummaryQueryParams { + this.memberships = listOf(Membership.JOIN) + this.includeType = listOf(TwoMMatrix.ROOM_TYPE) } - Log.d("RoomList", "Getting room summaries...") - val queryParamsBuilder = roomSummaryQueryParams() - roomSummaries = session.roomService().getRoomSummaries(queryParamsBuilder) - Log.d("RoomList", "Obtained room summaries: $roomSummaries") - } + ).observeAsState() Column(modifier) { if (roomSummaries == null) { diff --git a/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt b/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt index e30066f..1880bf8 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt @@ -36,4 +36,6 @@ object TwoMMatrix { } return null } + + const val ROOM_TYPE = "eu.steffo.twom.happening" }