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

Introduce LoadingStrategy.CacheOnly #48

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ And then to the module level `build.gradle.kts`:

```
dependencies {
implementation 'com.revolut.rxdata:dod:1.5.16'
implementation 'com.revolut.rxdata:core:1.5.16'
implementation 'com.revolut.rxdata:scheduler:1.5.16'
implementation 'com.revolut.rxdata:dod:1.5.17'
implementation 'com.revolut.rxdata:core:1.5.17'
implementation 'com.revolut.rxdata:scheduler:1.5.17'
}
```

Expand Down
2 changes: 1 addition & 1 deletion dfd/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dfd
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=dfd
POM_PACKAGING=jar
GROUP=com.revolut.flowdata
2 changes: 1 addition & 1 deletion dod-wrapper/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dod-wrapper
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=dod-wrapper
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
2 changes: 1 addition & 1 deletion dod/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dod
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=dod
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
19 changes: 14 additions & 5 deletions dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
val memCache = fromMemory(params)
val memoryIsEmpty = memCache == null
val subject = subject(params)
val loading = loadingStrategy.refreshMemory
|| memoryIsEmpty
|| failedNetworkRequests.containsKey(params)
|| (memoryCacheIsFromStorage[params] == true && loadingStrategy.refreshStorage)
val hasFailedNetworkRequest = failedNetworkRequests.containsKey(params)
val memCacheFromStorage = memoryCacheIsFromStorage[params] == true
val loading = when (loadingStrategy) {
LoadingStrategy.Auto -> memoryIsEmpty || memCacheFromStorage || hasFailedNetworkRequest
LoadingStrategy.CacheOnly -> false
LoadingStrategy.ForceReload -> true
LoadingStrategy.LazyReload -> memoryIsEmpty || hasFailedNetworkRequest
}

val observable: Observable<Data<Domain>> = if (memCache != null) {
concat(
Expand All @@ -125,7 +129,12 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
} else {
sharedStorageRequest.getOrLoad(params)
.flatMapObservable { cached ->
val needToFetchFromNetwork = loadingStrategy.refreshStorage || cached.content == null
val needToFetchFromNetwork = when (loadingStrategy) {
LoadingStrategy.Auto -> true
LoadingStrategy.CacheOnly -> false
LoadingStrategy.ForceReload -> true
LoadingStrategy.LazyReload -> cached.content == null
}
val cachedObservable = if (needToFetchFromNetwork) {
just(cached)
} else {
Expand Down
29 changes: 10 additions & 19 deletions dod/src/main/java/com/revolut/rxdata/dod/LoadingStrategy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@ package com.revolut.rxdata.dod

/**
* [DataObservableDelegate] observation loading strategy
*
* @param refreshMemory - if true data will be fetched from network even if there is something in the memory
* @param refreshStorage - if true data will be fetched from network even if there is something in the storage
*/
sealed class LoadingStrategy(
internal val refreshMemory: Boolean,
internal val refreshStorage: Boolean,
) {
sealed class LoadingStrategy {

/**
* data will be fetched from the Network even if the data exists in the cache
*/
object ForceReload: LoadingStrategy(
refreshMemory = true,
refreshStorage = true,
)
object ForceReload: LoadingStrategy()

/**
* data will not be fetched from the Network if the data exists in the memory cache
*/
object Auto: LoadingStrategy(
refreshMemory = false,
refreshStorage = true,
)
object Auto: LoadingStrategy()

/**
* data will not be fetched from the Network if the data exists in the cache memory or storage
*/
object LazyReload: LoadingStrategy(
refreshMemory = false,
refreshStorage = false,
)
object LazyReload: LoadingStrategy()

/**
* data will not be fetched from the Network even if there is no data in the cache
*/
object CacheOnly : LoadingStrategy()
}
Original file line number Diff line number Diff line change
Expand Up @@ -939,4 +939,58 @@ class DataObservableDelegateTest : BaseDataObservableDelegateTest() {

verify(fromNetwork, times(0)).invoke(eq(params))
}

@Test
fun `WHEN fromMemory has value AND LoadingStrategy is CacheOnly THEN subscriber receives memory value AND network request not triggered `() {
memCache[params] = cachedDomain

val testObserver =
dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.CacheOnly).test()

testObserver.assertValueCount(1)
testObserver.assertValueAt(0, Data(content = cachedDomain, error = null, loading = false))

ioScheduler.triggerActions()

verifyNoMoreInteractions(fromNetwork)
}

@Test
fun `WHEN fromMemory returns null, fromStorage has value AND LoadingStrategy is CacheOnly THEN subscriber receives storage value AND network request not triggered `() {
whenever(fromMemory.invoke(eq(params))).thenReturn(null)
storage[params] = cachedDomain

val testObserver =
dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.CacheOnly).test()

testObserver.assertValueCount(1)
testObserver.assertValueAt(0, Data(content = null, error = null, loading = true))
ioScheduler.triggerActions()

testObserver.assertValueCount(2)
testObserver.assertValueAt(1, Data(content = cachedDomain, error = null, loading = false))

ioScheduler.triggerActions()

verifyNoMoreInteractions(fromNetwork)
}

@Test
fun `WHEN fromMemory returns null, fromStorage returns null AND LoadingStrategy is CacheOnly THEN subscriber does not receive value AND network request not triggered`() {
whenever(fromNetwork.invoke(eq(params))).thenReturn(null)
whenever(fromMemory.invoke(eq(params))).thenReturn(null)
val testObserver =
dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.CacheOnly).test()

testObserver.assertValueCount(1)
testObserver.assertValueAt(0, Data(content = null, error = null, loading = true))
ioScheduler.triggerActions()

testObserver.assertValueCount(2)
testObserver.assertValueAt(1, Data(content = null, error = null, loading = false))

ioScheduler.triggerActions()

verifyNoMoreInteractions(fromNetwork)
}
}
2 changes: 1 addition & 1 deletion flow-extensions/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=extensions
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=flow-core
POM_PACKAGING=jar
GROUP=com.revolut.flowdata
2 changes: 1 addition & 1 deletion model/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=model
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=data
POM_PACKAGING=jar
GROUP=com.revolut.data
2 changes: 1 addition & 1 deletion rx-extensions/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=extensions
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=core
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
2 changes: 1 addition & 1 deletion scheduler/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=scheduler
VERSION_NAME=1.5.16
VERSION_NAME=1.5.17
POM_NAME=scheduler
POM_PACKAGING=aar
GROUP=com.revolut.rxdata
Loading