Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response caching and pop homescreen for backstack #115

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading