Small optimizations: removing animations, avoiding remembers

This commit is contained in:
Vitor Pamplona
2024-06-13 19:08:20 -04:00
parent 2503cbaeac
commit d7a22f609a
6 changed files with 33 additions and 45 deletions

View File

@@ -20,7 +20,6 @@
*/ */
package com.vitorpamplona.amethyst.ui.note package com.vitorpamplona.amethyst.ui.note
import androidx.compose.animation.Crossfade
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@@ -60,34 +59,32 @@ fun WatchBlockAndReport(
nav: (String) -> Unit, nav: (String) -> Unit,
normalNote: @Composable (canPreview: Boolean) -> Unit, normalNote: @Composable (canPreview: Boolean) -> Unit,
) { ) {
val hiddenState by accountViewModel.createIsHiddenFlow(note).collectAsStateWithLifecycle() val isHidden by accountViewModel.createIsHiddenFlow(note).collectAsStateWithLifecycle()
val showAnyway = val showAnyway =
remember { remember {
mutableStateOf(false) mutableStateOf(false)
} }
Crossfade(targetState = hiddenState, label = "CheckHiddenNoteCompose") { isHidden -> if (showAnyway.value) {
if (showAnyway.value) { normalNote(true)
normalNote(true) } else if (!isHidden.isPostHidden) {
} else if (!isHidden.isPostHidden) { if (isHidden.isAcceptable) {
if (isHidden.isAcceptable) { normalNote(isHidden.canPreview)
normalNote(isHidden.canPreview) } else {
} else { HiddenNote(
HiddenNote( isHidden.relevantReports,
isHidden.relevantReports, isHidden.isHiddenAuthor,
isHidden.isHiddenAuthor, accountViewModel,
accountViewModel, modifier,
modifier, nav,
nav, onClick = { showAnyway.value = true },
onClick = { showAnyway.value = true }, )
) }
} } else if (showHiddenWarning) {
} else if (showHiddenWarning) { // if it is a quoted or boosted note, how the hidden warning.
// if it is a quoted or boosted note, how the hidden warning. HiddenNoteByMe {
HiddenNoteByMe { showAnyway.value = true
showAnyway.value = true
}
} }
} }
} }

View File

