Avoids starting connections with default relays when resuming the app.

This commit is contained in:
Vitor Pamplona
2023-02-17 12:27:36 -05:00
parent 4afcf48392
commit f0e09197ff
6 changed files with 48 additions and 13 deletions
@@ -1,8 +1,10 @@
package com.vitorpamplona.amethyst.model package com.vitorpamplona.amethyst.model
import android.content.res.Resources import android.content.res.Resources
import android.util.Log
import androidx.core.os.ConfigurationCompat import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.service.relays.Constants import com.vitorpamplona.amethyst.service.relays.Constants
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
@@ -394,12 +396,21 @@ class Account(
}.toTypedArray() }.toTypedArray()
} }
fun reconnectIfRelaysHaveChanged() {
val newRelaySet = activeRelays() ?: convertLocalRelays()
if (!Client.isSameRelaySetConfig(newRelaySet)) {
Client.disconnect()
Client.connect(newRelaySet)
RelayPool.requestAndWatch()
}
}
init { init {
userProfile().liveRelays.observeForever { GlobalScope.launch(Dispatchers.Main) {
GlobalScope.launch(Dispatchers.IO) { userProfile().liveRelays.observeForever {
Client.disconnect() GlobalScope.launch(Dispatchers.IO) {
Client.connect(activeRelays() ?: convertLocalRelays()) reconnectIfRelaysHaveChanged()
RelayPool.requestAndWatch() }
} }
} }
} }
@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.model package com.vitorpamplona.amethyst.model
import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource
import com.vitorpamplona.amethyst.service.model.LnZapEvent import com.vitorpamplona.amethyst.service.model.LnZapEvent
@@ -221,10 +222,9 @@ class User(val pubkeyHex: String) {
} }
fun updateRelays(relayUse: Map<String, ContactListEvent.ReadWrite>) { fun updateRelays(relayUse: Map<String, ContactListEvent.ReadWrite>) {
if (relays != relayUse) { // no need to test if relays are different. The Account will check for us.
relays = relayUse relays = relayUse
liveRelays.invalidateData() liveRelays.invalidateData()
}
} }
fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) { fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) {
@@ -32,6 +32,20 @@ object Client: RelayPool.Listener {
this.relays = relays this.relays = relays
} }
fun isSameRelaySetConfig(newRelayConfig: Array<Relay>): Boolean {
if (relays.size != newRelayConfig.size) return false
relays.forEach { oldRelayInfo ->
val newRelayInfo = newRelayConfig.find { it.url == oldRelayInfo.url }
if (newRelayInfo == null) return false
if (!oldRelayInfo.isSameRelayConfig(newRelayInfo)) return false
}
return true
}
fun sendFilter( fun sendFilter(
subscriptionId: String = UUID.randomUUID().toString().substring(0..10), subscriptionId: String = UUID.randomUUID().toString().substring(0..10),
filters: List<TypedFilter> = listOf() filters: List<TypedFilter> = listOf()
@@ -204,6 +204,13 @@ class Relay(
socket?.send("""["CLOSE","$subscriptionId"]""") socket?.send("""["CLOSE","$subscriptionId"]""")
} }
fun isSameRelayConfig(other: Relay): Boolean {
return url == other.url
&& write == other.write
&& read == other.read
&& activeTypes == other.activeTypes
}
enum class Type { enum class Type {
// Websocket connected // Websocket connected
CONNECT, CONNECT,
@@ -75,8 +75,8 @@ class MainActivity : ComponentActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
// Only starts after login
ServiceManager.start() //ServiceManager.start()
} }
override fun onPause() { override fun onPause() {
@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.screen package com.vitorpamplona.amethyst.ui.screen
import androidx.compose.runtime.rememberCoroutineScope
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
@@ -21,8 +22,10 @@ class AccountStateViewModel(private val localPreferences: LocalPreferences): Vie
init { init {
// pulls account from storage. // pulls account from storage.
localPreferences.loadFromEncryptedStorage()?.let { viewModelScope.launch(Dispatchers.IO) {
login(it) localPreferences.loadFromEncryptedStorage()?.let {
login(it)
}
} }
} }