Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarta committed Sep 16, 2024
1 parent 41a2bb2 commit 23fa807
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -68,6 +71,7 @@ internal class TurboPathConfigurationRepository {
}
}

@VisibleForTesting
fun parseFromJson(json: String): TurboPathConfiguration? {
return try {
json.toObject(object : TypeToken<TurboPathConfiguration>() {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down

0 comments on commit 23fa807

Please sign in to comment.