diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt index 5448b38..3086030 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/MemberListItem.kt @@ -13,10 +13,8 @@ import androidx.compose.material3.ListItem import androidx.compose.material3.MaterialTheme 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.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue @@ -28,10 +26,10 @@ import eu.steffo.twom.R import eu.steffo.twom.composables.avatar.components.AvatarUser import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.matrix.LocalSession +import eu.steffo.twom.composables.viewroom.effects.resolveUser import eu.steffo.twom.utils.RSVPAnswer import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary -import org.matrix.android.sdk.api.session.user.model.User import kotlin.jvm.optionals.getOrNull @OptIn(ExperimentalFoundationApi::class) @@ -64,16 +62,8 @@ fun MemberListItem( return } - // TODO: Is this necessary? - - var user by remember { mutableStateOf(null) } - - LaunchedEffect(session, member.userId) { - val memberId = member.userId - Log.d("UserListItem", "Resolving user: $memberId") - user = session.userService().resolveUser(memberId) - Log.d("UserListItem", "Resolved user: $memberId") - } + // This might not be necessary; I'm not sure when the internal Matrix client resolves users + val user = resolveUser(member.userId) val scope = rememberCoroutineScope() diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/effects/resolveUser.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/effects/resolveUser.kt new file mode 100644 index 0000000..cf0dbb6 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/effects/resolveUser.kt @@ -0,0 +1,52 @@ +package eu.steffo.twom.composables.viewroom.effects + +import android.util.Log +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 eu.steffo.twom.composables.matrix.LocalSession +import kotlinx.coroutines.CancellationException +import org.matrix.android.sdk.api.session.user.model.User + +private const val TAG = "resolveUser" + +@Composable +fun resolveUser(userId: String?): User? { + val session = LocalSession.current + + var user by remember { mutableStateOf(null) } + + LaunchedEffect(session, userId) Fetch@{ + if (session == null) { + Log.d(TAG, "Session is null, clearing user...") + user = null + return@Fetch + } + if (userId == null) { + Log.d(TAG, "userId is null, clearing user...") + user = null + return@Fetch + } + + Log.i(TAG, "Resolving user: $userId") + try { + user = session.userService().resolveUser(userId) + } catch (e: CancellationException) { + // This makes sure no corrupt image is displayed, at least + Log.d(TAG, "Cancelled resolution of user: $userId", e) + user = null + return@Fetch + } catch (e: Throwable) { + Log.e(TAG, "Unable to resolve user: $userId", e) + user = null + return@Fetch + } + + Log.d(TAG, "Resolved user: $userId") + } + + return user +}