Skip to content

Commit

Permalink
Improve error handling when downloading new coin list.
Browse files Browse the repository at this point in the history
  • Loading branch information
hwki committed Feb 27, 2022
1 parent 978aeb4 commit 99e5efb
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 19 deletions.
4 changes: 2 additions & 2 deletions bitcoin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "com.brentpanther.bitcoinwidget"
minSdk 23
targetSdk 31
versionCode 295
versionName "8.2.1"
versionCode 296
versionName "8.2.2"

javaCompileOptions {
annotationProcessorOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import androidx.preference.PreferenceManager
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.internal.closeQuietly
import java.io.IOException
import java.util.concurrent.TimeUnit

object Repository {

private const val LAST_MODIFIED = "last_modified"
internal const val LAST_MODIFIED = "last_modified"
const val CURRENCY_FILE_NAME = "coins.json"
private val TAG = Repository::class.java.simpleName

Expand All @@ -38,10 +39,11 @@ object Repository {
304 -> Log.d(TAG, "No changes found in JSON file.")
200 -> {
Log.d(TAG, "Updated JSON file found.")
val json = response.body!!.bytes()
val os = context.openFileOutput(CURRENCY_FILE_NAME, Context.MODE_PRIVATE)
os.write(json)
os.close()
response.body?.byteStream()?.use {
val os = context.openFileOutput(CURRENCY_FILE_NAME, Context.MODE_PRIVATE)
it.copyTo(os)
os.closeQuietly()
}
prefs.edit().putString(LAST_MODIFIED, response.header("Last-Modified")).apply()
Log.d(TAG, "JSON downloaded.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ enum class Exchange(val exchangeName: String, shortName: String? = null) {
BITPANDA("Bitpanda") {

override fun getValue(coin: String, currency: String): String {
val url = "https://api.exchange.bitpanda.com/public/v1/market-ticker/${coin}_$currency"
return getJsonObject(url).get("last_price").asString
val url = "https://api.bitpanda.com/v1/ticker"
return getJsonObject(url).get(coin).asJsonObject.get(currency).asString
}
},
BITPAY("BitPay") {
Expand Down Expand Up @@ -497,7 +497,7 @@ enum class Exchange(val exchangeName: String, shortName: String? = null) {

override fun getValue(coin: String, currency: String): String {
val url = "https://api.hitbtc.com/api/3/public/ticker?symbols=$coin$currency"
return getJsonObject(url).get("$coin$currency").asJsonObject.asString
return getJsonObject(url).get("$coin$currency").asJsonObject.get("last").asString
}
},
HOO("Hoo") {
Expand Down Expand Up @@ -654,8 +654,8 @@ enum class Exchange(val exchangeName: String, shortName: String? = null) {
PARIBU("Paribu") {

override fun getValue(coin: String, currency: String): String {
val url = "https://api.p2pb2b.io/api/v2/public/ticker?market=${coin}_$currency"
return getJsonObject(url).get("result").asJsonObject.get("last").asString
val url = "https://www.paribu.com/ticker"
return getJsonObject(url).getAsJsonObject("${coin}_$currency").get("last").asString
}
},
PAYMIUM("Paymium") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ open class ExchangeData(val coinEntry: CoinEntry, json: InputStream) {
return exchangeNames.toTypedArray()
}

var numberExchanges = currencyExchange.count()

//TODO: change to use exchange
fun getDefaultExchange(currency: String): String {
val exchanges = currencyExchange[currency]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.brentpanther.bitcoinwidget.ui.settings
import android.app.Application
import android.content.Context
import android.util.Log
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager
import com.brentpanther.bitcoinwidget.Coin
import com.brentpanther.bitcoinwidget.CoinEntry
import com.brentpanther.bitcoinwidget.R
Expand All @@ -18,7 +20,7 @@ import com.brentpanther.bitcoinwidget.exchange.CustomExchangeData
import com.brentpanther.bitcoinwidget.exchange.ExchangeData
import com.brentpanther.bitcoinwidget.ui.settings.SettingsViewModel.DataState.Downloading
import com.brentpanther.bitcoinwidget.ui.settings.SettingsViewModel.DataState.Success
import com.google.gson.JsonSyntaxException
import com.google.gson.JsonParseException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.async
Expand Down Expand Up @@ -73,11 +75,18 @@ class SettingsViewModel(app: Application) : AndroidViewModel(app) {
if (coin.coin == Coin.CUSTOM) {
CustomExchangeData(coin, getJson(context))
} else {
ExchangeData(coin, getJson(context))
val data = ExchangeData(coin, getJson(context))
if (data.numberExchanges == 0) {
throw JsonParseException("No exchanges found.")
}
data
}
} catch(e: JsonSyntaxException) {
} catch(e: JsonParseException) {
Log.e("SettingsViewModel", "Error parsing JSON file, falling back to original.", e)
context.deleteFile(Repository.CURRENCY_FILE_NAME)
PreferenceManager.getDefaultSharedPreferences(context).edit {
remove(Repository.LAST_MODIFIED)
}
ExchangeData(coin, getJson(context))
} catch (e: FileNotFoundException) {
throw RuntimeException(e)
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/main/res/raw/cryptowidgetcoins_v2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bitcoin/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<item>ISO</item>
<item>NONE</item>
</string-array>
<string name="json_last_modified" translatable="false">Thu, 17 Feb 2022 16:32:00 GMT</string>
<string name="json_last_modified" translatable="false">Sat, 26 Feb 2022 22:20:07 GMT</string>
<string name="json_url" translatable="false">https://www.brentpanther.com/cryptowidgetcoins_v2.json</string>

<string name="settings_updating_data">Updating coin data…</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ class GenerateSupportedCoinsJson {
}

private fun bitrue(): List<String> {
return parse("https://openapi.bitrue.com/api/v1/exchangeInfo", "$.symbols[*].symbol")
val pairs = parse("https://openapi.bitrue.com/api/v1/exchangeInfo", "$.symbols[*].symbol")
return pairs.filterNot { it.contains("USDC") }
}

private fun bitvavo(): List<String> {
Expand Down Expand Up @@ -528,7 +529,8 @@ class GenerateSupportedCoinsJson {
}

private fun okx(): List<String> {
return parse("https://www.okx.com/api/v5/public/instruments?instType=SPOT", "$.data[*].instId")
val pairs = parse("https://www.okx.com/api/v5/public/instruments?instType=SPOT", "$.data[*].instId")
return pairs.filterNot { it.contains("USDC") }
}

private fun p2pb2b(): List<String> {
Expand Down
4 changes: 4 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/296.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Removed Abucoins and Urdubit.
Renamed OKEx to OKX.
Renamed Bithumb Pro to Biglobal.
Added BitMart, Digifinex, Hoo, Mexc, XT.com, and Yadio exchanges.

0 comments on commit 99e5efb

Please sign in to comment.