Skip to content

Commit a6a0c1f

Browse files
committed
feat(network): Implement HTTP Caching
1 parent b84acdc commit a6a0c1f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ turbine = "0.12.1"
1515
retrofit = "2.11.0"
1616
okhttp = "4.12.0"
1717
moshi = "1.15.1"
18+
androidx_annotation = "1.8.0"
1819

1920
[libraries]
2021
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -36,9 +37,10 @@ okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhtt
3637
okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp" }
3738
moshi = { group = "com.squareup.moshi", name = "moshi", version.ref = "moshi" }
3839
moshi-kotlin = { group = "com.squareup.moshi", name = "moshi-kotlin", version.ref = "moshi" }
40+
androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidx_annotation" }
3941

4042
[plugins]
4143
android-application = { id = "com.android.application", version.ref = "android_gradle_plugin" }
4244
android-library = { id = "com.android.library", version.ref = "android_gradle_plugin" }
4345
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin_gradle_plugin" }
44-
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp_gradle_plugin" }
46+
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp_gradle_plugin" }

network/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
implementation(libs.okhttp.logging.interceptor)
4040
implementation(libs.moshi)
4141
implementation(libs.moshi.kotlin)
42+
implementation(libs.androidx.annotation)
4243
testImplementation(libs.junit)
4344
androidTestImplementation(libs.androidx.test.ext.junit)
4445
androidTestImplementation(libs.androidx.test.espresso.core)

network/src/main/java/com/anysoftkeyboard/janus/network/WikipediaClient.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
package com.anysoftkeyboard.janus.network
22

3+
import android.content.Context
34
import com.squareup.moshi.Moshi
45
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
6+
import okhttp3.Cache
57
import okhttp3.OkHttpClient
68
import okhttp3.logging.HttpLoggingInterceptor
79
import retrofit2.Retrofit
810
import retrofit2.converter.moshi.MoshiConverterFactory
11+
import java.io.File
912

10-
object WikipediaClient {
11-
private const val BASE_URL = "https://en.wikipedia.org/w/"
13+
class WikipediaClient(context: Context) {
14+
private val BASE_URL = "https://en.wikipedia.org/w/"
15+
private val CACHE_SIZE = 10 * 1024 * 1024L // 10 MB
1216

1317
private val moshi = Moshi.Builder()
1418
.add(KotlinJsonAdapterFactory())
1519
.build()
1620

21+
private val cache = Cache(File(context.cacheDir, "http-cache"), CACHE_SIZE)
22+
1723
private val okHttpClient = OkHttpClient.Builder()
1824
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
25+
.cache(cache)
1926
.build()
2027

2128
val api: WikipediaApi by lazy {

0 commit comments

Comments
 (0)