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
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ dependencies {
}

implementation("androidx.webkit:webkit:1.9.0")
implementation("com.squareup.moshi:moshi-kotlin:1.15.2")
implementation(libs.androidx.core)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.legacy.support)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
public class CurrencyEntity implements Serializable {

//Change this after modifying the class
private static final long serialVersionUID = 7526472295622776147L;
private static final long serialVersionUID = 7526472295622777000L;

public static final String TAG = CurrencyEntity.class.getName();
public String code;
public String name;
public String name = "";
public float rate;
public String symbol;
public String symbol = "";

public CurrencyEntity(String code, String name, float rate, String symbol) {
this.code = code;
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/brainwallet/di/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.SharedPreferences
import com.brainwallet.BuildConfig
import com.brainwallet.data.repository.SettingRepository
import com.brainwallet.data.source.RemoteConfigSource
import com.brainwallet.tools.manager.BRApiManager
import com.brainwallet.tools.manager.APIManager
import com.brainwallet.tools.sqlite.CurrencyDataSource
import com.brainwallet.ui.screens.home.SettingsViewModel
import com.brainwallet.ui.screens.inputwords.InputWordsViewModel
Expand All @@ -32,7 +32,7 @@ val dataModule = module {
it.initialize()
}
}
single { BRApiManager(get()) }
single { APIManager(get()) }
single { CurrencyDataSource.getInstance(get()) }
single<SharedPreferences> { provideSharedPreferences(context = androidApplication()) }
single<SettingRepository> { SettingRepository.Impl(get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.brainwallet.presenter.activities.intro.RecoverActivity;
import com.brainwallet.presenter.activities.intro.WriteDownActivity;
import com.brainwallet.tools.animation.BRAnimator;
import com.brainwallet.tools.manager.BRApiManager;
import com.brainwallet.tools.manager.APIManager;
import com.brainwallet.tools.manager.InternetManager;
import com.brainwallet.tools.security.AuthManager;
import com.brainwallet.tools.security.BRKeyStore;
Expand Down Expand Up @@ -148,7 +148,7 @@ public static void init(Activity app) {


if (!(app instanceof RecoverActivity || app instanceof WriteDownActivity)) {
BRApiManager apiManager = KoinJavaComponent.get(BRApiManager.class);
APIManager apiManager = KoinJavaComponent.get(APIManager.class);
apiManager.startTimer(app);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ enum class ServiceItems(val key: String) {
WALLETOPS("wallet-ops"),
OPSALL("wallet-ops"),
WALLETSTART("start-date"),
AFDEVID("af-dev-id")
AFDEVID("af-dev-id"),
CLIENTCODE("client-code"),
}
119 changes: 119 additions & 0 deletions app/src/main/java/com/brainwallet/tools/manager/APIManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.brainwallet.tools.manager

import android.app.Activity
import android.content.Context
import com.brainwallet.data.model.CurrencyEntity
import com.brainwallet.data.source.RemoteConfigSource
import com.brainwallet.presenter.entities.ServiceItems
import com.brainwallet.tools.sqlite.CurrencyDataSource
import com.brainwallet.tools.util.BRConstants.BW_API_DEV_HOST
import com.brainwallet.tools.util.BRConstants.BW_API_PROD_HOST
import com.brainwallet.tools.util.Utils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import com.squareup.moshi.Moshi
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Types
import timber.log.Timber
import java.io.IOException
import java.util.*
import kotlin.collections.LinkedHashSet

class APIManager (private val remoteConfigSource: RemoteConfigSource) {
fun getPRODBaseURL(): String = BW_API_PROD_HOST
fun getDEVBaseURL(): String = BW_API_DEV_HOST

private var timer: Timer? = null
private var pollPeriod : Long = 5000

private val client = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()
private val type = Types.newParameterizedType(List::class.java, CurrencyEntity::class.java)
private val jsonAdapter: JsonAdapter<List<CurrencyEntity>> = moshi.adapter(type)

fun getCurrencies(context: Activity): Set<CurrencyEntity> {
val set = LinkedHashSet<CurrencyEntity>()
try {
val arr = fetchRates(context)
FeeManager.updateFeePerKb(context)
arr?.let { currencyList ->
val selectedISO = BRSharedPrefs.getIsoSymbol(context)
currencyList.forEachIndexed { i, tempCurrencyEntity ->
if (tempCurrencyEntity.code.equals(selectedISO, ignoreCase = true)) {
BRSharedPrefs.putIso(context, tempCurrencyEntity.code)
BRSharedPrefs.putCurrencyListPosition(context, i - 1)
}
set.add(tempCurrencyEntity)
}
} ?: Timber.d("timber: getCurrencies: failed to get currencies")
} catch (e: Exception) {
Timber.e(e)
}
return set.reversed().toSet()
}

private fun initializeTimerTask(context: Context) {
timer = Timer().apply {
schedule(object : TimerTask() {
override fun run() {
CoroutineScope(Dispatchers.IO).launch {
val tmp = getCurrencies(context as Activity)
withContext(Dispatchers.Main) {
CurrencyDataSource.getInstance(context).putCurrencies(tmp)
}
}
}
}, 0, pollPeriod)
}
}

fun startTimer(context: Context) {
if (timer != null) return
initializeTimerTask(context)
}

fun fetchRates(activity: Activity): List<CurrencyEntity>? {
val jsonString = createGETRequestURL(activity, "$BW_API_PROD_HOST/api/v1/rates")
return parseJsonArray(jsonString) ?: backupFetchRates(activity)
}

private fun backupFetchRates(activity: Activity): List<CurrencyEntity>? {
val jsonString = createGETRequestURL(activity, "$BW_API_DEV_HOST/api/v1/rates")
return parseJsonArray(jsonString)
}

private fun parseJsonArray(jsonString: String?): List<CurrencyEntity>? {
return try {
jsonString?.let { jsonAdapter.fromJson(it) }
} catch (e: IOException) {
Timber.e(e)
null
}
}

private fun createGETRequestURL(app: Context, url: String): String? {
val request = Request.Builder()
.url(url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.header("User-agent", Utils.getAgentString(app, "android/HttpURLConnection"))
.header("bw-client-code", Utils.fetchServiceItem(app, ServiceItems.CLIENTCODE))
.get()
.build()

return try {
client.newCall(request).execute().use { response ->
response.body?.string().also {
BRSharedPrefs.putSecureTime(app, System.currentTimeMillis())
}
}
} catch (e: IOException) {
Timber.e(e)
null
}
}
}
Loading