-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use StateList, models refactoring * Caching with Room, NetworkStatus implemented * Added user agent * fix Key [CurrencyCode] was already used java.lang.IllegalArgumentException: Key TRX was already used. If you are using LazyColumn/Row please make sure you provide a unique key for each item. * Format amount input * Fix api links
- Loading branch information
Showing
51 changed files
with
1,378 additions
and
1,044 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package space.taran.arkrate.data | ||
|
||
data class CurrencyAmount( | ||
val code: String, | ||
var amount: Double | ||
) |
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 space.taran.arkrate.data | ||
|
||
data class CurrencyName( | ||
val code: String, | ||
val name: String | ||
) |
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 space.taran.arkrate.data | ||
|
||
data class CurrencyRate( | ||
val code: String, | ||
val rate: Double | ||
) |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/space/taran/arkrate/data/CurrencyRepo.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,56 @@ | ||
package space.taran.arkrate.data | ||
|
||
import android.util.Log | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import space.taran.arkrate.data.db.CurrencyRateLocalDataSource | ||
import space.taran.arkrate.data.db.FetchTimestampDataSource | ||
import space.taran.arkrate.data.network.NetworkStatus | ||
import space.taran.arkrate.utils.withContextAndLock | ||
import java.util.concurrent.TimeUnit | ||
|
||
abstract class CurrencyRepo( | ||
private val local: CurrencyRateLocalDataSource, | ||
private val networkStatus: NetworkStatus, | ||
private val fetchTimestampDataSource: FetchTimestampDataSource | ||
) { | ||
protected abstract val type: CurrencyType | ||
private var currencyRates: List<CurrencyRate>? = null | ||
private var updatedTS: Long? = null | ||
private val mutex = Mutex() | ||
|
||
suspend fun getCurrencyRate(): List<CurrencyRate> = | ||
withContextAndLock(Dispatchers.IO, mutex) { | ||
if (!networkStatus.isOnline()) { | ||
currencyRates = local.getByType(type) | ||
return@withContextAndLock currencyRates!! | ||
} | ||
|
||
updatedTS ?: let { | ||
updatedTS = fetchTimestampDataSource.getTimestamp(type) | ||
} | ||
|
||
if ( | ||
updatedTS == null || | ||
updatedTS!! + dayInMillis < System.currentTimeMillis() | ||
) { | ||
currencyRates = fetchRemote() | ||
launch { fetchTimestampDataSource.rememberTimestamp(type) } | ||
launch { local.insert(currencyRates!!, type) } | ||
updatedTS = System.currentTimeMillis() | ||
} | ||
|
||
currencyRates ?: let { | ||
currencyRates = local.getByType(type) | ||
} | ||
Log.d("Test", "${currencyRates!!.sortedBy { it.code }}") | ||
return@withContextAndLock currencyRates!! | ||
} | ||
|
||
protected abstract suspend fun fetchRemote(): List<CurrencyRate> | ||
|
||
abstract suspend fun getCurrencyName(): List<CurrencyName> | ||
|
||
private val dayInMillis = TimeUnit.DAYS.toMillis(1) | ||
} |
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,5 @@ | ||
package space.taran.arkrate.data | ||
|
||
enum class CurrencyType { | ||
FIAT, CRYPTO | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/space/taran/arkrate/data/GeneralCurrencyRepo.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,27 @@ | ||
package space.taran.arkrate.data | ||
|
||
import space.taran.arkrate.data.crypto.CryptoCurrencyRepo | ||
import space.taran.arkrate.data.fiat.FiatCurrencyRepo | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class GeneralCurrencyRepo @Inject constructor( | ||
val fiatRepo: FiatCurrencyRepo, | ||
val cryptoRepo: CryptoCurrencyRepo | ||
) { | ||
private val currencyRepos = listOf( | ||
fiatRepo, | ||
cryptoRepo | ||
) | ||
|
||
suspend fun getCurrencyRate(): List<CurrencyRate> = | ||
currencyRepos.fold(emptyList()) { codeToRate, repo -> | ||
codeToRate + repo.getCurrencyRate() | ||
} | ||
|
||
suspend fun getCurrencyName(): List<CurrencyName> = | ||
currencyRepos.fold(emptyList()) { currencyName, repo -> | ||
currencyName + repo.getCurrencyName() | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.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,60 @@ | ||
package space.taran.arkrate.data.assets | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import space.taran.arkrate.data.CurrencyAmount | ||
import space.taran.arkrate.data.db.AssetsLocalDataSource | ||
import space.taran.arkrate.utils.replace | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class AssetsRepo @Inject constructor( | ||
private val local: AssetsLocalDataSource | ||
) { | ||
private var currencyAmountList = listOf<CurrencyAmount>() | ||
private val currencyAmountFlow = | ||
MutableStateFlow<List<CurrencyAmount>>(emptyList()) | ||
private val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
init { | ||
scope.launch { | ||
currencyAmountList = local.getAll() | ||
currencyAmountFlow.emit(currencyAmountList) | ||
} | ||
} | ||
|
||
fun allCurrencyAmount(): List<CurrencyAmount> = currencyAmountList | ||
|
||
fun allCurrencyAmountFlow(): StateFlow<List<CurrencyAmount>> = currencyAmountFlow | ||
|
||
suspend fun setCurrencyAmount(code: String, amount: Double) = | ||
withContext(Dispatchers.IO) { | ||
currencyAmountList.find { | ||
it.code == code | ||
}?.let { currencyAmount -> | ||
currencyAmountList = currencyAmountList.replace( | ||
currencyAmount, | ||
currencyAmount.copy(amount = amount) | ||
) | ||
} ?: let { | ||
currencyAmountList = | ||
currencyAmountList + CurrencyAmount(code, amount) | ||
} | ||
currencyAmountFlow.emit(currencyAmountList.toList()) | ||
local.insert(CurrencyAmount(code, amount)) | ||
} | ||
|
||
suspend fun removeCurrency(code: String) = withContext(Dispatchers.IO) { | ||
currencyAmountList.find { it.code == code }?.let { | ||
currencyAmountList = currencyAmountList - it | ||
} | ||
currencyAmountFlow.emit(currencyAmountList) | ||
local.delete(code) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/space/taran/arkrate/data/crypto/CryptoAPI.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,8 @@ | ||
package space.taran.arkrate.data.crypto | ||
|
||
import retrofit2.http.GET | ||
|
||
interface CryptoAPI { | ||
@GET("/ARK-Builders/ark-exchange-rates/main/crypto-rates.json") | ||
suspend fun getCryptoRates(): List<CryptoRateResponse> | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/space/taran/arkrate/data/crypto/CryptoCurrencyRepo.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,39 @@ | ||
package space.taran.arkrate.data.crypto | ||
|
||
import space.taran.arkrate.data.CurrencyName | ||
import space.taran.arkrate.data.CurrencyRate | ||
import space.taran.arkrate.data.CurrencyRepo | ||
import space.taran.arkrate.data.CurrencyType | ||
import space.taran.arkrate.data.network.NetworkStatus | ||
import space.taran.arkrate.data.db.CurrencyRateLocalDataSource | ||
import space.taran.arkrate.data.db.FetchTimestampDataSource | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class CryptoCurrencyRepo @Inject constructor( | ||
private val cryptoAPI: CryptoAPI, | ||
private val local: CurrencyRateLocalDataSource, | ||
private val networkStatus: NetworkStatus, | ||
private val fetchTimestampDataSource: FetchTimestampDataSource | ||
) : CurrencyRepo(local, networkStatus, fetchTimestampDataSource) { | ||
override val type = CurrencyType.CRYPTO | ||
|
||
override suspend fun fetchRemote(): List<CurrencyRate> = | ||
cryptoAPI.getCryptoRates().findUSDTPairs() | ||
|
||
override suspend fun getCurrencyName(): List<CurrencyName> = | ||
getCurrencyRate().map { | ||
CurrencyName(it.code, name = "") | ||
} | ||
|
||
// api returns pairs like ETHBTC, ETHBNB, ETHTRX, ETHUSDT | ||
// we only take USDT pairs | ||
private fun List<CryptoRateResponse>.findUSDTPairs() = | ||
mapNotNull { (code, price) -> | ||
if (code.takeLast(4) == "USDT") { | ||
CurrencyRate(code.dropLast(4), price) | ||
} else | ||
null | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/space/taran/arkrate/data/crypto/CryptoRatesResponse.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 space.taran.arkrate.data.crypto | ||
|
||
data class CryptoRateResponse( | ||
val symbol: String, | ||
val price: Double | ||
) |
41 changes: 41 additions & 0 deletions
41
app/src/main/java/space/taran/arkrate/data/db/AssetsDao.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,41 @@ | ||
package space.taran.arkrate.data.db | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Entity | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.PrimaryKey | ||
import androidx.room.Query | ||
import space.taran.arkrate.data.CurrencyAmount | ||
import javax.inject.Inject | ||
|
||
@Entity | ||
data class RoomCurrencyAmount( | ||
@PrimaryKey | ||
val code: String, | ||
val amount: Double | ||
) | ||
|
||
@Dao | ||
interface AssetsDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(currencyAmount: RoomCurrencyAmount) | ||
|
||
@Query("SELECT * FROM RoomCurrencyAmount") | ||
suspend fun getAll(): List<RoomCurrencyAmount> | ||
|
||
@Query("DELETE FROM RoomCurrencyAmount where code = :code") | ||
suspend fun delete(code: String) | ||
} | ||
|
||
class AssetsLocalDataSource @Inject constructor(val dao: AssetsDao) { | ||
suspend fun insert(currencyAmount: CurrencyAmount) = | ||
dao.insert(currencyAmount.toRoom()) | ||
|
||
suspend fun getAll() = dao.getAll().map { it.toCurrencyAmount() } | ||
|
||
suspend fun delete(code: String) = dao.delete(code) | ||
} | ||
|
||
private fun RoomCurrencyAmount.toCurrencyAmount() = CurrencyAmount(code, amount) | ||
private fun CurrencyAmount.toRoom() = RoomCurrencyAmount(code, amount) |
42 changes: 42 additions & 0 deletions
42
app/src/main/java/space/taran/arkrate/data/db/CurrencyRateDao.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,42 @@ | ||
package space.taran.arkrate.data.db | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Entity | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.PrimaryKey | ||
import androidx.room.Query | ||
import space.taran.arkrate.data.CurrencyRate | ||
import space.taran.arkrate.data.CurrencyType | ||
import javax.inject.Inject | ||
|
||
@Entity | ||
data class RoomCurrencyRate( | ||
@PrimaryKey | ||
val code: String, | ||
val currencyType: String, | ||
val rate: Double | ||
) | ||
|
||
@Dao | ||
interface CurrencyRateDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(currencyRate: List<RoomCurrencyRate>) | ||
|
||
@Query("SELECT * FROM RoomCurrencyRate where currencyType = :currencyType") | ||
suspend fun getByType(currencyType: String): List<RoomCurrencyRate> | ||
} | ||
|
||
class CurrencyRateLocalDataSource @Inject constructor(val dao: CurrencyRateDao) { | ||
suspend fun insert( | ||
currencyRate: List<CurrencyRate>, | ||
currencyType: CurrencyType | ||
) = dao.insert(currencyRate.map { it.toRoom(currencyType) }) | ||
|
||
suspend fun getByType(currencyType: CurrencyType) = | ||
dao.getByType(currencyType.name).map { it.toCurrencyRate() } | ||
} | ||
|
||
private fun RoomCurrencyRate.toCurrencyRate() = CurrencyRate(code, rate) | ||
private fun CurrencyRate.toRoom(currencyType: CurrencyType) = | ||
RoomCurrencyRate(code, currencyType.name, rate) |
Oops, something went wrong.