diff --git a/app/src/main/kotlin/uk/silverlabs/silverdroid/config/RemoteConfigLoader.kt b/app/src/main/kotlin/uk/silverlabs/silverdroid/config/RemoteConfigLoader.kt index 97fca43..6b5e879 100644 --- a/app/src/main/kotlin/uk/silverlabs/silverdroid/config/RemoteConfigLoader.kt +++ b/app/src/main/kotlin/uk/silverlabs/silverdroid/config/RemoteConfigLoader.kt @@ -24,7 +24,7 @@ object RemoteConfigLoader { suspend fun fetchConfig( remoteSettings: RemoteConfigSettings, userId: String? = null - ): Result = withContext(Dispatchers.IO) { + ): kotlin.Result = withContext(Dispatchers.IO) { try { val url = buildConfigUrl(remoteSettings, userId) Log.i(TAG, "Fetching config from: $url") @@ -50,15 +50,15 @@ object RemoteConfigLoader { val config = json.decodeFromString(response) Log.i(TAG, "Successfully loaded remote config for: ${config.appName}") - Result.success(config) + kotlin.Result.success(config) } else { val error = "HTTP $responseCode: ${connection.responseMessage}" Log.e(TAG, "Failed to fetch config: $error") - Result.failure(Exception(error)) + kotlin.Result.failure(Exception(error)) } } catch (e: Exception) { Log.e(TAG, "Error fetching remote config", e) - Result.failure(e) + kotlin.Result.failure(e) } } @@ -98,14 +98,12 @@ object RemoteConfigLoader { } // Fetch remote config - return when (val result = fetchConfig(remoteSettings, userId)) { - is Result.Success -> { + return fetchConfig(remoteSettings, userId).getOrElse { error -> + Log.w(TAG, "Remote config failed, falling back to local", error) + localConfig + }.also { + if (it != localConfig) { Log.i(TAG, "Using remote configuration") - result.value - } - is Result.Failure -> { - Log.w(TAG, "Remote config failed, falling back to local", result.error) - localConfig } } } @@ -114,8 +112,3 @@ object RemoteConfigLoader { private const val TAG = "RemoteConfigLoader" } } - -sealed class Result { - data class Success(val value: T) : Result() - data class Failure(val error: Exception) : Result() -}