1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 23:54:26 +00:00

Split effect from AvatarUserID

This commit is contained in:
Steffo 2024-01-30 04:38:05 +01:00
parent ac1d51657e
commit 58b0901408
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
2 changed files with 51 additions and 27 deletions

View file

@ -1,15 +1,8 @@
package eu.steffo.twom.composables.avatar package eu.steffo.twom.composables.avatar
import android.util.Log
import androidx.compose.runtime.Composable 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.Modifier
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.composables.matrix.LocalSession
@Composable @Composable
@Preview(name = "Regular", widthDp = 40, heightDp = 40) @Preview(name = "Regular", widthDp = 40, heightDp = 40)
@ -22,28 +15,9 @@ fun AvatarUserId(
contentDescription: String = "", contentDescription: String = "",
alpha: Float = 1.0f, alpha: Float = 1.0f,
) { ) {
val session = LocalSession.current
var avatarUrl by rememberSaveable { mutableStateOf<String?>(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( AvatarURL(
modifier = modifier, modifier = modifier,
url = avatarUrl, url = avatarUrlFromUserId(userId),
fallbackText = fallbackText, fallbackText = fallbackText,
contentDescription = contentDescription, contentDescription = contentDescription,
alpha = alpha, alpha = alpha,

View file

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