From 5a2d3faa75adc8d3b0e668aa0b0dc620a4520a56 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 30 Jan 2024 04:53:58 +0100 Subject: [PATCH] More avatar refactorings --- .../avatar/{ => components}/AvatarEmpty.kt | 2 +- .../avatar/{ => components}/AvatarImage.kt | 2 +- .../avatar/{ => components}/AvatarURL.kt | 3 +- .../avatar/{ => components}/AvatarUser.kt | 2 +- .../avatar/{ => components}/AvatarUserId.kt | 3 +- .../{ => effects}/avatarUrlFromUserId.kt | 2 +- .../{ => effects}/bitmapFromMatrixFile.kt | 10 +++--- .../AvatarPickerWrapbox.kt} | 17 +++------ .../configureroom/ConfigureRoomForm.kt | 28 ++++++++++++--- .../configureroom/ConfigureRoomScaffold.kt | 35 +++++++++++-------- .../composables/main/AccountIconButton.kt | 2 +- .../twom/composables/main/RoomListItem.kt | 2 +- .../composables/viewroom/MemberListItem.kt | 2 +- .../composables/viewroom/RoomIconButton.kt | 2 +- 14 files changed, 65 insertions(+), 47 deletions(-) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => components}/AvatarEmpty.kt (95%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => components}/AvatarImage.kt (96%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => components}/AvatarURL.kt (86%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => components}/AvatarUser.kt (94%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => components}/AvatarUserId.kt (85%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => effects}/avatarUrlFromUserId.kt (96%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{ => effects}/bitmapFromMatrixFile.kt (88%) rename app/src/main/java/eu/steffo/twom/composables/avatar/{AvatarPicker.kt => picker/AvatarPickerWrapbox.kt} (82%) diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarEmpty.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarEmpty.kt similarity index 95% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarEmpty.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarEmpty.kt index 35f0eb9..e9c308b 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarEmpty.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarEmpty.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.components import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarImage.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarImage.kt similarity index 96% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarImage.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarImage.kt index f2c418a..44fe804 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarImage.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarImage.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.components import androidx.compose.foundation.Image import androidx.compose.runtime.Composable diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarURL.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarURL.kt similarity index 86% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarURL.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarURL.kt index 31f7623..dd6cc6a 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarURL.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarURL.kt @@ -1,9 +1,10 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.components import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.tooling.preview.Preview +import eu.steffo.twom.composables.avatar.effects.bitmapFromMatrixFile @Composable @Preview(name = "Regular", widthDp = 40, heightDp = 40) diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUser.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUser.kt similarity index 94% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUser.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUser.kt index 41e35c6..5747838 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUser.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUser.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.components import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUserId.kt similarity index 85% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUserId.kt index c3c2edd..7a53e77 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarUserId.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/components/AvatarUserId.kt @@ -1,8 +1,9 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.components import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview +import eu.steffo.twom.composables.avatar.effects.avatarUrlFromUserId @Composable @Preview(name = "Regular", widthDp = 40, heightDp = 40) diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/effects/avatarUrlFromUserId.kt similarity index 96% rename from app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/effects/avatarUrlFromUserId.kt index 93279e5..c397bcc 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/avatarUrlFromUserId.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/effects/avatarUrlFromUserId.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.effects import android.util.Log import androidx.compose.runtime.Composable diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/bitmapFromMatrixFile.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/effects/bitmapFromMatrixFile.kt similarity index 88% rename from app/src/main/java/eu/steffo/twom/composables/avatar/bitmapFromMatrixFile.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/effects/bitmapFromMatrixFile.kt index 0e24ecd..8885d06 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/bitmapFromMatrixFile.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/effects/bitmapFromMatrixFile.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.effects import android.graphics.Bitmap import android.util.Log @@ -48,9 +48,9 @@ fun bitmapFromMatrixFile(url: String? = null): Bitmap? { } Log.i(TAG, "Downloading file at: $url") - lateinit var avatarFile: File + lateinit var file: File try { - avatarFile = session.fileService().downloadFile( + file = session.fileService().downloadFile( fileName = url.md5(), url = url, mimeType = null, @@ -67,9 +67,9 @@ fun bitmapFromMatrixFile(url: String? = null): Bitmap? { return@Fetch } - Log.d(TAG, "File for $url is: $avatarFile") + Log.d(TAG, "File for $url is: $file") - bitmap = BitmapUtilities.getCorrectedBitmap(resolver, avatarFile.toUri()) + bitmap = BitmapUtilities.getCorrectedBitmap(resolver, file.toUri()) } return bitmap diff --git a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarPicker.kt b/app/src/main/java/eu/steffo/twom/composables/avatar/picker/AvatarPickerWrapbox.kt similarity index 82% rename from app/src/main/java/eu/steffo/twom/composables/avatar/AvatarPicker.kt rename to app/src/main/java/eu/steffo/twom/composables/avatar/picker/AvatarPickerWrapbox.kt index 66d8308..f4c7f15 100644 --- a/app/src/main/java/eu/steffo/twom/composables/avatar/AvatarPicker.kt +++ b/app/src/main/java/eu/steffo/twom/composables/avatar/picker/AvatarPickerWrapbox.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.composables.avatar +package eu.steffo.twom.composables.avatar.picker import android.graphics.Bitmap import androidx.activity.compose.rememberLauncherForActivityResult @@ -9,7 +9,6 @@ import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.layout.Box import androidx.compose.runtime.Composable 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.utils.BitmapUtilities @@ -19,12 +18,10 @@ import eu.steffo.twom.utils.BitmapUtilities @Preview(name = "Regular", widthDp = 40, heightDp = 40) @Preview(name = "Double font scale", widthDp = 40, heightDp = 40, fontScale = 2f) @Preview(name = "Quadruple font scale", widthDp = 40, heightDp = 40, fontScale = 4f) -fun AvatarPicker( +fun AvatarPickerWrapbox( modifier: Modifier = Modifier, - fallbackText: String = "?", - value: Bitmap? = null, onPick: (bitmap: Bitmap?) -> Unit = {}, - alpha: Float = 1.0f, + contents: @Composable () -> Unit = {}, ) { val context = LocalContext.current val resolver = context.contentResolver @@ -32,9 +29,7 @@ fun AvatarPicker( val launcher = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) ImageSelect@{ it ?: return@ImageSelect - val correctedBitmap = BitmapUtilities.getCorrectedBitmap(resolver, it) - onPick(correctedBitmap) } @@ -45,10 +40,6 @@ fun AvatarPicker( onLongClick = { onPick(null) }, ) ) { - AvatarImage( - bitmap = value?.asImageBitmap(), - fallbackText = fallbackText, - alpha = alpha, - ) + contents() } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomForm.kt b/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomForm.kt index 8264973..cabdaf5 100644 --- a/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomForm.kt +++ b/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomForm.kt @@ -22,6 +22,7 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.contentDescription @@ -30,7 +31,9 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import eu.steffo.twom.R import eu.steffo.twom.activities.ConfigureRoomActivity -import eu.steffo.twom.composables.avatar.AvatarPicker +import eu.steffo.twom.composables.avatar.components.AvatarImage +import eu.steffo.twom.composables.avatar.components.AvatarURL +import eu.steffo.twom.composables.avatar.picker.AvatarPickerWrapbox import eu.steffo.twom.composables.theme.basePadding import eu.steffo.twom.utils.BitmapUtilities @@ -46,22 +49,39 @@ fun ConfigureRoomForm( // TODO: How to load the original avatar from the URL? var avatarBitmap by remember { mutableStateOf(null) } + val fallbackText = if (name.isEmpty()) { + null + } else { + name[0].toString() + } + val context = LocalContext.current val activity = context as Activity Column(modifier) { Row(Modifier.basePadding()) { val avatarContentDescription = stringResource(R.string.create_avatar_label) - AvatarPicker( + AvatarPickerWrapbox( modifier = Modifier .size(60.dp) .clip(MaterialTheme.shapes.medium) .semantics { this.contentDescription = avatarContentDescription }, - value = avatarBitmap, onPick = { avatarBitmap = it }, - ) + ) { + if (avatarBitmap == null) { + AvatarURL( + url = initialConfiguration?.avatarUri?.toString(), + fallbackText = fallbackText, + ) + } else { + AvatarImage( + bitmap = avatarBitmap?.asImageBitmap(), + fallbackText = fallbackText, + ) + } + } TextField( modifier = Modifier .height(60.dp) diff --git a/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomScaffold.kt b/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomScaffold.kt index 064f418..ce91f78 100644 --- a/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/composables/configureroom/ConfigureRoomScaffold.kt @@ -3,29 +3,34 @@ package eu.steffo.twom.composables.configureroom import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.Modifier -import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.activities.ConfigureRoomActivity +import eu.steffo.twom.composables.matrix.LocalSession import eu.steffo.twom.composables.theme.TwoMTheme +import eu.steffo.twom.utils.TwoMGlobals @Composable -@Preview fun ConfigureRoomScaffold( initialConfiguration: ConfigureRoomActivity.Configuration? = null, ) { + val session = TwoMGlobals.matrix.authenticationService().getLastAuthenticatedSession()!! + TwoMTheme { - Scaffold( - topBar = { - ConfigureActivityTopBar( - initialName = initialConfiguration?.name, - ) - }, - content = { - ConfigureRoomForm( - modifier = Modifier.padding(it), - initialConfiguration = initialConfiguration, - ) - } - ) + CompositionLocalProvider(LocalSession provides session) { + Scaffold( + topBar = { + ConfigureActivityTopBar( + initialName = initialConfiguration?.name, + ) + }, + content = { + ConfigureRoomForm( + modifier = Modifier.padding(it), + initialConfiguration = initialConfiguration, + ) + } + ) + } } } \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/composables/main/AccountIconButton.kt b/app/src/main/java/eu/steffo/twom/composables/main/AccountIconButton.kt index 16e05e9..2738812 100644 --- a/app/src/main/java/eu/steffo/twom/composables/main/AccountIconButton.kt +++ b/app/src/main/java/eu/steffo/twom/composables/main/AccountIconButton.kt @@ -21,7 +21,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R import eu.steffo.twom.activities.LoginActivity -import eu.steffo.twom.composables.avatar.AvatarUserId +import eu.steffo.twom.composables.avatar.components.AvatarUserId import eu.steffo.twom.composables.matrix.LocalSession @Composable diff --git a/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt b/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt index 5615475..0655ed1 100644 --- a/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt +++ b/app/src/main/java/eu/steffo/twom/composables/main/RoomListItem.kt @@ -25,7 +25,7 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import eu.steffo.twom.R import eu.steffo.twom.activities.ViewRoomActivity -import eu.steffo.twom.composables.avatar.AvatarURL +import eu.steffo.twom.composables.avatar.components.AvatarURL import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.matrix.LocalSession 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 13082a5..5448b38 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 @@ -25,7 +25,7 @@ import androidx.compose.ui.draw.clip import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import eu.steffo.twom.R -import eu.steffo.twom.composables.avatar.AvatarUser +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.utils.RSVPAnswer diff --git a/app/src/main/java/eu/steffo/twom/composables/viewroom/RoomIconButton.kt b/app/src/main/java/eu/steffo/twom/composables/viewroom/RoomIconButton.kt index 3dabcd7..99be980 100644 --- a/app/src/main/java/eu/steffo/twom/composables/viewroom/RoomIconButton.kt +++ b/app/src/main/java/eu/steffo/twom/composables/viewroom/RoomIconButton.kt @@ -24,7 +24,7 @@ import androidx.compose.ui.unit.dp import androidx.core.net.toFile import eu.steffo.twom.R import eu.steffo.twom.activities.ConfigureRoomActivity -import eu.steffo.twom.composables.avatar.AvatarURL +import eu.steffo.twom.composables.avatar.components.AvatarURL import eu.steffo.twom.composables.errorhandling.ErrorIconButton import eu.steffo.twom.composables.matrix.LocalSession import kotlinx.coroutines.launch