Skip to content

Commit

Permalink
improved cache init api by adding size to the same builder call
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyad-37 committed Nov 18, 2018
1 parent 5dfca42 commit ab0c8c4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fun createDataService(context: Context, db: AppDatabase): IDataService {
return DataServiceFactory(DataServiceConfig.Builder(context)
.baseUrl(API_BASE_URL)
.okHttpBuilder(getOkHttpBuilder())
.withSQLite(object : DataBaseManagerUtil {
.withRoom(object : DataBaseManagerUtil {
override fun getDataBaseManager(dataClass: Class<*>): DataBaseManager? {
return RoomManager(db, object : DaoResolver {
override fun <E> getDao(dataClass: Class<E>): BaseDao<E> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,15 @@ data class DataServiceConfig internal constructor(val context: Context,
return this
}

fun withCache(expiryAmount: Long, timeUnit: TimeUnit): Builder {
fun withCache(expiryAmount: Long, timeUnit: TimeUnit, cacheSize: Int = 0): Builder {
this.withCache = true
this.cacheDuration = expiryAmount
this.timeUnit = timeUnit
return this
}

fun cacheSize(cacheSize: Int): Builder {
this.cacheSize = cacheSize
return this
}

fun withSQLite(dataBaseManagerUtil: DataBaseManagerUtil): Builder {
fun withRoom(dataBaseManagerUtil: DataBaseManagerUtil): Builder {
this.dataBaseManagerUtil = dataBaseManagerUtil
this.withSQLite = true
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ class DataServiceConfigTest {
cache = Cache(File("", "http-cache"), (10 * 1024 * 1024).toLong())
mDataServiceConfig = DataServiceConfig.Builder(mockContext)
.baseUrl(URL)
.cacheSize(cacheSize)
.okHttpBuilder(builder)
.okHttpCache(cache)
.withCache(3, TimeUnit.MINUTES)
.withSQLite(object : DataBaseManagerUtil {
.withCache(3, TimeUnit.MINUTES, cacheSize)
.withRoom(object : DataBaseManagerUtil {
override fun getDataBaseManager(dataClass: Class<*>): DataBaseManager? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.content.Context
import android.support.test.rule.BuildConfig
import com.zeyad.usecases.db.DataBaseManager
import com.zeyad.usecases.utils.DataBaseManagerUtil
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import okhttp3.Cache
import okhttp3.OkHttpClient
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -24,7 +24,6 @@ import java.util.concurrent.TimeUnit
@Config(constants = BuildConfig::class, sdk = [25])
class DataServiceFactoryTest {
private val URL = "https://api.github.com/"
private val cacheSize = 8192
private lateinit var mDataServiceConfig: DataServiceConfig
private lateinit var mockContext: Context
private lateinit var builder: OkHttpClient.Builder
Expand All @@ -43,10 +42,9 @@ class DataServiceFactoryTest {
(10 * 1024 * 1024).toLong())
mDataServiceConfig = DataServiceConfig.Builder(mockContext)
.baseUrl(URL)
.cacheSize(cacheSize)
.okHttpBuilder(builder)
.okHttpCache(cache)
.withSQLite(object : DataBaseManagerUtil {
.withRoom(object : DataBaseManagerUtil {
override fun getDataBaseManager(dataClass: Class<*>): DataBaseManager? {
// RoomManager()
return null
Expand Down

0 comments on commit ab0c8c4

Please sign in to comment.