From e9f8bd9e0648f3be1fd753ffb9d53d9bfa766c43 Mon Sep 17 00:00:00 2001 From: starry-shivam Date: Sun, 18 Feb 2024 13:28:51 +0530 Subject: [PATCH] Add response caching and pop homescreen for backstack Signed-off-by: starry-shivam --- .../java/com/starry/myne/di/MainModule.kt | 2 +- .../com/starry/myne/repo/BookRepository.kt | 24 +++++++++++++++++-- .../ui/screens/home/composables/HomeScreen.kt | 2 +- .../starry/myne/ui/screens/main/MainScreen.kt | 6 ++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/starry/myne/di/MainModule.kt b/app/src/main/java/com/starry/myne/di/MainModule.kt index 70d972db..e3075175 100644 --- a/app/src/main/java/com/starry/myne/di/MainModule.kt +++ b/app/src/main/java/com/starry/myne/di/MainModule.kt @@ -51,7 +51,7 @@ class MainModule { @Singleton @Provides - fun provideBooksApi() = BookRepository() + fun provideBooksApi(@ApplicationContext context: Context) = BookRepository(context) @Singleton @Provides diff --git a/app/src/main/java/com/starry/myne/repo/BookRepository.kt b/app/src/main/java/com/starry/myne/repo/BookRepository.kt index 43a4bb9e..aecd6762 100644 --- a/app/src/main/java/com/starry/myne/repo/BookRepository.kt +++ b/app/src/main/java/com/starry/myne/repo/BookRepository.kt @@ -16,6 +16,7 @@ package com.starry.myne.repo +import android.content.Context import com.google.gson.Gson import com.starry.myne.BuildConfig import com.starry.myne.repo.models.BookSet @@ -23,13 +24,17 @@ import com.starry.myne.repo.models.ExtraInfo import com.starry.myne.utils.book.BookLanguage import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext +import okhttp3.Cache +import okhttp3.CacheControl import okhttp3.Call import okhttp3.Callback +import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import org.json.JSONException import org.json.JSONObject +import java.io.File import java.io.IOException import java.net.URLEncoder import java.util.concurrent.TimeUnit @@ -37,23 +42,38 @@ import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine +class CacheInterceptor : Interceptor { + override fun intercept(chain: Interceptor.Chain): Response { + val response: Response = chain.proceed(chain.request()) + val cacheControl = CacheControl.Builder() + .maxAge(10, TimeUnit.DAYS) + .build() + return response.newBuilder() + .header("Cache-Control", cacheControl.toString()) + .build() + } +} -class BookRepository { +class BookRepository(context: Context) { private val baseApiUrl = "https://myne.pooloftears.xyz/books" private val googleBooksUrl = "https://www.googleapis.com/books/v1/volumes" + + @Suppress("USELESS_ELVIS") private val googleApiKey = BuildConfig.GOOGLE_API_KEY ?: "AIzaSyBCaXx-U0sbEpGVPWylSggC4RaR4gCGkVE" // Backup API key - private val okHttpClient = OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .readTimeout(100, TimeUnit.SECONDS) + .cache(Cache(File(context.cacheDir, "http-cache"), 16L * 1024L * 1024L)) // 16 MiB + .addNetworkInterceptor(CacheInterceptor()) .build() private val gsonClient = Gson() + suspend fun getAllBooks( page: Long, bookLanguage: BookLanguage = BookLanguage.AllBooks diff --git a/app/src/main/java/com/starry/myne/ui/screens/home/composables/HomeScreen.kt b/app/src/main/java/com/starry/myne/ui/screens/home/composables/HomeScreen.kt index ad953fb6..9566fde2 100644 --- a/app/src/main/java/com/starry/myne/ui/screens/home/composables/HomeScreen.kt +++ b/app/src/main/java/com/starry/myne/ui/screens/home/composables/HomeScreen.kt @@ -226,7 +226,7 @@ fun HomeScreenScaffold( ) { Crossfade( targetState = topBarState.isSearchBarVisible, - animationSpec = tween(durationMillis = 200) + animationSpec = tween(durationMillis = 200), label = "search cross fade" ) { if (it) { SearchAppBar(onCloseIconClicked = { diff --git a/app/src/main/java/com/starry/myne/ui/screens/main/MainScreen.kt b/app/src/main/java/com/starry/myne/ui/screens/main/MainScreen.kt index f727a599..4eac7014 100644 --- a/app/src/main/java/com/starry/myne/ui/screens/main/MainScreen.kt +++ b/app/src/main/java/com/starry/myne/ui/screens/main/MainScreen.kt @@ -113,9 +113,8 @@ fun BottomBar( if (bottomBarDestination) { systemUiController.setNavigationBarColor( - color = (MaterialTheme.colorScheme.surfaceColorAtElevation( - 3.dp - )), darkIcons = settingsViewModel.getCurrentTheme() == ThemeMode.Light + color = MaterialTheme.colorScheme.surfaceColorAtElevation(3.dp), + darkIcons = settingsViewModel.getCurrentTheme() == ThemeMode.Light ) } else { systemUiController.setNavigationBarColor( @@ -145,7 +144,6 @@ fun BottomBar( ) { navController.navigate(screen.route) { popUpTo(navController.graph.findStartDestination().id) - launchSingleTop = true } } }