Moves checks to run to outside the LaunchedEffect

pull/469/head
Vitor Pamplona 2023-06-22 14:30:22 -04:00
rodzic 9dbe7379cd
commit a4dd3aa488
11 zmienionych plików z 71 dodań i 56 usunięć

Wyświetl plik

@ -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<ImmutableList<Renderable>>(persistentListOf()) }
LaunchedEffect(key1 = text) {
if (emojis.isNotEmpty()) {
if (emojis.isNotEmpty()) {
LaunchedEffect(key1 = text) {
launch(Dispatchers.Default) {
val newEmojiList = assembleAnnotatedList(text, emojis)
if (newEmojiList.isNotEmpty()) {

Wyświetl plik

@ -512,8 +512,8 @@ private fun ObserveNIP19Event(
) {
var baseNote by remember(it) { mutableStateOf<Note?>(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<User?>(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 ->

Wyświetl plik

@ -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

Wyświetl plik

@ -130,14 +130,16 @@ fun DisplayAccount(
accountViewModel: AccountViewModel,
accountStateViewModel: AccountStateViewModel
) {
var baseUser by remember { mutableStateOf<User?>(null) }
var baseUser by remember { mutableStateOf<User?>(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
}
}
}
}

Wyświetl plik

@ -244,8 +244,8 @@ fun LoadUser(baseUserHex: String, content: @Composable (User) -> Unit) {
mutableStateOf<User?>(LocalCache.getUserIfExists(baseUserHex))
}
LaunchedEffect(key1 = baseUserHex) {
if (user == null) {
if (user == null) {
LaunchedEffect(key1 = baseUserHex) {
launch(Dispatchers.IO) {
user = LocalCache.checkGetOrCreateUser(baseUserHex)
}

Wyświetl plik

@ -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(

Wyświetl plik

@ -1939,8 +1939,8 @@ fun LoadChannel(baseChannelHex: String, content: @Composable (Channel) -> Unit)
mutableStateOf<Channel?>(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<User?>(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<ZoomableContent?>(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<Note?>(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<ZoomableContent?>(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) }

Wyświetl plik

@ -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)
}
}

Wyświetl plik

@ -519,9 +519,11 @@ fun ChannelHeader(
var baseChannel by remember { mutableStateOf<Channel?>(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)
}
}
}

Wyświetl plik

@ -49,13 +49,15 @@ fun ChatroomScreen(
) {
if (userId == null) return
var userRoom by remember { mutableStateOf<User?>(null) }
var userRoom by remember(userId) { mutableStateOf<User?>(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
}
}
}
}

Wyświetl plik

@ -113,8 +113,8 @@ fun ProfileScreen(userId: String?, accountViewModel: AccountViewModel, nav: (Str
var userBase by remember { mutableStateOf<User?>(LocalCache.getUserIfExists(userId)) }
LaunchedEffect(userId) {
if (userBase == null) {
if (userBase == null) {
LaunchedEffect(userId) {
// waits to resolve.
withContext(Dispatchers.IO) {
val newUserBase = LocalCache.checkGetOrCreateUser(userId)