From 6d21653b3635013543ce0e7055f19835e35d0159 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 22 Feb 2023 09:58:35 -0500 Subject: [PATCH] init block is positionally aware.. in some phones... --- .../vitorpamplona/amethyst/model/Account.kt | 89 ++++++++++--------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 9a4149b64..21b2bc334 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -64,6 +64,11 @@ class Account( @Transient var userProfile: User? = null + // Observers line up here. + val live: AccountLiveData = AccountLiveData(this) + val liveLanguages: AccountLiveData = AccountLiveData(this) + val saveable: AccountLiveData = AccountLiveData(this) + fun userProfile(): User { userProfile?.let { return it } @@ -420,7 +425,10 @@ class Account( } private fun updateContactListTo(newContactList: ContactListEvent?) { - if ((newContactList?.follows?.size ?: 0) > 0 && latestContactList != newContactList) { + if (newContactList?.follows.isNullOrEmpty()) return + + // Events might be different objects, we have to compare their ids. + if (latestContactList?.id?.toHex() != newContactList?.id?.toHex()) { latestContactList = newContactList saveable.invalidateData() } @@ -448,48 +456,6 @@ class Account( } } - init { - latestContactList?.let { - println("Loading saved contacts ${it.toJson()}") - if (userProfile().latestContactList == null) { - LocalCache.consume(it) - } - } - - // Observes relays to restart connections - userProfile().live().relays.observeForever { - GlobalScope.launch(Dispatchers.IO) { - reconnectIfRelaysHaveChanged() - } - } - - // saves contact list for the next time. - userProfile().live().follows.observeForever { - GlobalScope.launch(Dispatchers.IO) { - updateContactListTo(userProfile().latestContactList) - } - } - - // imports transient blocks due to spam. - LocalCache.antiSpam.liveSpam.observeForever { - GlobalScope.launch(Dispatchers.IO) { - it.cache.spamMessages.snapshot().values.forEach { - if (it.pubkeyHex !in transientHiddenUsers && it.duplicatedMessages > 5) { - val userToBlock = LocalCache.getOrCreateUser(it.pubkeyHex) - if (userToBlock != userProfile() && userToBlock !in userProfile().follows) { - transientHiddenUsers = transientHiddenUsers + it.pubkeyHex - } - } - } - } - } - } - - // Observers line up here. - val live: AccountLiveData = AccountLiveData(this) - val liveLanguages: AccountLiveData = AccountLiveData(this) - val saveable: AccountLiveData = AccountLiveData(this) - fun isHidden(user: User) = user.pubkeyHex in hiddenUsers || user.pubkeyHex in transientHiddenUsers fun isAcceptable(user: User): Boolean { @@ -531,6 +497,43 @@ class Account( saveable.invalidateData() } + + init { + latestContactList?.let { + println("Loading saved contacts ${it.toJson()}") + if (userProfile().latestContactList == null) { + LocalCache.consume(it) + } + } + + // Observes relays to restart connections + userProfile().live().relays.observeForever { + GlobalScope.launch(Dispatchers.IO) { + reconnectIfRelaysHaveChanged() + } + } + + // saves contact list for the next time. + userProfile().live().follows.observeForever { + GlobalScope.launch(Dispatchers.IO) { + updateContactListTo(userProfile().latestContactList) + } + } + + // imports transient blocks due to spam. + LocalCache.antiSpam.liveSpam.observeForever { + GlobalScope.launch(Dispatchers.IO) { + it.cache.spamMessages.snapshot().values.forEach { + if (it.pubkeyHex !in transientHiddenUsers && it.duplicatedMessages > 5) { + val userToBlock = LocalCache.getOrCreateUser(it.pubkeyHex) + if (userToBlock != userProfile() && userToBlock !in userProfile().follows) { + transientHiddenUsers = transientHiddenUsers + it.pubkeyHex + } + } + } + } + } + } } class AccountLiveData(private val account: Account): LiveData(AccountState(account)) {