|
1 | 1 | package dev.hotwire.turbo.config
|
2 | 2 |
|
3 | 3 | import android.content.Context
|
4 |
| -import dev.hotwire.turbo.util.toObject |
5 |
| -import com.google.gson.reflect.TypeToken |
6 | 4 | import dev.hotwire.turbo.util.dispatcherProvider
|
7 | 5 | import kotlinx.coroutines.CoroutineScope
|
8 | 6 | import kotlinx.coroutines.Job
|
@@ -30,29 +28,31 @@ internal class TurboPathConfigurationLoader(val context: Context) : CoroutineSco
|
30 | 28 | loadCachedConfigurationForUrl(url, onCompletion)
|
31 | 29 |
|
32 | 30 | launch {
|
33 |
| - repository.getRemoteConfiguration(url)?.let { |
34 |
| - onCompletion(load(it)) |
35 |
| - cacheConfigurationForUrl(url, load(it)) |
| 31 | + repository.getRemoteConfiguration(url)?.let { remoteConfigJson -> |
| 32 | + repository.parseFromJson(remoteConfigJson)?.let { config -> |
| 33 | + onCompletion(config) |
| 34 | + cacheConfigurationForUrl(url, config) |
| 35 | + } |
36 | 36 | }
|
37 | 37 | }
|
38 | 38 | }
|
39 | 39 |
|
40 | 40 | private fun loadBundledAssetConfiguration(filePath: String, onCompletion: (TurboPathConfiguration) -> Unit) {
|
41 |
| - val configuration = repository.getBundledConfiguration(context, filePath) |
42 |
| - onCompletion(load(configuration)) |
| 41 | + val bundledConfigJson = repository.getBundledConfiguration(context, filePath) |
| 42 | + repository.parseFromJson(bundledConfigJson)?.let { config -> |
| 43 | + onCompletion(config) |
| 44 | + } |
43 | 45 | }
|
44 | 46 |
|
45 | 47 | private fun loadCachedConfigurationForUrl(url: String, onCompletion: (TurboPathConfiguration) -> Unit) {
|
46 |
| - repository.getCachedConfigurationForUrl(context, url)?.let { |
47 |
| - onCompletion(load(it)) |
| 48 | + repository.getCachedConfigurationForUrl(context, url)?.let { cachedConfigJson -> |
| 49 | + repository.parseFromJson(cachedConfigJson)?.let { config -> |
| 50 | + onCompletion(config) |
| 51 | + } |
48 | 52 | }
|
49 | 53 | }
|
50 | 54 |
|
51 | 55 | private fun cacheConfigurationForUrl(url: String, pathConfiguration: TurboPathConfiguration) {
|
52 | 56 | repository.cacheConfigurationForUrl(context, url, pathConfiguration)
|
53 | 57 | }
|
54 |
| - |
55 |
| - private fun load(json: String): TurboPathConfiguration { |
56 |
| - return json.toObject(object : TypeToken<TurboPathConfiguration>() {}) |
57 |
| - } |
58 | 58 | }
|
0 commit comments