Skip to content

Commit

Permalink
Added option to show the wallet value of a given bitcoin address.
Browse files Browse the repository at this point in the history
Removed toast message for clicking on error icon on widgets, as this stopped working. Now show configuration screen with error message.
  • Loading branch information
hwki committed Nov 11, 2024
1 parent ded6ab7 commit 34e7bd4
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 97 deletions.
4 changes: 2 additions & 2 deletions bitcoin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "com.brentpanther.bitcoinwidget"
minSdk = 23
targetSdk = 35
versionCode = 335
versionName = "8.6.0"
versionCode = 336
versionName = "8.6.1"
}

buildFeatures {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "01c616eedee5c021678bbc985446b3ac",
"entities": [
{
"tableName": "Widget",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `widgetId` INTEGER NOT NULL, `widgetType` TEXT NOT NULL, `exchange` TEXT NOT NULL, `coin` TEXT NOT NULL, `currency` TEXT NOT NULL, `coinCustomId` TEXT, `coinCustomName` TEXT, `currencyCustomName` TEXT, `showExchangeLabel` INTEGER NOT NULL, `showCoinLabel` INTEGER NOT NULL, `showIcon` INTEGER NOT NULL, `numDecimals` INTEGER NOT NULL, `currencySymbol` TEXT, `theme` TEXT NOT NULL, `nightMode` TEXT NOT NULL, `coinUnit` TEXT, `currencyUnit` TEXT, `customIcon` TEXT, `portraitTextSize` INTEGER, `landscapeTextSize` INTEGER, `lastValue` TEXT, `amountHeld` REAL, `address` TEXT, `showAmountLabel` INTEGER NOT NULL, `useInverse` INTEGER NOT NULL, `lastUpdated` INTEGER NOT NULL, `state` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "widgetId",
"columnName": "widgetId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "widgetType",
"columnName": "widgetType",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "exchange",
"columnName": "exchange",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "coin",
"columnName": "coin",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "currency",
"columnName": "currency",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "coinCustomId",
"columnName": "coinCustomId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "coinCustomName",
"columnName": "coinCustomName",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "currencyCustomName",
"columnName": "currencyCustomName",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "showExchangeLabel",
"columnName": "showExchangeLabel",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "showCoinLabel",
"columnName": "showCoinLabel",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "showIcon",
"columnName": "showIcon",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "numDecimals",
"columnName": "numDecimals",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "currencySymbol",
"columnName": "currencySymbol",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "theme",
"columnName": "theme",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "nightMode",
"columnName": "nightMode",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "coinUnit",
"columnName": "coinUnit",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "currencyUnit",
"columnName": "currencyUnit",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "customIcon",
"columnName": "customIcon",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "portraitTextSize",
"columnName": "portraitTextSize",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "landscapeTextSize",
"columnName": "landscapeTextSize",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "lastValue",
"columnName": "lastValue",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "amountHeld",
"columnName": "amountHeld",
"affinity": "REAL",
"notNull": false
},
{
"fieldPath": "address",
"columnName": "address",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "showAmountLabel",
"columnName": "showAmountLabel",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "useInverse",
"columnName": "useInverse",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "lastUpdated",
"columnName": "lastUpdated",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "state",
"columnName": "state",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_Widget_widgetId",
"unique": true,
"columnNames": [
"widgetId"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_Widget_widgetId` ON `${TABLE_NAME}` (`widgetId`)"
}
],
"foreignKeys": []
},
{
"tableName": "Configuration",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `refresh` INTEGER NOT NULL, `consistentSize` INTEGER NOT NULL, `dataMigrationVersion` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "refresh",
"columnName": "refresh",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "consistentSize",
"columnName": "consistentSize",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "dataMigrationVersion",
"columnName": "dataMigrationVersion",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '01c616eedee5c021678bbc985446b3ac')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum class NightMode {
}

enum class WidgetState {
DRAFT, CURRENT, STALE, RATE_LIMITED, ERROR
DRAFT, CURRENT, STALE, RATE_LIMITED, ERROR, INVALID_ADDRESS
}

enum class WidgetType(@StringRes val widgetName: Int, @StringRes val widgetSummary: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ data class Widget(
var landscapeTextSize: Int? = null,
var lastValue: String? = null,
var amountHeld: Double? = null,
var address: String? = null,
var showAmountLabel: Boolean,
var useInverse: Boolean,
var lastUpdated: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.brentpanther.bitcoinwidget.db
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
Expand All @@ -11,7 +12,14 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import com.brentpanther.bitcoinwidget.WidgetApplication
import java.io.File

@Database(version = 5, entities = [Widget::class, Configuration::class], exportSchema = true)
@Database(
version = 6,
entities = [Widget::class, Configuration::class],
exportSchema = true,
autoMigrations = [
AutoMigration (from = 5, to = 6)
]
)
abstract class WidgetDatabase : RoomDatabase() {

abstract fun widgetDao(): WidgetDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object ExchangeHelper {

fun getStream(url: String): InputStream? = get(url)?.body?.byteStream()

private fun getString(url: String, headers: Headers? = null) = get(url, headers)?.body?.string() ?: ""
fun getString(url: String, headers: Headers? = null) = get(url, headers)?.body?.string() ?: ""

private fun get(url: String, headers: Headers? = null): Response? {
return try {
Expand All @@ -43,7 +43,7 @@ object ExchangeHelper {
throw RateLimitedException()
}
return response
} catch (e: IllegalArgumentException) {
} catch (_: IllegalArgumentException) {
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class WidgetBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_CONFIGURATION_CHANGED -> refreshAllWidgets(context)
"message" -> postMessage(context, intent.getIntExtra("message", 0))
"refresh" -> refreshWidget(context, intent)
}
}
Expand Down Expand Up @@ -47,7 +46,4 @@ class WidgetBroadcastReceiver : BroadcastReceiver() {
WidgetUpdater.update(context, intArrayOf(widgetId), manual = true)
}
}

private fun postMessage(context: Context, message: Int) =
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ open class PriceWidgetDataStrategy(widgetId: Int) : WidgetDataStrategy(widgetId)
if (widget.state != WidgetState.DRAFT) widget.state = WidgetState.STALE
}
Log.w(TAG, "Error getting value from exchange: ${widget.exchange}.", e)
} catch (e: RateLimitedException) {
} catch (_: RateLimitedException) {
widget.state = WidgetState.RATE_LIMITED
Log.w(TAG, "Exchange is rate limited: ${widget.exchange}.")
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
package com.brentpanther.bitcoinwidget.strategy.data

import com.brentpanther.bitcoinwidget.WidgetState
import com.brentpanther.bitcoinwidget.exchange.ExchangeHelper

class ValueWidgetDataStrategy(widgetId: Int) : PriceWidgetDataStrategy(widgetId) {

override fun setData(value: String) {
widget?.apply {
val valueDouble = value.toDoubleOrNull() ?: 0.0
val amountHeld = amountHeld ?: 0.0
lastValue = (valueDouble * amountHeld).toString()
val amountHeld = amountHeld
lastValue = when {
amountHeld != null -> (valueDouble * amountHeld).toString()
address != null -> {
val url = "https://blockchain.info/q/addressbalance/$address"
val response = ExchangeHelper.getString(url)
if ("error" in response) {
state = WidgetState.INVALID_ADDRESS
lastValue
}
else {
val balance = response.toDoubleOrNull() ?: throw IllegalStateException()
(valueDouble * balance * .00000001).toString()
}
}
else -> lastValue
}
lastUpdated = System.currentTimeMillis()
}

}

}
Loading

0 comments on commit 34e7bd4

Please sign in to comment.