From 53ec9d777f7e4c1d98d8e9f17af7db9177637c85 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 8 May 2023 16:10:16 -0400 Subject: [PATCH] - Moving postValue to IO thread - launching navigate in a thread --- .../amethyst/NotificationCache.kt | 8 ++---- .../amethyst/model/AntiSpamFilter.kt | 8 ++---- .../vitorpamplona/amethyst/model/Channel.kt | 8 ++---- .../amethyst/model/LocalCache.kt | 2 +- .../com/vitorpamplona/amethyst/model/Note.kt | 8 ++---- .../com/vitorpamplona/amethyst/model/User.kt | 8 ++---- .../amethyst/ui/note/BadgeCompose.kt | 10 ++++--- .../amethyst/ui/note/BoostSetCompose.kt | 27 ++++++++++++++----- .../amethyst/ui/note/LikeSetCompose.kt | 26 +++++++++++++----- .../amethyst/ui/note/MessageSetCompose.kt | 7 ++++- .../amethyst/ui/note/MultiSetCompose.kt | 4 ++- .../amethyst/ui/note/NoteCompose.kt | 4 ++- .../amethyst/ui/note/ZapSetCompose.kt | 22 ++++++++++----- .../amethyst/ui/screen/ChatroomFeedView.kt | 2 +- .../amethyst/ui/screen/loggedIn/MainScreen.kt | 4 +-- 15 files changed, 87 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/NotificationCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/NotificationCache.kt index 0dc17365f..2bb193fba 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/NotificationCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/NotificationCache.kt @@ -38,19 +38,15 @@ object NotificationCache { class NotificationLiveData(val cache: NotificationCache) : LiveData(NotificationState(cache)) { // Refreshes observers in batches. - private val bundler = BundledUpdate(300, Dispatchers.Main) { + private val bundler = BundledUpdate(300, Dispatchers.IO) { if (hasActiveObservers()) { - refresh() + postValue(NotificationState(cache)) } } fun invalidateData() { bundler.invalidate() } - - fun refresh() { - postValue(NotificationState(cache)) - } } class NotificationState(val cache: NotificationCache) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt index 34b26bba3..520a51e2a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/AntiSpamFilter.kt @@ -55,19 +55,15 @@ class AntiSpamFilter { class AntiSpamLiveData(val cache: AntiSpamFilter) : LiveData(AntiSpamState(cache)) { // Refreshes observers in batches. - private val bundler = BundledUpdate(300, Dispatchers.Main) { + private val bundler = BundledUpdate(300, Dispatchers.IO) { if (hasActiveObservers()) { - refresh() + postValue(AntiSpamState(cache)) } } fun invalidateData() { bundler.invalidate() } - - private fun refresh() { - postValue(AntiSpamState(cache)) - } } class AntiSpamState(val cache: AntiSpamFilter) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Channel.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Channel.kt index c5cda7629..7716d04cc 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Channel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Channel.kt @@ -74,9 +74,9 @@ class Channel(val idHex: String) { class ChannelLiveData(val channel: Channel) : LiveData(ChannelState(channel)) { // Refreshes observers in batches. - private val bundler = BundledUpdate(300, Dispatchers.Main) { + private val bundler = BundledUpdate(300, Dispatchers.IO) { if (hasActiveObservers()) { - refresh() + postValue(ChannelState(channel)) } } @@ -84,10 +84,6 @@ class ChannelLiveData(val channel: Channel) : LiveData(ChannelStat bundler.invalidate() } - private fun refresh() { - postValue(ChannelState(channel)) - } - override fun onActive() { super.onActive() NostrSingleChannelDataSource.add(channel.idHex) 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 6469b5b26..dc7177f98 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -916,7 +916,7 @@ object LocalCache { class LocalCacheLiveData : LiveData>(setOf()) { // Refreshes observers in batches. - private val bundler = BundledInsert(300, Dispatchers.Main) + private val bundler = BundledInsert(300, Dispatchers.IO) fun invalidateData(newNote: Note) { bundler.invalidateList(newNote) { bundledNewNotes -> diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index aa3ce4ad5..c391efeb6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -410,9 +410,9 @@ class NoteLiveSet(u: Note) { class NoteLiveData(val note: Note) : LiveData(NoteState(note)) { // Refreshes observers in batches. - private val bundler = BundledUpdate(300, Dispatchers.Main) { + private val bundler = BundledUpdate(300, Dispatchers.IO) { if (hasActiveObservers()) { - refresh() + postValue(NoteState(note)) } } @@ -420,10 +420,6 @@ class NoteLiveData(val note: Note) : LiveData(NoteState(note)) { bundler.invalidate() } - private fun refresh() { - postValue(NoteState(note)) - } - override fun onActive() { super.onActive() if (note is AddressableNote) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt index 392cc17f8..7d5877952 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt @@ -396,9 +396,9 @@ class UserMetadata { class UserLiveData(val user: User) : LiveData(UserState(user)) { // Refreshes observers in batches. - private val bundler = BundledUpdate(300, Dispatchers.Main) { + private val bundler = BundledUpdate(300, Dispatchers.IO) { if (hasActiveObservers()) { - refresh() + postValue(UserState(user)) } } @@ -406,10 +406,6 @@ class UserLiveData(val user: User) : LiveData(UserState(user)) { bundler.invalidate() } - private fun refresh() { - postValue(UserState(user)) - } - override fun onActive() { super.onActive() NostrSingleUserDataSource.add(user) 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 3f71245ce..066777618 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 @@ -75,10 +75,12 @@ fun BadgeCompose(likeSetCard: BadgeCard, isInnerNote: Boolean = false, routeForL .background(backgroundColor) .combinedClickable( onClick = { - routeFor( - note, - accountViewModel.userProfile() - )?.let { navController.navigate(it) } + scope.launch { + routeFor( + note, + accountViewModel.userProfile() + )?.let { navController.navigate(it) } + } }, onLongClick = { popupExpanded = true } ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt index 107e02fe1..10b61d0d6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/BoostSetCompose.kt @@ -17,6 +17,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -31,6 +32,7 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.ui.screen.BoostSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @@ -45,6 +47,8 @@ fun BoostSetCompose(boostSetCard: BoostSetCard, isInnerNote: Boolean = false, ro val noteEvent = note?.event var popupExpanded by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() + if (note == null) { BlankNote(Modifier, isInnerNote) } else { @@ -65,12 +69,19 @@ fun BoostSetCompose(boostSetCard: BoostSetCard, isInnerNote: Boolean = false, ro } Column( - modifier = Modifier.background(backgroundColor).combinedClickable( - onClick = { - routeFor(note, account.userProfile())?.let { navController.navigate(it) } - }, - onLongClick = { popupExpanded = true } - ) + modifier = Modifier + .background(backgroundColor) + .combinedClickable( + onClick = { + scope.launch { + routeFor( + note, + account.userProfile() + )?.let { navController.navigate(it) } + } + }, + onLongClick = { popupExpanded = true } + ) ) { Row( modifier = Modifier @@ -90,7 +101,9 @@ fun BoostSetCompose(boostSetCard: BoostSetCard, isInnerNote: Boolean = false, ro Icon( painter = painterResource(R.drawable.ic_retweeted), null, - modifier = Modifier.size(16.dp).align(Alignment.TopEnd), + modifier = Modifier + .size(16.dp) + .align(Alignment.TopEnd), tint = Color.Unspecified ) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt index 9436512a8..eb22f8631 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/LikeSetCompose.kt @@ -17,6 +17,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -31,6 +32,7 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.ui.screen.LikeSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @@ -44,6 +46,7 @@ fun LikeSetCompose(likeSetCard: LikeSetCard, isInnerNote: Boolean = false, route val noteEvent = note?.event var popupExpanded by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() if (note == null) { BlankNote(Modifier, isInnerNote) @@ -65,12 +68,19 @@ fun LikeSetCompose(likeSetCard: LikeSetCard, isInnerNote: Boolean = false, route } Column( - modifier = Modifier.background(backgroundColor).combinedClickable( - onClick = { - routeFor(note, account.userProfile())?.let { navController.navigate(it) } - }, - onLongClick = { popupExpanded = true } - ) + modifier = Modifier + .background(backgroundColor) + .combinedClickable( + onClick = { + scope.launch { + routeFor( + note, + account.userProfile() + )?.let { navController.navigate(it) } + } + }, + onLongClick = { popupExpanded = true } + ) ) { Row( modifier = Modifier @@ -90,7 +100,9 @@ fun LikeSetCompose(likeSetCard: LikeSetCard, isInnerNote: Boolean = false, route Icon( painter = painterResource(R.drawable.ic_liked), null, - modifier = Modifier.size(16.dp).align(Alignment.TopEnd), + modifier = Modifier + .size(16.dp) + .align(Alignment.TopEnd), tint = Color.Unspecified ) } 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 094f6f033..4e6fa71fe 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 @@ -69,7 +69,12 @@ fun MessageSetCompose(messageSetCard: MessageSetCard, isInnerNote: Boolean = fal Column( modifier = Modifier.background(backgroundColor).combinedClickable( onClick = { - routeFor(note, accountViewModel.userProfile())?.let { navController.navigate(it) } + scope.launch { + routeFor( + note, + accountViewModel.userProfile() + )?.let { navController.navigate(it) } + } }, onLongClick = { popupExpanded = true } ) 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 7b454639a..1aebd5efa 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 @@ -89,7 +89,9 @@ fun MultiSetCompose(multiSetCard: MultiSetCard, routeForLastRead: String, accoun .background(backgroundColor) .combinedClickable( onClick = { - routeFor(note, account.userProfile())?.let { navController.navigate(it) } + scope.launch { + routeFor(note, account.userProfile())?.let { navController.navigate(it) } + } }, onLongClick = { popupExpanded = true } ) 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 d552c92a3..5c6bdc959 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 @@ -274,7 +274,9 @@ fun NoteComposeInner( modifier = modifier .combinedClickable( onClick = { - routeFor(note, loggedIn)?.let { navController.navigate(it) } + scope.launch { + routeFor(note, loggedIn)?.let { navController.navigate(it) } + } }, onLongClick = { popupExpanded = true } ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt index d0c8d7b78..b99f3007c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ZapSetCompose.kt @@ -19,6 +19,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -33,6 +34,7 @@ import com.vitorpamplona.amethyst.ui.screen.ZapSetCard import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @OptIn(ExperimentalFoundationApi::class) @@ -46,6 +48,7 @@ fun ZapSetCompose(zapSetCard: ZapSetCard, isInnerNote: Boolean = false, routeFor val noteEvent = note?.event var popupExpanded by remember { mutableStateOf(false) } + val scope = rememberCoroutineScope() if (note == null) { BlankNote(Modifier, isInnerNote) @@ -67,12 +70,19 @@ fun ZapSetCompose(zapSetCard: ZapSetCard, isInnerNote: Boolean = false, routeFor } Column( - modifier = Modifier.background(backgroundColor).combinedClickable( - onClick = { - routeFor(note, account.userProfile())?.let { navController.navigate(it) } - }, - onLongClick = { popupExpanded = true } - ) + modifier = Modifier + .background(backgroundColor) + .combinedClickable( + onClick = { + scope.launch { + routeFor( + note, + account.userProfile() + )?.let { navController.navigate(it) } + } + }, + onLongClick = { popupExpanded = true } + ) ) { Row( modifier = Modifier diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomFeedView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomFeedView.kt index 169aefd3e..1f67a61f3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomFeedView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/ChatroomFeedView.kt @@ -67,7 +67,7 @@ fun ChatroomFeedView(viewModel: FeedViewModel, accountViewModel: AccountViewMode } } } - FeedState.Loading -> { + is FeedState.Loading -> { LoadingFeed() } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt index 937332cdc..94c960493 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt @@ -41,7 +41,7 @@ import kotlinx.coroutines.launch @OptIn(ExperimentalMaterialApi::class) @Composable fun MainScreen(accountViewModel: AccountViewModel, accountStateViewModel: AccountStateViewModel, startingPage: String? = null) { - val coroutineScope = rememberCoroutineScope() + val scope = rememberCoroutineScope() val navController = rememberNavController() val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed)) val sheetState = rememberModalBottomSheetState( @@ -69,7 +69,7 @@ fun MainScreen(accountViewModel: AccountViewModel, accountStateViewModel: Accoun drawerContent = { DrawerContent(navController, scaffoldState, sheetState, accountViewModel) BackHandler(enabled = scaffoldState.drawerState.isOpen) { - coroutineScope.launch { scaffoldState.drawerState.close() } + scope.launch { scaffoldState.drawerState.close() } } }, floatingActionButton = {