This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7417c97
commit c9ef7b3
Showing
16 changed files
with
143 additions
and
76 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/ivy/wallet/domain/action/exchange/SyncExchangeRatesAct.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.ivy.wallet.domain.action.exchange | ||
|
||
import com.ivy.frp.action.Action | ||
import com.ivy.wallet.io.network.RestClient | ||
import com.ivy.wallet.io.persistence.dao.ExchangeRateDao | ||
import com.ivy.wallet.io.persistence.data.ExchangeRateEntity | ||
import timber.log.Timber | ||
import javax.inject.Inject | ||
|
||
class SyncExchangeRatesAct @Inject constructor( | ||
private val exchangeRateDao: ExchangeRateDao, | ||
restClient: RestClient | ||
) : Action<SyncExchangeRatesAct.Input, Unit>() { | ||
data class Input( | ||
val baseCurrency: String, | ||
) | ||
|
||
private val service = restClient.exchangeRatesService | ||
|
||
companion object { | ||
private val URLS = listOf( | ||
"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur.json", | ||
"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur.min.json", | ||
"https://raw.githubusercontent.com/fawazahmed0/currency-api/1/latest/currencies/eur.min.json", | ||
"https://raw.githubusercontent.com/fawazahmed0/currency-api/1/latest/currencies/eur.json", | ||
) | ||
} | ||
|
||
override suspend fun Input.willDo() = io { | ||
sync(baseCurrency) | ||
} | ||
|
||
private suspend fun sync(baseCurrency: String) { | ||
if (baseCurrency.isBlank()) return | ||
val baseCurrencyLower = baseCurrency.lowercase() | ||
|
||
var eurRates: Map<String, Double> = emptyMap() | ||
for (url in URLS) { | ||
eurRates = fetchEurRates(url) | ||
if (eurRates.isNotEmpty()) break | ||
} | ||
if (eurRates.isEmpty()) return | ||
|
||
// At this point we must have non-empty EUR rates | ||
// Now we must convert them to base currency | ||
/* | ||
"eur": { | ||
"bgn": 1.955902, | ||
"usd": 1.062366, | ||
} | ||
*/ | ||
val eurBaseCurr = eurRates[baseCurrencyLower] ?: return | ||
|
||
val rateEntities = eurRates.mapNotNull { (target, rate) -> | ||
try { | ||
val baseTargetRate = rate / eurBaseCurr | ||
ExchangeRateEntity( | ||
baseCurrency = baseCurrency.uppercase(), | ||
currency = target.uppercase(), | ||
rate = baseTargetRate | ||
) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
null | ||
} | ||
}.toList() | ||
Timber.d("Updating exchange rates: $rateEntities") | ||
exchangeRateDao.saveAll(rateEntities) | ||
} | ||
|
||
private suspend fun fetchEurRates(url: String): Map<String, Double> { | ||
return try { | ||
service.getExchangeRates(url).eur | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
emptyMap() | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
app/src/main/java/com/ivy/wallet/domain/deprecated/logic/currency/ExchangeRatesLogic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
app/src/main/java/com/ivy/wallet/io/network/request/currency/CoinbaseRatesResponse.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/ivy/wallet/io/network/request/currency/ExchangeRatesResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.ivy.wallet.io.network.request.currency | ||
|
||
data class ExchangeRatesResponse( | ||
val date: String, | ||
val eur: Map<String, Double>, | ||
) |
20 changes: 0 additions & 20 deletions
20
app/src/main/java/com/ivy/wallet/io/network/service/CoinbaseService.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/ivy/wallet/io/network/service/ExchangeRatesService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ivy.wallet.io.network.service | ||
|
||
import com.ivy.wallet.io.network.request.currency.ExchangeRatesResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Url | ||
|
||
interface ExchangeRatesService { | ||
@GET | ||
suspend fun getExchangeRates( | ||
@Url url: String, | ||
): ExchangeRatesResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/ivy/wallet/io/persistence/migration/Migration122to123_ExchangeRates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ivy.wallet.io.persistence.migration | ||
|
||
import androidx.room.migration.Migration | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
|
||
class Migration122to123_ExchangeRates : Migration(122, 123) { | ||
override fun migrate(database: SupportSQLiteDatabase) { | ||
database.execSQL("ALTER TABLE exchange_rates ADD COLUMN manualOverride INTEGER NOT NULL DEFAULT 0") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters