From a4dd3aa488c4c01119c7b49e4cf35cdaf00fc492 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 22 Jun 2023 14:30:22 -0400 Subject: [PATCH] Moves checks to run to outside the LaunchedEffect --- .../amethyst/ui/components/ClickableRoute.kt | 16 ++++++------ .../amethyst/ui/components/RichTextViewer.kt | 8 +++--- .../amethyst/ui/components/UrlPreview.kt | 4 +-- .../ui/navigation/AccountSwitchBottomSheet.kt | 16 ++++++------ .../amethyst/ui/note/ChatroomCompose.kt | 4 +-- .../ui/note/NIP05VerificationDisplay.kt | 4 +-- .../amethyst/ui/note/NoteCompose.kt | 24 ++++++++++-------- .../amethyst/ui/screen/ThreadFeedView.kt | 25 ++++++++++++------- .../ui/screen/loggedIn/ChannelScreen.kt | 8 +++--- .../ui/screen/loggedIn/ChatroomScreen.kt | 14 ++++++----- .../ui/screen/loggedIn/ProfileScreen.kt | 4 +-- 11 files changed, 71 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt index 01249e1b8..4f7403d96 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ClickableRoute.kt @@ -92,8 +92,8 @@ private fun LoadNote( ) { var noteBase by remember(hex) { mutableStateOf(LocalCache.getNoteIfExists(hex)) } - LaunchedEffect(key1 = hex) { - if (noteBase == null) { + if (noteBase == null) { + LaunchedEffect(key1 = hex) { launch(Dispatchers.IO) { noteBase = LocalCache.checkGetOrCreateNote(hex) } @@ -186,8 +186,8 @@ private fun DisplayAddress( ) { var noteBase by remember(nip19) { mutableStateOf(LocalCache.getNoteIfExists(nip19.hex)) } - LaunchedEffect(key1 = nip19.hex) { - if (noteBase == null) { + if (noteBase == null) { + LaunchedEffect(key1 = nip19.hex) { launch(Dispatchers.IO) { noteBase = LocalCache.checkGetOrCreateAddressableNote(nip19.hex) } @@ -225,8 +225,8 @@ private fun DisplayUser( ) { var userBase by remember(nip19) { mutableStateOf(LocalCache.getUserIfExists(nip19.hex)) } - LaunchedEffect(key1 = nip19.hex) { - if (userBase == null) { + if (userBase == null) { + LaunchedEffect(key1 = nip19.hex) { launch(Dispatchers.IO) { userBase = LocalCache.checkGetOrCreateUser(nip19.hex) } @@ -396,8 +396,8 @@ fun CreateTextWithEmoji( ) { var emojiList by remember(text) { mutableStateOf>(persistentListOf()) } - LaunchedEffect(key1 = text) { - if (emojis.isNotEmpty()) { + if (emojis.isNotEmpty()) { + LaunchedEffect(key1 = text) { launch(Dispatchers.Default) { val newEmojiList = assembleAnnotatedList(text, emojis) if (newEmojiList.isNotEmpty()) { 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 ad01385e5..113f3dd22 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 @@ -512,8 +512,8 @@ private fun ObserveNIP19Event( ) { var baseNote by remember(it) { mutableStateOf(LocalCache.getNoteIfExists(it.hex)) } - LaunchedEffect(key1 = it.hex) { - if (baseNote == null) { + if (baseNote == null) { + LaunchedEffect(key1 = it.hex) { launch(Dispatchers.IO) { if (it.type == Nip19.Type.NOTE || it.type == Nip19.Type.EVENT || it.type == Nip19.Type.ADDRESS) { LocalCache.checkGetOrCreateNote(it.hex)?.let { note -> @@ -549,8 +549,8 @@ private fun ObserveNIP19User( ) { var baseUser by remember(it) { mutableStateOf(LocalCache.getUserIfExists(it.hex)) } - LaunchedEffect(key1 = it.hex) { - if (baseUser == null) { + if (baseUser == null) { + LaunchedEffect(key1 = it.hex) { launch(Dispatchers.IO) { if (it.type == Nip19.Type.USER) { LocalCache.checkGetOrCreateUser(it.hex)?.let { user -> diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/UrlPreview.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/UrlPreview.kt index be0a8d1e4..3a9bf1fb1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/UrlPreview.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/UrlPreview.kt @@ -21,8 +21,8 @@ fun UrlPreview(url: String, urlText: String) { } // Doesn't use a viewModel because of viewModel reusing issues (too many UrlPreview are created). - LaunchedEffect(url) { - if (urlPreviewState == UrlPreviewState.Loading) { + if (urlPreviewState == UrlPreviewState.Loading) { + LaunchedEffect(url) { launch(Dispatchers.IO) { UrlCachedPreviewer.previewInfo(url) { urlPreviewState = it diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AccountSwitchBottomSheet.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AccountSwitchBottomSheet.kt index a79e7dbe2..d1be111cd 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AccountSwitchBottomSheet.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/navigation/AccountSwitchBottomSheet.kt @@ -130,14 +130,16 @@ fun DisplayAccount( accountViewModel: AccountViewModel, accountStateViewModel: AccountStateViewModel ) { - var baseUser by remember { mutableStateOf(null) } + var baseUser by remember { mutableStateOf(LocalCache.getUserIfExists(decodePublicKey(acc.npub).toHexKey())) } - LaunchedEffect(key1 = acc.npub) { - launch(Dispatchers.IO) { - baseUser = try { - LocalCache.getOrCreateUser(decodePublicKey(acc.npub).toHexKey()) - } catch (e: Exception) { - null + if (baseUser == null) { + LaunchedEffect(key1 = acc.npub) { + launch(Dispatchers.IO) { + baseUser = try { + LocalCache.getOrCreateUser(decodePublicKey(acc.npub).toHexKey()) + } catch (e: Exception) { + null + } } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt index f5ae56f8f..348d112d6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomCompose.kt @@ -244,8 +244,8 @@ fun LoadUser(baseUserHex: String, content: @Composable (User) -> Unit) { mutableStateOf(LocalCache.getUserIfExists(baseUserHex)) } - LaunchedEffect(key1 = baseUserHex) { - if (user == null) { + if (user == null) { + LaunchedEffect(key1 = baseUserHex) { launch(Dispatchers.IO) { user = LocalCache.checkGetOrCreateUser(baseUserHex) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt index 18ecc42ff..e793da24a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt @@ -52,8 +52,8 @@ fun nip05VerificationAsAState(user: UserMetadata, pubkeyHex: String): MutableSta mutableStateOf(default) } - LaunchedEffect(key1 = user.nip05) { - if (nip05Verified.value == null) { + if (nip05Verified.value == null) { + LaunchedEffect(key1 = user.nip05) { launch(Dispatchers.IO) { user.nip05?.ifBlank { null }?.let { nip05 -> Nip05Verifier().verifyNip05( 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 0de6c69e8..f83537d13 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 @@ -1939,8 +1939,8 @@ fun LoadChannel(baseChannelHex: String, content: @Composable (Channel) -> Unit) mutableStateOf(LocalCache.getChannelIfExists(baseChannelHex)) } - LaunchedEffect(key1 = baseChannelHex) { - if (channel == null) { + if (channel == null) { + LaunchedEffect(key1 = baseChannelHex) { launch(Dispatchers.IO) { channel = LocalCache.checkGetOrCreateChannel(baseChannelHex) } @@ -2061,8 +2061,8 @@ private fun DisplayQuoteAuthor( ) { var userBase by remember { mutableStateOf(LocalCache.getUserIfExists(authorHex)) } - if (userBase == null) { - LaunchedEffect(Unit) { + LaunchedEffect(Unit) { + if (userBase == null) { launch(Dispatchers.IO) { userBase = LocalCache.checkGetOrCreateUser(authorHex) } @@ -2416,8 +2416,8 @@ fun FileHeaderDisplay(note: Note) { var content by remember { mutableStateOf(null) } - LaunchedEffect(key1 = event.id) { - if (content == null) { + if (content == null) { + LaunchedEffect(key1 = event.id) { launch(Dispatchers.IO) { val blurHash = event.blurhash() val hash = event.hash() @@ -2447,9 +2447,11 @@ fun FileStorageHeaderDisplay(baseNote: Note) { var fileNote by remember { mutableStateOf(null) } - LaunchedEffect(key1 = eventHeader.id) { - launch(Dispatchers.IO) { - fileNote = eventHeader.dataEventId()?.let { LocalCache.checkGetOrCreateNote(it) } + if (fileNote == null) { + LaunchedEffect(key1 = eventHeader.id) { + launch(Dispatchers.IO) { + fileNote = eventHeader.dataEventId()?.let { LocalCache.checkGetOrCreateNote(it) } + } } } @@ -2459,8 +2461,8 @@ fun FileStorageHeaderDisplay(baseNote: Note) { var content by remember { mutableStateOf(null) } - LaunchedEffect(key1 = eventHeader.id, key2 = noteState, key3 = note?.event) { - if (content == null) { + if (content == null) { + LaunchedEffect(key1 = eventHeader.id, key2 = noteState, key3 = note?.event) { launch(Dispatchers.IO) { val uri = "nostr:" + baseNote.toNEvent() val localDir = note?.idHex?.let { File(File(appContext.externalCacheDir, "NIP95"), it) } 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 6132fd002..796b7822c 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 @@ -90,7 +90,10 @@ import com.vitorpamplona.amethyst.ui.theme.lessImportantLink import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.selectedNote import kotlinx.collections.immutable.toImmutableSet +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @OptIn(ExperimentalMaterialApi::class) @Composable @@ -120,17 +123,21 @@ fun ThreadFeedView(noteId: String, viewModel: FeedViewModel, accountViewModel: A is FeedState.Loaded -> { refreshing = false LaunchedEffect(noteId) { - // waits to load the thread to scroll to item. - delay(100) - val noteForPosition = state.feed.value.filter { it.idHex == noteId }.firstOrNull() - var position = state.feed.value.indexOf(noteForPosition) + launch(Dispatchers.IO) { + // waits to load the thread to scroll to item. + delay(100) + val noteForPosition = state.feed.value.filter { it.idHex == noteId }.firstOrNull() + var position = state.feed.value.indexOf(noteForPosition) - if (position >= 0) { - if (position >= 1 && position < state.feed.value.size - 1) { - position-- // show the replying note + if (position >= 0) { + if (position >= 1 && position < state.feed.value.size - 1) { + position-- // show the replying note + } + + withContext(Dispatchers.Main) { + listState.animateScrollToItem(position) + } } - - listState.animateScrollToItem(position) } } 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 7519190dd..836ada65a 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 @@ -519,9 +519,11 @@ fun ChannelHeader( var baseChannel by remember { mutableStateOf(LocalCache.channels[channelHex]) } val scope = rememberCoroutineScope() - LaunchedEffect(key1 = channelHex) { - scope.launch(Dispatchers.IO) { - baseChannel = LocalCache.checkGetOrCreateChannel(channelHex) + if (baseChannel == null) { + LaunchedEffect(key1 = channelHex) { + scope.launch(Dispatchers.IO) { + baseChannel = LocalCache.checkGetOrCreateChannel(channelHex) + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt index 16f1f1527..f29f0f761 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt @@ -49,13 +49,15 @@ fun ChatroomScreen( ) { if (userId == null) return - var userRoom by remember { mutableStateOf(null) } + var userRoom by remember(userId) { mutableStateOf(null) } - LaunchedEffect(userId) { - launch(Dispatchers.IO) { - val newUser = LocalCache.checkGetOrCreateUser(userId) - if (newUser != userRoom) { - userRoom = newUser + if (userRoom == null) { + LaunchedEffect(userId) { + launch(Dispatchers.IO) { + val newUser = LocalCache.checkGetOrCreateUser(userId) + if (newUser != userRoom) { + userRoom = newUser + } } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt index 8eda11841..e33c54ce0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt @@ -113,8 +113,8 @@ fun ProfileScreen(userId: String?, accountViewModel: AccountViewModel, nav: (Str var userBase by remember { mutableStateOf(LocalCache.getUserIfExists(userId)) } - LaunchedEffect(userId) { - if (userBase == null) { + if (userBase == null) { + LaunchedEffect(userId) { // waits to resolve. withContext(Dispatchers.IO) { val newUserBase = LocalCache.checkGetOrCreateUser(userId)