From b35192a6a913ed0a773a3dab43b122a79c80353a Mon Sep 17 00:00:00 2001 From: Artem Mochalov Date: Tue, 26 Dec 2023 16:37:08 +0000 Subject: [PATCH 1/2] Reload from network when LazyReload was used before --- .../rxdata/dod/DataObservableDelegate.kt | 8 +++- .../rxdata/dod/DataObservableDelegateTest.kt | 46 +++++++++++++++++++ 2 files changed, 53 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..154ba99 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 && loadingStrategy.refreshStorage) 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..0ad20b4 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,50 @@ 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)) + } + + @Test + fun `WHEN previous LoadingStrategy is LazyReload AND memory cache is empty THEN observe LoadingStrategy LazyReload will not reload from the network`() { + 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]) + + whenever(fromMemory.invoke(any())).thenReturn(cachedDomain) + + dataObservableDelegate.observe(params = params, loadingStrategy = LoadingStrategy.LazyReload) + .extractContent() + .test().apply { ioScheduler.triggerActions() } + .assertValues(cachedDomain) + + verify(fromNetwork, times(0)).invoke(eq(params)) + } } From 3c4578086fb2eab2091ae6954b7b25eb072c6e49 Mon Sep 17 00:00:00 2001 From: Artem Mochalov Date: Thu, 4 Jan 2024 11:38:07 +0000 Subject: [PATCH 2/2] rename the field --- .../java/com/revolut/rxdata/dod/DataObservableDelegate.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 154ba99..45e7658 100644 --- a/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt +++ b/dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt @@ -59,7 +59,7 @@ class DataObservableDelegate constructor( } .doAfterSuccess { domain -> failedNetworkRequests.remove(params) - needToBeRefreshed.remove(params) + memoryCacheIsFromStorage.remove(params) val data = Data(content = domain) subject(params).onNext(data) @@ -94,7 +94,7 @@ class DataObservableDelegate constructor( * even if forceReload == false and memory is not empty */ private val failedNetworkRequests = ConcurrentHashMap() - private val needToBeRefreshed = ConcurrentHashMap() + private val memoryCacheIsFromStorage = ConcurrentHashMap() /** * Requests data from network and subscribes to updates @@ -110,7 +110,7 @@ class DataObservableDelegate constructor( val loading = loadingStrategy.refreshMemory || memoryIsEmpty || failedNetworkRequests.containsKey(params) - || (needToBeRefreshed[params] == true && loadingStrategy.refreshStorage) + || (memoryCacheIsFromStorage[params] == true && loadingStrategy.refreshStorage) val observable: Observable> = if (memCache != null) { concat( @@ -129,7 +129,7 @@ class DataObservableDelegate constructor( val cachedObservable = if (needToFetchFromNetwork) { just(cached) } else { - needToBeRefreshed[params] = true + memoryCacheIsFromStorage[params] = true just(cached.copy(loading = false)) } concat(