@@ -25,7 +25,11 @@ import java.util.Date
25
25
import javax.inject.Inject
26
26
import javax.inject.Singleton
27
27
import kotlin.coroutines.resume
28
+ import kotlin.time.Duration
29
+ import kotlin.time.Duration.Companion.convert
28
30
import kotlin.time.Duration.Companion.minutes
31
+ import kotlin.time.DurationUnit
32
+ import kotlin.time.ExperimentalTime
29
33
30
34
interface Exchange {
31
35
val localRate: Rate
@@ -41,7 +45,7 @@ interface Exchange {
41
45
fun rateForUsd (): Rate ?
42
46
}
43
47
44
- class ExchangeNull : Exchange {
48
+ class ExchangeNull : Exchange {
45
49
override val localRate: Rate
46
50
get() = Rate .oneToOne
47
51
@@ -73,7 +77,7 @@ class CodeExchange @Inject constructor(
73
77
private val currencyApi : CurrencyApi ,
74
78
private val networkOracle : NetworkOracle ,
75
79
private val defaultCurrency : () -> Currency ? ,
76
- ): Exchange, CoroutineScope by CoroutineScope(Dispatchers .IO ) {
80
+ ) : Exchange, CoroutineScope by CoroutineScope(Dispatchers .IO ) {
77
81
78
82
private val db = Database .getInstance()
79
83
@@ -168,7 +172,13 @@ class CodeExchange @Inject constructor(
168
172
val localCurrency = defaultCurrency()
169
173
val rate = localCurrency?.let { rates.rateFor(it) }
170
174
_localRate .value = if (rate != null ) {
171
- Timber .d(" Updated the entry currency: $localCurrency , Staleness ${System .currentTimeMillis() - rates.dateMillis} ms, Date: ${Date (rates.dateMillis)} " )
175
+ Timber .d(
176
+ " Updated the entry currency: $localCurrency , Staleness ${System .currentTimeMillis() - rates.dateMillis} ms, Date: ${
177
+ Date (
178
+ rates.dateMillis
179
+ )
180
+ } "
181
+ )
172
182
rate
173
183
} else {
174
184
Timber .d(" Rate for $localCurrency not found. Defaulting to USD." )
@@ -178,7 +188,13 @@ class CodeExchange @Inject constructor(
178
188
179
189
val entryRate = entryCurrency?.let { rates.rateFor(it) }
180
190
this .entryRate = if (entryRate != null ) {
181
- Timber .d(" Updated the entry currency: $entryCurrency , Staleness ${System .currentTimeMillis() - rates.dateMillis} ms, Date: ${Date (rates.dateMillis)} " )
191
+ Timber .d(
192
+ " Updated the entry currency: $entryCurrency , Staleness ${System .currentTimeMillis() - rates.dateMillis} ms, Date: ${
193
+ Date (
194
+ rates.dateMillis
195
+ )
196
+ } "
197
+ )
182
198
entryRate
183
199
} else {
184
200
Timber .d(" Rate for $entryCurrency not found. Defaulting to USD." )
@@ -187,6 +203,7 @@ class CodeExchange @Inject constructor(
187
203
188
204
}
189
205
206
+ @OptIn(ExperimentalTime ::class )
190
207
@SuppressLint(" CheckResult" )
191
208
private suspend fun fetchExchangeRates () = suspendCancellableCoroutine { cont ->
192
209
Timber .d(" fetching rates" )
@@ -201,7 +218,14 @@ class CodeExchange @Inject constructor(
201
218
if (rates.none { it.currency == CurrencyCode .KIN }) {
202
219
rates.add(Rate (fx = 1.0 , currency = CurrencyCode .KIN ))
203
220
}
204
- cont.resume(rates.toList() to System .currentTimeMillis())
221
+
222
+ cont.resume(
223
+ rates.toList() to convert(
224
+ value = response.asOf.seconds.toDouble(),
225
+ sourceUnit = DurationUnit .SECONDS ,
226
+ targetUnit = DurationUnit .MILLISECONDS
227
+ ).toLong()
228
+ )
205
229
}, {
206
230
ErrorUtils .handleError(it)
207
231
cont.resume(emptyList<Rate >() to System .currentTimeMillis())
@@ -212,7 +236,9 @@ class CodeExchange @Inject constructor(
212
236
}
213
237
214
238
private data class RatesBox (val dateMillis : Long , val rates : Map <CurrencyCode , Rate >) {
215
- constructor (dateMillis: Long , rates: List <Rate >): this (dateMillis, rates.associateBy { it.currency })
239
+ constructor (dateMillis: Long , rates: List <Rate >) : this (
240
+ dateMillis,
241
+ rates.associateBy { it.currency })
216
242
217
243
val isEmpty: Boolean
218
244
get() = rates.isEmpty()
0 commit comments