Moves all note states to leaf compositors

This commit is contained in:
Vitor Pamplona
2023-05-28 15:34:13 -04:00
parent acbd41ce61
commit 5e3703e849
2 changed files with 207 additions and 172 deletions
@@ -83,7 +83,6 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Channel import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.NoteState
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.model.AudioTrackEvent import com.vitorpamplona.amethyst.service.model.AudioTrackEvent
import com.vitorpamplona.amethyst.service.model.BadgeAwardEvent import com.vitorpamplona.amethyst.service.model.BadgeAwardEvent
@@ -151,9 +150,9 @@ fun NoteCompose(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
println("AARender NoteCompose")
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return val account = remember(accountState) { accountState?.account } ?: return
val loggedIn = remember(accountState) { accountState?.account?.userProfile() } ?: return
val noteState by baseNote.live().metadata.observeAsState() val noteState by baseNote.live().metadata.observeAsState()
val note = remember(noteState) { noteState?.note } ?: return val note = remember(noteState) { noteState?.note } ?: return
@@ -161,13 +160,11 @@ fun NoteCompose(
val noteReportsState by baseNote.live().reports.observeAsState() val noteReportsState by baseNote.live().reports.observeAsState()
val noteForReports = remember(noteReportsState) { noteReportsState?.note } ?: return val noteForReports = remember(noteReportsState) { noteReportsState?.note } ?: return
val noteEvent = remember(noteState) { note.event }
val channelHex = remember(noteState) { note.channelHex() }
val isSensitive = remember(noteState) { note.event?.isSensitive() ?: false } val isSensitive = remember(noteState) { note.event?.isSensitive() ?: false }
if (note.event == null) {
var popupExpanded by remember { mutableStateOf(false) } var popupExpanded by remember { mutableStateOf(false) }
if (noteEvent == null) {
BlankNote( BlankNote(
remember { remember {
modifier.combinedClickable( modifier.combinedClickable(
@@ -178,13 +175,11 @@ fun NoteCompose(
isBoostedNote isBoostedNote
) )
note.let { NoteQuickActionMenu(note, popupExpanded, { popupExpanded = false }, accountViewModel)
NoteQuickActionMenu(it, popupExpanded, { popupExpanded = false }, accountViewModel)
}
} else if (account.isHidden(noteForReports.author!!) || (isSensitive && account.showSensitiveContent == false)) { } else if (account.isHidden(noteForReports.author!!) || (isSensitive && account.showSensitiveContent == false)) {
// Does nothing // Does nothing
} else { } else {
var showHiddenNote by remember { mutableStateOf(false) } var showReportedNote by remember { mutableStateOf(false) }
var isAcceptableAndCanPreview by remember { mutableStateOf(Pair(true, true)) } var isAcceptableAndCanPreview by remember { mutableStateOf(Pair(true, true)) }
LaunchedEffect(key1 = noteReportsState, key2 = accountState) { LaunchedEffect(key1 = noteReportsState, key2 = accountState) {
@@ -203,23 +198,68 @@ fun NoteCompose(
} }
} }
if (!isAcceptableAndCanPreview.first && !showHiddenNote) { if (!isAcceptableAndCanPreview.first && !showReportedNote) {
HiddenNote( HiddenNote(
account.getRelevantReports(noteForReports), account.getRelevantReports(noteForReports),
account.userProfile(), account.userProfile(),
modifier, modifier,
isBoostedNote, isBoostedNote,
nav, nav,
onClick = { showHiddenNote = true } onClick = { showReportedNote = true }
) )
} else if ((noteEvent is ChannelCreateEvent || noteEvent is ChannelMetadataEvent) && channelHex != null) { } else {
NormalNote(
baseNote,
routeForLastRead,
modifier,
isBoostedNote,
isQuotedNote,
unPackReply,
makeItShort,
addMarginTop,
isAcceptableAndCanPreview.second,
parentBackgroundColor,
accountViewModel,
nav
)
}
}
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun NormalNote(
baseNote: Note,
routeForLastRead: String? = null,
modifier: Modifier = Modifier,
isBoostedNote: Boolean = false,
isQuotedNote: Boolean = false,
unPackReply: Boolean = true,
makeItShort: Boolean = false,
addMarginTop: Boolean = true,
canPreview: Boolean = true,
parentBackgroundColor: Color? = null,
accountViewModel: AccountViewModel,
nav: (String) -> Unit
) {
println("AARender NormalNote")
val accountState by accountViewModel.accountLiveData.observeAsState()
val account = remember(accountState) { accountState?.account } ?: return
val loggedIn = remember(accountState) { accountState?.account?.userProfile() } ?: return
val noteEvent = remember { baseNote.event }
val channelHex = remember { baseNote.channelHex() }
var popupExpanded by remember { mutableStateOf(false) }
if ((noteEvent is ChannelCreateEvent || noteEvent is ChannelMetadataEvent) && channelHex != null) {
ChannelHeader(channelHex = channelHex, account = account, nav = nav) ChannelHeader(channelHex = channelHex, account = account, nav = nav)
} else if (noteEvent is BadgeDefinitionEvent) { } else if (noteEvent is BadgeDefinitionEvent) {
BadgeDisplay(baseNote = note) BadgeDisplay(baseNote = baseNote)
} else if (noteEvent is FileHeaderEvent) { } else if (noteEvent is FileHeaderEvent) {
FileHeaderDisplay(note) FileHeaderDisplay(baseNote)
} else if (noteEvent is FileStorageHeaderEvent) { } else if (noteEvent is FileStorageHeaderEvent) {
FileStorageHeaderDisplay(note) FileStorageHeaderDisplay(baseNote)
} else { } else {
var isNew by remember { mutableStateOf<Boolean>(false) } var isNew by remember { mutableStateOf<Boolean>(false) }
@@ -230,7 +270,7 @@ fun NoteCompose(
routeForLastRead?.let { routeForLastRead?.let {
val lastTime = NotificationCache.load(it) val lastTime = NotificationCache.load(it)
val createdAt = note.createdAt() val createdAt = baseNote.createdAt()
if (createdAt != null) { if (createdAt != null) {
NotificationCache.markAsRead(it, createdAt) NotificationCache.markAsRead(it, createdAt)
@@ -263,7 +303,7 @@ fun NoteCompose(
.combinedClickable( .combinedClickable(
onClick = { onClick = {
scope.launch { scope.launch {
routeFor(note, loggedIn)?.let { routeFor(baseNote, loggedIn)?.let {
nav(it) nav(it)
} }
} }
@@ -304,7 +344,7 @@ fun NoteCompose(
if (noteEvent !is RepostEvent && !makeItShort && !isQuotedNote) { if (noteEvent !is RepostEvent && !makeItShort && !isQuotedNote) {
SecondUserInfoRow( SecondUserInfoRow(
note, baseNote,
account, account,
nav nav
) )
@@ -314,7 +354,7 @@ fun NoteCompose(
if (!makeItShort) { if (!makeItShort) {
ReplyRow( ReplyRow(
note, baseNote,
unPackReply, unPackReply,
backgroundColor, backgroundColor,
account, account,
@@ -325,50 +365,50 @@ fun NoteCompose(
when (noteEvent) { when (noteEvent) {
is ReactionEvent -> { is ReactionEvent -> {
RenderReaction(note, backgroundColor, accountViewModel, nav) RenderReaction(baseNote, backgroundColor, accountViewModel, nav)
} }
is RepostEvent -> { is RepostEvent -> {
RenderRepost(note, backgroundColor, accountViewModel, nav) RenderRepost(baseNote, backgroundColor, accountViewModel, nav)
} }
is ReportEvent -> { is ReportEvent -> {
RenderReport(note) RenderReport(baseNote)
} }
is LongTextNoteEvent -> { is LongTextNoteEvent -> {
RenderLongFormContent(note, loggedIn, accountViewModel, nav) RenderLongFormContent(baseNote, loggedIn, accountViewModel, nav)
} }
is BadgeAwardEvent -> { is BadgeAwardEvent -> {
RenderBadgeAward(note, backgroundColor, accountViewModel, nav) RenderBadgeAward(baseNote, backgroundColor, accountViewModel, nav)
} }
is PeopleListEvent -> { is PeopleListEvent -> {
RenderPeopleList(noteState, backgroundColor, accountViewModel, nav) RenderPeopleList(baseNote, backgroundColor, accountViewModel, nav)
} }
is AudioTrackEvent -> { is AudioTrackEvent -> {
RenderAudioTrack(note, loggedIn, accountViewModel, nav) RenderAudioTrack(baseNote, loggedIn, accountViewModel, nav)
} }
is PinListEvent -> { is PinListEvent -> {
RenderPinListEvent(noteState, backgroundColor, accountViewModel, nav) RenderPinListEvent(baseNote, backgroundColor, accountViewModel, nav)
} }
is PrivateDmEvent -> { is PrivateDmEvent -> {
RenderPrivateMessage(note, makeItShort, isAcceptableAndCanPreview.second, backgroundColor, accountViewModel, nav) RenderPrivateMessage(baseNote, makeItShort, canPreview, backgroundColor, accountViewModel, nav)
} }
is HighlightEvent -> { is HighlightEvent -> {
RenderHighlight(note, makeItShort, isAcceptableAndCanPreview.second, backgroundColor, accountViewModel, nav) RenderHighlight(baseNote, makeItShort, canPreview, backgroundColor, accountViewModel, nav)
} }
is PollNoteEvent -> { is PollNoteEvent -> {
RenderPoll( RenderPoll(
note, baseNote,
makeItShort, makeItShort,
isAcceptableAndCanPreview.second, canPreview,
backgroundColor, backgroundColor,
accountViewModel, accountViewModel,
nav nav
@@ -377,9 +417,9 @@ fun NoteCompose(
else -> { else -> {
RenderTextEvent( RenderTextEvent(
note, baseNote,
makeItShort, makeItShort,
isAcceptableAndCanPreview.second, canPreview,
backgroundColor, backgroundColor,
accountViewModel, accountViewModel,
nav nav
@@ -387,8 +427,7 @@ fun NoteCompose(
} }
} }
NoteQuickActionMenu(note, popupExpanded, { popupExpanded = false }, accountViewModel) NoteQuickActionMenu(baseNote, popupExpanded, { popupExpanded = false }, accountViewModel)
}
} }
} }
} }
@@ -648,16 +687,14 @@ private fun RenderPrivateMessage(
@Composable @Composable
fun RenderPeopleList( fun RenderPeopleList(
noteState: NoteState?, baseNote: Note,
backgroundColor: Color, backgroundColor: Color,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
DisplayPeopleList(noteState, backgroundColor, accountViewModel, nav) DisplayPeopleList(baseNote, backgroundColor, accountViewModel, nav)
noteState?.note?.let { ReactionsRow(baseNote, accountViewModel, nav)
ReactionsRow(it, accountViewModel, nav)
}
Divider( Divider(
modifier = Modifier.padding(top = 10.dp), modifier = Modifier.padding(top = 10.dp),
@@ -667,13 +704,12 @@ fun RenderPeopleList(
@Composable @Composable
fun DisplayPeopleList( fun DisplayPeopleList(
noteState: NoteState?, baseNote: Note,
backgroundColor: Color, backgroundColor: Color,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
val note = remember(noteState) { noteState?.note } ?: return val noteEvent = baseNote.event as? PeopleListEvent ?: return
val noteEvent = note.event as? PeopleListEvent ?: return
var members by remember { mutableStateOf<List<User>>(listOf()) } var members by remember { mutableStateOf<List<User>>(listOf()) }
@@ -699,7 +735,7 @@ fun DisplayPeopleList(
textAlign = TextAlign.Center textAlign = TextAlign.Center
) )
LaunchedEffect(key1 = noteState) { LaunchedEffect(Unit) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
members = noteEvent.bookmarkedPeople().mapNotNull { hex -> members = noteEvent.bookmarkedPeople().mapNotNull { hex ->
LocalCache.checkGetOrCreateUser(hex) LocalCache.checkGetOrCreateUser(hex)
@@ -876,16 +912,16 @@ private fun RenderRepost(
@Composable @Composable
private fun RenderPinListEvent( private fun RenderPinListEvent(
noteState: NoteState?, baseNote: Note,
backgroundColor: Color, backgroundColor: Color,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
val noteEvent = noteState?.note?.event as? PinListEvent ?: return val noteEvent = baseNote.event as? PinListEvent ?: return
PinListHeader(noteState, backgroundColor, accountViewModel, nav) PinListHeader(baseNote, backgroundColor, accountViewModel, nav)
ReactionsRow(noteState?.note, accountViewModel, nav) ReactionsRow(baseNote, accountViewModel, nav)
Divider( Divider(
modifier = Modifier.padding(top = 10.dp), modifier = Modifier.padding(top = 10.dp),
@@ -895,15 +931,14 @@ private fun RenderPinListEvent(
@Composable @Composable
fun PinListHeader( fun PinListHeader(
noteState: NoteState?, baseNote: Note,
backgroundColor: Color, backgroundColor: Color,
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
val note = remember(noteState) { noteState?.note } ?: return val noteEvent = baseNote.event as? PinListEvent ?: return
val noteEvent = note.event as? PinListEvent ?: return
var pins by remember { mutableStateOf<List<String>>(noteEvent.pins()) } var pins by remember { mutableStateOf(noteEvent.pins()) }
var expanded by remember { var expanded by remember {
mutableStateOf(false) mutableStateOf(false)
@@ -353,11 +353,11 @@ fun NoteMaster(
) { ) {
Column() { Column() {
if (noteEvent is PeopleListEvent) { if (noteEvent is PeopleListEvent) {
DisplayPeopleList(noteState, MaterialTheme.colors.background, accountViewModel, nav) DisplayPeopleList(baseNote, MaterialTheme.colors.background, accountViewModel, nav)
} else if (noteEvent is AudioTrackEvent) { } else if (noteEvent is AudioTrackEvent) {
AudioTrackHeader(noteEvent, note, account.userProfile(), nav) AudioTrackHeader(noteEvent, note, account.userProfile(), nav)
} else if (noteEvent is PinListEvent) { } else if (noteEvent is PinListEvent) {
PinListHeader(noteState, MaterialTheme.colors.background, accountViewModel, nav) PinListHeader(baseNote, MaterialTheme.colors.background, accountViewModel, nav)
} else if (noteEvent is HighlightEvent) { } else if (noteEvent is HighlightEvent) {
DisplayHighlight( DisplayHighlight(
noteEvent.quote(), noteEvent.quote(),