1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-24 17:14:24 +00:00

Export resolveUser to a separate file

This commit is contained in:
Steffo 2024-01-31 03:57:28 +01:00
parent c1b84d14f4
commit 63ef1a4edd
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
2 changed files with 55 additions and 13 deletions

View file

@ -13,10 +13,8 @@ import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue 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.avatar.components.AvatarUser
import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.ErrorText
import eu.steffo.twom.composables.matrix.LocalSession import eu.steffo.twom.composables.matrix.LocalSession
import eu.steffo.twom.composables.viewroom.effects.resolveUser
import eu.steffo.twom.utils.RSVPAnswer import eu.steffo.twom.utils.RSVPAnswer
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary 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 import kotlin.jvm.optionals.getOrNull
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@ -64,16 +62,8 @@ fun MemberListItem(
return return
} }
// TODO: Is this necessary? // This might not be necessary; I'm not sure when the internal Matrix client resolves users
val user = resolveUser(member.userId)
var user by remember { mutableStateOf<User?>(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")
}
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()

View file

@ -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<User?>(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
}