Skip to content

Commit 23fa807

Browse files
committed
Refactor code
1 parent 41a2bb2 commit 23fa807

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationLoader.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.hotwire.turbo.config
22

33
import android.content.Context
4+
import dev.hotwire.turbo.config.Turbo.config
45
import dev.hotwire.turbo.util.dispatcherProvider
56
import kotlinx.coroutines.CoroutineScope
67
import kotlinx.coroutines.Job
@@ -38,17 +39,14 @@ internal class TurboPathConfigurationLoader(val context: Context) : CoroutineSco
3839
}
3940

4041
private fun loadBundledAssetConfiguration(filePath: String, onCompletion: (TurboPathConfiguration) -> Unit) {
41-
val bundledConfigJson = repository.getBundledConfiguration(context, filePath)
42-
repository.parseFromJson(bundledConfigJson)?.let { config ->
42+
repository.getBundledConfiguration(context, filePath)?.let { config ->
4343
onCompletion(config)
4444
}
4545
}
4646

4747
private fun loadCachedConfigurationForUrl(url: String, onCompletion: (TurboPathConfiguration) -> Unit) {
48-
repository.getCachedConfigurationForUrl(context, url)?.let { cachedConfigJson ->
49-
repository.parseFromJson(cachedConfigJson)?.let { config ->
50-
onCompletion(config)
51-
}
48+
repository.getCachedConfigurationForUrl(context, url)?.let { config ->
49+
onCompletion(config)
5250
}
5351
}
5452

turbo/src/main/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepository.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dev.hotwire.turbo.config
22

33
import android.content.Context
44
import android.content.SharedPreferences
5+
import androidx.annotation.VisibleForTesting
56
import androidx.core.content.edit
67
import com.google.gson.reflect.TypeToken
78
import dev.hotwire.turbo.http.TurboHttpClient
@@ -23,12 +24,14 @@ internal class TurboPathConfigurationRepository {
2324
}
2425
}
2526

26-
fun getBundledConfiguration(context: Context, filePath: String): String {
27-
return contentFromAsset(context, filePath)
27+
fun getBundledConfiguration(context: Context, filePath: String): TurboPathConfiguration? {
28+
val bundledConfigJson = contentFromAsset(context, filePath)
29+
return parseFromJson(bundledConfigJson)
2830
}
2931

30-
fun getCachedConfigurationForUrl(context: Context, url: String): String? {
31-
return prefs(context).getString(url, null)
32+
fun getCachedConfigurationForUrl(context: Context, url: String): TurboPathConfiguration? {
33+
val cachedConfigJson = prefs(context).getString(url, null)
34+
return cachedConfigJson?.let { parseFromJson(it) }
3235
}
3336

3437
fun cacheConfigurationForUrl(context: Context, url: String, pathConfiguration: TurboPathConfiguration) {
@@ -68,6 +71,7 @@ internal class TurboPathConfigurationRepository {
6871
}
6972
}
7073

74+
@VisibleForTesting
7175
fun parseFromJson(json: String): TurboPathConfiguration? {
7276
return try {
7377
json.toObject(object : TypeToken<TurboPathConfiguration>() {})

turbo/src/test/kotlin/dev/hotwire/turbo/config/TurboPathConfigurationRepositoryTest.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.os.Build
55
import androidx.test.core.app.ApplicationProvider
66
import dev.hotwire.turbo.BaseRepositoryTest
7+
import dev.hotwire.turbo.config.Turbo.config
78
import dev.hotwire.turbo.http.TurboHttpClient
89
import kotlinx.coroutines.Dispatchers
910
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -60,10 +61,9 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() {
6061

6162
@Test
6263
fun getBundledAssetConfiguration() {
63-
val json = repository.getBundledConfiguration(context, "json/test-configuration.json")
64-
assertThat(json).isNotNull()
64+
val config = repository.getBundledConfiguration(context, "json/test-configuration.json")
65+
assertThat(config).isNotNull()
6566

66-
val config = repository.parseFromJson(json)
6767
assertThat(config?.rules?.size).isEqualTo(10)
6868
}
6969

@@ -73,10 +73,9 @@ class TurboPathConfigurationRepositoryTest : BaseRepositoryTest() {
7373
val config = requireNotNull(repository.parseFromJson(json()))
7474
repository.cacheConfigurationForUrl(context, url, config)
7575

76-
val json = repository.getCachedConfigurationForUrl(context, url)
77-
assertThat(json).isNotNull()
76+
val cachedConfig = repository.getCachedConfigurationForUrl(context, url)
77+
assertThat(cachedConfig).isNotNull()
7878

79-
val cachedConfig = repository.parseFromJson(json!!)
8079
assertThat(cachedConfig?.rules?.size).isEqualTo(1)
8180
}
8281

0 commit comments

Comments
 (0)