Skip to content

Commit

Permalink
Add response caching and pop homescreen for backstack (#115)
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Feb 18, 2024
1 parent 7df7750 commit a2ca9f1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/starry/myne/di/MainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class MainModule {

@Singleton
@Provides
fun provideBooksApi() = BookRepository()
fun provideBooksApi(@ApplicationContext context: Context) = BookRepository(context)

@Singleton
@Provides
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/com/starry/myne/repo/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,64 @@

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
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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -145,7 +144,6 @@ fun BottomBar(
) {
navController.navigate(screen.route) {
popUpTo(navController.graph.findStartDestination().id)
launchSingleTop = true
}
}
}
Expand Down

0 comments on commit a2ca9f1

Please sign in to comment.