Skip to content

Commit

Permalink
mapping CurrencyResponse directly to OfflineRates
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan committed Sep 12, 2021
1 parent e65bf73 commit b1748e3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ApiController(
apiRepository
.getRatesViaApi(base.name)
.execute({ currencyResponse ->
offlineRatesRepository.insertOfflineRates(currencyResponse.rates)
offlineRatesRepository.insertOfflineRates(currencyResponse)
}, { error ->
kermit.e(error) { error.message.toString() }
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class CalculatorViewModel(
data.rates = it
calculateConversions(it)
_state.update(rateState = RateState.Online(it.date))
offlineRatesRepository.insertOfflineRates(it)
}.also {
offlineRatesRepository.insertOfflineRates(currencyResponse)
}

private fun getRatesFailed(t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.github.mustafaozhan.ccc.client.util.calculateAdRewardEnd
import com.github.mustafaozhan.ccc.client.util.isRewardExpired
import com.github.mustafaozhan.ccc.client.util.launchIgnored
import com.github.mustafaozhan.ccc.client.util.toDateString
import com.github.mustafaozhan.ccc.client.util.toRates
import com.github.mustafaozhan.ccc.client.viewmodel.settings.SettingsData.Companion.SYNC_DELAY
import com.github.mustafaozhan.ccc.common.api.ApiRepository
import com.github.mustafaozhan.ccc.common.db.currency.CurrencyRepository
Expand Down Expand Up @@ -70,7 +69,7 @@ class SettingsViewModel(
delay(SYNC_DELAY)

apiRepository.getRatesViaBackend(name).execute(
success = { offlineRatesRepository.insertOfflineRates(it.toRates()) },
success = { offlineRatesRepository.insertOfflineRates(it) },
error = { error -> kermit.e(error) { error.message.toString() } }
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.github.mustafaozhan.ccc.common.db.offlinerates

import com.github.mustafaozhan.ccc.common.model.CurrencyResponse
import com.github.mustafaozhan.ccc.common.model.Rates

interface OfflineRatesRepository {
fun insertOfflineRates(rates: Rates)
fun insertOfflineRates(currencyResponse: CurrencyResponse)

fun getOfflineRatesByBase(baseName: String): Rates?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import com.github.mustafaozhan.ccc.common.mapper.toCurrencyResponseEntity
import com.github.mustafaozhan.ccc.common.mapper.toModel
import com.github.mustafaozhan.ccc.common.mapper.toOfflineRates
import com.github.mustafaozhan.ccc.common.mapper.toSerializedString
import com.github.mustafaozhan.ccc.common.model.Rates
import com.github.mustafaozhan.ccc.common.model.CurrencyResponse
import com.github.mustafaozhan.logmob.kermit

internal class OfflineRatesRepositoryImpl(
private val offlineRatesQueries: OfflineRatesQueries
) : OfflineRatesRepository {

override fun insertOfflineRates(rates: Rates) = offlineRatesQueries.insertOfflineRates(
rates.toOfflineRates()
).also { kermit.d { "OfflineRatesRepositoryImpl insertOfflineRates ${rates.base}" } }
override fun insertOfflineRates(currencyResponse: CurrencyResponse) = offlineRatesQueries
.insertOfflineRates(currencyResponse.toOfflineRates())
.also { kermit.d { "OfflineRatesRepositoryImpl insertOfflineRates ${currencyResponse.base}" } }

override fun getOfflineRatesByBase(baseName: String) = offlineRatesQueries
.getOfflineRatesByBase(baseName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.mustafaozhan.ccc.common.mapper

import com.github.mustafaozhan.ccc.common.entity.CurrencyResponseEntity
import com.github.mustafaozhan.ccc.common.model.CurrencyResponse
import com.github.mustafaozhan.ccc.common.db.sql.Offline_rates as OfflineRates

internal fun CurrencyResponseEntity.toModel(
fallbackBase: String = base
Expand All @@ -10,3 +11,30 @@ internal fun CurrencyResponseEntity.toModel(
date = date,
rates = rates.toModel()
)

internal fun CurrencyResponse.toOfflineRates() = OfflineRates(
base,
date,
rates.aed, rates.afn, rates.all, rates.amd, rates.ang, rates.aoa, rates.ars, rates.aud,
rates.awg, rates.azn, rates.bam, rates.bbd, rates.bdt, rates.bgn, rates.bhd, rates.bif,
rates.bmd, rates.bnd, rates.bob, rates.brl, rates.bsd, rates.btc, rates.btn, rates.bwp,
rates.byn, rates.bzd, rates.cad, rates.cdf, rates.chf, rates.clf, rates.clp, rates.cnh,
rates.cny, rates.cop, rates.crc, rates.cuc, rates.cup, rates.cve, rates.czk, rates.djf,
rates.dkk, rates.dop, rates.dzd, rates.egp, rates.ern, rates.etb, rates.eur, rates.fjd,
rates.fkp, rates.gbp, rates.gel, rates.ggp, rates.ghs, rates.gip, rates.gmd, rates.gnf,
rates.gtq, rates.gyd, rates.hkd, rates.hnl, rates.hrk, rates.htg, rates.huf, rates.idr,
rates.ils, rates.imp, rates.inr, rates.iqd, rates.irr, rates.isk, rates.jep, rates.jmd,
rates.jod, rates.jpy, rates.kes, rates.kgs, rates.khr, rates.kmf, rates.kpw, rates.krw,
rates.kwd, rates.kyd, rates.kzt, rates.lak, rates.lbp, rates.lkr, rates.lrd, rates.lsl,
rates.lyd, rates.mad, rates.mdl, rates.mga, rates.mkd, rates.mmk, rates.mnt, rates.mop,
rates.mro, rates.mru, rates.mur, rates.mvr, rates.mwk, rates.mxn, rates.myr, rates.mzn,
rates.nad, rates.ngn, rates.nio, rates.nok, rates.npr, rates.nzd, rates.omr, rates.pab,
rates.pen, rates.pgk, rates.php, rates.pkr, rates.pln, rates.pyg, rates.qar, rates.ron,
rates.rsd, rates.rub, rates.rwf, rates.sar, rates.sbd, rates.scr, rates.sdg, rates.sek,
rates.sgd, rates.shp, rates.sll, rates.sos, rates.srd, rates.ssp, rates.std, rates.stn,
rates.svc, rates.syp, rates.szl, rates.thb, rates.tjs, rates.tmt, rates.tnd, rates.top,
rates.`try`, rates.ttd, rates.twd, rates.tzs, rates.uah, rates.ugx, rates.usd, rates.uyu,
rates.uzs, rates.ves, rates.vnd, rates.vuv, rates.wst, rates.xaf, rates.xag, rates.xau,
rates.xcd, rates.xdr, rates.xof, rates.xpd, rates.xpf, rates.xpt, rates.yer, rates.zar,
rates.zmw, rates.zwl
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.mustafaozhan.ccc.common.mapper

import com.github.mustafaozhan.ccc.common.entity.RatesEntity
import com.github.mustafaozhan.ccc.common.model.Rates
import com.github.mustafaozhan.ccc.common.db.sql.Offline_rates as OfflineRates

internal fun RatesEntity.toModel() = Rates(
base, date, aed, afn, all, amd, ang, aoa, ars, aud, awg, azn, bam, bbd, bdt, bgn, bhd, bif,
Expand All @@ -16,16 +15,3 @@ internal fun RatesEntity.toModel() = Rates(
tnd, top, `try`, ttd, twd, tzs, uah, ugx, usd, uyu, uzs, ves, vnd, vuv, wst, xaf, xag, xau,
xcd, xdr, xof, xpd, xpf, xpt, yer, zar, zmw, zwl
)

internal fun Rates.toOfflineRates() = OfflineRates(
base, date, aed, afn, all, amd, ang, aoa, ars, aud, awg, azn, bam, bbd, bdt, bgn, bhd, bif,
bmd, bnd, bob, brl, bsd, btc, btn, bwp, byn, bzd, cad, cdf, chf, clf, clp, cnh, cny, cop,
crc, cuc, cup, cve, czk, djf, dkk, dop, dzd, egp, ern, etb, eur, fjd, fkp, gbp, gel, ggp,
ghs, gip, gmd, gnf, gtq, gyd, hkd, hnl, hrk, htg, huf, idr, ils, imp, inr, iqd, irr, isk,
jep, jmd, jod, jpy, kes, kgs, khr, kmf, kpw, krw, kwd, kyd, kzt, lak, lbp, lkr, lrd, lsl,
lyd, mad, mdl, mga, mkd, mmk, mnt, mop, mro, mru, mur, mvr, mwk, mxn, myr, mzn, nad, ngn,
nio, nok, npr, nzd, omr, pab, pen, pgk, php, pkr, pln, pyg, qar, ron, rsd, rub, rwf, sar,
sbd, scr, sdg, sek, sgd, shp, sll, sos, srd, ssp, std, stn, svc, syp, szl, thb, tjs, tmt,
tnd, top, `try`, ttd, twd, tzs, uah, ugx, usd, uyu, uzs, ves, vnd, vuv, wst, xaf, xag, xau,
xcd, xdr, xof, xpd, xpf, xpt, yer, zar, zmw, zwl
)

0 comments on commit b1748e3

Please sign in to comment.