Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8: Crash upon [add_new] clicking #13

Merged
merged 9 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@ android {
dependencies {
implementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.14.0'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.3"
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'io.coil-kt:coil:2.2.1'
implementation 'io.coil-kt:coil-gif:2.2.1'
implementation 'io.coil-kt:coil-compose:2.2.1'
implementation "androidx.navigation:navigation-compose:2.5.2"
implementation "com.google.accompanist:accompanist-navigation-animation:0.27.0"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation 'androidx.activity:activity-compose:1.5.1'

implementation "com.google.dagger:dagger:2.42"
kapt "com.google.dagger:dagger-compiler:2.42"

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".App"
android:name=".presentation.App"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand All @@ -18,7 +18,7 @@
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".presentation.MainActivity"
android:exported="true"
android:theme="@style/Theme.Exchange">
<intent-filter>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/CurrencyAmount.kt
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
)
6 changes: 6 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/CurrencyName.kt
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
)
6 changes: 6 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/CurrencyRate.kt
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
)
12 changes: 12 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/CurrencyRepo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package space.taran.arkrate.data

import kotlinx.coroutines.flow.StateFlow

interface CurrencyRepo {

// ETH -> 1500.0
suspend fun getCurrencyRate(): List<CurrencyRate>

// ETH -> Ethereum
suspend fun getCurrencyName(): List<CurrencyName>
}
27 changes: 27 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/GeneralCurrencyRepo.kt
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
) : CurrencyRepo {
private val currencyRepos = listOf(
fiatRepo,
cryptoRepo
)

override suspend fun getCurrencyRate(): List<CurrencyRate> =
currencyRepos.fold(emptyList()) { codeToRate, repo ->
codeToRate + repo.getCurrencyRate()
}

override suspend fun getCurrencyName(): List<CurrencyName> =
currencyRepos.fold(emptyList()) { currencyName, repo ->
currencyName + repo.getCurrencyName()
}
}
45 changes: 45 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/assets/AssetsRepo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package space.taran.arkrate.data.assets

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.withContext
import space.taran.arkrate.data.CurrencyAmount

class AssetsRepo(
private val dbPath: String
) {
private val db = AppDatabase(dbPath)
private val codeToAmount = mutableListOf<CurrencyAmount>()
private val codeToAmountFlow = MutableSharedFlow<List<CurrencyAmount>>()

suspend fun init() = withContext(Dispatchers.IO) {
codeToAmount.addAll(
db.getAllExchange().map { CurrencyAmount(it.name, it.number) })
codeToAmountFlow.emit(codeToAmount)
}

fun allCurrencyAmount() = codeToAmount

fun allCurrencyAmountFlow(): SharedFlow<List<CurrencyAmount>?> = codeToAmountFlow

suspend fun setCurrencyAmount(code: String, amount: Double) {
codeToAmount.find {
it.code == code
}?.let {
it.amount = amount
} ?: let {
codeToAmount.add(CurrencyAmount(code, amount))
}
codeToAmountFlow.emit(codeToAmount)
db.setExchange(code, amount)
}

suspend fun removeCurrency(code: String) {
codeToAmount.find { it.code == code }?.let {
codeToAmount.remove(it)
}
codeToAmountFlow.emit(codeToAmount)
db.remove(code)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package space.taran.arkrate.storage
package space.taran.arkrate.data.assets

import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
Expand All @@ -15,7 +15,6 @@ data class Exchange(
var number: Double
)


class AppDatabase(val filePath: String) {

fun getAllExchange(): List<Exchange> {
Expand Down
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("/api/v3/ticker/price")
suspend fun getCryptoRates(): List<CryptoRateResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package space.taran.arkrate.data.crypto

import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import space.taran.arkrate.data.CurrencyName
import space.taran.arkrate.data.CurrencyRate
import space.taran.arkrate.data.CurrencyRepo
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class CryptoCurrencyRepo @Inject constructor(
private val cryptoAPI: CryptoAPI
) : CurrencyRepo {
private var cryptoWithRates: List<CurrencyRate>? = null
set(value) {
updatedTS = System.currentTimeMillis()
field = value
}
private var updatedTS: Long? = null

override suspend fun getCurrencyRate(): List<CurrencyRate> =
withContext(Dispatchers.IO) {
Log.d("T", "$dayInMillis")
if (cryptoWithRates == null ||
updatedTS!! + dayInMillis < System.currentTimeMillis()
) {
cryptoWithRates = cryptoAPI.getCryptoRates().findUSDTPairs()
}

cryptoWithRates!!
}

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
kirillt marked this conversation as resolved.
Show resolved Hide resolved
private fun List<CryptoRateResponse>.findUSDTPairs() =
mapNotNull { (code, price) ->
if (code.takeLast(4) == "USDT") {
CurrencyRate(code.dropLast(4), price)
} else
null
}

private val dayInMillis = TimeUnit.DAYS.toMillis(1)
}
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
)
8 changes: 8 additions & 0 deletions app/src/main/java/space/taran/arkrate/data/fiat/FiatAPI.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package space.taran.arkrate.data.fiat

import retrofit2.http.GET

interface FiatAPI {
@GET("/ARK-Builders/ark-exchange-rates/main/rates.json")
suspend fun get(): FiatRateResponse
}
Loading