Skip to content

Commit

Permalink
Introduce LoadingStrategy for the DataObservableDelegateWrapper (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Mochalov <artyom.mochalov@revolut.com>
  • Loading branch information
artyomychsvc and Artem Mochalov authored Dec 14, 2023
1 parent 94e2187 commit 36792cb
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 23 deletions.
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.14'
implementation 'com.revolut.rxdata:core:1.5.14'
implementation 'com.revolut.rxdata:scheduler:1.5.14'
implementation 'com.revolut.rxdata:dod:1.5.15'
implementation 'com.revolut.rxdata:core:1.5.15'
implementation 'com.revolut.rxdata:scheduler:1.5.15'
}
```

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.14
VERSION_NAME=1.5.15
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.14
VERSION_NAME=1.5.15
POM_NAME=dod-wrapper
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revolut.rxdata.dod_wrapper

import com.revolut.data.model.Data
import com.revolut.rxdata.dod.DataObservableDelegate
import com.revolut.rxdata.dod.LoadingStrategy
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.rx2.asFlow
Expand Down Expand Up @@ -63,10 +64,17 @@ class DataObservableDelegateWrapper<Params : Any, Domain : Any>(
}
)

@Deprecated(
message = "please migrate to the method with the LoadingStrategy",
replaceWith = ReplaceWith("fun observe(params: Params, loadingStrategy: LoadingStrategy)")
)
fun observe(params: Params, forceReload: Boolean = true): Flow<Data<Domain>> =
observe(params, loadingStrategy = if (forceReload) LoadingStrategy.ForceReload else LoadingStrategy.Auto)

fun observe(params: Params, loadingStrategy: LoadingStrategy): Flow<Data<Domain>> =
inner.observe(
params = params,
forceReload = forceReload,
loadingStrategy = loadingStrategy,
).asFlow()

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revolut.rxdata.dod_wrapper

import app.cash.turbine.test
import com.revolut.data.model.Data
import com.revolut.rxdata.dod.LoadingStrategy
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions.assertEquals
Expand Down Expand Up @@ -42,7 +43,7 @@ class DataObservableDelegateWrapperTest {
fun `GIVEN no data in memory and storage WHEN observe THEN return loading and network data`() = runTest {
val delegate = createDelegate()

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(loading = true)
awaitItem() shouldBe Data(content = networkResult)
}
Expand All @@ -56,7 +57,7 @@ class DataObservableDelegateWrapperTest {
fromStorage = { throw IOException("No data") },
)

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(loading = true)
awaitItem() shouldBe Data(error = IOException("No data"), loading = true)
awaitItem() shouldBe Data(content = networkResult)
Expand All @@ -70,7 +71,7 @@ class DataObservableDelegateWrapperTest {
storage[param] = storageResult
val delegate = createDelegate()

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(loading = true)
awaitItem() shouldBe Data(content = storageResult, loading = true)
awaitItem() shouldBe Data(content = networkResult)
Expand All @@ -84,7 +85,7 @@ class DataObservableDelegateWrapperTest {
memoryCache[param] = memoryResult
val delegate = createDelegate()

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
}
}
Expand All @@ -94,7 +95,7 @@ class DataObservableDelegateWrapperTest {
memoryCache[param] = memoryResult
val delegate = createDelegate()

delegate.observe(params = param, forceReload = true).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.ForceReload).test {
awaitItem() shouldBe Data(content = memoryResult, loading = true)
awaitItem() shouldBe Data(content = networkResult)
}
Expand All @@ -107,7 +108,7 @@ class DataObservableDelegateWrapperTest {
fromNetwork = { throw IOException("Network error") },
)

delegate.observe(params = param, forceReload = true).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.ForceReload).test {
awaitItem() shouldBe Data(content = memoryResult, loading = true)
awaitItem() shouldBe Data(content = memoryResult, error = IOException("Network error"))
}
Expand All @@ -119,7 +120,7 @@ class DataObservableDelegateWrapperTest {
val delegate = createDelegate()
val updatedValue = "Updated value"

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
delegate.updateAll(param, updatedValue)
awaitItem() shouldBe Data(content = updatedValue)
Expand All @@ -134,7 +135,7 @@ class DataObservableDelegateWrapperTest {
val delegate = createDelegate()
val updatedValue = "Updated value"

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
delegate.updateMemory(param, updatedValue)
awaitItem() shouldBe Data(content = updatedValue)
Expand All @@ -149,7 +150,7 @@ class DataObservableDelegateWrapperTest {
val delegate = createDelegate()
val updatedValue = "Updated value"

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
delegate.updateStorage(param, updatedValue)
awaitItem() shouldBe Data(content = updatedValue)
Expand All @@ -164,7 +165,7 @@ class DataObservableDelegateWrapperTest {
val delegate = createDelegate()
val updatedValue = "Updated value"

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
memoryCache[param] = updatedValue
delegate.notifyFromMemory { true }
Expand All @@ -178,7 +179,7 @@ class DataObservableDelegateWrapperTest {
storage[param] = storageResult
val delegate = createDelegate()

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
delegate.remove(param)
awaitItem() shouldBe Data(content = null)
Expand All @@ -193,7 +194,7 @@ class DataObservableDelegateWrapperTest {
storage[param] = storageResult
val delegate = createDelegate()

delegate.observe(params = param, forceReload = false).test {
delegate.observe(params = param, loadingStrategy = LoadingStrategy.Auto).test {
awaitItem() shouldBe Data(content = memoryResult)
delegate.reload(param)
awaitItem() shouldBe Data(content = memoryResult, loading = true)
Expand Down
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.14
VERSION_NAME=1.5.15
POM_NAME=dod
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
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.14
VERSION_NAME=1.5.15
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.14
VERSION_NAME=1.5.15
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.14
VERSION_NAME=1.5.15
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.14
VERSION_NAME=1.5.15
POM_NAME=scheduler
POM_PACKAGING=aar
GROUP=com.revolut.rxdata

0 comments on commit 36792cb

Please sign in to comment.