summaryrefslogtreecommitdiff
path: root/merchant-terminal/src/main/java/net/taler/merchantpos/order/LiveOrder.kt
blob: a77e39b436121575aeb9f5bfa43437f0855bb26f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * 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.merchantpos.order

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
import net.taler.merchantpos.order.RestartState.DISABLED
import net.taler.merchantpos.order.RestartState.ENABLED
import net.taler.merchantpos.order.RestartState.UNDO

internal enum class RestartState { ENABLED, DISABLED, UNDO }

internal interface LiveOrder {
    val order: LiveData<Order>
    val orderTotal: LiveData<Amount>
    val restartState: LiveData<RestartState>
    val modifyOrderAllowed: LiveData<Boolean>
    val lastAddedProduct: ConfigProduct?
    val selectedProductKey: String?
    fun restartOrUndo()
    fun selectOrderLine(product: ConfigProduct?)
    fun increaseSelectedOrderLine()
    fun decreaseSelectedOrderLine()
}

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, 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?
        get() = selectedOrderLine.value?.id
    override val modifyOrderAllowed =
        CombinedLiveData(restartState, selectedOrderLine) { restartState, selectedOrderLine ->
            restartState != DISABLED && selectedOrderLine != null
        }
    override var lastAddedProduct: ConfigProduct? = null
    private var undoOrder: Order? = null

    @UiThread
    internal fun addProduct(product: ConfigProduct) {
        lastAddedProduct = product
        order.value = order.value!! + product
        restartState.value = ENABLED
    }

    @UiThread
    internal fun removeProduct(product: ConfigProduct) {
        val modifiedOrder = order.value!! - product
        order.value = modifiedOrder
        restartState.value = if (modifiedOrder.products.isEmpty()) DISABLED else ENABLED
    }

    @UiThread
    internal fun isEmpty() = order.value!!.products.isEmpty()

    @UiThread
    override fun restartOrUndo() {
        if (restartState.value == UNDO) {
            order.value = undoOrder
            restartState.value = ENABLED
            undoOrder = null
        } else {
            undoOrder = order.value
            order.value = Order(id, currency, availableCategories)
            restartState.value = UNDO
        }
    }

    @UiThread
    override fun selectOrderLine(product: ConfigProduct?) {
        selectedOrderLine.value = product
    }

    @UiThread
    override fun increaseSelectedOrderLine() {
        val orderLine = selectedOrderLine.value ?: throw IllegalStateException()
        addProduct(orderLine)
    }

    @UiThread
    override fun decreaseSelectedOrderLine() {
        val orderLine = selectedOrderLine.value ?: throw IllegalStateException()
        removeProduct(orderLine)
    }

}