From e0647411489e657bde6f8eb750652b07f407cdaf Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 23 Jan 2023 20:54:56 -0300 Subject: [PATCH] Moving coroutines from Main to Default, which is more ideal for memory intensive tasks --- .../com/vitorpamplona/amethyst/model/Note.kt | 2 +- .../com/vitorpamplona/amethyst/model/User.kt | 2 +- .../amethyst/service/relays/Relay.kt | 9 ++++++--- .../amethyst/ui/screen/CardFeedViewModel.kt | 19 +++++++++--------- .../amethyst/ui/screen/FeedViewModel.kt | 20 ++++++++++--------- .../amethyst/ui/screen/UserFeedViewModel.kt | 19 +++++++++--------- .../ui/screen/loggedIn/NotificationScreen.kt | 4 ++++ 7 files changed, 43 insertions(+), 32 deletions(-) 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 845aaabb2..3e7516453 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -110,7 +110,7 @@ class Note(val idHex: String) { if (handlerWaiting) return handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.Main) + val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { delay(100) live.refresh() 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 8adbceaae..df2c447dd 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt @@ -182,7 +182,7 @@ class User(val pubkey: ByteArray) { if (handlerWaiting) return handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.Main) + val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { delay(100) live.refresh() diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt index 1963c697b..a6fed1137 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt @@ -1,7 +1,11 @@ package com.vitorpamplona.amethyst.service.relays +import android.util.Log import com.google.gson.JsonElement +import com.vitorpamplona.amethyst.model.LocalCache +import nostr.postr.events.ContactListEvent import nostr.postr.events.Event +import nostr.postr.toNpub import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response @@ -34,7 +38,6 @@ class Relay( } fun requestAndWatch() { - println("Connecting with ${url}") val request = Request.Builder().url(url).build() val listener = object : WebSocketListener() { @@ -98,8 +101,8 @@ class Relay( socket?.close(1000, "Normal close") // Failures disconnect the relay. socket = null - //println("Relay onFailure ${url}, ${response?.message}") - t.printStackTrace() + Log.w("Relay", "Relay onFailure ${url}, ${response?.message}") + //t.printStackTrace() listeners.forEach { it.onError(this@Relay, "", Error("WebSocket Failure. Response: ${response}. Exception: ${t.message}", t)) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt index 1b4193408..b3c39a08a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt @@ -27,7 +27,7 @@ class CardFeedViewModel(val dataSource: NostrDataSource): ViewModel() { private var lastNotes: List? = null fun refresh() { - val scope = CoroutineScope(Job() + Dispatchers.IO) + val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { refreshSuspended() } @@ -92,16 +92,17 @@ class CardFeedViewModel(val dataSource: NostrDataSource): ViewModel() { } var handlerWaiting = false - @Synchronized fun invalidateData() { - if (handlerWaiting) return + synchronized(handlerWaiting) { + if (handlerWaiting) return - handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.IO) - scope.launch { - delay(100) - refresh() - handlerWaiting = false + handlerWaiting = true + val scope = CoroutineScope(Job() + Dispatchers.Default) + scope.launch { + delay(100) + refresh() + handlerWaiting = false + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt index f5e86444d..d547c0b5c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt @@ -1,5 +1,6 @@ package com.vitorpamplona.amethyst.ui.screen +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.LocalCache @@ -63,7 +64,7 @@ abstract class FeedViewModel(val dataSource: NostrDataSource): ViewModel() } fun refresh() { - viewModelScope.launch(Dispatchers.IO) { + viewModelScope.launch(Dispatchers.Default) { val notes = newListFromDataSource() val oldNotesState = feedContent.value @@ -90,16 +91,17 @@ abstract class FeedViewModel(val dataSource: NostrDataSource): ViewModel() } var handlerWaiting = false - @Synchronized fun invalidateData() { - if (handlerWaiting) return + synchronized(handlerWaiting) { + if (handlerWaiting) return - handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.IO) - scope.launch { - delay(100) - refresh() - handlerWaiting = false + handlerWaiting = true + val scope = CoroutineScope(Job() + Dispatchers.Default) + scope.launch { + delay(100) + refresh() + handlerWaiting = false + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt index 8e762defe..73f30cbe4 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt @@ -33,7 +33,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource): ViewModel() val feedContent = _feedContent.asStateFlow() fun refresh() { - val scope = CoroutineScope(Job() + Dispatchers.IO) + val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { refreshSuspended() } @@ -64,16 +64,17 @@ open class UserFeedViewModel(val dataSource: NostrDataSource): ViewModel() } var handlerWaiting = false - @Synchronized fun invalidateData() { - if (handlerWaiting) return + synchronized(handlerWaiting) { + if (handlerWaiting) return - handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.IO) - scope.launch { - delay(100) - refresh() - handlerWaiting = false + handlerWaiting = true + val scope = CoroutineScope(Job() + Dispatchers.Default) + scope.launch { + delay(100) + refresh() + handlerWaiting = false + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt index c374bec4a..ceff271d1 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/NotificationScreen.kt @@ -18,6 +18,10 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel fun NotificationScreen(accountViewModel: AccountViewModel, navController: NavController) { val feedViewModel: CardFeedViewModel = viewModel { CardFeedViewModel( NostrNotificationDataSource ) } + LaunchedEffect(Unit) { + feedViewModel.refresh() + } + Column(Modifier.fillMaxHeight()) { Column( modifier = Modifier.padding(vertical = 0.dp)