Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 52 additions & 6 deletions app/src/main/java/com/brainwallet/data/model/Fee.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.brainwallet.data.model

import android.annotation.SuppressLint
import com.brainwallet.R
import com.brainwallet.tools.manager.FeeManager.ECONOMY
import com.brainwallet.tools.manager.FeeManager.FeeType
import com.brainwallet.tools.manager.FeeManager.LUXURY
import com.brainwallet.tools.manager.FeeManager.REGULAR
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.math.ceil
import kotlin.math.round

@Serializable
data class Fee(
@JvmField
@SerialName("fee_per_kb")
@SerialName("fee_per_kb_luxury")
var luxury: Long,
@JvmField
@SerialName("fee_per_kb_economy")
@SerialName("fee_per_kb")
var regular: Long,
@JvmField
@SerialName("fee_per_kb_luxury")
@SerialName("fee_per_kb_economy")
var economy: Long,
var timestamp: Long
) {
companion object {
//from legacy
Expand All @@ -25,13 +32,52 @@ data class Fee(
private const val defaultLuxuryFeePerKB: Long = 66746L
private const val defaultTimestamp: Long = 1583015199122L

// {"fee_per_kb":5289,"fee_per_kb_economy":2645,"fee_per_kb_luxury":10578}

@JvmStatic
val Default = Fee(
defaultLuxuryFeePerKB,
defaultRegularFeePerKB,
defaultEconomyFeePerKB,
defaultTimestamp
)

}
}


data class FeeOption(
@FeeType
val type: String,
val feePerKb: Long,
val labelStringId: Int,
)

fun Fee.toFeeOptions(): List<FeeOption> = listOf(
FeeOption(
type = ECONOMY,
feePerKb = economy,
labelStringId = R.string.network_fee_options_low
),
FeeOption(
type = REGULAR,
feePerKb = regular,
labelStringId = R.string.network_fee_options_medium
),
FeeOption(
type = LUXURY,
feePerKb = luxury,
labelStringId = R.string.network_fee_options_top
),
)

fun FeeOption.getFiat(currencyEntity: CurrencyEntity): Float {
val satoshisPerLtc = 100_000_000.0
val feeInLtc = feePerKb / satoshisPerLtc
return (feeInLtc * currencyEntity.rate).toFloat()
}

@SuppressLint("DefaultLocale")
fun FeeOption.getFiatFormatted(currencyEntity: CurrencyEntity): String {
val fiatValue = getFiat(currencyEntity)
val formatted = String.format("%.3f", fiatValue)
return "${currencyEntity.symbol}$formatted"
}
30 changes: 25 additions & 5 deletions app/src/main/java/com/brainwallet/data/repository/LtcRepository.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.brainwallet.data.repository

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.brainwallet.data.model.CurrencyEntity
import com.brainwallet.data.model.Fee
import com.brainwallet.data.source.RemoteApiSource
import com.brainwallet.di.json
import com.brainwallet.tools.manager.BRSharedPrefs
import com.brainwallet.tools.manager.FeeManager
import com.brainwallet.tools.sqlite.CurrencyDataSource
import kotlinx.serialization.encodeToString

interface LtcRepository {
suspend fun fetchRates(): List<CurrencyEntity>
Expand All @@ -16,7 +20,8 @@ interface LtcRepository {
class Impl(
private val context: Context,
private val remoteApiSource: RemoteApiSource,
private val currencyDataSource: CurrencyDataSource
private val currencyDataSource: CurrencyDataSource,
private val sharedPreferences: SharedPreferences,
) : LtcRepository {

//todo: make it offline first here later, currently just using CurrencyDataSource.getAllCurrencies
Expand All @@ -42,18 +47,33 @@ interface LtcRepository {
}

override suspend fun fetchFeePerKb(): Fee {
val lastUpdateTime = sharedPreferences.getLong(PREF_KEY_NETWORK_FEE_PER_KB_CACHED_AT, 0)
val currentTime = System.currentTimeMillis()
val cachedFee = sharedPreferences.getString(PREF_KEY_NETWORK_FEE_PER_KB, null)
?.let { json.decodeFromString<Fee>(it) }

return runCatching {
val fee = remoteApiSource.getFeePerKb()
// Check if cache exists and is less than 6 hours old
if (cachedFee != null && (currentTime - lastUpdateTime) < 6 * 60 * 60 * 1000) {
return cachedFee
}

//todo: cache
val fee = remoteApiSource.getFeePerKb()
sharedPreferences.edit {
putString(PREF_KEY_NETWORK_FEE_PER_KB, json.encodeToString(fee))
putLong(PREF_KEY_NETWORK_FEE_PER_KB_CACHED_AT, currentTime)
}

return fee
}.getOrElse { Fee.Default }
}.getOrElse {
cachedFee ?: Fee.Default
}
}

}

companion object {

const val PREF_KEY_NETWORK_FEE_PER_KB = "network_fee_per_kb"
const val PREF_KEY_NETWORK_FEE_PER_KB_CACHED_AT = "${PREF_KEY_NETWORK_FEE_PER_KB}_cached_at"
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/brainwallet/di/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ val dataModule = module {
single { CurrencyDataSource.getInstance(get()) }
single<SharedPreferences> { provideSharedPreferences(context = androidApplication()) }
single<SettingRepository> { SettingRepository.Impl(get(), get()) }
single<LtcRepository> { LtcRepository.Impl(get(), get(), get()) }
single<LtcRepository> { LtcRepository.Impl(get(), get(), get(), get()) }
}

val viewModelModule = module {
Expand Down
Loading