@@ -113,13 +113,13 @@ import com.vitorpamplona.amethyst.ui.note.types.RenderWikiContent
import com.vitorpamplona.amethyst.ui.note.types.VideoDisplay import com.vitorpamplona.amethyst.ui.note.types.VideoDisplay
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.RenderChannelHeader import com.vitorpamplona.amethyst.ui.screen.loggedIn.RenderChannelHeader
import com.vitorpamplona.amethyst.ui.theme.DoubleHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
import com.vitorpamplona.amethyst.ui.theme.Font12SP import com.vitorpamplona.amethyst.ui.theme.Font12SP
import com.vitorpamplona.amethyst.ui.theme.HalfDoubleVertSpacer import com.vitorpamplona.amethyst.ui.theme.HalfDoubleVertSpacer
import com.vitorpamplona.amethyst.ui.theme.HalfEndPadding import com.vitorpamplona.amethyst.ui.theme.HalfEndPadding
import com.vitorpamplona.amethyst.ui.theme.HalfPadding import com.vitorpamplona.amethyst.ui.theme.HalfPadding
import com.vitorpamplona.amethyst.ui.theme.HalfStartPadding import com.vitorpamplona.amethyst.ui.theme.HalfStartPadding
import com.vitorpamplona.amethyst.ui.theme.RowColSpacing10dp
import com.vitorpamplona.amethyst.ui.theme.RowColSpacing5dp import com.vitorpamplona.amethyst.ui.theme.RowColSpacing5dp
import com.vitorpamplona.amethyst.ui.theme.Size25dp import com.vitorpamplona.amethyst.ui.theme.Size25dp
import com.vitorpamplona.amethyst.ui.theme.Size30Modifier import com.vitorpamplona.amethyst.ui.theme.Size30Modifier
@@ -458,10 +458,10 @@ fun InnerNoteWithReactions(
} else { } else {
boostedNoteModifier boostedNoteModifier
}, },
horizontalArrangement = RowColSpacing10dp,
) { ) {
if (notBoostedNorQuote) { if (notBoostedNorQuote) {
AuthorAndRelayInformation(baseNote, accountViewModel, nav) AuthorAndRelayInformation(baseNote, accountViewModel, nav)
Spacer(modifier = DoubleHorzSpacer)
} }
Column(Modifier.fillMaxWidth()) { Column(Modifier.fillMaxWidth()) {

View File

@@ -154,12 +154,8 @@ fun ReactionsRow(
) { ) {
val wantsToSeeReactions = remember(baseNote) { mutableStateOf(false) } val wantsToSeeReactions = remember(baseNote) { mutableStateOf(false) }
Spacer(modifier = HalfDoubleVertSpacer)
InnerReactionRow(baseNote, showReactionDetail, addPadding, wantsToSeeReactions, editState, accountViewModel, nav) InnerReactionRow(baseNote, showReactionDetail, addPadding, wantsToSeeReactions, editState, accountViewModel, nav)
Spacer(modifier = HalfDoubleVertSpacer)
LoadAndDisplayZapraiser(baseNote, showReactionDetail, wantsToSeeReactions, accountViewModel) LoadAndDisplayZapraiser(baseNote, showReactionDetail, wantsToSeeReactions, accountViewModel)
if (showReactionDetail && wantsToSeeReactions.value) { if (showReactionDetail && wantsToSeeReactions.value) {

View File

@@ -248,9 +248,7 @@ private fun FeedLoaded(
state = listState, state = listState,
) { ) {
itemsIndexed(state.feed.value, key = { _, item -> item.idHex }) { _, item -> itemsIndexed(state.feed.value, key = { _, item -> item.idHex }) { _, item ->
val defaultModifier = remember { Modifier.fillMaxWidth().animateItemPlacement() } Row(Modifier.fillMaxWidth().animateItemPlacement()) {
Row(defaultModifier) {
NoteCompose( NoteCompose(
item, item,
routeForLastRead = routeForLastRead, routeForLastRead = routeForLastRead,

View File

@@ -276,10 +276,10 @@ class AccountViewModel(
return if (isPostHidden) { return if (isPostHidden) {
// Spam + Blocked Users + Hidden Words + Sensitive Content // Spam + Blocked Users + Hidden Words + Sensitive Content
NoteComposeReportState(isPostHidden, false, false, isHiddenAuthor, persistentSetOf()) NoteComposeReportState(isPostHidden, false, false, isHiddenAuthor)
} else if (isFromLoggedIn || isFromLoggedInFollow) { } else if (isFromLoggedIn || isFromLoggedInFollow) {
// No need to process if from trusted people // No need to process if from trusted people
NoteComposeReportState(isPostHidden, true, true, isHiddenAuthor, persistentSetOf()) NoteComposeReportState(isPostHidden, true, true, isHiddenAuthor)
} else { } else {
val newCanPreview = !note.hasAnyReports() val newCanPreview = !note.hasAnyReports()
@@ -287,22 +287,20 @@ class AccountViewModel(
if (newCanPreview && newIsAcceptable) { if (newCanPreview && newIsAcceptable) {
// No need to process reports if nothing is wrong // No need to process reports if nothing is wrong
NoteComposeReportState(isPostHidden, true, true, false, persistentSetOf()) NoteComposeReportState(isPostHidden, true, true, false)
} else { } else {
val newRelevantReports = account.getRelevantReports(note)
NoteComposeReportState( NoteComposeReportState(
isPostHidden, isPostHidden,
newIsAcceptable, newIsAcceptable,
newCanPreview, newCanPreview,
false, false,
newRelevantReports.toImmutableSet(), account.getRelevantReports(note).toImmutableSet(),
) )
} }
} }
} }
val noteIsHiddenFlows = LruCache<Note, StateFlow<NoteComposeReportState>>(300) private val noteIsHiddenFlows = LruCache<Note, StateFlow<NoteComposeReportState>>(300)
fun createIsHiddenFlow(note: Note): StateFlow<NoteComposeReportState> = fun createIsHiddenFlow(note: Note): StateFlow<NoteComposeReportState> =
noteIsHiddenFlows.get(note) noteIsHiddenFlows.get(note)
@@ -312,9 +310,7 @@ class AccountViewModel(
note.flow().metadata.stateFlow, note.flow().metadata.stateFlow,
note.flow().reports.stateFlow, note.flow().reports.stateFlow,
) { hiddenUsers, followingUsers, metadata, reports -> ) { hiddenUsers, followingUsers, metadata, reports ->
emit( emit(isNoteAcceptable(metadata.note, hiddenUsers, followingUsers.users))
isNoteAcceptable(metadata.note, hiddenUsers, followingUsers.users),
)
}.stateIn( }.stateIn(
viewModelScope, viewModelScope,
SharingStarted.Eagerly, SharingStarted.Eagerly,
@@ -323,7 +319,7 @@ class AccountViewModel(
noteIsHiddenFlows.put(note, it) noteIsHiddenFlows.put(note, it)
} }
val noteMustShowExpandButtonFlows = LruCache<Note, StateFlow<Boolean>>(300) private val noteMustShowExpandButtonFlows = LruCache<Note, StateFlow<Boolean>>(300)
fun createMustShowExpandButtonFlows(note: Note): StateFlow<Boolean> = fun createMustShowExpandButtonFlows(note: Note): StateFlow<Boolean> =
noteMustShowExpandButtonFlows.get(note) noteMustShowExpandButtonFlows.get(note)

View File

@@ -112,6 +112,7 @@ val BigPadding = Modifier.padding(15.dp)
val RowColSpacing = Arrangement.spacedBy(3.dp) val RowColSpacing = Arrangement.spacedBy(3.dp)
val RowColSpacing5dp = Arrangement.spacedBy(5.dp) val RowColSpacing5dp = Arrangement.spacedBy(5.dp)
val RowColSpacing10dp = Arrangement.spacedBy(10.dp)
val HalfHorzPadding = Modifier.padding(horizontal = 5.dp) val HalfHorzPadding = Modifier.padding(horizontal = 5.dp)
val HalfVertPadding = Modifier.padding(vertical = 5.dp) val HalfVertPadding = Modifier.padding(vertical = 5.dp)
@@ -154,8 +155,8 @@ val VideoReactionColumnPadding = Modifier.padding(bottom = 75.dp)
val DividerThickness = 0.25.dp val DividerThickness = 0.25.dp
val ReactionRowHeight = Modifier.height(24.dp) val ReactionRowHeight = Modifier.padding(vertical = 7.dp).height(24.dp)
val ReactionRowHeightWithPadding = Modifier.height(24.dp).padding(start = 10.dp) val ReactionRowHeightWithPadding = Modifier.padding(vertical = 7.dp).height(24.dp).padding(start = 10.dp)
val ReactionRowHeightChat = Modifier.height(25.dp) val ReactionRowHeightChat = Modifier.height(25.dp)
val UserNameRowHeight = Modifier.fillMaxWidth() val UserNameRowHeight = Modifier.fillMaxWidth()
val UserNameMaxRowHeight = Modifier.fillMaxWidth() val UserNameMaxRowHeight = Modifier.fillMaxWidth()