Skip to content

Commit

Permalink
Reload from network when LazyReload was used before
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Mochalov committed Jan 4, 2024
1 parent 36792cb commit b35192a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
}
.doAfterSuccess { domain ->
failedNetworkRequests.remove(params)
needToBeRefreshed.remove(params)

val data = Data(content = domain)
subject(params).onNext(data)
Expand Down Expand Up @@ -93,6 +94,7 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
* even if forceReload == false and memory is not empty
*/
private val failedNetworkRequests = ConcurrentHashMap<Params, Throwable>()
private val needToBeRefreshed = ConcurrentHashMap<Params, Boolean>()

/**
* Requests data from network and subscribes to updates
Expand All @@ -105,7 +107,10 @@ 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)
val loading = loadingStrategy.refreshMemory
|| memoryIsEmpty
|| failedNetworkRequests.containsKey(params)
|| (needToBeRefreshed[params] == true && loadingStrategy.refreshStorage)

val observable: Observable<Data<Domain>> = if (memCache != null) {
concat(
Expand All @@ -124,6 +129,7 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
val cachedObservable = if (needToFetchFromNetwork) {
just(cached)
} else {
needToBeRefreshed[params] = true
just(cached.copy(loading = false))
}
concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit b35192a

Please sign in to comment.