aboutsummaryrefslogtreecommitdiff
path: root/merchant-terminal/src
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-03-20 15:43:23 -0300
committerTorsten Grote <t@grobox.de>2020-03-20 15:43:23 -0300
commitdb8b71418b766258a7a4bda91e496b1b03cb28cd (patch)
treebd221d1a7ecf77d7752ea7a6086da344e74619cc /merchant-terminal/src
parent82b8b57dc16112b859150696199774fcf06655e1 (diff)
downloadtaler-android-db8b71418b766258a7a4bda91e496b1b03cb28cd.tar.gz
taler-android-db8b71418b766258a7a4bda91e496b1b03cb28cd.tar.bz2
taler-android-db8b71418b766258a7a4bda91e496b1b03cb28cd.zip
Let all apps use the same Amount class
The wallet now also uses taler-kotlin-common
Diffstat (limited to 'merchant-terminal/src')
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/config/MerchantConfig.kt5
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt2
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/history/MerchantHistoryFragment.kt4
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundFragment.kt6
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundUriFragment.kt4
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt10
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt12
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderManager.kt9
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderStateFragment.kt7
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/order/ProductsFragment.kt2
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt2
-rw-r--r--merchant-terminal/src/main/java/net/taler/merchantpos/payment/ProcessPaymentFragment.kt4
-rw-r--r--merchant-terminal/src/main/res/values/strings.xml2
13 files changed, 32 insertions, 37 deletions
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/config/MerchantConfig.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/config/MerchantConfig.kt
index 8141f0f..0e707d3 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/config/MerchantConfig.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/config/MerchantConfig.kt
@@ -68,14 +68,15 @@ data class ConfigProduct(
override val productId: String?,
override val description: String,
override val descriptionI18n: Map<String, String>?,
- override val price: String,
+ override val price: Amount,
override val location: String?,
override val image: String?,
val categories: List<Int>,
@JsonIgnore
val quantity: Int = 0
) : Product() {
- val priceAsDouble by lazy { Amount.fromString(price).amount.toDouble() }
+ @get:JsonIgnore
+ val totalPrice by lazy { price * quantity }
fun toContractProduct() = ContractProduct(
productId = productId,
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt
index fc3f93a..3aaf3a4 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/history/HistoryManager.kt
@@ -42,7 +42,7 @@ data class HistoryItem(
val timestamp: Timestamp
) {
@get:JsonIgnore
- val amount: Amount by lazy { Amount.fromString(amountStr) }
+ val amount: Amount by lazy { Amount.fromJSONString(amountStr) }
@get:JsonIgnore
val time = timestamp.ms
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/history/MerchantHistoryFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/history/MerchantHistoryFragment.kt
index afa925d..1099eda 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/history/MerchantHistoryFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/history/MerchantHistoryFragment.kt
@@ -16,7 +16,6 @@
package net.taler.merchantpos.history
-import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
@@ -148,8 +147,7 @@ private class HistoryItemAdapter(private val listener: RefundClickListener) :
fun bind(item: HistoryItem) {
orderSummaryView.text = item.summary
val amount = item.amount
- @SuppressLint("SetTextI18n")
- orderAmountView.text = "${amount.amount} ${amount.currency}"
+ orderAmountView.text = amount.toString()
orderIdView.text = v.context.getString(R.string.history_ref_no, item.orderId)
orderTimeView.text = item.time.toRelativeTime(v.context)
refundButton.setOnClickListener { listener.onRefundClicked(item) }
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundFragment.kt
index aa2489a..609eadd 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundFragment.kt
@@ -52,7 +52,7 @@ class RefundFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val item = refundManager.toBeRefunded ?: throw IllegalStateException()
- amountInputView.setText(item.amount.amount)
+ amountInputView.setText(item.amount.toString())
currencyView.text = item.amount.currency
abortButton.setOnClickListener { findNavController().navigateUp() }
refundButton.setOnClickListener { onRefundButtonClicked(item) }
@@ -64,8 +64,8 @@ class RefundFragment : Fragment() {
private fun onRefundButtonClicked(item: HistoryItem) {
val inputAmount = amountInputView.text.toString().toDouble()
- if (inputAmount > item.amount.amount.toDouble()) {
- amountView.error = getString(R.string.refund_error_max_amount, item.amount.amount)
+ if (inputAmount > item.amountStr.toDouble()) { // TODO real Amount comparision
+ amountView.error = getString(R.string.refund_error_max_amount, item.amountStr)
return
}
if (inputAmount <= 0.0) {
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundUriFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundUriFragment.kt
index 6e5b96d..1bc4002 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundUriFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/history/RefundUriFragment.kt
@@ -16,7 +16,6 @@
package net.taler.merchantpos.history
-import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@@ -53,8 +52,7 @@ class RefundUriFragment : Fragment() {
if (hasNfc(requireContext())) R.string.refund_intro_nfc else R.string.refund_intro
refundIntroView.setText(introRes)
- @SuppressLint("SetTextI18n")
- refundAmountView.text = "${result.amount} ${result.item.amount.currency}"
+ refundAmountView.text = result.amount.toString()
refundRefView.text =
getString(R.string.refund_order_ref, result.item.orderId, result.reason)
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt
index 847326b..f8d465b 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt
@@ -20,6 +20,7 @@ import androidx.annotation.UiThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
+import net.taler.common.Amount
import net.taler.common.CombinedLiveData
import net.taler.merchantpos.config.Category
import net.taler.merchantpos.config.ConfigProduct
@@ -31,7 +32,7 @@ internal enum class RestartState { ENABLED, DISABLED, UNDO }
internal interface LiveOrder {
val order: LiveData<Order>
- val orderTotal: LiveData<Double>
+ val orderTotal: LiveData<Amount>
val restartState: LiveData<RestartState>
val modifyOrderAllowed: LiveData<Boolean>
val lastAddedProduct: ConfigProduct?
@@ -44,12 +45,13 @@ internal interface LiveOrder {
internal class MutableLiveOrder(
val id: Int,
+ private val currency: String,
private val productsByCategory: HashMap<Category, ArrayList<ConfigProduct>>
) : LiveOrder {
private val availableCategories: Map<Int, Category>
get() = productsByCategory.keys.map { it.id to it }.toMap()
- override val order: MutableLiveData<Order> = MutableLiveData(Order(id, availableCategories))
- override val orderTotal: LiveData<Double> = Transformations.map(order) { it.total }
+ override val order: MutableLiveData<Order> = MutableLiveData(Order(id, currency, availableCategories))
+ override val orderTotal: LiveData<Amount> = Transformations.map(order) { it.total }
override val restartState = MutableLiveData(DISABLED)
private val selectedOrderLine = MutableLiveData<ConfigProduct>()
override val selectedProductKey: String?
@@ -86,7 +88,7 @@ internal class MutableLiveOrder(
undoOrder = null
} else {
undoOrder = order.value
- order.value = Order(id, availableCategories)
+ order.value = Order(id, currency, availableCategories)
restartState.value = UNDO
}
}
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
index 5954e63..ff6e6b7 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/Order.kt
@@ -16,10 +16,11 @@
package net.taler.merchantpos.order
+import net.taler.common.Amount
import net.taler.merchantpos.config.Category
import net.taler.merchantpos.config.ConfigProduct
-data class Order(val id: Int, val availableCategories: Map<Int, Category>) {
+data class Order(val id: Int, val currency: String, val availableCategories: Map<Int, Category>) {
val products = ArrayList<ConfigProduct>()
val title: String = id.toString()
val summary: String
@@ -29,17 +30,14 @@ data class Order(val id: Int, val availableCategories: Map<Int, Category>) {
"$quantity x ${category.localizedName}"
}.joinToString()
}
- val total: Double
+ val total: Amount
get() {
- var total = 0.0
+ var total = Amount.zero(currency)
products.forEach { product ->
- val price = product.priceAsDouble
- total += price * product.quantity
+ total += product.price * product.quantity
}
return total
}
- val totalAsString: String
- get() = String.format("%.2f", total)
operator fun plus(product: ConfigProduct): Order {
val i = products.indexOf(product)
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderManager.kt
index a30c264..ff2be48 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderManager.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderManager.kt
@@ -24,7 +24,6 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
-import net.taler.common.Amount.Companion.fromString
import net.taler.merchantpos.R
import net.taler.merchantpos.config.Category
import net.taler.merchantpos.config.ConfigProduct
@@ -41,6 +40,7 @@ class OrderManager(
val TAG = OrderManager::class.java.simpleName
}
+ private lateinit var currency: String
private var orderCounter: Int = 0
private val mCurrentOrderId = MutableLiveData<Int>()
internal val currentOrderId: LiveData<Int> = mCurrentOrderId
@@ -75,7 +75,7 @@ class OrderManager(
// group products by categories
productsByCategory.clear()
products.forEach { product ->
- val productCurrency = fromString(product.price).currency
+ val productCurrency = product.price.currency
if (productCurrency != currency) {
Log.e(TAG, "Product $product has currency $productCurrency, $currency expected")
return context.getString(
@@ -98,12 +98,13 @@ class OrderManager(
}
}
return if (productsByCategory.size > 0) {
+ this.currency = currency
mCategories.postValue(categories)
mProducts.postValue(productsByCategory[categories[0]])
// Initialize first empty order, note this won't work when updating config mid-flight
if (orders.isEmpty()) {
val id = orderCounter++
- orders[id] = MutableLiveOrder(id, productsByCategory)
+ orders[id] = MutableLiveOrder(id, currency, productsByCategory)
mCurrentOrderId.postValue(id)
}
null // success, no error string
@@ -129,7 +130,7 @@ class OrderManager(
}
if (nextId == null) {
nextId = orderCounter++
- orders[nextId] = MutableLiveOrder(nextId, productsByCategory)
+ orders[nextId] = MutableLiveOrder(nextId, currency, productsByCategory)
}
val currentOrder = order(currentId)
if (currentOrder.isEmpty()) orders.remove(currentId)
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderStateFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderStateFragment.kt
index a90334b..f792d7a 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderStateFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/OrderStateFragment.kt
@@ -95,12 +95,11 @@ class OrderStateFragment : Fragment() {
onOrderChanged(order, tracker)
})
liveOrder.orderTotal.observe(viewLifecycleOwner, Observer { orderTotal ->
- if (orderTotal == 0.0) {
+ if (orderTotal.isZero()) {
totalView.fadeOut()
totalView.text = null
} else {
- val currency = viewModel.configManager.merchantConfig?.currency
- totalView.text = getString(R.string.order_total, orderTotal, currency)
+ totalView.text = getString(R.string.order_total, orderTotal)
totalView.fadeIn()
}
})
@@ -184,7 +183,7 @@ private class OrderAdapter : Adapter<OrderViewHolder>() {
v.isActivated = selected
quantity.text = product.quantity.toString()
name.text = product.localizedDescription
- price.text = String.format("%.2f", product.priceAsDouble * product.quantity)
+ price.text = product.totalPrice.amountStr
}
}
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/order/ProductsFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/order/ProductsFragment.kt
index d4da73f..00eb509 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/order/ProductsFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/order/ProductsFragment.kt
@@ -104,7 +104,7 @@ private class ProductAdapter(
fun bind(product: ConfigProduct) {
name.text = product.localizedDescription
- price.text = product.priceAsDouble.toString()
+ price.text = product.price.amountStr
v.setOnClickListener { listener.onProductSelected(product) }
}
}
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
index 4cfb069..f83370e 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/PaymentManager.kt
@@ -71,7 +71,7 @@ class PaymentManager(
val merchantConfig = configManager.merchantConfig!!
val currency = merchantConfig.currency!!
- val amount = "$currency:${order.totalAsString}"
+ val amount = order.total.toJSONString()
val summary = order.summary
val summaryI18n = order.summaryI18n
diff --git a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/ProcessPaymentFragment.kt b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/ProcessPaymentFragment.kt
index 1d61894..9c9457c 100644
--- a/merchant-terminal/src/main/java/net/taler/merchantpos/payment/ProcessPaymentFragment.kt
+++ b/merchant-terminal/src/main/java/net/taler/merchantpos/payment/ProcessPaymentFragment.kt
@@ -16,7 +16,6 @@
package net.taler.merchantpos.payment
-import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@@ -73,8 +72,7 @@ class ProcessPaymentFragment : Fragment() {
return
}
payIntroView.fadeIn()
- @SuppressLint("SetTextI18n")
- amountView.text = "${payment.order.totalAsString} ${payment.currency}"
+ amountView.text = payment.order.total.toString()
payment.orderId?.let {
orderRefView.text = getString(R.string.payment_order_ref, it)
orderRefView.fadeIn()
diff --git a/merchant-terminal/src/main/res/values/strings.xml b/merchant-terminal/src/main/res/values/strings.xml
index ae82f96..863ae6f 100644
--- a/merchant-terminal/src/main/res/values/strings.xml
+++ b/merchant-terminal/src/main/res/values/strings.xml
@@ -9,7 +9,7 @@
<string name="order_label_title">Order #%s</string>
<!-- The first placeholder is the amount and the second the currency -->
- <string name="order_total">Total: %1$.2f %2$s</string>
+ <string name="order_total">Total: %s</string>
<string name="order_restart">Restart</string>
<string name="order_undo">Undo</string>
<string name="order_previous">Prev</string>