commit c8dbdaf02d200287cc1242f4826a6c9637398640
parent 0e28f811d20457925615e9dfe4a5471fb7689b05
Author: Torsten Grote <t@grobox.de>
Date: Wed, 26 Aug 2020 09:54:47 -0300
[cashier] adapt to latest bank API changes (no more SignedAmount)
Diffstat:
5 files changed, 44 insertions(+), 51 deletions(-)
diff --git a/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt b/cashier/src/main/java/net/taler/cashier/BalanceFragment.kt
@@ -33,7 +33,6 @@ import kotlinx.android.synthetic.main.fragment_balance.*
import net.taler.cashier.BalanceFragmentDirections.Companion.actionBalanceFragmentToTransactionFragment
import net.taler.cashier.withdraw.LastTransaction
import net.taler.cashier.withdraw.WithdrawStatus
-import net.taler.common.SignedAmount
import net.taler.common.exhaustive
import net.taler.common.fadeIn
import net.taler.common.fadeOut
diff --git a/cashier/src/main/java/net/taler/cashier/MainViewModel.kt b/cashier/src/main/java/net/taler/cashier/MainViewModel.kt
@@ -34,9 +34,9 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.taler.cashier.HttpHelper.makeJsonGetRequest
import net.taler.cashier.withdraw.WithdrawManager
-import net.taler.common.SignedAmount
import net.taler.common.getIncompatibleStringOrNull
import net.taler.common.isOnline
+import net.taler.lib.common.Amount
import net.taler.lib.common.AmountParserException
import net.taler.lib.common.Version
@@ -94,8 +94,7 @@ class MainViewModel(private val app: Application) : AndroidViewModel(app) {
val result = when (val response = makeJsonGetRequest(url, config)) {
is HttpJsonResult.Success -> {
// check if bank's version is compatible with app
- // TODO use real version response when fixed in bank
- val version = "0:0:0" // response.json.getString("version")
+ val version = response.json.getString("version")
val versionIncompatible = VERSION_BANK.getIncompatibleStringOrNull(app, version)
if (versionIncompatible != null) {
ConfigResult.Error(false, versionIncompatible)
@@ -141,11 +140,18 @@ class MainViewModel(private val app: Application) : AndroidViewModel(app) {
Log.d(TAG, "Checking balance at $url")
val result = when (val response = makeJsonGetRequest(url, config)) {
is HttpJsonResult.Success -> {
- val balance = response.json.getString("balance")
try {
- BalanceResult.Success(SignedAmount.fromJSONString(balance))
- } catch (e: AmountParserException) {
- BalanceResult.Error("invalid amount: $balance")
+ val balance = response.json.getString("balance")
+ val positive = when (val creditDebitIndicator =
+ response.json.getString("credit_debit_indicator")) {
+ "credit" -> true
+ "debit" -> false
+ else -> throw AmountParserException("Unexpected credit_debit_indicator: $creditDebitIndicator")
+ }
+ BalanceResult.Success(SignedAmount(positive, Amount.fromJSONString(balance)))
+ } catch (e: Exception) {
+ Log.e(TAG, "Error parsing balance", e)
+ BalanceResult.Error("Invalid amount:\n${response.json.toString(2)}")
}
}
is HttpJsonResult.Error -> {
diff --git a/cashier/src/main/java/net/taler/cashier/SignedAmount.kt b/cashier/src/main/java/net/taler/cashier/SignedAmount.kt
@@ -0,0 +1,30 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.cashier
+
+import net.taler.lib.common.Amount
+
+data class SignedAmount(
+ val positive: Boolean,
+ val amount: Amount
+) {
+
+ override fun toString(): String {
+ return if (positive) "$amount" else "-$amount"
+ }
+
+}
diff --git a/taler-kotlin-android/build.gradle b/taler-kotlin-android/build.gradle
@@ -68,7 +68,7 @@ dependencies {
// JSON parsing and serialization
api "org.jetbrains.kotlinx:kotlinx-serialization-core:1.0.0-RC"
- lintPublish 'com.github.thirdegg:lint-rules:0.0.4-alpha'
+ lintPublish 'com.github.thirdegg:lint-rules:0.0.5-alpha'
testImplementation 'junit:junit:4.13'
testImplementation 'org.json:json:20200518'
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/SignedAmount.kt b/taler-kotlin-android/src/main/java/net/taler/common/SignedAmount.kt
@@ -1,42 +0,0 @@
-/*
- * This file is part of GNU Taler
- * (C) 2020 Taler Systems S.A.
- *
- * GNU Taler is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free Software
- * Foundation; either version 3, or (at your option) any later version.
- *
- * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
- */
-
-package net.taler.common
-
-import android.annotation.SuppressLint
-import net.taler.lib.common.Amount
-import net.taler.lib.common.AmountParserException
-
-data class SignedAmount(
- val positive: Boolean,
- val amount: Amount
-) {
-
- companion object {
- @Throws(AmountParserException::class)
- @SuppressLint("CheckedExceptions")
- fun fromJSONString(str: String): SignedAmount = when (str.substring(0, 1)) {
- "-" -> SignedAmount(false, Amount.fromJSONString(str.substring(1)))
- "+" -> SignedAmount(true, Amount.fromJSONString(str.substring(1)))
- else -> SignedAmount(true, Amount.fromJSONString(str))
- }
- }
-
- override fun toString(): String {
- return if (positive) "$amount" else "-$amount"
- }
-
-}