From af784a5bda25ad19641742444bd0baa2decde6bf Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 1 Mar 2024 14:50:28 -0500 Subject: [PATCH] Editting the current version of the post instead of only the original post. --- .../amethyst/model/LocalCache.kt | 2 +- .../amethyst/ui/actions/EditPostView.kt | 4 ++-- .../amethyst/ui/actions/EditPostViewModel.kt | 3 ++- .../amethyst/ui/note/BadgeCompose.kt | 2 +- .../amethyst/ui/note/MessageSetCompose.kt | 2 +- .../amethyst/ui/note/MultiSetCompose.kt | 5 +++-- .../amethyst/ui/note/NoteCompose.kt | 19 ++++++++++++------- .../amethyst/ui/note/UserProfilePicture.kt | 10 ++++++++++ .../amethyst/ui/screen/ThreadFeedView.kt | 2 +- .../ui/screen/loggedIn/ChannelScreen.kt | 10 +++++----- .../ui/screen/loggedIn/VideoScreen.kt | 1 + 11 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index fc50dc25e..0d4852585 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -1404,7 +1404,7 @@ object LocalCache { note.loadEvent(event, author, emptyList()) event.editedNote()?.let { - getNoteIfExists(it)?.let { editedNote -> + checkGetOrCreateNote(it)?.let { editedNote -> modificationCache.remove(editedNote.idHex) editedNote.liveSet?.innerModifications?.invalidateData() } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostView.kt index 1d35c13f0..e389b7abc 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostView.kt @@ -37,7 +37,6 @@ import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.rememberScrollState @@ -115,6 +114,7 @@ import kotlinx.coroutines.withContext fun EditPostView( onClose: () -> Unit, edit: Note, + versionLookingAt: Note?, accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { @@ -128,7 +128,7 @@ fun EditPostView( var relayList = remember { accountViewModel.account.activeWriteRelays().toImmutableList() } LaunchedEffect(Unit) { - postViewModel.load(edit, accountViewModel) + postViewModel.load(edit, versionLookingAt, accountViewModel) launch(Dispatchers.IO) { postViewModel.imageUploadingError.collect { error -> diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt index 99c0b6314..13c188653 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt @@ -82,6 +82,7 @@ open class EditPostViewModel() : ViewModel() { open fun load( edit: Note, + versionLookingAt: Note?, accountViewModel: AccountViewModel, ) { this.accountViewModel = accountViewModel @@ -90,7 +91,7 @@ open class EditPostViewModel() : ViewModel() { canAddInvoice = accountViewModel.userProfile().info?.lnAddress() != null contentToAddUrl = null - message = TextFieldValue(edit.event?.content() ?: "") + message = TextFieldValue(versionLookingAt?.event?.content() ?: edit.event?.content() ?: "") urlPreview = findUrlInMessage() editedFromNote = edit diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt index fe987ca47..c37701b43 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BadgeCompose.kt @@ -167,7 +167,7 @@ fun BadgeCompose( tint = MaterialTheme.colorScheme.placeholderText, ) - NoteDropDownMenu(note, popupExpanded, accountViewModel, nav) + NoteDropDownMenu(note, popupExpanded, null, accountViewModel, nav) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt index 17e95d00c..c48dc99d7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MessageSetCompose.kt @@ -129,7 +129,7 @@ fun MessageSetCompose( nav = nav, ) - NoteDropDownMenu(baseNote, popupExpanded, accountViewModel, nav) + NoteDropDownMenu(baseNote, popupExpanded, null, accountViewModel, nav) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt index 5a8cb21ff..15e302737 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt @@ -74,6 +74,7 @@ import com.vitorpamplona.amethyst.ui.screen.CombinedZap import com.vitorpamplona.amethyst.ui.screen.MultiSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.DividerThickness +import com.vitorpamplona.amethyst.ui.theme.HalfTopPadding import com.vitorpamplona.amethyst.ui.theme.NotificationIconModifier import com.vitorpamplona.amethyst.ui.theme.NotificationIconModifierSmaller import com.vitorpamplona.amethyst.ui.theme.Size10dp @@ -158,7 +159,7 @@ fun MultiSetCompose( NoteCompose( baseNote = baseNote, routeForLastRead = null, - modifier = remember { Modifier.padding(top = 5.dp) }, + modifier = HalfTopPadding, isBoostedNote = true, showHidden = showHidden, parentBackgroundColor = backgroundColor, @@ -166,7 +167,7 @@ fun MultiSetCompose( nav = nav, ) - NoteDropDownMenu(baseNote, popupExpanded, accountViewModel, nav) + NoteDropDownMenu(baseNote, popupExpanded, null, accountViewModel, nav) } Divider( diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt index 46f0a96f6..ea6361e6a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt @@ -782,7 +782,7 @@ fun LongCommunityHeader( ) Spacer(DoubleHorzSpacer) NormalTimeAgo(baseNote = baseNote, Modifier.weight(1f)) - MoreOptionsButton(baseNote, accountViewModel, nav) + MoreOptionsButton(baseNote, null, accountViewModel, nav) } } @@ -2907,7 +2907,7 @@ fun FirstUserInfoRow( TimeAgo(baseNote) - MoreOptionsButton(baseNote, accountViewModel, nav) + MoreOptionsButton(baseNote, editState, accountViewModel, nav) } } @@ -2916,6 +2916,10 @@ fun observeEdits( baseNote: Note, accountViewModel: AccountViewModel, ): State> { + if (baseNote.event !is TextNoteEvent) { + return remember { mutableStateOf(GenericLoadable.Empty()) } + } + val editState = remember(baseNote.idHex) { val cached = accountViewModel.cachedModificationEventsForNote(baseNote) @@ -2934,10 +2938,10 @@ fun observeEdits( ) } - val updatedNote = baseNote.live().innerModifications.observeAsState() + val updatedNote by baseNote.live().innerModifications.observeAsState() LaunchedEffect(key1 = updatedNote) { - updatedNote.value?.note?.let { + updatedNote?.note?.let { accountViewModel.findModificationEventsForNote(it) { newModifications -> if (newModifications.isEmpty()) { if (editState.value !is GenericLoadable.Empty) { @@ -2999,6 +3003,7 @@ private fun BoostedMark() { @Composable fun MoreOptionsButton( baseNote: Note, + editState: State>? = null, accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { @@ -3014,6 +3019,7 @@ fun MoreOptionsButton( NoteDropDownMenu( baseNote, popupExpanded, + editState, accountViewModel, nav, ) @@ -3834,9 +3840,8 @@ private fun RenderShortRepositoryHeader( accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { - val noteState = baseNote.live().metadata.observeAsState() - val note = remember(noteState) { noteState.value?.note } ?: return - val noteEvent = note.event as? GitRepositoryEvent ?: return + val noteState by baseNote.live().metadata.observeAsState() + val noteEvent = noteState?.note?.event as? GitRepositoryEvent ?: return Column( modifier = MaterialTheme.colorScheme.replyModifier.padding(10.dp), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt index 747b6c2e9..04c1cd08b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt @@ -43,6 +43,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.Immutable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState +import androidx.compose.runtime.State import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState @@ -67,6 +68,7 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.actions.EditPostView +import com.vitorpamplona.amethyst.ui.components.GenericLoadable import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImage import com.vitorpamplona.amethyst.ui.components.RobohashFallbackAsyncImage import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel @@ -456,6 +458,7 @@ data class DropDownParams( fun NoteDropDownMenu( note: Note, popupExpanded: MutableState, + editState: State>? = null, accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { @@ -482,12 +485,19 @@ fun NoteDropDownMenu( } if (wantsToEditPost.value) { + // avoids changing while drafting a note and a new event shows up. + val versionLookingAt = + remember { + (editState?.value as? GenericLoadable.Loaded)?.loaded?.modificationToShow?.value + } + EditPostView( onClose = { popupExpanded.value = false wantsToEditPost.value = false }, edit = note, + versionLookingAt = versionLookingAt, accountViewModel = accountViewModel, nav = nav, ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt index c54c31228..4b0c5f7e8 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ThreadFeedView.kt @@ -438,7 +438,7 @@ fun NoteMaster( tint = MaterialTheme.colorScheme.placeholderText, ) - NoteDropDownMenu(baseNote, moreActionsExpanded, accountViewModel, nav) + NoteDropDownMenu(baseNote, moreActionsExpanded, editState, accountViewModel, nav) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt index f24fca756..86f9e325d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt @@ -710,8 +710,8 @@ fun ShortChannelHeader( nav: (String) -> Unit, showFlag: Boolean, ) { - val channelState = baseChannel.live.observeAsState() - val channel = remember(channelState) { channelState.value?.channel } ?: return + val channelState by baseChannel.live.observeAsState() + val channel = channelState?.channel ?: return val automaticallyShowProfilePicture = remember { @@ -782,8 +782,8 @@ fun LongChannelHeader( accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { - val channelState = baseChannel.live.observeAsState() - val channel = remember(channelState) { channelState.value?.channel } ?: return + val channelState by baseChannel.live.observeAsState() + val channel = channelState?.channel ?: return Row( lineModifier, @@ -865,7 +865,7 @@ fun LongChannelHeader( ) Spacer(DoubleHorzSpacer) NormalTimeAgo(note, remember { Modifier.weight(1f) }) - MoreOptionsButton(note, accountViewModel, nav) + MoreOptionsButton(note, null, accountViewModel, nav) } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/VideoScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/VideoScreen.kt index 9f3e9136e..1dface6d1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/VideoScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/VideoScreen.kt @@ -406,6 +406,7 @@ private fun VideoUserOptionAction( NoteDropDownMenu( note, popupExpanded, + null, accountViewModel, nav, )