From f92a3d0a996a108f5eaf25e78c79d64c3914893b Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Wed, 26 Aug 2026 10:01:24 -0400 Subject: [PATCH] android: detekt's return limit, on two functions I wrote after it caught me once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `dev` went red on 1e2b42a and nobody was watching — the CI wait was killed with the session, so the push was never confirmed. Checked on the way back in. Both findings are ReturnCount: four exits against a limit of two. The same rule caught continueChecklist earlier the same day, which is the annoying part — I had the lesson and wrote two more guard-clause ladders anyway. checkInBackground becomes a `when`, which it wanted to be regardless: it is four mutually exclusive situations and one action, and the ladder made that read like a sequence of unrelated escapes. onWifi folds its three null checks into one nullable chain. Same behaviour, and the `caps != null &&` reads as what it is — an uncertain answer being treated as no. --- .../com/fabledsword/thoughtsync/AppUpdate.kt | 10 ++-- .../thoughtsync/ui/UpdateViewModel.kt | 47 ++++++++++--------- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/android/app/src/main/java/com/fabledsword/thoughtsync/AppUpdate.kt b/android/app/src/main/java/com/fabledsword/thoughtsync/AppUpdate.kt index 1c61ae6..10f9855 100644 --- a/android/app/src/main/java/com/fabledsword/thoughtsync/AppUpdate.kt +++ b/android/app/src/main/java/com/fabledsword/thoughtsync/AppUpdate.kt @@ -79,10 +79,12 @@ object AppUpdate { * Every uncertain answer is `false`: the cautious one costs nothing. */ fun onWifi(context: Context): Boolean { - val manager = context.getSystemService(ConnectivityManager::class.java) ?: return false - val active = manager.activeNetwork ?: return false - val caps = manager.getNetworkCapabilities(active) ?: return false - return caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) && + val caps = + context + .getSystemService(ConnectivityManager::class.java) + ?.let { manager -> manager.activeNetwork?.let(manager::getNetworkCapabilities) } + return caps != null && + caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) && caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) } diff --git a/android/app/src/main/java/com/fabledsword/thoughtsync/ui/UpdateViewModel.kt b/android/app/src/main/java/com/fabledsword/thoughtsync/ui/UpdateViewModel.kt index 7e21e12..9207e33 100644 --- a/android/app/src/main/java/com/fabledsword/thoughtsync/ui/UpdateViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/thoughtsync/ui/UpdateViewModel.kt @@ -88,30 +88,31 @@ class UpdateViewModel( * when one ran recently: flicking between two apps is not a request to re-check. */ fun checkInBackground() { - if (state.busy) return - - // Already fetched and waved away — say so again. "Later" is for that sitting, - // not forever, and without this the early return below would mean a single - // dismissal silenced the update permanently. Which is precisely the "lost" - // this whole path exists to prevent. - if (state.ready) { - if (state.nagDismissed) state = state.copy(nagDismissed = false) - return - } - - // Already found one and never fetched it — almost always because the last look - // happened on mobile data. Retry the FETCH rather than the check, and ignore - // the interval: this is what makes an update found on the train arrive when - // the person gets home, instead of waiting out six hours first. - if (state.available != null) { - if (AppUpdate.onWifi(context)) viewModelScope.launch { download() } - return - } - val now = System.currentTimeMillis() - if (now - lastCheckAt < CHECK_INTERVAL_MS) return - lastCheckAt = now - runCheck(fetch = true) + when { + state.busy -> Unit + + // Already fetched and waved away — say so again. "Later" is for that + // sitting, not forever, and without this branch a single dismissal would + // silence the update permanently. Which is precisely the "lost" this whole + // path exists to prevent. + state.ready -> if (state.nagDismissed) state = state.copy(nagDismissed = false) + + // Found one and never fetched it — almost always because the last look + // happened on mobile data. Retry the FETCH rather than the check, and + // ignore the interval: this is what makes an update found on the train + // arrive when the person gets home instead of waiting out six hours. + state.available != null -> + if (AppUpdate.onWifi(context)) viewModelScope.launch { download() } + + // Flicking between two apps is not a request to re-check. + now - lastCheckAt < CHECK_INTERVAL_MS -> Unit + + else -> { + lastCheckAt = now + runCheck(fetch = true) + } + } } private fun runCheck(fetch: Boolean) {