Marks some methods to never run on the Main Thread

This commit is contained in:
Vitor Pamplona
2023-06-04 12:17:19 -04:00
parent 74b05e3d6a
commit b34c1f98d5
7 changed files with 41 additions and 0 deletions
@@ -2,6 +2,7 @@ package com.vitorpamplona.amethyst.model
import android.util.Log import android.util.Log
import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.service.model.* import com.vitorpamplona.amethyst.service.model.*
import com.vitorpamplona.amethyst.service.nip19.Nip19 import com.vitorpamplona.amethyst.service.nip19.Nip19
import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.service.relays.Relay
@@ -850,6 +851,8 @@ object LocalCache {
} }
fun findUsersStartingWith(username: String): List<User> { fun findUsersStartingWith(username: String): List<User> {
checkNotInMainThread()
val key = decodePublicKeyAsHexOrNull(username) val key = decodePublicKeyAsHexOrNull(username)
if (key != null && users[key] != null) { if (key != null && users[key] != null) {
@@ -864,6 +867,8 @@ object LocalCache {
} }
fun findNotesStartingWith(text: String): List<Note> { fun findNotesStartingWith(text: String): List<Note> {
checkNotInMainThread()
val key = try { val key = try {
Nip19.uriToRoute(text)?.hex ?: Hex.decode(text).toHexKey() Nip19.uriToRoute(text)?.hex ?: Hex.decode(text).toHexKey()
} catch (e: Exception) { } catch (e: Exception) {
@@ -889,6 +894,8 @@ object LocalCache {
} }
fun findChannelsStartingWith(text: String): List<Channel> { fun findChannelsStartingWith(text: String): List<Channel> {
checkNotInMainThread()
val key = try { val key = try {
Nip19.uriToRoute(text)?.hex ?: Hex.decode(text).toHexKey() Nip19.uriToRoute(text)?.hex ?: Hex.decode(text).toHexKey()
} catch (e: Exception) { } catch (e: Exception) {
@@ -917,6 +924,8 @@ object LocalCache {
} }
fun pruneOldAndHiddenMessages(account: Account) { fun pruneOldAndHiddenMessages(account: Account) {
checkNotInMainThread()
channels.forEach { it -> channels.forEach { it ->
val toBeRemoved = it.value.pruneOldAndHiddenMessages(account) val toBeRemoved = it.value.pruneOldAndHiddenMessages(account)
@@ -935,6 +944,8 @@ object LocalCache {
} }
fun pruneHiddenMessages(account: Account) { fun pruneHiddenMessages(account: Account) {
checkNotInMainThread()
val toBeRemoved = account.hiddenUsers.map { val toBeRemoved = account.hiddenUsers.map {
(users[it]?.notes ?: emptySet()) (users[it]?.notes ?: emptySet())
}.flatten() }.flatten()
@@ -962,6 +973,8 @@ object LocalCache {
} }
fun pruneContactLists(userAccount: Account) { fun pruneContactLists(userAccount: Account) {
checkNotInMainThread()
var removingContactList = 0 var removingContactList = 0
users.values.forEach { users.values.forEach {
if (it != userAccount.userProfile() && (it.liveSet == null || it.liveSet?.isInUse() == false) && it.latestContactList != null) { if (it != userAccount.userProfile() && (it.liveSet == null || it.liveSet?.isInUse() == false) && it.latestContactList != null) {
@@ -981,6 +994,8 @@ object LocalCache {
} }
fun consume(event: Event, relay: Relay?) { fun consume(event: Event, relay: Relay?) {
checkNotInMainThread()
if (!event.hasValidSignature()) return if (!event.hasValidSignature()) return
try { try {
@@ -20,6 +20,8 @@ class BlurHashFetcher(
) : Fetcher { ) : Fetcher {
override suspend fun fetch(): FetchResult { override suspend fun fetch(): FetchResult {
checkNotInMainThread()
val encodedHash = data.toString().removePrefix("bluehash:") val encodedHash = data.toString().removePrefix("bluehash:")
val hash = URLDecoder.decode(encodedHash, "utf-8") val hash = URLDecoder.decode(encodedHash, "utf-8")
@@ -0,0 +1,11 @@
package com.vitorpamplona.amethyst.service
import android.os.Looper
fun checkNotInMainThread() {
if (isMainThread()) throw OnMainThreadException("It should not be in the MainThread")
}
fun isMainThread() = Looper.myLooper() == Looper.getMainLooper()
class OnMainThreadException(str: String) : RuntimeException(str)
@@ -10,6 +10,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.service.model.BadgeAwardEvent import com.vitorpamplona.amethyst.service.model.BadgeAwardEvent
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
@@ -79,6 +80,8 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
@Synchronized @Synchronized
private fun refreshSuspended() { private fun refreshSuspended() {
checkNotInMainThread()
val notes = localFilter.feed() val notes = localFilter.feed()
val thisAccount = (localFilter as? NotificationFeedFilter)?.account val thisAccount = (localFilter as? NotificationFeedFilter)?.account
@@ -117,6 +120,8 @@ open class CardFeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
} }
private fun convertToCard(notes: Collection<Note>): List<Card> { private fun convertToCard(notes: Collection<Note>): List<Card> {
checkNotInMainThread()
val reactionsPerEvent = mutableMapOf<Note, MutableList<Note>>() val reactionsPerEvent = mutableMapOf<Note, MutableList<Note>>()
notes notes
.filter { it.event is ReactionEvent } .filter { it.event is ReactionEvent }
@@ -9,6 +9,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.ui.components.BundledInsert import com.vitorpamplona.amethyst.ui.components.BundledInsert
import com.vitorpamplona.amethyst.ui.components.BundledUpdate import com.vitorpamplona.amethyst.ui.components.BundledUpdate
import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter import com.vitorpamplona.amethyst.ui.dal.AdditiveFeedFilter
@@ -148,6 +149,8 @@ abstract class FeedViewModel(val localFilter: FeedFilter<Note>) : ViewModel() {
} }
fun refreshSuspended() { fun refreshSuspended() {
checkNotInMainThread()
val notes = newListFromDataSource() val notes = newListFromDataSource()
val oldNotesState = _feedContent.value val oldNotesState = _feedContent.value
@@ -5,6 +5,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.ui.components.BundledUpdate import com.vitorpamplona.amethyst.ui.components.BundledUpdate
import com.vitorpamplona.amethyst.ui.dal.FeedFilter import com.vitorpamplona.amethyst.ui.dal.FeedFilter
import com.vitorpamplona.amethyst.ui.dal.UserProfileZapsFeedFilter import com.vitorpamplona.amethyst.ui.dal.UserProfileZapsFeedFilter
@@ -30,6 +31,7 @@ open class LnZapFeedViewModel(val dataSource: FeedFilter<Pair<Note, Note>>) : Vi
} }
private fun refreshSuspended() { private fun refreshSuspended() {
checkNotInMainThread()
val notes = dataSource.loadTop() val notes = dataSource.loadTop()
val oldNotesState = _feedContent.value val oldNotesState = _feedContent.value
@@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.ui.components.BundledUpdate import com.vitorpamplona.amethyst.ui.components.BundledUpdate
import com.vitorpamplona.amethyst.ui.dal.FeedFilter import com.vitorpamplona.amethyst.ui.dal.FeedFilter
import com.vitorpamplona.amethyst.ui.dal.HiddenAccountsFeedFilter import com.vitorpamplona.amethyst.ui.dal.HiddenAccountsFeedFilter
@@ -58,6 +59,8 @@ open class UserFeedViewModel(val dataSource: FeedFilter<User>) : ViewModel() {
} }
private fun refreshSuspended() { private fun refreshSuspended() {
checkNotInMainThread()
val notes = dataSource.loadTop().toImmutableList() val notes = dataSource.loadTop().toImmutableList()
val oldNotesState = _feedContent.value val oldNotesState = _feedContent.value