-
Notifications
You must be signed in to change notification settings - Fork 416
/
StockPriceDataSource.kt
36 lines (29 loc) 路 1.08 KB
/
StockPriceDataSource.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.lukaslechner.coroutineusecasesonandroid.usecases.flow.usecase3
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.FlowMockApi
import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.Stock
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.retry
import retrofit2.HttpException
import timber.log.Timber
interface StockPriceDataSource {
val latestStockList: Flow<List<Stock>>
}
class NetworkStockPriceDataSource(mockApi: FlowMockApi) : StockPriceDataSource {
override val latestStockList: Flow<List<Stock>> = flow {
while (true) {
Timber.tag("Flow").d("Fetching current stock prices")
val currentStockList = mockApi.getCurrentStockPrices()
emit(currentStockList)
delay(5_000)
}
}.retry { cause ->
Timber.tag("Flow").d("Enter retry operator with $cause")
val shouldRetry = cause is HttpException
if (shouldRetry) {
delay(5_000)
}
shouldRetry
}
}