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

View File

@ -1,8 +1,10 @@
package com.vitorpamplona.amethyst.model
import android.content.res.Resources
import android.util.Log
import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.service.relays.Constants
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
@ -394,12 +396,21 @@ class Account(
}.toTypedArray()
}
fun reconnectIfRelaysHaveChanged() {
val newRelaySet = activeRelays() ?: convertLocalRelays()
if (!Client.isSameRelaySetConfig(newRelaySet)) {
Client.disconnect()
Client.connect(newRelaySet)
RelayPool.requestAndWatch()
}
}
init {
userProfile().liveRelays.observeForever {
GlobalScope.launch(Dispatchers.IO) {
Client.disconnect()
Client.connect(activeRelays() ?: convertLocalRelays())
RelayPool.requestAndWatch()
GlobalScope.launch(Dispatchers.Main) {
userProfile().liveRelays.observeForever {
GlobalScope.launch(Dispatchers.IO) {
reconnectIfRelaysHaveChanged()
}
}
}
}

View File

@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.model
import android.util.Log
import androidx.lifecycle.LiveData
import com.vitorpamplona.amethyst.service.NostrSingleUserDataSource
import com.vitorpamplona.amethyst.service.model.LnZapEvent
@ -221,10 +222,9 @@ class User(val pubkeyHex: String) {
}
fun updateRelays(relayUse: Map<String, ContactListEvent.ReadWrite>) {
if (relays != relayUse) {
relays = relayUse
liveRelays.invalidateData()
}
// no need to test if relays are different. The Account will check for us.
relays = relayUse
liveRelays.invalidateData()
}
fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) {

View File

@ -32,6 +32,20 @@ object Client: RelayPool.Listener {
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(
subscriptionId: String = UUID.randomUUID().toString().substring(0..10),
filters: List<TypedFilter> = listOf()

View File

@ -204,6 +204,13 @@ class Relay(
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 {
// Websocket connected
CONNECT,

View File

@ -75,8 +75,8 @@ class MainActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
ServiceManager.start()
// Only starts after login
//ServiceManager.start()
}
override fun onPause() {

View File

@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.screen
import androidx.compose.runtime.rememberCoroutineScope
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.LocalPreferences
@ -21,8 +22,10 @@ class AccountStateViewModel(private val localPreferences: LocalPreferences): Vie
init {
// pulls account from storage.
localPreferences.loadFromEncryptedStorage()?.let {
login(it)
viewModelScope.launch(Dispatchers.IO) {
localPreferences.loadFromEncryptedStorage()?.let {
login(it)
}
}
}