mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-05 19:08:23 +00:00
Merge pull request #338 from greenart7c3/claude/async-tor-startup-KRi5l
Improve app startup performance by deferring Tor initialization
This commit is contained in:
@@ -262,24 +262,10 @@ class Amber :
|
||||
LocalPreferences.switchToAccount(this@Amber, LocalPreferences.allSavedAccounts(this@Amber).first().npub)
|
||||
}
|
||||
LocalPreferences.reloadApp()
|
||||
|
||||
// Start Tor immediately in the background without blocking app startup
|
||||
if (settings.torMode == TorMode.BUILTIN && !BuildFlavorChecker.isOfflineFlavor()) {
|
||||
var attempt = 0
|
||||
while (!TorManager.isRunning.value) {
|
||||
if (attempt > 0) {
|
||||
TorManager.showRetrying()
|
||||
TorManager.stop()
|
||||
delay(3000)
|
||||
}
|
||||
TorManager.start(this@Amber, applicationIOScope)
|
||||
attempt++
|
||||
withTimeoutOrNull(120_000L) {
|
||||
TorManager.isRunning.first { it }
|
||||
}
|
||||
}
|
||||
}
|
||||
checkForNewRelaysAndUpdateAllFilters(true)
|
||||
if (settings.killSwitch.value) {
|
||||
disconnectIntentionally()
|
||||
TorManager.start(this@Amber, applicationIOScope)
|
||||
}
|
||||
|
||||
launch(Dispatchers.Main) {
|
||||
@@ -307,7 +293,31 @@ class Amber :
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Signal app startup complete so UI shows immediately
|
||||
isStartingApp.value = false
|
||||
|
||||
// Wait for Tor to be ready before establishing relay connections
|
||||
if (settings.torMode == TorMode.BUILTIN && !BuildFlavorChecker.isOfflineFlavor()) {
|
||||
var attempt = 0
|
||||
while (!TorManager.isRunning.value) {
|
||||
if (attempt > 0) {
|
||||
TorManager.showRetrying()
|
||||
TorManager.stop()
|
||||
delay(3000)
|
||||
TorManager.start(this@Amber, applicationIOScope)
|
||||
}
|
||||
attempt++
|
||||
withTimeoutOrNull(120_000L) {
|
||||
TorManager.isRunning.first { it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkForNewRelaysAndUpdateAllFilters(true)
|
||||
if (settings.killSwitch.value) {
|
||||
disconnectIntentionally()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to run migrations", e)
|
||||
isStartingApp.value = false
|
||||
@@ -317,6 +327,12 @@ class Amber :
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun waitForTorIfNeeded() {
|
||||
if (settings.torMode == TorMode.BUILTIN && !BuildFlavorChecker.isOfflineFlavor()) {
|
||||
TorManager.isRunning.first { it }
|
||||
}
|
||||
}
|
||||
|
||||
fun startServiceFromUi() {
|
||||
startService()
|
||||
}
|
||||
|
||||
@@ -96,6 +96,8 @@ class ConnectivityService : Service() {
|
||||
while (Amber.instance.isStartingAppState.value) {
|
||||
delay(1000)
|
||||
}
|
||||
// Wait for Tor to be ready before connecting (if using built-in Tor)
|
||||
Amber.instance.waitForTorIfNeeded()
|
||||
if (!BuildFlavorChecker.isOfflineFlavor() && !Amber.instance.settings.killSwitch.value) {
|
||||
Amber.instance.client.connect()
|
||||
Amber.instance.applicationIOScope.launch {
|
||||
|
||||
Reference in New Issue
Block a user