mirror of
https://github.com/Steffo99/twom.git
synced 2024-11-21 23:54:26 +00:00
More avatar refactorings
This commit is contained in:
parent
58b0901408
commit
5a2d3faa75
14 changed files with 65 additions and 47 deletions
|
@ -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.background
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
|
@ -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.foundation.Image
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
|
@ -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.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
import eu.steffo.twom.composables.avatar.effects.bitmapFromMatrixFile
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview(name = "Regular", widthDp = 40, heightDp = 40)
|
@Preview(name = "Regular", widthDp = 40, heightDp = 40)
|
|
@ -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.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
|
@ -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.runtime.Composable
|
||||||
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.avatar.effects.avatarUrlFromUserId
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview(name = "Regular", widthDp = 40, heightDp = 40)
|
@Preview(name = "Regular", widthDp = 40, heightDp = 40)
|
|
@ -1,4 +1,4 @@
|
||||||
package eu.steffo.twom.composables.avatar
|
package eu.steffo.twom.composables.avatar.effects
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
|
@ -1,4 +1,4 @@
|
||||||
package eu.steffo.twom.composables.avatar
|
package eu.steffo.twom.composables.avatar.effects
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
@ -48,9 +48,9 @@ fun bitmapFromMatrixFile(url: String? = null): Bitmap? {
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i(TAG, "Downloading file at: $url")
|
Log.i(TAG, "Downloading file at: $url")
|
||||||
lateinit var avatarFile: File
|
lateinit var file: File
|
||||||
try {
|
try {
|
||||||
avatarFile = session.fileService().downloadFile(
|
file = session.fileService().downloadFile(
|
||||||
fileName = url.md5(),
|
fileName = url.md5(),
|
||||||
url = url,
|
url = url,
|
||||||
mimeType = null,
|
mimeType = null,
|
||||||
|
@ -67,9 +67,9 @@ fun bitmapFromMatrixFile(url: String? = null): Bitmap? {
|
||||||
return@Fetch
|
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
|
return bitmap
|
|
@ -1,4 +1,4 @@
|
||||||
package eu.steffo.twom.composables.avatar
|
package eu.steffo.twom.composables.avatar.picker
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
|
@ -9,7 +9,6 @@ import androidx.compose.foundation.combinedClickable
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.utils.BitmapUtilities
|
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 = "Regular", widthDp = 40, heightDp = 40)
|
||||||
@Preview(name = "Double font scale", widthDp = 40, heightDp = 40, fontScale = 2f)
|
@Preview(name = "Double font scale", widthDp = 40, heightDp = 40, fontScale = 2f)
|
||||||
@Preview(name = "Quadruple font scale", widthDp = 40, heightDp = 40, fontScale = 4f)
|
@Preview(name = "Quadruple font scale", widthDp = 40, heightDp = 40, fontScale = 4f)
|
||||||
fun AvatarPicker(
|
fun AvatarPickerWrapbox(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
fallbackText: String = "?",
|
|
||||||
value: Bitmap? = null,
|
|
||||||
onPick: (bitmap: Bitmap?) -> Unit = {},
|
onPick: (bitmap: Bitmap?) -> Unit = {},
|
||||||
alpha: Float = 1.0f,
|
contents: @Composable () -> Unit = {},
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val resolver = context.contentResolver
|
val resolver = context.contentResolver
|
||||||
|
@ -32,9 +29,7 @@ fun AvatarPicker(
|
||||||
val launcher =
|
val launcher =
|
||||||
rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) ImageSelect@{
|
rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) ImageSelect@{
|
||||||
it ?: return@ImageSelect
|
it ?: return@ImageSelect
|
||||||
|
|
||||||
val correctedBitmap = BitmapUtilities.getCorrectedBitmap(resolver, it)
|
val correctedBitmap = BitmapUtilities.getCorrectedBitmap(resolver, it)
|
||||||
|
|
||||||
onPick(correctedBitmap)
|
onPick(correctedBitmap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,10 +40,6 @@ fun AvatarPicker(
|
||||||
onLongClick = { onPick(null) },
|
onLongClick = { onPick(null) },
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
AvatarImage(
|
contents()
|
||||||
bitmap = value?.asImageBitmap(),
|
|
||||||
fallbackText = fallbackText,
|
|
||||||
alpha = alpha,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -22,6 +22,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.semantics.contentDescription
|
import androidx.compose.ui.semantics.contentDescription
|
||||||
|
@ -30,7 +31,9 @@ import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
import eu.steffo.twom.activities.ConfigureRoomActivity
|
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.composables.theme.basePadding
|
||||||
import eu.steffo.twom.utils.BitmapUtilities
|
import eu.steffo.twom.utils.BitmapUtilities
|
||||||
|
|
||||||
|
@ -46,22 +49,39 @@ fun ConfigureRoomForm(
|
||||||
// TODO: How to load the original avatar from the URL?
|
// TODO: How to load the original avatar from the URL?
|
||||||
var avatarBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
var avatarBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||||
|
|
||||||
|
val fallbackText = if (name.isEmpty()) {
|
||||||
|
null
|
||||||
|
} else {
|
||||||
|
name[0].toString()
|
||||||
|
}
|
||||||
|
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val activity = context as Activity
|
val activity = context as Activity
|
||||||
|
|
||||||
Column(modifier) {
|
Column(modifier) {
|
||||||
Row(Modifier.basePadding()) {
|
Row(Modifier.basePadding()) {
|
||||||
val avatarContentDescription = stringResource(R.string.create_avatar_label)
|
val avatarContentDescription = stringResource(R.string.create_avatar_label)
|
||||||
AvatarPicker(
|
AvatarPickerWrapbox(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.size(60.dp)
|
.size(60.dp)
|
||||||
.clip(MaterialTheme.shapes.medium)
|
.clip(MaterialTheme.shapes.medium)
|
||||||
.semantics {
|
.semantics {
|
||||||
this.contentDescription = avatarContentDescription
|
this.contentDescription = avatarContentDescription
|
||||||
},
|
},
|
||||||
value = avatarBitmap,
|
|
||||||
onPick = { avatarBitmap = it },
|
onPick = { avatarBitmap = it },
|
||||||
)
|
) {
|
||||||
|
if (avatarBitmap == null) {
|
||||||
|
AvatarURL(
|
||||||
|
url = initialConfiguration?.avatarUri?.toString(),
|
||||||
|
fallbackText = fallbackText,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
AvatarImage(
|
||||||
|
bitmap = avatarBitmap?.asImageBitmap(),
|
||||||
|
fallbackText = fallbackText,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
TextField(
|
TextField(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.height(60.dp)
|
.height(60.dp)
|
||||||
|
|
|
@ -3,29 +3,34 @@ package eu.steffo.twom.composables.configureroom
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
|
||||||
import eu.steffo.twom.activities.ConfigureRoomActivity
|
import eu.steffo.twom.activities.ConfigureRoomActivity
|
||||||
|
import eu.steffo.twom.composables.matrix.LocalSession
|
||||||
import eu.steffo.twom.composables.theme.TwoMTheme
|
import eu.steffo.twom.composables.theme.TwoMTheme
|
||||||
|
import eu.steffo.twom.utils.TwoMGlobals
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview
|
|
||||||
fun ConfigureRoomScaffold(
|
fun ConfigureRoomScaffold(
|
||||||
initialConfiguration: ConfigureRoomActivity.Configuration? = null,
|
initialConfiguration: ConfigureRoomActivity.Configuration? = null,
|
||||||
) {
|
) {
|
||||||
|
val session = TwoMGlobals.matrix.authenticationService().getLastAuthenticatedSession()!!
|
||||||
|
|
||||||
TwoMTheme {
|
TwoMTheme {
|
||||||
Scaffold(
|
CompositionLocalProvider(LocalSession provides session) {
|
||||||
topBar = {
|
Scaffold(
|
||||||
ConfigureActivityTopBar(
|
topBar = {
|
||||||
initialName = initialConfiguration?.name,
|
ConfigureActivityTopBar(
|
||||||
)
|
initialName = initialConfiguration?.name,
|
||||||
},
|
)
|
||||||
content = {
|
},
|
||||||
ConfigureRoomForm(
|
content = {
|
||||||
modifier = Modifier.padding(it),
|
ConfigureRoomForm(
|
||||||
initialConfiguration = initialConfiguration,
|
modifier = Modifier.padding(it),
|
||||||
)
|
initialConfiguration = initialConfiguration,
|
||||||
}
|
)
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -21,7 +21,7 @@ import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
import eu.steffo.twom.activities.LoginActivity
|
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
|
import eu.steffo.twom.composables.matrix.LocalSession
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
@ -25,7 +25,7 @@ import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
import eu.steffo.twom.activities.ViewRoomActivity
|
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.ErrorText
|
||||||
import eu.steffo.twom.composables.errorhandling.LocalizableError
|
import eu.steffo.twom.composables.errorhandling.LocalizableError
|
||||||
import eu.steffo.twom.composables.matrix.LocalSession
|
import eu.steffo.twom.composables.matrix.LocalSession
|
||||||
|
|
|
@ -25,7 +25,7 @@ import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import eu.steffo.twom.R
|
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.errorhandling.ErrorText
|
||||||
import eu.steffo.twom.composables.matrix.LocalSession
|
import eu.steffo.twom.composables.matrix.LocalSession
|
||||||
import eu.steffo.twom.utils.RSVPAnswer
|
import eu.steffo.twom.utils.RSVPAnswer
|
||||||
|
|
|
@ -24,7 +24,7 @@ import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.net.toFile
|
import androidx.core.net.toFile
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
import eu.steffo.twom.activities.ConfigureRoomActivity
|
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.errorhandling.ErrorIconButton
|
||||||
import eu.steffo.twom.composables.matrix.LocalSession
|
import eu.steffo.twom.composables.matrix.LocalSession
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
Loading…
Reference in a new issue