Skip to content

Commit

Permalink
[nyoman] add cache retrofit
Browse files Browse the repository at this point in the history
  • Loading branch information
inyomanw committed Apr 11, 2019
1 parent c08097a commit 812fc48
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion corelibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {

buildTypes {
release {
minifyEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.inyomanw.corelibrary.deps

import android.content.Context
import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.internal.bind.DateTypeAdapter
import dagger.Module
import dagger.Provides
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
Expand All @@ -16,7 +18,15 @@ import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
class NetworkModule(private val baseUrl: String) {
class NetworkModule(private val baseUrl: String, private val context : Context) {

companion object {
const val REQUEST_TIMEOUT = 20
const val CACHE_DIR_SIZE_30MB = 30 * 1024 * 1024
const val KEEP_ALIVE_DURATION = (30 * 1000).toLong()
const val MAX_IDLE_CONNECTIONS = 10
}

@Provides
@Singleton
fun providesGson(): Gson {
Expand All @@ -34,9 +44,17 @@ class NetworkModule(private val baseUrl: String) {

@Provides
@Singleton
fun providesOkHttpClient(loggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
fun providesCache() : Cache {
return Cache(context.externalCacheDir ?: context.cacheDir, CACHE_DIR_SIZE_30MB.toLong())
}

@Provides
@Singleton
fun providesOkHttpClient(loggingInterceptor: HttpLoggingInterceptor,
cache: Cache): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(cache)
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
Expand Down
19 changes: 19 additions & 0 deletions corelibrary/src/main/java/com/inyomanw/corelibrary/utils/Ext.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.inyomanw.corelibrary.utils

import android.app.Activity
import android.content.Context
import android.support.v7.app.AlertDialog
import android.view.View
import android.widget.EditText
import android.widget.ImageView
Expand Down Expand Up @@ -127,4 +129,21 @@ fun String.changeDateFormatWithoutTimeZone(oldPattern: String, newPattern: Strin
e.printStackTrace()
}
return res
}

fun Activity.showDialog(message : String, cancelable : Boolean = false,
positiveButton : String, positiveAction: () -> Unit = {},
negativeButton : String, negativeAction : () -> Unit = {}) {
val dialogBuilder = AlertDialog.Builder(this).apply {
setMessage(message)
setCancelable(cancelable)
setPositiveButton(positiveButton) { dialog, _ ->
positiveAction()
dialog.dismiss()
}
setNegativeButton(negativeButton) { dialog, which ->
negativeAction()
dialog.dismiss()
}
}
}
4 changes: 0 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
org.gradle.daemon=false
org.gradle.parallel=true
org.gradle.configureondemand=true
android.enableBuildScriptClasspathCheck=false
kotlin.code.style=official

0 comments on commit 812fc48

Please sign in to comment.