mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-11 04:03:51 +02:00
Avoids starting connections with default relays when resuming the app.
This commit is contained in:
@@ -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 {
|
||||||
|
GlobalScope.launch(Dispatchers.Main) {
|
||||||
userProfile().liveRelays.observeForever {
|
userProfile().liveRelays.observeForever {
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
Client.disconnect()
|
reconnectIfRelaysHaveChanged()
|
||||||
Client.connect(activeRelays() ?: convertLocalRelays())
|
}
|
||||||
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,11 +222,10 @@ 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) {
|
||||||
info = newUserInfo
|
info = newUserInfo
|
||||||
|
@@ -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,10 +22,12 @@ class AccountStateViewModel(private val localPreferences: LocalPreferences): Vie
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
// pulls account from storage.
|
// pulls account from storage.
|
||||||
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
localPreferences.loadFromEncryptedStorage()?.let {
|
localPreferences.loadFromEncryptedStorage()?.let {
|
||||||
login(it)
|
login(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun login(key: String) {
|
fun login(key: String) {
|
||||||
val pattern = Pattern.compile(".+@.+\\.[a-z]+")
|
val pattern = Pattern.compile(".+@.+\\.[a-z]+")
|
||||||
|
Reference in New Issue
Block a user