mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-06 03:18:23 +00:00
Fix relay reconnecting every time the app opens
This commit is contained in:
@@ -2,6 +2,8 @@ package com.greenart7c3.nostrsigner
|
||||
|
||||
import android.app.Application
|
||||
import com.greenart7c3.nostrsigner.database.AppDatabase
|
||||
import com.greenart7c3.nostrsigner.relays.Client
|
||||
import com.greenart7c3.nostrsigner.relays.Relay
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class NostrSigner : Application() {
|
||||
@@ -23,6 +25,21 @@ class NostrSigner : Application() {
|
||||
return databases[npub]!!
|
||||
}
|
||||
|
||||
fun checkForNewRelays() {
|
||||
val savedRelays = mutableSetOf<String>()
|
||||
LocalPreferences.allSavedAccounts().forEach { accountInfo ->
|
||||
val database = getDatabase(accountInfo.npub)
|
||||
database.applicationDao().getAllApplications().forEach {
|
||||
it.application.relays.forEach { url ->
|
||||
if (url.isNotBlank()) {
|
||||
savedRelays.add(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Client.addRelays(savedRelays.map { Relay(it) }.toTypedArray())
|
||||
}
|
||||
|
||||
companion object {
|
||||
lateinit var instance: NostrSigner
|
||||
private set
|
||||
|
||||
@@ -43,6 +43,7 @@ object Client : RelayPool.Listener {
|
||||
@Synchronized
|
||||
fun addRelays(relays: Array<Relay>) {
|
||||
if (isSameRelaySetConfig(relays)) return
|
||||
Log.d("Relay", "Relay Pool changed")
|
||||
RelayPool.disconnect()
|
||||
RelayPool.unregister(this)
|
||||
RelayPool.unloadRelays()
|
||||
@@ -54,13 +55,13 @@ object Client : RelayPool.Listener {
|
||||
@Synchronized
|
||||
fun reconnect(
|
||||
relays: Array<Relay>?,
|
||||
onlyIfChanged: Boolean = false,
|
||||
onlyIfChanged: Boolean = true,
|
||||
) {
|
||||
Log.d("Relay", "Relay Pool Reconnecting to ${relays?.size} relays")
|
||||
checkNotInMainThread()
|
||||
|
||||
if (onlyIfChanged) {
|
||||
if (!isSameRelaySetConfig(relays)) {
|
||||
Log.d("Relay", "Relay Pool Reconnecting to ${relays?.size} relays")
|
||||
if (Client.relays.isNotEmpty()) {
|
||||
RelayPool.disconnect()
|
||||
RelayPool.unregister(this)
|
||||
@@ -75,6 +76,7 @@ object Client : RelayPool.Listener {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.d("Relay", "Relay Pool Reconnecting to ${relays?.size} relays")
|
||||
if (Client.relays.isNotEmpty()) {
|
||||
RelayPool.disconnect()
|
||||
RelayPool.unregister(this)
|
||||
|
||||
@@ -108,7 +108,7 @@ object HttpClientManager {
|
||||
defaultHttpClient!!
|
||||
} else {
|
||||
if (this.defaultHttpClientWithoutProxy == null) {
|
||||
this.defaultHttpClientWithoutProxy = buildHttpClient(null, defaultTimeout)
|
||||
this.defaultHttpClientWithoutProxy = buildHttpClient(null, DEFAULT_TIMEOUT_ON_WIFI)
|
||||
}
|
||||
defaultHttpClientWithoutProxy!!
|
||||
}
|
||||
|
||||
@@ -272,22 +272,9 @@ object IntentUtils {
|
||||
encryptedContent,
|
||||
) {
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
if (RelayPool.getAll().isEmpty()) {
|
||||
val database = NostrSigner.instance.getDatabase(account.keyPair.pubKey.toNpub())
|
||||
val savedRelays = mutableListOf<Relay>()
|
||||
database.applicationDao().getAllApplications().forEach {
|
||||
it.application.relays.forEach { url ->
|
||||
if (url.isNotBlank()) {
|
||||
if (!savedRelays.any { relay -> relay.url == url }) {
|
||||
savedRelays.add(Relay(url))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Client.addRelays(relays.toTypedArray())
|
||||
if (LocalPreferences.getNotificationType() == NotificationType.DIRECT) {
|
||||
Client.reconnect(relays.toTypedArray())
|
||||
}
|
||||
NostrSigner.instance.checkForNewRelays()
|
||||
if (LocalPreferences.getNotificationType() == NotificationType.DIRECT) {
|
||||
Client.reconnect(RelayPool.getAll().toTypedArray())
|
||||
delay(1000)
|
||||
}
|
||||
|
||||
|
||||
@@ -39,14 +39,12 @@ import com.greenart7c3.nostrsigner.NostrSigner
|
||||
import com.greenart7c3.nostrsigner.R
|
||||
import com.greenart7c3.nostrsigner.models.IntentData
|
||||
import com.greenart7c3.nostrsigner.relays.Client
|
||||
import com.greenart7c3.nostrsigner.relays.Relay
|
||||
import com.greenart7c3.nostrsigner.relays.RelayPool
|
||||
import com.greenart7c3.nostrsigner.service.ConnectivityService
|
||||
import com.greenart7c3.nostrsigner.service.IntentUtils
|
||||
import com.greenart7c3.nostrsigner.service.NotificationDataSource
|
||||
import com.vitorpamplona.quartz.encoders.toNpub
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -116,28 +114,13 @@ fun AccountScreen(
|
||||
|
||||
SideEffect {
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val relays = mutableListOf<Relay>()
|
||||
LocalPreferences.allSavedAccounts().forEach { acc ->
|
||||
val db = NostrSigner.instance.getDatabase(acc.npub)
|
||||
db.applicationDao().getAllApplications().forEach {
|
||||
it.application.relays.forEach { url ->
|
||||
if (url.isNotBlank()) {
|
||||
if (!relays.any { relay -> relay.url == url }) {
|
||||
relays.add(Relay(url))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delay(1000)
|
||||
Client.addRelays(relays.toTypedArray())
|
||||
NostrSigner.instance.checkForNewRelays()
|
||||
@Suppress("KotlinConstantConditions")
|
||||
if (LocalPreferences.getNotificationType() == NotificationType.DIRECT && BuildConfig.FLAVOR != "offline") {
|
||||
NostrSigner.instance.applicationContext.startService(
|
||||
Intent(NostrSigner.instance.applicationContext, ConnectivityService::class.java),
|
||||
)
|
||||
Client.reconnect(relays.toTypedArray())
|
||||
Client.reconnect(RelayPool.getAll().toTypedArray())
|
||||
NotificationDataSource.start()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavController
|
||||
import com.greenart7c3.nostrsigner.LocalPreferences
|
||||
import com.greenart7c3.nostrsigner.NostrSigner
|
||||
import com.greenart7c3.nostrsigner.R
|
||||
import com.greenart7c3.nostrsigner.database.AppDatabase
|
||||
import com.greenart7c3.nostrsigner.database.ApplicationEntity
|
||||
@@ -54,7 +55,7 @@ import com.greenart7c3.nostrsigner.database.ApplicationWithPermissions
|
||||
import com.greenart7c3.nostrsigner.models.Account
|
||||
import com.greenart7c3.nostrsigner.models.Permission
|
||||
import com.greenart7c3.nostrsigner.relays.Client
|
||||
import com.greenart7c3.nostrsigner.relays.Relay
|
||||
import com.greenart7c3.nostrsigner.relays.RelayPool
|
||||
import com.greenart7c3.nostrsigner.ui.actions.ActivityDialog
|
||||
import com.greenart7c3.nostrsigner.ui.actions.DeleteDialog
|
||||
import com.greenart7c3.nostrsigner.ui.actions.EditRelaysDialog
|
||||
@@ -139,19 +140,9 @@ fun EditPermission(
|
||||
.applicationDao()
|
||||
.delete(applicationData)
|
||||
|
||||
val relays = mutableListOf<Relay>()
|
||||
database.applicationDao().getAllApplications().forEach {
|
||||
it.application.relays.forEach { url ->
|
||||
if (url.isNotBlank()) {
|
||||
if (!relays.any { relay -> relay.url == url }) {
|
||||
relays.add(Relay(url))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Client.addRelays(relays.toTypedArray())
|
||||
NostrSigner.instance.checkForNewRelays()
|
||||
if (LocalPreferences.getNotificationType() == NotificationType.DIRECT) {
|
||||
Client.reconnect(relays.toTypedArray())
|
||||
Client.reconnect(RelayPool.getAll().toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,19 +418,9 @@ fun EditPermission(
|
||||
permissions,
|
||||
),
|
||||
)
|
||||
val relays = mutableListOf<Relay>()
|
||||
database.applicationDao().getAllApplications().forEach {
|
||||
it.application.relays.forEach { url ->
|
||||
if (url.isNotBlank()) {
|
||||
if (!relays.any { relay -> relay.url == url }) {
|
||||
relays.add(Relay(url))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Client.addRelays(relays.toTypedArray())
|
||||
NostrSigner.instance.checkForNewRelays()
|
||||
if (LocalPreferences.getNotificationType() == NotificationType.DIRECT) {
|
||||
Client.reconnect(relays.toTypedArray())
|
||||
Client.reconnect(RelayPool.getAll().toTypedArray())
|
||||
}
|
||||
|
||||
scope.launch(Dispatchers.Main) {
|
||||
|
||||
Reference in New Issue
Block a user