diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt index 924e6c1d0..f4de579f0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt @@ -32,7 +32,6 @@ import com.halilibo.richtext.markdown.Markdown import com.halilibo.richtext.markdown.MarkdownParseOptions import com.halilibo.richtext.ui.material.MaterialRichText import com.vitorpamplona.amethyst.model.HashtagIcon -import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.checkForHashtagWithIcon @@ -57,6 +56,7 @@ import com.vitorpamplona.amethyst.ui.note.LoadUser import com.vitorpamplona.amethyst.ui.note.NoteCompose import com.vitorpamplona.amethyst.ui.note.toShortenHex import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.screen.loggedIn.LoadedBechLink import com.vitorpamplona.amethyst.ui.theme.Font17SP import com.vitorpamplona.amethyst.ui.theme.HalfVertPadding import com.vitorpamplona.amethyst.ui.theme.MarkdownTextStyle @@ -404,7 +404,7 @@ private fun ObserveNIP19Event( if (baseNote == null) { LaunchedEffect(key1 = it.hex) { if (it.type == Nip19.Type.NOTE || it.type == Nip19.Type.EVENT || it.type == Nip19.Type.ADDRESS) { - accountViewModel.checkGetOrCreateNote(it.hex)?.let { note -> + accountViewModel.checkGetOrCreateNote(it.hex) { note -> launch(Dispatchers.Main) { baseNote = note } } } @@ -461,30 +461,14 @@ private fun ObserveUser(user: User, onRefresh: () -> Unit) { } } -@Immutable -data class LoadedBechLink(val baseNote: Note?, val nip19: Nip19.Return) - @Composable fun BechLink(word: String, canPreview: Boolean, backgroundColor: MutableState, accountViewModel: AccountViewModel, nav: (String) -> Unit) { var loadedLink by remember { mutableStateOf(null) } if (loadedLink == null) { LaunchedEffect(key1 = word) { - launch(Dispatchers.IO) { - Nip19.uriToRoute(word)?.let { - var returningNote: Note? = null - if (it.type == Nip19.Type.NOTE || it.type == Nip19.Type.EVENT || it.type == Nip19.Type.ADDRESS) { - LocalCache.checkGetOrCreateNote(it.hex)?.let { note -> - returningNote = note - } - } - - val newLink = LoadedBechLink(returningNote, it) - - launch(Dispatchers.Main) { - loadedLink = newLink - } - } + accountViewModel.parseNIP19(word) { + loadedLink = it } } } @@ -504,7 +488,7 @@ fun BechLink(word: String, canPreview: Boolean, backgroundColor: MutableState 16) { word.replaceRange(8, word.length - 8, ":") } else { 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 d3b1d1e8b..4ba139024 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 @@ -233,7 +233,7 @@ fun NoteCompose( ) { val hasEvent by baseNote.live().hasEvent.observeAsState(baseNote.event != null) - Crossfade(targetState = hasEvent) { + Crossfade(targetState = hasEvent, label = "Event presence") { if (it) { CheckHiddenNoteCompose( note = baseNote, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteQuickActionMenu.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteQuickActionMenu.kt index 4a8d53283..cdae0628a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteQuickActionMenu.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteQuickActionMenu.kt @@ -114,10 +114,11 @@ private fun VerticalDivider(color: Color) = fun LongPressToQuickAction(baseNote: Note, accountViewModel: AccountViewModel, content: @Composable (() -> Unit) -> Unit) { val popupExpanded = remember { mutableStateOf(false) } val showPopup = remember { { popupExpanded.value = true } } + val hidePopup = remember { { popupExpanded.value = false } } content(showPopup) - NoteQuickActionMenu(baseNote, popupExpanded.value, { popupExpanded.value = false }, accountViewModel) + NoteQuickActionMenu(baseNote, popupExpanded.value, hidePopup, accountViewModel) } @Composable diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 91455dedf..e2b26e48e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -606,7 +606,7 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { return LocalCache.getUserIfExists(hex) } - suspend fun checkGetOrCreateNote(key: HexKey): Note? { + private suspend fun checkGetOrCreateNote(key: HexKey): Note? { return LocalCache.checkGetOrCreateNote(key) } @@ -634,7 +634,7 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { } } - suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? { + private suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? { return LocalCache.getOrCreateAddressableNote(key) } @@ -654,7 +654,7 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { } } - suspend fun checkGetOrCreateChannel(key: HexKey): Channel? { + private suspend fun checkGetOrCreateChannel(key: HexKey): Channel? { return LocalCache.checkGetOrCreateChannel(key) } @@ -705,9 +705,27 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { } } + fun parseNIP19(str: String, onNote: (LoadedBechLink) -> Unit) { + viewModelScope.launch(Dispatchers.IO) { + Nip19.uriToRoute(str)?.let { + var returningNote: Note? = null + if (it.type == Nip19.Type.NOTE || it.type == Nip19.Type.EVENT || it.type == Nip19.Type.ADDRESS) { + LocalCache.checkGetOrCreateNote(it.hex)?.let { note -> + returningNote = note + } + } + + onNote(LoadedBechLink(returningNote, it)) + } + } + } + class Factory(val account: Account) : ViewModelProvider.Factory { override fun create(modelClass: Class): AccountViewModel { return AccountViewModel(account) as AccountViewModel } } } + +@Immutable +data class LoadedBechLink(val baseNote: Note?, val nip19: Nip19.Return)