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
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
applicationId = "ltd.grunt.brainwallet"
minSdk = 29
targetSdk = 34
versionCode = 202503312
versionName = "v4.4.3"
versionCode = 202504251
versionName = "v4.4.7"

multiDexEnabled = true
base.archivesName.set("${defaultConfig.versionName}(${defaultConfig.versionCode})")
Expand Down
27 changes: 1 addition & 26 deletions app/src/main/java/com/brainwallet/data/model/CurrencyEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,4 @@ data class CurrencyEntity(
var rate: Float = 0F,
@JvmField
var symbol: String = ""
) : Serializable {
// @JvmField
// var code: String? = null
// @JvmField
// var name: String? = null
// @JvmField
// var rate: Float = 0f
// @JvmField
// var symbol: String? = null
//
// constructor(code: String?, name: String?, rate: Float, symbol: String?) {
// this.code = code
// this.name = name
// this.rate = rate
// this.symbol = symbol
// }
//
// constructor()
//
// companion object {
// //Change this after modifying the class
// private const val serialVersionUID = 7526472295622776147L
//
// val TAG: String = CurrencyEntity::class.java.name
// }
}
) : Serializable
37 changes: 37 additions & 0 deletions app/src/main/java/com/brainwallet/data/model/Fee.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.brainwallet.data.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Fee(
@JvmField
@SerialName("fee_per_kb")
var luxury: Long,
@JvmField
@SerialName("fee_per_kb_economy")
var regular: Long,
@JvmField
@SerialName("fee_per_kb_luxury")
var economy: Long,
var timestamp: Long
) {
companion object {
//from legacy
// this is the default that matches the mobile-api if the server is unavailable
private const val defaultEconomyFeePerKB: Long =
2500L // From legacy minimum. default min is 1000 as Litecoin Core version v0.17.1
private const val defaultRegularFeePerKB: Long = 25000L
private const val defaultLuxuryFeePerKB: Long = 66746L
private const val defaultTimestamp: Long = 1583015199122L

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

}
}
47 changes: 33 additions & 14 deletions app/src/main/java/com/brainwallet/data/repository/LtcRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.brainwallet.data.repository

import android.content.Context
import com.brainwallet.data.model.CurrencyEntity
import com.brainwallet.data.model.Fee
import com.brainwallet.data.source.RemoteApiSource
import com.brainwallet.tools.manager.BRSharedPrefs
import com.brainwallet.tools.manager.FeeManager
import com.brainwallet.tools.sqlite.CurrencyDataSource

