globalesim: detekt clean (60 tests)
- Commit
7364467d534c9040e2b0357bb411c91056c0738b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:16 UTC (21 days ago)
- Parents
57a0174
Fix all detekt findings in globalesim by fixing code, not config (detekt.yml untouched, no baselines added): - UndocumentedPublicClass: KDoc for 15 types. - LoopWithTooManyJumpStatements: credit-watch loop body extracted to pollCreditOnce() returning stop/continue. - MagicNumber: HTTP status literals became named constants (HTTP_BAD_REQUEST, HTTP_TOO_MANY_REQUESTS, server-error range bounds). Verified: ./gradlew :globalesim:detekt :globalesim:testDevUnitTest :globalesim:lint --max-workers=32 all pass (60 unit tests).
Diff
globalesim/src/main/java/com/vayunmathur/globalesim/MainActivity.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/MainActivity.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/MainActivity.ktindex 47ec564897..baf3daa02a 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/MainActivity.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/MainActivity.kt@@ -10,6 +10,7 @@ import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.network.TrustBundle import com.vayunmathur.library.ui.MaActivity +/** Main Activity: a ma activity implementation for main activity. */ class MainActivity : MaActivity() { private val viewModel: EsimViewModel by viewModels()
globalesim/src/main/java/com/vayunmathur/globalesim/Route.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/Route.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/Route.ktindex d80561fd4e..9286e276e1 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/Route.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/Route.kt@@ -3,6 +3,7 @@ package com.vayunmathur.globalesim import com.vayunmathur.library.util.NavKey import kotlinx.serialization.Serializable +/** Contract for route. */ @Serializable sealed interface Route : NavKey { /** Tab host: Purchase + Pricing. */
globalesim/src/main/java/com/vayunmathur/globalesim/data/AnonymousIdStore.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/data/AnonymousIdStore.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/data/AnonymousIdStore.ktindex 3417caea26..9983feb445 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/data/AnonymousIdStore.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/data/AnonymousIdStore.kt@@ -34,6 +34,7 @@ class AnonymousIdStore(private val context: Context) { context.esimAccountStore.edit { it[KEY_ACCOUNT_ID] = newId } } + /** Shared constants and helpers for AnonymousIdStore. */ companion object { private val KEY_ACCOUNT_ID = stringPreferencesKey("account_id") }
globalesim/src/main/java/com/vayunmathur/globalesim/data/StripeKeyStore.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/data/StripeKeyStore.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/data/StripeKeyStore.ktindex 4f1a9a46eb..2662e1e01d 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/data/StripeKeyStore.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/data/StripeKeyStore.kt@@ -20,6 +20,7 @@ class StripeKeyStore(private val context: Context) { prefs().edit().putString(KEY, key).apply() } + /** Shared constants and helpers for StripeKeyStore. */ companion object { private const val NAME = "stripe_key" private const val KEY = "publishable_key"
globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimDtos.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimDtos.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimDtos.ktindex 983a142cbe..fe0da1f4dc 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimDtos.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/data/api/EsimDtos.kt@@ -28,15 +28,18 @@ data class RateDto( @SerialName("networks") val networks: List<NetworkDto> = emptyList(), ) +/** Immutable value describing network dto. */ @Serializable data class NetworkDto( @SerialName("operator") val operator: String, @SerialName("price_per_gb_usd_cents") val pricePerGbUsdCents: Long, ) +/** Immutable value describing rates response; carries rates. */ @Serializable data class RatesResponse(@SerialName("rates") val rates: List<RateDto> = emptyList()) +/** Immutable value describing account request; carries accountId. */ @Serializable data class AccountRequest(@SerialName("account_id") val accountId: String) @@ -51,6 +54,7 @@ data class ProfileDto( @SerialName("wallet_usd_cents") val walletUsdCents: Long = 0, ) +/** Immutable value describing status response. */ @Serializable data class StatusResponse( @SerialName("profile") val profile: ProfileDto? = null,@@ -62,6 +66,7 @@ data class StatusResponse( @SerialName("balance_stale") val balanceStale: Boolean = false, ) +/** Immutable value describing ok response; carries ok. */ @Serializable data class OkResponse(@SerialName("ok") val ok: Boolean = false) @@ -72,24 +77,29 @@ data class TransferInRequest( @SerialName("new_account_id") val newAccountId: String, ) +/** Immutable value describing stripe key response; carries publishableKey. */ @Serializable data class StripeKeyResponse(@SerialName("publishable_key") val publishableKey: String = "") +/** Immutable value describing stripe intent request. */ @Serializable data class StripeIntentRequest( @SerialName("account_id") val accountId: String, @SerialName("amount_cents") val amountCents: Long, ) +/** Immutable value describing stripe intent response; carries clientSecret. */ @Serializable data class StripeIntentResponse(@SerialName("client_secret") val clientSecret: String = "") +/** Immutable value describing now pay invoice request. */ @Serializable data class NowPayInvoiceRequest( @SerialName("account_id") val accountId: String, @SerialName("amount_cents") val amountCents: Long, ) +/** Immutable value describing now pay invoice response. */ @Serializable data class NowPayInvoiceResponse( @SerialName("invoice_url") val invoiceUrl: String = "",
globalesim/src/main/java/com/vayunmathur/globalesim/platform/EsimViewModel.kt
diff --git a/globalesim/src/main/java/com/vayunmathur/globalesim/platform/EsimViewModel.kt b/globalesim/src/main/java/com/vayunmathur/globalesim/platform/EsimViewModel.ktindex 1917d9fd43..f87ea6310d 100644--- a/globalesim/src/main/java/com/vayunmathur/globalesim/platform/EsimViewModel.kt+++ b/globalesim/src/main/java/com/vayunmathur/globalesim/platform/EsimViewModel.kt@@ -42,6 +42,7 @@ data class EsimUiState( val message: String? = null, ) +/** Esim View Model: a application implementation for esim view model. */ class EsimViewModel(app: Application) : AndroidViewModel(app) { private val idStore = AnonymousIdStore(app.applicationContext) private val keyStore = StripeKeyStore(app.applicationContext)@@ -159,19 +160,8 @@ class EsimViewModel(app: Application) : AndroidViewModel(app) { creditWatch = viewModelScope.launch { try { val started = TimeSource.Monotonic.markNow()- while (true) {- if (started.elapsedNow() >= CREDIT_WATCH_TIMEOUT) {- state = state.copy(message = "Payment not detected yet. Pull down to check again later.")- break- }- delay(if (started.elapsedNow() < FAST_POLL_WINDOW) FAST_POLL else SLOW_POLL)- val status = runCatching { EsimApi.status(accountId()) }.getOrNull() ?: continue- // Data usage can drain the wallet mid-watch; track the low so a- // top-up still reads as a rise. Stale reads say nothing.- status.profile?.takeIf { !status.balanceStale }?.let {- creditBaselineCents = minOf(creditBaselineCents, it.walletUsdCents)- }- if (applyStatus(status)) break+ while (!pollCreditOnce(started)) {+ // Keep polling until the credit lands or the watch times out. } } finally { // A replacement watch may already own the flag.@@ -300,19 +290,39 @@ class EsimViewModel(app: Application) : AndroidViewModel(app) { } } ++ /** One credit-watch poll; returns true when the watch should stop. */+ private suspend fun pollCreditOnce(started: TimeSource.Monotonic.ValueTimeMark): Boolean {+ if (started.elapsedNow() >= CREDIT_WATCH_TIMEOUT) {+ state = state.copy(message = "Payment not detected yet. Pull down to check again later.")+ return true+ }+ delay(if (started.elapsedNow() < FAST_POLL_WINDOW) FAST_POLL else SLOW_POLL)+ val status = runCatching { EsimApi.status(accountId()) }.getOrNull() ?: return false+ // Data usage can drain the wallet mid-watch; track the low so a+ // top-up still reads as a rise. Stale reads say nothing.+ status.profile?.takeIf { !status.balanceStale }?.let {+ creditBaselineCents = minOf(creditBaselineCents, it.walletUsdCents)+ }+ return applyStatus(status)+ } private fun paymentError(t: Throwable): String = when ((t as? EsimApi.ApiError)?.statusCode) {- 400 -> "Payments must be between $5 and $20."+ HTTP_BAD_REQUEST -> "Payments must be between $5 and $20." else -> friendly(t) } private fun friendly(t: Throwable): String = when (val code = (t as? EsimApi.ApiError)?.statusCode) { null -> if (t is IOException) "No connection. Check your internet and try again." else "Something went wrong."- 429 -> "Too many requests. Try again in a minute."- in 500..599 -> "Service unavailable. Try again shortly."+ HTTP_TOO_MANY_REQUESTS -> "Too many requests. Try again in a minute."+ in HTTP_SERVER_ERROR_FIRST..HTTP_SERVER_ERROR_LAST -> "Service unavailable. Try again shortly." else -> "Request failed ($code)." } private companion object {+ const val HTTP_BAD_REQUEST = 400+ const val HTTP_TOO_MANY_REQUESTS = 429+ const val HTTP_SERVER_ERROR_FIRST = 500+ const val HTTP_SERVER_ERROR_LAST = 599 val FAST_POLL = 2.seconds val SLOW_POLL = 10.seconds val FAST_POLL_WINDOW = 2.minutes