diff --git a/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt b/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt index 340cbb7..3c57a80 100644 --- a/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt +++ b/app/src/main/java/eu/steffo/twom/create/AvatarSelector.kt @@ -15,10 +15,10 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview -import eu.steffo.twom.matrix.avatar.AvatarFromBitmap +import eu.steffo.twom.matrix.avatar.AvatarFromImageBitmap @Composable -@Preview +@Preview(widthDp = 40, heightDp = 40) fun AvatarSelector( modifier: Modifier = Modifier, onSelectAvatar: (bitmap: Bitmap) -> Unit = {}, @@ -47,7 +47,7 @@ fun AvatarSelector( launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)) } ) { - AvatarFromBitmap( + AvatarFromImageBitmap( bitmap = selection?.asImageBitmap(), ) } diff --git a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromBitmap.kt b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromImageBitmap.kt similarity index 80% rename from app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromBitmap.kt rename to app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromImageBitmap.kt index f4a57c2..c42d7d1 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromBitmap.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromImageBitmap.kt @@ -2,13 +2,15 @@ package eu.steffo.twom.matrix.avatar import androidx.compose.foundation.Image import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.tooling.preview.Preview @Composable @Preview(widthDp = 40, heightDp = 40) -fun AvatarFromBitmap( +fun AvatarFromImageBitmap( modifier: Modifier = Modifier, bitmap: ImageBitmap? = null, fallbackText: String = "?", @@ -25,6 +27,8 @@ fun AvatarFromBitmap( modifier = modifier, bitmap = bitmap, contentDescription = contentDescription, + contentScale = ContentScale.Crop, + alignment = Alignment.Center, ) } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt index 9671462..4a89c68 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/avatar/AvatarFromURL.kt @@ -1,16 +1,15 @@ package eu.steffo.twom.matrix.avatar +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.util.Log -import androidx.compose.foundation.Image 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.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.matrix.LocalSession @@ -24,7 +23,7 @@ fun AvatarFromURL( contentDescription: String = "", ) { val session = LocalSession.current - var avatar by remember { mutableStateOf(null) } + var bitmap by rememberSaveable { mutableStateOf(null) } LaunchedEffect(session, url) GetAvatar@{ if (session == null) { @@ -48,21 +47,19 @@ fun AvatarFromURL( ) // TODO: Should I check the MIME type? And the size of the image? Log.d("Avatar", "File for $url is: $avatarFile") - val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath) - Log.d("Avatar", "Bitmap for $url is: $avatarBitmap") - avatar = avatarBitmap.asImageBitmap() + bitmap = BitmapFactory.decodeFile(avatarFile.absolutePath) } - if (session == null || url == null || avatar == null) { + if (session == null || url == null || bitmap == null) { AvatarFromDefault( modifier = modifier, fallbackText = fallbackText, contentDescription = contentDescription ) } else { - Image( + AvatarFromImageBitmap( modifier = modifier, - bitmap = avatar!!, + bitmap = bitmap!!.asImageBitmap(), contentDescription = contentDescription, ) }