interface LtcRepository {
suspend fun fetchRates(): List<CurrencyEntity>
//todo

suspend fun fetchFeePerKb(): Fee

class Impl(
private val context: Context,
Expand All @@ -19,22 +21,39 @@ interface LtcRepository {

//todo: make it offline first here later, currently just using CurrencyDataSource.getAllCurrencies
override suspend fun fetchRates(): List<CurrencyEntity> {
val rates = remoteApiSource.getRates()

//legacy logic
FeeManager.updateFeePerKb(context)
val selectedISO = BRSharedPrefs.getIsoSymbol(context)
rates.forEachIndexed { index, currencyEntity ->
if (currencyEntity.code.equals(selectedISO, ignoreCase = true)) {
BRSharedPrefs.putIso(context, currencyEntity.code)
BRSharedPrefs.putCurrencyListPosition(context, index - 1)
return runCatching {
val rates = remoteApiSource.getRates()

//legacy logic
FeeManager.updateFeePerKb(context)
val selectedISO = BRSharedPrefs.getIsoSymbol(context)
rates.forEachIndexed { index, currencyEntity ->
if (currencyEntity.code.equals(selectedISO, ignoreCase = true)) {
BRSharedPrefs.putIso(context, currencyEntity.code)
BRSharedPrefs.putCurrencyListPosition(context, index - 1)
}
}
}

//save to local
currencyDataSource.putCurrencies(rates)
return rates
//save to local
currencyDataSource.putCurrencies(rates)
return rates
}.getOrElse { currencyDataSource.getAllCurrencies(true) }

}

override suspend fun fetchFeePerKb(): Fee {
return runCatching {
val fee = remoteApiSource.getFeePerKb()

//todo: cache

return fee
}.getOrElse { Fee.Default }
}

}

companion object {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ interface SelectedPeersRepository {
override fun onResponse(call: Call, response: Response) {
val jsonString = response.body?.string()

if (response.isSuccessful.not()) {
continuation.resume(cachedPeers ?: emptySet())
return
}

val parsedResult = jsonString?.let {
val jsonElement = json.parseToJsonElement(it)
val dataObject = jsonElement.jsonObject["data"]?.jsonObject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.brainwallet.data.source

import com.brainwallet.data.model.CurrencyEntity
import com.brainwallet.data.model.Fee
import retrofit2.http.GET

//TODO
interface RemoteApiSource {

@GET("api/v1/rates")
@GET("v1/rates")
suspend fun getRates(): List<CurrencyEntity>

@GET("v1/fee-per-kb")
suspend fun getFeePerKb()
suspend fun getFeePerKb(): Fee

// https://prod.apigsltd.net/moonpay/buy?address=ltc1qjnsg3p9rt4r4vy7ncgvrywdykl0zwhkhcp8ue0&code=USD&idate=1742331930290&uid=ec51fa950b271ff3
// suspend fun getMoonPayBuy()
Expand Down
23 changes: 16 additions & 7 deletions app/src/main/java/com/brainwallet/di/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.content.Context
import android.content.SharedPreferences
import com.brainwallet.BuildConfig
import com.brainwallet.data.repository.LtcRepository
import com.brainwallet.data.repository.SelectedPeersRepository
import com.brainwallet.data.repository.SettingRepository
import com.brainwallet.data.source.RemoteApiSource
import com.brainwallet.data.source.RemoteConfigSource
import com.brainwallet.data.repository.SelectedPeersRepository
import com.brainwallet.tools.sqlite.CurrencyDataSource
import com.brainwallet.tools.util.BRConstants
import com.brainwallet.ui.screens.home.SettingsViewModel
Expand All @@ -31,6 +31,7 @@ import org.koin.android.ext.koin.androidApplication
import org.koin.core.module.dsl.viewModel
import org.koin.core.module.dsl.viewModelOf
import org.koin.dsl.module
import retrofit2.HttpException
import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory

Expand All @@ -46,7 +47,7 @@ val dataModule = module {
factory { provideOkHttpClient() }
single { provideRetrofit(get(), BRConstants.BW_API_PROD_HOST) }

single { provideApi(get()) }
single { provideApi<RemoteApiSource>(get()) }

single<RemoteConfigSource> {
RemoteConfigSource.FirebaseImpl(Firebase.remoteConfig).also {
Expand Down Expand Up @@ -91,17 +92,25 @@ private fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addHeader("Content-Type", "application/json")
.addHeader("X-Litecoin-Testnet", "false")
.addHeader("Accept-Language", "en")
// .addHeader("User-agent",)
chain.proceed(requestBuilder.build())
}
.addInterceptor { chain ->
val request = chain.request()
runCatching {
chain.proceed(request)
val result = chain.proceed(request)
if (result.isSuccessful.not()) {
throw HttpException(
retrofit2.Response.error<Any>(
result.code,
result.body ?: result.peekBody(Long.MAX_VALUE)
)
)
}
result
}.getOrElse {
//retry using dev host
val newRequest = request.newBuilder()
.url(BRConstants.BW_API_DEV_HOST + request.url.encodedPath)
.url("${BRConstants.LEGACY_BW_API_DEV_HOST}/api${request.url.encodedPath}") //legacy dev api need prefix path /api
.build()
chain.proceed(newRequest)
}
Expand Down Expand Up @@ -129,5 +138,5 @@ internal fun provideRetrofit(
)
.build()

internal fun provideApi(retrofit: Retrofit): RemoteApiSource =
retrofit.create(RemoteApiSource::class.java)
internal inline fun <reified T> provideApi(retrofit: Retrofit): T =
retrofit.create(T::class.java)
15 changes: 0 additions & 15 deletions app/src/main/java/com/brainwallet/presenter/entities/Fee.java

This file was deleted.

Loading