From 9dd5b9cad931ba5c8d78069f9328a91f1fa89209 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Fri, 18 Nov 2022 14:27:27 -0500 Subject: [PATCH] Don't reload Nebula unless config is updated (#91) --- .../defined/mobile_nebula/DNUpdateWorker.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/kotlin/net/defined/mobile_nebula/DNUpdateWorker.kt b/android/app/src/main/kotlin/net/defined/mobile_nebula/DNUpdateWorker.kt index bd6182d..70851bf 100644 --- a/android/app/src/main/kotlin/net/defined/mobile_nebula/DNUpdateWorker.kt +++ b/android/app/src/main/kotlin/net/defined/mobile_nebula/DNUpdateWorker.kt @@ -40,13 +40,19 @@ class DNUpdateWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, param private fun updateSite(site: Site) { try { DNUpdateLock(site).use { - if (updater.updateSite(site)) { - // Reload Nebula if this is the currently active site + val res = updater.updateSite(site) + + // Reload Nebula if this is the currently active site + if (res == DNSiteUpdater.Result.CONFIG_UPDATED) { Intent().also { intent -> intent.action = NebulaVpnService.ACTION_RELOAD intent.putExtra("id", site.id) context.sendBroadcast(intent) } + } + + // Update the UI on any change + if (res != DNSiteUpdater.Result.NOOP) { Intent().also { intent -> intent.action = MainActivity.ACTION_REFRESH_SITES context.sendBroadcast(intent) @@ -77,9 +83,13 @@ class DNSiteUpdater( private val context: Context, private val apiClient: APIClient, ) { - fun updateSite(site: Site): Boolean { + enum class Result { + CONFIG_UPDATED, CREDENTIALS_UPDATED, NOOP + } + + fun updateSite(site: Site): Result { if (!site.managed) { - return false + return Result.NOOP } val credentials = site.getDNCredentials(context) @@ -97,21 +107,23 @@ class DNSiteUpdater( if (!credentials.invalid) { site.invalidateDNCredentials(context) Log.d(TAG, "Invalidated credentials in site ${site.name}") + return Result.CREDENTIALS_UPDATED } - return true + return Result.NOOP } if (newSite != null) { newSite.save(context) Log.d(TAG, "Updated site ${site.id}: ${site.name}") - return true + return Result.CONFIG_UPDATED } if (credentials.invalid) { site.validateDNCredentials(context) Log.d(TAG, "Revalidated credentials in site ${site.id}: ${site.name}") + return Result.CREDENTIALS_UPDATED } - return false + return Result.NOOP } } \ No newline at end of file