From 905c2a6df14b024cc6aeef9faa68244ee9167b1f Mon Sep 17 00:00:00 2001 From: Artem Mochalov Date: Tue, 26 Dec 2023 16:37:08 +0000 Subject: [PATCH] Reload from network when LazyReload was used before --- .../rxdata/dod/DataObservableDelegate.kt | 8 ++++++- .../rxdata/dod/DataObservableDelegateTest.kt | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt b/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt index f9aa24d..8d9ecc9 100644 --- a/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt +++ b/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt @@ -59,6 +59,7 @@ class DataObservableDelegate constructor( } .doAfterSuccess { domain -> failedNetworkRequests.remove(params) + needToBeRefreshed.remove(params) val data = Data(content = domain) subject(params).onNext(data) @@ -93,6 +94,7 @@ class DataObservableDelegate constructor( * even if forceReload == false and memory is not empty */ private val failedNetworkRequests = ConcurrentHashMap() + private val needToBeRefreshed = ConcurrentHashMap() /** * Requests data from network and subscribes to updates @@ -105,7 +107,10 @@ class DataObservableDelegate constructor( val memCache = fromMemory(params) val memoryIsEmpty = memCache == null val subject = subject(params) - val loading = loadingStrategy.refreshMemory || memoryIsEmpty || failedNetworkRequests.containsKey(params) + val loading = loadingStrategy.refreshMemory + || memoryIsEmpty + || failedNetworkRequests.containsKey(params) + || needToBeRefreshed[params] == true val observable: Observable> = if (memCache != null) { concat( @@ -124,6 +129,7 @@ class DataObservableDelegate constructor( val cachedObservable = if (needToFetchFromNetwork) { just(cached) } else { + needToBeRefreshed[params] = true just(cached.copy(loading = false)) } concat( diff --git a/dod/src/test/java/com/revolut/rxdata/dod/DataObservableDelegateTest.kt b/dod/src/test/java/com/revolut/rxdata/dod/DataObservableDelegateTest.kt index 4b2e0a9..4d964d4 100644 --- a/dod/src/test/java/com/revolut/rxdata/dod/DataObservableDelegateTest.kt +++ b/dod/src/test/java/com/revolut/rxdata/dod/DataObservableDelegateTest.kt @@ -893,4 +893,28 @@ class DataObservableDelegateTest : BaseDataObservableDelegateTest() { verify(fromStorage, only()).invoke(eq(params)) verify(toStorage, only()).invoke(eq(params), eq(domain)) } + + @Test + fun `WHEN previous LoadingStrategy is LazyReload AND memory cache is empty THEN observe LoadingStrategy Auto will reload`() { + whenever(fromMemory.invoke(any())).thenReturn(null) + storage[params] = cachedDomain + + dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.LazyReload) + .extractContent() + .test().apply { ioScheduler.triggerActions() } + .assertValues(cachedDomain) + + assertEquals(cachedDomain, memCache[params]) + + verify(fromNetwork, times(0)).invoke(eq(params)) + + whenever(fromNetwork.invoke(eq(params))).thenReturn(Single.fromCallable { domain }) + + dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.Auto) + .extractContent() + .test().apply { ioScheduler.triggerActions() } + .assertValues(cachedDomain, domain) + + verify(fromNetwork, times(1)).invoke(eq(params)) + } }