Restructures Stable elements for minor performance gains.

This commit is contained in:
Vitor Pamplona
2023-06-18 18:57:02 -04:00
parent a3efd15b95
commit be8848fb1b
5 changed files with 121 additions and 102 deletions
@@ -13,8 +13,6 @@ import okhttp3.Request
import okhttp3.Response import okhttp3.Response
class Nip05Verifier() { class Nip05Verifier() {
val client = HttpClient.getHttpClient()
fun assembleUrl(nip05address: String): String? { fun assembleUrl(nip05address: String): String? {
val parts = nip05address.trim().split("@") val parts = nip05address.trim().split("@")
@@ -49,7 +47,7 @@ class Nip05Verifier() {
.url(url) .url(url)
.build() .build()
client.newCall(request).enqueue(object : Callback { HttpClient.getHttpClient().newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
response.use { response.use {
if (it.isSuccessful) { if (it.isSuccessful) {
@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.components package com.vitorpamplona.amethyst.ui.components
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.service.checkNotInMainThread import com.vitorpamplona.amethyst.service.checkNotInMainThread
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -15,6 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean
/** /**
* This class is designed to have a waiting time between two calls of invalidate * This class is designed to have a waiting time between two calls of invalidate
*/ */
@Stable
class BundledUpdate( class BundledUpdate(
val delay: Long, val delay: Long,
val dispatcher: CoroutineDispatcher = Dispatchers.Default val dispatcher: CoroutineDispatcher = Dispatchers.Default
@@ -51,6 +53,7 @@ class BundledUpdate(
/** /**
* This class is designed to have a waiting time between two calls of invalidate * This class is designed to have a waiting time between two calls of invalidate
*/ */
@Stable
class BundledInsert<T>( class BundledInsert<T>(
val delay: Long, val delay: Long,
val dispatcher: CoroutineDispatcher = Dispatchers.Default val dispatcher: CoroutineDispatcher = Dispatchers.Default
@@ -222,30 +222,35 @@ fun RenderZapGallery(
nav: (String) -> Unit, nav: (String) -> Unit,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
Row(remember { Modifier.fillMaxWidth() }) { Row(Modifier.fillMaxWidth()) {
Box( ZapIcon()
modifier = remember {
Modifier
.width(55.dp)
.padding(0.dp)
}
) {
Icon(
imageVector = Icons.Default.Bolt,
contentDescription = "Zaps",
tint = BitcoinOrange,
modifier = remember {
Modifier
.size(25.dp)
.align(Alignment.TopEnd)
}
)
}
AuthorGalleryZaps(zapEvents, backgroundColor, nav, accountViewModel) AuthorGalleryZaps(zapEvents, backgroundColor, nav, accountViewModel)
} }
} }
@Composable
private fun ZapIcon() {
Box(
modifier = remember {
Modifier
.width(55.dp)
.padding(0.dp)
}
) {
Icon(
imageVector = Icons.Default.Bolt,
contentDescription = "Zaps",
tint = BitcoinOrange,
modifier = remember {
Modifier
.size(25.dp)
.align(Alignment.TopEnd)
}
)
}
}
@Composable @Composable
fun RenderBoostGallery( fun RenderBoostGallery(
boostEvents: ImmutableList<Note>, boostEvents: ImmutableList<Note>,
@@ -313,7 +318,7 @@ private fun AuthorPictureAndComment(
nav: (String) -> Unit, nav: (String) -> Unit,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
var content by remember { val content = remember {
mutableStateOf( mutableStateOf(
ZapAmountCommentNotification( ZapAmountCommentNotification(
user = zapRequest.author, user = zapRequest.author,
@@ -330,28 +335,24 @@ private fun AuthorPictureAndComment(
val amount = (zapEvent?.event as? LnZapEvent)?.amount val amount = (zapEvent?.event as? LnZapEvent)?.amount
if (decryptedContent != null) { if (decryptedContent != null) {
val newAuthor = LocalCache.getOrCreateUser(decryptedContent.pubKey) val newAuthor = LocalCache.getOrCreateUser(decryptedContent.pubKey)
content = ZapAmountCommentNotification(newAuthor, decryptedContent.content.ifBlank { null }, showAmountAxis(amount)) content.value = ZapAmountCommentNotification(newAuthor, decryptedContent.content.ifBlank { null }, showAmountAxis(amount))
} else { } else {
if (!zapRequest.event?.content().isNullOrBlank() || amount != null) { if (!zapRequest.event?.content().isNullOrBlank() || amount != null) {
content = ZapAmountCommentNotification(zapRequest.author, zapRequest.event?.content()?.ifBlank { null }, showAmountAxis(amount)) content.value = ZapAmountCommentNotification(zapRequest.author, zapRequest.event?.content()?.ifBlank { null }, showAmountAxis(amount))
} }
} }
} }
} }
} }
val route by remember { Row(
derivedStateOf { modifier = Modifier.clickable {
"User/${content.user?.pubkeyHex}" nav("User/${content.value.user?.pubkeyHex}")
} },
} verticalAlignment = Alignment.CenterVertically
) {
content.user?.let { user ->
AuthorPictureAndComment( AuthorPictureAndComment(
author = user, authorComment = content,
comment = content.comment,
amount = content.amount,
route = route,
backgroundColor = backgroundColor, backgroundColor = backgroundColor,
nav = nav, nav = nav,
accountViewModel = accountViewModel accountViewModel = accountViewModel
@@ -375,69 +376,76 @@ val commentTextSize = 12.sp
@Composable @Composable
private fun AuthorPictureAndComment( private fun AuthorPictureAndComment(
author: User, authorComment: MutableState<ZapAmountCommentNotification>,
comment: String?,
amount: String?,
route: String,
backgroundColor: MutableState<Color>, backgroundColor: MutableState<Color>,
nav: (String) -> Unit, nav: (String) -> Unit,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
val modifier = remember { Box(modifier = sizedModifier, contentAlignment = Alignment.BottomCenter) {
Modifier.clickable { DisplayPicture(authorComment, accountViewModel)
nav(route) DisplayAmount(authorComment)
}
} }
Row( DisplayComment(authorComment, backgroundColor, nav, accountViewModel)
modifier = modifier, }
verticalAlignment = Alignment.CenterVertically
) {
Box(modifier = sizedModifier, contentAlignment = Alignment.BottomCenter) {
FastNoteAuthorPicture(
author = author,
size = Size35dp,
accountViewModel = accountViewModel,
pictureModifier = simpleModifier
)
amount?.let { @Composable
Box( fun DisplayPicture(authorComment: MutableState<ZapAmountCommentNotification>, accountViewModel: AccountViewModel) {
modifier = amountBoxModifier, authorComment.value.user?.let {
contentAlignment = Alignment.BottomCenter FastNoteAuthorPicture(
) { author = it,
val backgroundColor = MaterialTheme.colors.overPictureBackground size = Size35dp,
Box( accountViewModel = accountViewModel,
modifier = remember { pictureModifier = simpleModifier
Modifier )
.fillMaxWidth() }
.drawBehind { drawRect(backgroundColor) } }
},
contentAlignment = Alignment.BottomCenter @Composable
) { fun DisplayAmount(authorComment: MutableState<ZapAmountCommentNotification>) {
Text( authorComment.value.amount?.let {
text = it, Box(
fontWeight = FontWeight.Bold, modifier = amountBoxModifier,
color = MaterialTheme.colors.secondaryVariant, contentAlignment = Alignment.BottomCenter
fontSize = commentTextSize, ) {
modifier = bottomPadding1dp val backgroundColor = MaterialTheme.colors.overPictureBackground
) Box(
} modifier = remember {
} Modifier
.fillMaxWidth()
.drawBehind { drawRect(backgroundColor) }
},
contentAlignment = Alignment.BottomCenter
) {
Text(
text = it,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colors.secondaryVariant,
fontSize = commentTextSize,
modifier = bottomPadding1dp
)
} }
} }
}
}
comment?.let { @Composable
TranslatableRichTextViewer( fun DisplayComment(
content = it, authorComment: MutableState<ZapAmountCommentNotification>,
canPreview = true, backgroundColor: MutableState<Color>,
tags = remember { ImmutableListOfLists() }, nav: (String) -> Unit,
modifier = textBoxModifier, accountViewModel: AccountViewModel
backgroundColor = backgroundColor, ) {
accountViewModel = accountViewModel, authorComment.value.comment?.let {
nav = nav TranslatableRichTextViewer(
) content = it,
} canPreview = true,
tags = remember { ImmutableListOfLists() },
modifier = textBoxModifier,
backgroundColor = backgroundColor,
accountViewModel = accountViewModel,
nav = nav
)
} }
} }
@@ -477,19 +485,28 @@ private fun NotePictureAndComment(
nav: (String) -> Unit, nav: (String) -> Unit,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
val author by remember(baseNote) { val author = remember(baseNote) {
derivedStateOf { mutableStateOf(
baseNote.author ZapAmountCommentNotification(
user = baseNote.author,
comment = null,
amount = null
)
)
}
val modifier = remember(baseNote) {
Modifier.clickable {
nav("User/${baseNote.author?.pubkeyHex}")
} }
} }
val route by remember(baseNote) { Row(
derivedStateOf { modifier = modifier,
"User/${baseNote.author?.pubkeyHex}" verticalAlignment = Alignment.CenterVertically
} ) {
AuthorPictureAndComment(authorComment = author, backgroundColor, nav, accountViewModel)
} }
author?.let { AuthorPictureAndComment(it, null, null, route, backgroundColor, nav, accountViewModel) }
} }
@Composable @Composable
@@ -2706,9 +2706,7 @@ private fun VerticalRelayPanelWithFlow(
val showMoreRelaysButtonIconButtonModifier = Modifier.size(24.dp) val showMoreRelaysButtonIconButtonModifier = Modifier.size(24.dp)
val showMoreRelaysButtonIconModifier = Modifier.size(15.dp) val showMoreRelaysButtonIconModifier = Modifier.size(15.dp)
val showMoreRelaysButtonBoxModifer = Modifier val showMoreRelaysButtonBoxModifer = Modifier.fillMaxWidth().height(25.dp)
.fillMaxWidth()
.height(25.dp)
@Composable @Composable
private fun ShowMoreRelaysButton(onClick: () -> Unit) { private fun ShowMoreRelaysButton(onClick: () -> Unit) {
@@ -2879,7 +2877,7 @@ fun UserPicture(
@Composable @Composable
private fun ObserveAndDisplayFollowingMark(userHex: String, iconSize: Dp, accountViewModel: AccountViewModel) { private fun ObserveAndDisplayFollowingMark(userHex: String, iconSize: Dp, accountViewModel: AccountViewModel) {
val accountFollowsState by accountViewModel.account.userProfile().live().follows.observeAsState() val accountFollowsState by accountViewModel.userFollows.observeAsState()
var showFollowingMark by remember { mutableStateOf(false) } var showFollowingMark by remember { mutableStateOf(false) }
@@ -15,6 +15,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountState import com.vitorpamplona.amethyst.model.AccountState
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.model.UserState
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.service.model.Event import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.model.LnZapEvent import com.vitorpamplona.amethyst.service.model.LnZapEvent
@@ -32,6 +33,8 @@ class AccountViewModel(val account: Account) : ViewModel() {
val accountLanguagesLiveData: LiveData<AccountState> = account.liveLanguages.map { it } val accountLanguagesLiveData: LiveData<AccountState> = account.liveLanguages.map { it }
val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it } val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it }
val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it }
fun isWriteable(): Boolean { fun isWriteable(): Boolean {
return account.isWriteable() return account.isWriteable()
} }