kopia lustrzana https://github.com/vitorpamplona/amethyst
248 wiersze
11 KiB
Kotlin
248 wiersze
11 KiB
Kotlin
/**
|
|
* Copyright (c) 2024 Vitor Pamplona
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
* the Software without restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
* Software, and to permit persons to whom the Software is furnished to do so,
|
|
* subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
package com.vitorpamplona.amethyst.ui.note.types
|
|
|
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
|
import androidx.compose.foundation.Image
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.foundation.border
|
|
import androidx.compose.foundation.combinedClickable
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.Row
|
|
import androidx.compose.foundation.layout.Spacer
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.height
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.layout.size
|
|
import androidx.compose.foundation.shape.CircleShape
|
|
import androidx.compose.foundation.text.ClickableText
|
|
import androidx.compose.material3.LocalTextStyle
|
|
import androidx.compose.material3.MaterialTheme
|
|
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.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.draw.clip
|
|
import androidx.compose.ui.layout.ContentScale
|
|
import androidx.compose.ui.platform.LocalClipboardManager
|
|
import androidx.compose.ui.platform.LocalUriHandler
|
|
import androidx.compose.ui.res.painterResource
|
|
import androidx.compose.ui.res.stringResource
|
|
import androidx.compose.ui.text.AnnotatedString
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.compose.ui.unit.sp
|
|
import coil.compose.AsyncImage
|
|
import com.vitorpamplona.amethyst.R
|
|
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
|
|
import com.vitorpamplona.amethyst.model.Note
|
|
import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
|
|
import com.vitorpamplona.amethyst.ui.components.TranslatableRichTextViewer
|
|
import com.vitorpamplona.amethyst.ui.components.ZoomableImageDialog
|
|
import com.vitorpamplona.amethyst.ui.note.LinkIcon
|
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
|
import com.vitorpamplona.amethyst.ui.theme.Size16Modifier
|
|
import com.vitorpamplona.amethyst.ui.theme.Size35dp
|
|
import com.vitorpamplona.amethyst.ui.theme.placeholderText
|
|
import com.vitorpamplona.quartz.events.AppDefinitionEvent
|
|
import com.vitorpamplona.quartz.events.EmptyTagList
|
|
import com.vitorpamplona.quartz.events.UserMetadata
|
|
import com.vitorpamplona.quartz.events.toImmutableListOfLists
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.withContext
|
|
|
|
@OptIn(ExperimentalFoundationApi::class)
|
|
@Composable
|
|
fun RenderAppDefinition(
|
|
note: Note,
|
|
accountViewModel: AccountViewModel,
|
|
nav: (String) -> Unit,
|
|
) {
|
|
val noteEvent = note.event as? AppDefinitionEvent ?: return
|
|
|
|
var metadata by remember { mutableStateOf<UserMetadata?>(null) }
|
|
|
|
LaunchedEffect(key1 = noteEvent) {
|
|
withContext(Dispatchers.Default) { metadata = noteEvent.appMetaData() }
|
|
}
|
|
|
|
metadata?.let {
|
|
Box {
|
|
val clipboardManager = LocalClipboardManager.current
|
|
val uri = LocalUriHandler.current
|
|
|
|
if (!it.banner.isNullOrBlank()) {
|
|
var zoomImageDialogOpen by remember { mutableStateOf(false) }
|
|
|
|
AsyncImage(
|
|
model = it.banner,
|
|
contentDescription = stringResource(id = R.string.profile_image),
|
|
contentScale = ContentScale.FillWidth,
|
|
modifier =
|
|
Modifier
|
|
.fillMaxWidth()
|
|
.height(125.dp)
|
|
.combinedClickable(
|
|
onClick = {},
|
|
onLongClick = { clipboardManager.setText(AnnotatedString(it.banner!!)) },
|
|
),
|
|
)
|
|
|
|
if (zoomImageDialogOpen) {
|
|
ZoomableImageDialog(
|
|
imageUrl = RichTextParser.parseImageOrVideo(it.banner!!),
|
|
onDismiss = { zoomImageDialogOpen = false },
|
|
accountViewModel = accountViewModel,
|
|
)
|
|
}
|
|
} else {
|
|
Image(
|
|
painter = painterResource(R.drawable.profile_banner),
|
|
contentDescription = stringResource(id = R.string.profile_banner),
|
|
contentScale = ContentScale.FillWidth,
|
|
modifier =
|
|
Modifier
|
|
.fillMaxWidth()
|
|
.height(125.dp),
|
|
)
|
|
}
|
|
|
|
Column(
|
|
modifier =
|
|
Modifier
|
|
.fillMaxWidth()
|
|
.padding(horizontal = 10.dp)
|
|
.padding(top = 75.dp),
|
|
) {
|
|
Row(
|
|
horizontalArrangement = Arrangement.SpaceBetween,
|
|
verticalAlignment = Alignment.Bottom,
|
|
) {
|
|
var zoomImageDialogOpen by remember { mutableStateOf(false) }
|
|
|
|
Box(Modifier.size(100.dp)) {
|
|
it.picture?.let {
|
|
AsyncImage(
|
|
model = it,
|
|
contentDescription = null,
|
|
contentScale = ContentScale.FillWidth,
|
|
modifier =
|
|
Modifier
|
|
.border(
|
|
3.dp,
|
|
MaterialTheme.colorScheme.background,
|
|
CircleShape,
|
|
)
|
|
.clip(shape = CircleShape)
|
|
.fillMaxSize()
|
|
.background(MaterialTheme.colorScheme.background)
|
|
.combinedClickable(
|
|
onClick = { zoomImageDialogOpen = true },
|
|
onLongClick = { clipboardManager.setText(AnnotatedString(it)) },
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
if (zoomImageDialogOpen) {
|
|
ZoomableImageDialog(
|
|
imageUrl = RichTextParser.parseImageOrVideo(it.banner!!),
|
|
onDismiss = { zoomImageDialogOpen = false },
|
|
accountViewModel = accountViewModel,
|
|
)
|
|
}
|
|
|
|
Spacer(Modifier.weight(1f))
|
|
|
|
Row(
|
|
modifier =
|
|
Modifier
|
|
.height(Size35dp)
|
|
.padding(bottom = 3.dp),
|
|
) {}
|
|
}
|
|
|
|
val name = remember(it) { it.anyName() }
|
|
name?.let {
|
|
Row(
|
|
verticalAlignment = Alignment.Bottom,
|
|
modifier = Modifier.padding(top = 7.dp),
|
|
) {
|
|
CreateTextWithEmoji(
|
|
text = it,
|
|
tags =
|
|
remember {
|
|
(note.event?.tags() ?: emptyArray()).toImmutableListOfLists()
|
|
},
|
|
fontWeight = FontWeight.Bold,
|
|
fontSize = 25.sp,
|
|
)
|
|
}
|
|
}
|
|
|
|
val website = remember(it) { it.website }
|
|
if (!website.isNullOrEmpty()) {
|
|
Row(verticalAlignment = Alignment.CenterVertically) {
|
|
LinkIcon(Size16Modifier, MaterialTheme.colorScheme.placeholderText)
|
|
|
|
ClickableText(
|
|
text = AnnotatedString(website.removePrefix("https://")),
|
|
onClick = { website.let { runCatching { uri.openUri(it) } } },
|
|
style = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.primary),
|
|
modifier = Modifier.padding(top = 1.dp, bottom = 1.dp, start = 5.dp),
|
|
)
|
|
}
|
|
}
|
|
|
|
it.about?.let {
|
|
Row(
|
|
modifier = Modifier.padding(top = 5.dp, bottom = 5.dp),
|
|
) {
|
|
val tags =
|
|
remember(note) {
|
|
note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList
|
|
}
|
|
val bgColor = MaterialTheme.colorScheme.background
|
|
val backgroundColor = remember { mutableStateOf(bgColor) }
|
|
TranslatableRichTextViewer(
|
|
content = it,
|
|
canPreview = false,
|
|
quotesLeft = 1,
|
|
tags = tags,
|
|
backgroundColor = backgroundColor,
|
|
id = note.idHex,
|
|
accountViewModel = accountViewModel,
|
|
nav = nav,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|