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

Coroutines + Flow #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It is fully customisable and supports compression / encryption.
What sets it apart is that it is designed to work with as little setup as possible and does not require any refactoring of your existing code.
You can start caching your Retrofit calls simply by adding an annotation to the existing client's methods without needing to change their signature or any call handling code.

Alternatively, you can leave your clients' code entirely untouched and decide which call to cache be implementing a cache predicate which will intercept any request and let you decide ad hoc caching rules before the network call is made.
Alternatively, you can leave your clients' code entirely untouched and decide which call to cache be implementing a operation mapper which will intercept any request and let you decide ad hoc caching rules before the network call is made.
All requests are cached uniquely based on the query parameters and response model class. The cache can also be invalidated or cleared on a per request basis (taking the original parameters into account for request uniqueness).

This library's goal is to introduce no side effect to the existing code and it was designed to be added or removed completely transparently.
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ android {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/app_release.kotlin_module'
exclude 'META-INF/core_release.kotlin_module'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.Op
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.Operation.Local
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.Operation.Remote
import dev.pthomain.android.dejavu.demo.dejavu.clients.model.CatFactResponse
import dev.pthomain.android.dejavu.demo.presenter.base.CompositePresenter
import dev.pthomain.android.dejavu.demo.presenter.base.CompositePresenter.*
import dev.pthomain.android.glitchy.core.interceptor.error.glitch.Glitch
import dev.pthomain.android.dejavu.demo.presenter.base.CompositePresenter.Method
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.glitch.Glitch
import java.text.SimpleDateFormat
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package dev.pthomain.android.dejavu.demo.dejavu.clients.factories

import android.content.Context
import android.os.Build.VERSION.SDK_INT
import dev.pthomain.android.boilerplate.core.builder.ExtensionBuilder
import dev.pthomain.android.boilerplate.core.utils.log.Logger
import dev.pthomain.android.dejavu.DejaVu
import dev.pthomain.android.dejavu.demo.dejavu.DejaVuRetrofitClient
Expand All @@ -38,8 +39,9 @@ import dev.pthomain.android.dejavu.serialisation.Serialiser
import dev.pthomain.android.dejavu.serialisation.compression.Compression
import dev.pthomain.android.dejavu.serialisation.encryption.Encryption
import dev.pthomain.android.dejavu.volley.DejaVuVolley
import dev.pthomain.android.glitchy.core.interceptor.error.NetworkErrorPredicate
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.NetworkErrorPredicate
import dev.pthomain.android.mumbo.Mumbo
import org.koin.core.module.Module

class DejaVuFactory(
private val logger: Logger,
Expand All @@ -62,102 +64,68 @@ class DejaVuFactory(

private fun persistenceModuleProvider(
persistence: PersistenceType,
serialiser: Serialiser
) =
when (persistence) {
FILE -> filePersistenceModule(serialiser)
MEMORY -> memoryPersistenceModule(serialiser)
SQLITE -> sqlitePersistenceModule(serialiser)
}

private fun filePersistenceModule(serialiser: Serialiser) = FilePersistence(
decorators,
serialiser
)

private fun memoryPersistenceModule(serialiser: Serialiser) = MemoryPersistence(
decorators,
serialiser
)

private fun sqlitePersistenceModule(serialiser: Serialiser) = SqlitePersistence(
decorators,
serialiser
)
serialiser: Serialiser,
) = when (persistence) {
FILE -> FilePersistence(decorators, serialiser)
MEMORY -> MemoryPersistence(decorators, serialiser)
SQLITE -> SqlitePersistence(decorators, serialiser)
}

enum class PersistenceType {
FILE,
MEMORY,
SQLITE
}

private fun <E> dejaVuBuilder(
persistence: PersistenceType,
serialiserType: SerialiserType,
errorFactoryType: ErrorFactoryType<E>
) where E : Throwable,
E : NetworkErrorPredicate =
DejaVu.builder(
context,
errorFactoryType.errorFactory,
persistenceModuleProvider(
persistence,
serialiserType.serialiser
),
logger
)

private fun <E> dejaVuRetrofit(
persistence: PersistenceType,
serialiserType: SerialiserType,
errorFactoryType: ErrorFactoryType<E>
) where E : Throwable,
E : NetworkErrorPredicate =
dejaVuBuilder(
persistence,
serialiserType,
errorFactoryType
).extend(DejaVuRetrofit.extension<E>()).build()

private fun <E> dejaVuVolley(
persistence: PersistenceType,
serialiserType: SerialiserType,
errorFactoryType: ErrorFactoryType<E>
class DejaVuDependencies<E>(
val persistence: PersistenceType,
val serialiserType: SerialiserType,
val errorFactoryType: ErrorFactoryType<E>,
) where E : Throwable,
E : NetworkErrorPredicate =
dejaVuBuilder(
persistence,
serialiserType,
errorFactoryType
).extend(DejaVuVolley.extension<E>()).build()
E : NetworkErrorPredicate

private fun <E> dejaVuBuilder(dependencies: DejaVuDependencies<E>)
where E : Throwable,
E : NetworkErrorPredicate = with(dependencies) {
DejaVu.builder(
context,
errorFactoryType.errorFactory,
persistenceModuleProvider(
persistence,
serialiserType.serialiser
),
logger
)
}

fun <E> createRetrofit(
persistence: PersistenceType,
serialiserType: SerialiserType,
errorFactoryType: ErrorFactoryType<E>
fun <E, C, B : ExtensionBuilder<D, Module, B>, D> createClient(
dependencies: DejaVuDependencies<E>,
extensionBuilder: B,
clientBuilder: (D, Logger) -> C,
) where E : Throwable,
E : NetworkErrorPredicate =
DejaVuRetrofitClient(
dejaVuRetrofit(
persistence,
serialiserType,
errorFactoryType
),
clientBuilder(
dejaVuBuilder(dependencies)
.extend(extensionBuilder)
.build(),
logger
)

fun <E> createVolley(
persistence: PersistenceType,
serialiserType: SerialiserType,
errorFactoryType: ErrorFactoryType<E>
) where E : Throwable,
E : NetworkErrorPredicate =
DejaVuVolleyClient(
dejaVuVolley(
persistence,
serialiserType,
errorFactoryType
),
logger
fun <E> createRetrofit(dependencies: DejaVuDependencies<E>)
where E : Throwable,
E : NetworkErrorPredicate =
createClient(
dependencies,
DejaVuRetrofit.extension<E>(),
::DejaVuRetrofitClient
)

fun <E> createVolley(dependencies: DejaVuDependencies<E>)
where E : Throwable,
E : NetworkErrorPredicate =
createClient(
dependencies,
DejaVuVolley.extension<E>(),
::DejaVuVolleyClient
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package dev.pthomain.android.dejavu.demo.dejavu.clients.factories
import dev.pthomain.android.dejavu.demo.dejavu.error.CustomApiError
import dev.pthomain.android.dejavu.demo.dejavu.error.CustomApiErrorFactory
import dev.pthomain.android.dejavu.serialisation.gson.GsonGlitchFactory
import dev.pthomain.android.glitchy.core.interceptor.error.ErrorFactory
import dev.pthomain.android.glitchy.core.interceptor.error.NetworkErrorPredicate
import dev.pthomain.android.glitchy.core.interceptor.error.glitch.Glitch
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.ErrorFactory
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.NetworkErrorPredicate
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.glitch.Glitch

sealed class ErrorFactoryType<E>(val errorFactory: ErrorFactory<E>)
where E : Throwable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package dev.pthomain.android.dejavu.demo.dejavu.error

import dev.pthomain.android.boilerplate.core.utils.kotlin.ifElse
import dev.pthomain.android.glitchy.core.interceptor.error.NetworkErrorPredicate
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.NetworkErrorPredicate
import java.io.IOException

class CustomApiError(override val cause: Throwable) : Throwable(), NetworkErrorPredicate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

package dev.pthomain.android.dejavu.demo.dejavu.error

import dev.pthomain.android.glitchy.core.interceptor.error.ErrorFactory
import dev.pthomain.android.glitchy.core.interceptor.outcome.Outcome
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.ErrorFactory
import dev.pthomain.android.glitchy.core.interceptor.interceptors.outcome.Outcome

class CustomApiErrorFactory : ErrorFactory<CustomApiError> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package dev.pthomain.android.dejavu.demo.presenter.retrofit
import dev.pthomain.android.boilerplate.core.utils.log.Logger
import dev.pthomain.android.dejavu.demo.DemoActivity
import dev.pthomain.android.dejavu.demo.dejavu.DejaVuRetrofitClient
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.DejaVuFactory.DejaVuDependencies
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.DejaVuFactory.PersistenceType
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.ErrorFactoryType
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.ErrorFactoryType.Custom
Expand All @@ -34,7 +35,7 @@ import dev.pthomain.android.dejavu.demo.dejavu.clients.retrofit.RetrofitObservab
import dev.pthomain.android.dejavu.demo.dejavu.clients.retrofit.RetrofitSingleClients
import dev.pthomain.android.dejavu.demo.dejavu.error.CustomApiError
import dev.pthomain.android.dejavu.demo.presenter.base.BaseDemoPresenter
import dev.pthomain.android.glitchy.core.interceptor.error.glitch.Glitch
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.glitch.Glitch

internal abstract class BaseRetrofitDemoPresenter(
demoActivity: DemoActivity,
Expand All @@ -47,15 +48,19 @@ internal abstract class BaseRetrofitDemoPresenter(
@Suppress("UNCHECKED_CAST")
override fun newClient(persistence: PersistenceType) = when (errorFactoryType) {
Default -> dejaVuFactory.createRetrofit(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<Glitch>
DejaVuDependencies(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<Glitch>
)
)

Custom -> dejaVuFactory.createRetrofit(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<CustomApiError>
DejaVuDependencies(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<CustomApiError>
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import com.android.volley.toolbox.BasicNetwork
import com.android.volley.toolbox.HurlStack
import com.android.volley.toolbox.NoCache
import dev.pthomain.android.boilerplate.core.utils.log.Logger
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.PlainRequestMetadata
import dev.pthomain.android.dejavu.cache.metadata.response.DejaVuResult
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.CachePriority
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.CachePriority.FreshnessPriority
import dev.pthomain.android.dejavu.cache.metadata.token.instruction.operation.Operation
import dev.pthomain.android.dejavu.demo.DemoActivity
import dev.pthomain.android.dejavu.demo.dejavu.DejaVuVolleyClient
import dev.pthomain.android.dejavu.demo.dejavu.clients.base.ObservableClients
import dev.pthomain.android.dejavu.demo.dejavu.clients.base.SingleClients
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.DejaVuFactory.DejaVuDependencies
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.DejaVuFactory.PersistenceType
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.ErrorFactoryType
import dev.pthomain.android.dejavu.demo.dejavu.clients.factories.ErrorFactoryType.Custom
Expand All @@ -21,7 +22,8 @@ import dev.pthomain.android.dejavu.demo.dejavu.clients.model.CatFactResponse
import dev.pthomain.android.dejavu.demo.dejavu.error.CustomApiError
import dev.pthomain.android.dejavu.demo.presenter.base.BaseDemoPresenter
import dev.pthomain.android.dejavu.demo.presenter.base.OperationPresenterDelegate
import dev.pthomain.android.glitchy.core.interceptor.error.glitch.Glitch
import dev.pthomain.android.glitchy.core.interceptor.interceptors.error.glitch.Glitch
import io.reactivex.Observable

internal class VolleyPresenter(
demoActivity: DemoActivity,
Expand All @@ -41,15 +43,19 @@ internal class VolleyPresenter(
@Suppress("UNCHECKED_CAST")
override fun newClient(persistence: PersistenceType) = when (errorFactoryType) {
Default -> dejaVuFactory.createVolley(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<Glitch>
DejaVuDependencies(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<Glitch>
)
)

Custom -> dejaVuFactory.createVolley(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<CustomApiError>
DejaVuDependencies(
persistence,
serialiserType,
errorFactoryType as ErrorFactoryType<CustomApiError>
)
)
}

Expand All @@ -69,13 +75,15 @@ internal class VolleyPresenter(
override fun getInvalidateResult() =
delegate.getInvalidateResult()

private fun executeOperation(cacheOperation: Operation) =
dejaVuClient.observableFactory.createResult(
queue,
cacheOperation,
PlainRequestMetadata(
CatFactResponse::class.java,
BASE_URL + ENDPOINT
)
)
//FIXME
private fun executeOperation(cacheOperation: Operation): Observable<DejaVuResult<CatFactResponse>> =
Observable.error(NotImplementedError("Volley is not currently supported"))
// dejaVuClient.observableFactory.createResult(
// queue,
// cacheOperation,
// PlainRequestMetadata(
// CatFactResponse::class.java,
// BASE_URL + ENDPOINT
// )
// )
}
Loading