-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewCartDialog.kt
94 lines (81 loc) · 3.1 KB
/
ViewCartDialog.kt
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
package io.github.assis10t.bobandroid
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import io.github.assis10t.bobandroid.pojo.Item
import io.github.assis10t.bobandroid.pojo.Order
import kotlinx.android.synthetic.main.dialog_view_cart.*
import org.w3c.dom.Text
import timber.log.Timber
class ViewCartDialog(context: Context, val warehouseId: String): Dialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val data = getCart(context)
if (data.isEmpty()) {
setContentView(R.layout.dialog_view_cart_empty)
return
} else {
setContentView(R.layout.dialog_view_cart)
}
cart.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
cart.adapter = CartAdapter(data)
val totalAmount =
if (data.isEmpty())
0.0
else
data
.map{ it.quantity!! * it.price }
.reduce { a, b -> a + b}
total.text = "£${"%.2f".format(totalAmount)}"
clear.setOnClickListener {
clearCart(context)
dismiss()
}
complete_order.setOnClickListener {
val order = Order.Factory()
.items(data)
.warehouseId(warehouseId)
.build()
ServerConnection()
.makeOrder(context, order) { err ->
if (err != null) {
Toast.makeText(context, err.message, Toast.LENGTH_LONG).show()
return@makeOrder
}
clearCart(context)
dismiss()
}
}
}
class CartAdapter(val items: List<Item>): RecyclerView.Adapter<CartAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_cart_item, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(vh: ViewHolder, pos: Int) {
val item = items[pos]
vh.name.text = item.name
vh.quantity.text = "${item.quantity}"
vh.price.text =
if (item.unit == null)
"£${"%.2f".format(item.price)}"
else
"£${"%.2f".format(item.price)}/${item.unit}"
}
class ViewHolder(v: View): RecyclerView.ViewHolder(v) {
val name: TextView = v.findViewById(R.id.name)
val quantity: TextView = v.findViewById(R.id.quantity)
val price: TextView = v.findViewById(R.id.price)
}
}
}