Fix RemoteConfigLoader to use Kotlin's built-in Result type

Changed from custom Result sealed class to kotlin.Result which has
proper success/failure factory methods. Updated pattern matching to
use getOrElse for cleaner error handling.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-05 20:05:48 +01:00
parent d173c08a0c
commit 876db1751a

View File

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