diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt index 7a11446..c3c2edd 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt @@ -1,15 +1,8 @@ package eu.steffo.twom.composables.avatar -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.saveable.rememberSaveable -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview -import eu.steffo.twom.composables.matrix.LocalSession @Composable @Preview(name = "Regular", widthDp = 40, heightDp = 40) @@ -22,28 +15,9 @@ fun AvatarUserId( contentDescription: String = "", alpha: Float = 1.0f, ) { - val session = LocalSession.current - var avatarUrl by rememberSaveable { mutableStateOf(null) } - - LaunchedEffect(session, userId) GetAvatarUrl@{ - if (session == null) { - Log.d("AvatarUser", "Not doing anything, session is null.") - return@GetAvatarUrl - } - - if (userId.isEmpty()) { - Log.d("AvatarUser", "Not doing anything, userId is empty.") - return@GetAvatarUrl - } - - Log.d("AvatarUser", "Retrieving avatar url for: $userId...") - avatarUrl = session.profileService().getAvatarUrl(userId).getOrNull() - Log.d("AvatarUser", "Retrieved avatar url for $userId: $avatarUrl") - } - AvatarURL( modifier = modifier, - url = avatarUrl, + url = avatarUrlFromUserId(userId), fallbackText = fallbackText, contentDescription = contentDescription, alpha = alpha, diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt new file mode 100644 index 0000000..93279e5 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt @@ -0,0 +1,50 @@ +package eu.steffo.twom.composables.avatar + +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.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import eu.steffo.twom.composables.matrix.LocalSession +import kotlinx.coroutines.CancellationException + +private const val TAG = "avatarUrlFromUserId" + +@Composable +fun avatarUrlFromUserId(userId: String): String? { + val session = LocalSession.current + + var url by rememberSaveable { mutableStateOf(null) } + + LaunchedEffect(session, userId) Fetch@{ + if (session == null) { + Log.d(TAG, "Session is null, clearing URL...") + url = null + return@Fetch + } + if (userId.isEmpty()) { + Log.d(TAG, "UserID is empty, clearing URL...") + url = null + return@Fetch + } + + Log.i(TAG, "Retrieving avatar URL for: $userId...") + try { + url = session.profileService().getAvatarUrl(userId).getOrNull() + } catch (e: CancellationException) { + Log.i(TAG, "Cancelled retrieval of avatar URL of: $userId", e) + url = null + return@Fetch + } catch (e: Throwable) { + Log.e(TAG, "Unable to retrieve avatar URL of: $userId", e) + url = null + return@Fetch + } + + Log.d(TAG, "Avatar URL for $userId is: $url") + } + + return url +} \ No newline at end of file