From 23fa80797141040fe3210429fc0e0517f774e1c1 Mon Sep 17 00:00:00 2001 From: Milan Barta Date: Mon, 16 Sep 2024 15:41:11 +0200 Subject: [PATCH] Refactor code --- .../turbo/config/TurboPathConfigurationLoader.kt | 10 ++++------ .../turbo/config/TurboPathConfigurationRepository.kt | 12 ++++++++---- .../config/TurboPathConfigurationRepositoryTest.kt | 11 +++++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationLoader.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationLoader.kt index 8ea9ece6..3ce876c3 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationLoader.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationLoader.kt @@ -1,6 +1,7 @@ package dev.hotwire.turbo.config import android.content.Context +import dev.hotwire.turbo.config.Turbo.config import dev.hotwire.turbo.util.dispatcherProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job @@ -38,17 +39,14 @@ internal class TurboPathConfigurationLoader(val context: Context) : CoroutineSco } private fun loadBundledAssetConfiguration(filePath: String, onCompletion: (TurboPathConfiguration) -> Unit) { - val bundledConfigJson = repository.getBundledConfiguration(context, filePath) - repository.parseFromJson(bundledConfigJson)?.let { config -> + repository.getBundledConfiguration(context, filePath)?.let { config -> onCompletion(config) } } private fun loadCachedConfigurationForUrl(url: String, onCompletion: (TurboPathConfiguration) -> Unit) { - repository.getCachedConfigurationForUrl(context, url)?.let { cachedConfigJson -> - repository.parseFromJson(cachedConfigJson)?.let { config -> - onCompletion(config) - } + repository.getCachedConfigurationForUrl(context, url)?.let { config -> + onCompletion(config) } } diff --git a/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepository.kt b/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepository.kt index 3fcd9635..bdc3672a 100644 --- a/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepository.kt +++ b/turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepository.kt @@ -2,6 +2,7 @@ package dev.hotwire.turbo.config import android.content.Context import android.content.SharedPreferences +import androidx.annotation.VisibleForTesting import androidx.core.content.edit import com.google.gson.reflect.TypeToken import dev.hotwire.turbo.http.TurboHttpClient @@ -23,12 +24,14 @@ internal class TurboPathConfigurationRepository { } } - fun getBundledConfiguration(context: Context, filePath: String): String { - return contentFromAsset(context, filePath) + fun getBundledConfiguration(context: Context, filePath: String): TurboPathConfiguration? { + val bundledConfigJson = contentFromAsset(context, filePath) + return parseFromJson(bundledConfigJson) } - fun getCachedConfigurationForUrl(context: Context, url: String): String? { - return prefs(context).getString(url, null) + fun getCachedConfigurationForUrl(context: Context, url: String): TurboPathConfiguration? { + val cachedConfigJson = prefs(context).getString(url, null) + return cachedConfigJson?.let { parseFromJson(it) } } fun cacheConfigurationForUrl(context: Context, url: String, pathConfiguration: TurboPathConfiguration) { @@ -68,6 +71,7 @@ internal class TurboPathConfigurationRepository { } } + @VisibleForTesting fun parseFromJson(json: String): TurboPathConfiguration? { return try { json.toObject(object : TypeToken() {}) diff --git a/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt b/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt index e05777df..a2f6e7fe 100644 --- a/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt +++ b/turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt @@ -4,6 +4,7 @@ import android.content.Context import android.os.Build import androidx.test.core.app.ApplicationProvider import dev.hotwire.turbo.BaseRepositoryTest +import dev.hotwire.turbo.config.Turbo.config import dev.hotwire.turbo.http.TurboHttpClient import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -60,10 +61,9 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() { @Test fun getBundledAssetConfiguration() { - val json = repository.getBundledConfiguration(context, "json/test-configuration.json") - assertThat(json).isNotNull() + val config = repository.getBundledConfiguration(context, "json/test-configuration.json") + assertThat(config).isNotNull() - val config = repository.parseFromJson(json) assertThat(config?.rules?.size).isEqualTo(10) } @@ -73,10 +73,9 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() { val config = requireNotNull(repository.parseFromJson(json())) repository.cacheConfigurationForUrl(context, url, config) - val json = repository.getCachedConfigurationForUrl(context, url) - assertThat(json).isNotNull() + val cachedConfig = repository.getCachedConfigurationForUrl(context, url) + assertThat(cachedConfig).isNotNull() - val cachedConfig = repository.parseFromJson(json!!) assertThat(cachedConfig?.rules?.size).isEqualTo(1) }