1+ package io.snabble.sdk
2+
3+ import android.util.LruCache
4+ import kotlin.jvm.JvmOverloads
5+ import io.snabble.sdk.codes.ScannedCode
6+ import java.math.BigDecimal
7+ import java.text.NumberFormat
8+
9+ /* *
10+ * A price formatter for formatting prices using the provided currency information.
11+ *
12+ */
13+ class PriceFormatter (private val project : Project ) {
14+ private val numberFormat: NumberFormat = NumberFormat .getCurrencyInstance(project.currencyLocale)
15+ private val cache: LruCache <Int , String > = LruCache (100 )
16+
17+ init {
18+ numberFormat.currency = project.currency
19+ val fractionDigits = project.currencyFractionDigits
20+ numberFormat.minimumFractionDigits = fractionDigits
21+ numberFormat.maximumFractionDigits = fractionDigits
22+ }
23+
24+ /* *
25+ * Format a price.
26+ */
27+ @JvmOverloads
28+ fun format (price : Int , allowZeroPrice : Boolean = true): String {
29+ if (price == 0 && ! allowZeroPrice) {
30+ return " "
31+ }
32+
33+ val cachedValue = cache[price]
34+ if (cachedValue != null ) {
35+ return cachedValue
36+ }
37+
38+ val fractionDigits = project.currencyFractionDigits
39+ val bigDecimal = BigDecimal (price)
40+ val divider = BigDecimal (10 ).pow(fractionDigits)
41+ val dividedPrice = bigDecimal.divide(divider, fractionDigits, project.roundingMode)
42+ val formattedPrice = numberFormat.format(dividedPrice)
43+
44+ // Android 4.x and 6 (but not 5 and 7+) are shipping with ICU versions
45+ // that have the currency symbol set to HUF instead of Ft for Locale hu_HU
46+ //
47+ // including the whole ICU library as a dependency increased APK size by 10MB
48+ // so we are overriding the result here instead for consistency
49+ if (project.currency.currencyCode == " HUF" ) {
50+ return formattedPrice.replace(" HUF" , " Ft" )
51+ }
52+
53+ cache.put(price, formattedPrice)
54+ return formattedPrice
55+ }
56+
57+ /* *
58+ * Format a price of a Product.
59+ *
60+ * Display's in units for example gram's if a Product is using conversion units.
61+ */
62+ fun format (product : Product , price : Int ): String {
63+ var formattedString = format(price, false )
64+ val type = product.type
65+ var referenceUnit = product.referenceUnit
66+ if (referenceUnit == null ) {
67+ referenceUnit = Unit .KILOGRAM
68+ }
69+ if (type == Product .Type .UserWeighed || type == Product .Type .PreWeighed ) {
70+ formattedString + = " / " + referenceUnit.displayValue
71+ }
72+ return formattedString
73+ }
74+
75+ /* *
76+ * Format a price of a Product or a ScannedCode if the ScannedCode is containing price information.
77+ *
78+ * Display's in units for example gram's if a Product is using conversion units.
79+ */
80+ @JvmOverloads
81+ fun format (product : Product , discountedPrice : Boolean = true, scannedCode : ScannedCode ? = null): String {
82+ var price = product.listPrice
83+ if (discountedPrice) {
84+ price = product.getPrice(project.customerCardId)
85+ }
86+ if (scannedCode != null && scannedCode.hasPrice()) {
87+ price = scannedCode.price
88+ }
89+ return format(product, price)
90+ }
91+ }
0 commit comments