Skip to content

Commit b3490ba

Browse files
committed
Fixed Coins.ph.
Fixed a crash when adding a coin that does not have an associated image.
1 parent 59f40a5 commit b3490ba

File tree

8 files changed

+24
-24
lines changed

8 files changed

+24
-24
lines changed

bitcoin/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
applicationId "com.brentpanther.bitcoinwidget"
1414
minSdk 23
1515
targetSdk 34
16-
versionCode 323
17-
versionName "8.4.4"
16+
versionCode 324
17+
versionName "8.4.5"
1818

1919
}
2020

bitcoin/src/main/java/com/brentpanther/bitcoinwidget/Repository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ object Repository {
7979
return
8080
}
8181

82-
ExchangeHelper.getStream(url).use { stream ->
82+
ExchangeHelper.getStream(url)?.use { stream ->
8383
ByteArrayOutputStream().use { os ->
8484
BitmapFactory.decodeStream(stream)?.let { image ->
8585
image.compress(Bitmap.CompressFormat.PNG, 100, os)

bitcoin/src/main/java/com/brentpanther/bitcoinwidget/exchange/Exchange.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,8 @@ enum class Exchange(val exchangeName: String, shortName: String? = null) {
279279
COINSPH("Coins.ph") {
280280

281281
override fun getValue(coin: String, currency: String): String? {
282-
val region = if (currency == "THB") "TH" else "PH"
283-
val url = "https://quote.coins.ph/v2/markets/$coin-$currency?region=$region"
284-
val obj = getJsonObject(url)
285-
val bid = obj["bid"].asString?.toDoubleOrNull() ?: return null
286-
val ask = obj["ask"].asString?.toDoubleOrNull() ?: return null
287-
return ((bid + ask) / 2).toString()
282+
val url = "https://api.pro.coins.ph/openapi/quote/v1/avgPrice?symbol=$coin$currency"
283+
return getJsonObject(url)["price"].asString
288284
}
289285
},
290286
COINTREE("Cointree") {

bitcoin/src/main/java/com/brentpanther/bitcoinwidget/exchange/ExchangeHelper.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ object ExchangeHelper {
2828
@Throws(IOException::class)
2929
fun getJsonArray(url: String) = Json.decodeFromString<JsonArray>(getString(url))
3030

31-
fun getStream(url: String): InputStream = get(url).body!!.byteStream()
32-
33-
private fun getString(url: String, headers: Headers? = null) = get(url, headers).body!!.string()
34-
35-
private fun get(url: String, headers: Headers? = null): Response {
36-
var builder = Request.Builder().url(url)
37-
headers?.let {
38-
builder = builder.headers(it)
31+
fun getStream(url: String): InputStream? = get(url)?.body?.byteStream()
32+
33+
private fun getString(url: String, headers: Headers? = null) = get(url, headers)?.body?.string() ?: ""
34+
35+
private fun get(url: String, headers: Headers? = null): Response? {
36+
return try {
37+
var builder = Request.Builder().url(url)
38+
headers?.let {
39+
builder = builder.headers(it)
40+
}
41+
val request = builder.build()
42+
client.newCall(request).execute()
43+
} catch (e: IllegalArgumentException) {
44+
null
3945
}
40-
val request = builder.build()
41-
return client.newCall(request).execute()
4246
}
4347

4448
@Throws(IOException::class)

bitcoin/src/main/res/raw/cryptowidgetcoins_v2.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

bitcoin/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<item>ISO</item>
9595
<item>NONE</item>
9696
</string-array>
97-
<string name="json_last_modified" translatable="false">Wed, 29 Nov 2023 23:27:06 GMT</string>
97+
<string name="json_last_modified" translatable="false">Sun, 10 Dec 2023 22:44:10 GMT</string>
9898
<string name="json_url" translatable="false">https://www.brentpanther.com/cryptowidgetcoins_v2.json</string>
9999

100100
<string name="error_restricted_battery_saver">Unable to refresh, Battery Saver is on</string>

bitcoin/src/test/java/com/brentpanther/bitcoinwidget/GenerateSupportedCoinsJson.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,7 @@ class GenerateSupportedCoinsJson {
441441
}
442442

443443
private fun coinsph(): List<String> {
444-
val pairs1 = parse("https://quote.coins.ph/v2/markets", "$.markets[*].symbol")
445-
val pairs2 = parse("https://quote.coins.ph/v2/markets?region=TH", "$.markets[*].symbol")
446-
return pairs1 + pairs2
444+
return parse("https://api.pro.coins.ph/openapi/v1/pairs", "$[*].symbol")
447445
}
448446

449447
private fun cointree(): List<String> {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed Coins.ph.
2+
Fixed a crash when adding a coin that does not have an associated image.

0 commit comments

Comments
 (0)