Skip to content

Commit a332f39

Browse files
committed
Merge branch 'develop'
2 parents ef56bb3 + aea2445 commit a332f39

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

api/src/main/java/com/getcode/network/client/TransactionReceiver.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,32 @@ class TransactionReceiver @Inject constructor(
5959
}
6060
}
6161

62-
fun receiveFromRelationship( organizer: Organizer, limit: Kin? = null): Kin {
62+
fun receiveFromRelationship(organizer: Organizer, limit: Kin? = null): Kin {
6363
var receivedTotal = Kin.fromKin(0)
6464

6565
run loop@{
6666
organizer.relationshipsLargestFirst().onEach { relationship ->
6767
Timber.d("Receiving from relationships: ${relationship.partialBalance}")
6868

69-
val intent = transactionRepository.receiveFromRelationship(
70-
relationship.domain, relationship.partialBalance, organizer
71-
).blockingGet()
69+
if (relationship.partialBalance > 0) {
70+
// Ignore empty relationship accounts
71+
} else {
72+
val intent = transactionRepository.receiveFromRelationship(
73+
domain = relationship.domain,
74+
amount = relationship.partialBalance,
75+
organizer = organizer
76+
).blockingGet()
7277

73-
receivedTotal += relationship.partialBalance
78+
receivedTotal += relationship.partialBalance
7479

75-
if (intent is IntentDeposit) {
76-
setTray(organizer, intent.resultTray)
77-
}
80+
if (intent is IntentDeposit) {
81+
setTray(organizer, intent.resultTray)
82+
}
7883

79-
// Bail early if a limit is set
80-
if (limit != null && receivedTotal >= limit) {
81-
return@loop // break loop
84+
// Bail early if a limit is set
85+
if (limit != null && receivedTotal >= limit) {
86+
return@loop // break loop
87+
}
8288
}
8389
}
8490
}

api/src/main/java/com/getcode/network/exchange/Exchange.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import java.util.Date
2525
import javax.inject.Inject
2626
import javax.inject.Singleton
2727
import kotlin.coroutines.resume
28+
import kotlin.time.Duration
29+
import kotlin.time.Duration.Companion.convert
2830
import kotlin.time.Duration.Companion.minutes
31+
import kotlin.time.DurationUnit
32+
import kotlin.time.ExperimentalTime
2933

3034
interface Exchange {
3135
val localRate: Rate
@@ -41,7 +45,7 @@ interface Exchange {
4145
fun rateForUsd(): Rate?
4246
}
4347

44-
class ExchangeNull: Exchange {
48+
class ExchangeNull : Exchange {
4549
override val localRate: Rate
4650
get() = Rate.oneToOne
4751

@@ -73,7 +77,7 @@ class CodeExchange @Inject constructor(
7377
private val currencyApi: CurrencyApi,
7478
private val networkOracle: NetworkOracle,
7579
private val defaultCurrency: () -> Currency?,
76-
): Exchange, CoroutineScope by CoroutineScope(Dispatchers.IO) {
80+
) : Exchange, CoroutineScope by CoroutineScope(Dispatchers.IO) {
7781

7882
private val db = Database.getInstance()
7983

@@ -168,7 +172,13 @@ class CodeExchange @Inject constructor(
168172
val localCurrency = defaultCurrency()
169173
val rate = localCurrency?.let { rates.rateFor(it) }
170174
_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+
)
172182
rate
173183
} else {
174184
Timber.d("Rate for $localCurrency not found. Defaulting to USD.")
@@ -178,7 +188,13 @@ class CodeExchange @Inject constructor(
178188

179189
val entryRate = entryCurrency?.let { rates.rateFor(it) }
180190
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+
)
182198
entryRate
183199
} else {
184200
Timber.d("Rate for $entryCurrency not found. Defaulting to USD.")
@@ -187,6 +203,7 @@ class CodeExchange @Inject constructor(
187203

188204
}
189205

206+
@OptIn(ExperimentalTime::class)
190207
@SuppressLint("CheckResult")
191208
private suspend fun fetchExchangeRates() = suspendCancellableCoroutine { cont ->
192209
Timber.d("fetching rates")
@@ -201,7 +218,14 @@ class CodeExchange @Inject constructor(
201218
if (rates.none { it.currency == CurrencyCode.KIN }) {
202219
rates.add(Rate(fx = 1.0, currency = CurrencyCode.KIN))
203220
}
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+
)
205229
}, {
206230
ErrorUtils.handleError(it)
207231
cont.resume(emptyList<Rate>() to System.currentTimeMillis())
@@ -212,7 +236,9 @@ class CodeExchange @Inject constructor(
212236
}
213237

214238
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 })
216242

217243
val isEmpty: Boolean
218244
get() = rates.isEmpty()

0 commit comments

Comments
 (0)