summaryrefslogtreecommitdiff
path: root/app/src/main/java/net/taler/wallet/ShowBalance.kt
blob: b9e52ff62c354650c11134f8c96d66e74a26c6ce (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * 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.wallet


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.google.zxing.integration.android.IntentIntegrator
import com.google.zxing.integration.android.IntentIntegrator.QR_CODE_TYPES
import me.zhanghai.android.materialprogressbar.MaterialProgressBar
import org.json.JSONObject

class WalletBalanceAdapter(private var myDataset: WalletBalances, private var model: WalletViewModel) :
    RecyclerView.Adapter<WalletBalanceAdapter.MyViewHolder>() {

    init {
        setHasStableIds(false)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val rowView =
            LayoutInflater.from(parent.context).inflate(R.layout.balance_row, parent, false)
        return MyViewHolder(rowView)
    }

    override fun getItemCount(): Int {
        return myDataset.byCurrency.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val amount = myDataset.byCurrency[position].available
        val amountIncoming = myDataset.byCurrency[position].pendingIncoming
        val currencyView = holder.rowView.findViewById<TextView>(R.id.balance_currency)
        currencyView.text = amount.currency
        val amountView = holder.rowView.findViewById<TextView>(R.id.balance_amount)
        amountView.text = amount.amount

        val amountIncomingRow = holder.rowView.findViewById<View>(R.id.balance_row_pending)

        val amountIncomingView = holder.rowView.findViewById<TextView>(R.id.balance_pending)
        if (amountIncoming.isZero()) {
            amountIncomingRow.visibility = View.GONE
        } else {
            amountIncomingRow.visibility = View.VISIBLE
            amountIncomingView.text = "${amountIncoming.amount} ${amountIncoming.currency}"
        }
    }

    fun update(updatedBalances: WalletBalances) {
        this.myDataset = updatedBalances
        this.notifyDataSetChanged()
    }

    class MyViewHolder(val rowView: View) : RecyclerView.ViewHolder(rowView)
}

class PendingOperationsAdapter(private var myDataset: PendingOperations) :
    RecyclerView.Adapter<PendingOperationsAdapter.MyViewHolder>() {

    private var listener: PendingOperationClickListener? = null


    init {
        setHasStableIds(false)
    }

    fun setPendingOperationClickListener(listener: PendingOperationClickListener) {
        this.listener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val rowView =
            LayoutInflater.from(parent.context).inflate(R.layout.pending_row, parent, false)
        return MyViewHolder(rowView)
    }

    override fun getItemCount(): Int {
        return myDataset.pending.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val p = myDataset.pending[position]
        val pendingContainer = holder.rowView.findViewById<LinearLayout>(R.id.pending_container)
        pendingContainer.setOnClickListener {
            this.listener?.onPendingOperationClick(p.type, p.detail)
        }
        when (p.type) {
            "proposal-choice" -> {
                val btn1 = holder.rowView.findViewById<TextView>(R.id.button_pending_action_1)
                btn1.text = "Refuse Proposal"
                btn1.visibility = View.VISIBLE
                btn1.setOnClickListener {
                    this.listener?.onPendingOperationActionClick(p.type, p.detail)
                }
            }
            else -> {
                val btn1 = holder.rowView.findViewById<TextView>(R.id.button_pending_action_1)
                btn1.text = "(no action)"
                btn1.visibility = View.GONE
                btn1.setOnClickListener {}
            }
        }
        val textView = holder.rowView.findViewById<TextView>(R.id.pending_text)
        val subTextView = holder.rowView.findViewById<TextView>(R.id.pending_subtext)
        subTextView.text = p.detail.toString(1)
        textView.text = p.type
    }

    fun update(updatedDataset: PendingOperations) {
        this.myDataset = updatedDataset
        this.notifyDataSetChanged()
    }

    class MyViewHolder(val rowView: View) : RecyclerView.ViewHolder(rowView)
}

interface PendingOperationClickListener {
    fun onPendingOperationClick(type: String, detail: JSONObject)
    fun onPendingOperationActionClick(type: String, detail: JSONObject)
}

/**
 * A simple [Fragment] subclass.
 *
 */
class ShowBalance : Fragment(), PendingOperationClickListener {

    private lateinit var pendingOperationsLabel: View
    lateinit var balancesView: RecyclerView
    lateinit var balancesPlaceholderView: TextView
    lateinit var model: WalletViewModel
    lateinit var balancesAdapter: WalletBalanceAdapter

    lateinit var pendingAdapter: PendingOperationsAdapter

    private fun triggerLoading() {
        val loading: Boolean =
            (model.testWithdrawalInProgress.value == true) || (model.balances.value == null) || !model.balances.value!!.initialized

        val myActivity = activity!!
        val progressBar = myActivity.findViewById<MaterialProgressBar>(R.id.progress_bar)
        if (loading) {
            progressBar.visibility = View.VISIBLE
        } else {
            progressBar.visibility = View.INVISIBLE
        }
    }

    override fun onResume() {
        super.onResume()
        triggerLoading()
        Log.v("taler-wallet", "called onResume on ShowBalance")
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.retry_pending -> {
                model.retryPendingNow()
                true
            }
            R.id.reload_balance -> {
                triggerLoading()
                model.balances.value = WalletBalances(false, listOf())
                model.getBalances()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        activity?.menuInflater?.inflate(R.menu.balance, menu)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)

        model = activity?.run {
            ViewModelProviders.of(this)[WalletViewModel::class.java]
        } ?: throw Exception("Invalid Activity")

    }


    private fun updateBalances(balances: WalletBalances) {
        if (!balances.initialized) {
            balancesPlaceholderView.visibility = View.GONE
            balancesView.visibility = View.GONE
        } else if (balances.byCurrency.isEmpty()) {
            balancesPlaceholderView.visibility = View.VISIBLE
            balancesView.visibility = View.GONE
        } else {
            balancesPlaceholderView.visibility = View.GONE
            balancesView.visibility = View.VISIBLE
        }
        Log.v(TAG, "updating balances ${balances}")
        balancesAdapter.update(balances)
    }

    private fun updatePending(pendingOperations: PendingOperations) {
        if (pendingOperations.pending.size == 0) {
            pendingOperationsLabel.visibility = View.GONE
        } else {
            pendingOperationsLabel.visibility = View.VISIBLE
        }
        pendingAdapter.update(pendingOperations)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_show_balance, container, false)
        val payQrButton = view.findViewById<Button>(R.id.button_pay_qr)
        payQrButton.setOnClickListener {
            IntentIntegrator(activity).apply {
                setBeepEnabled(true)
                setOrientationLocked(false)
            }.initiateScan(QR_CODE_TYPES)
        }

        this.balancesView = view.findViewById(R.id.list_balances)
        this.balancesPlaceholderView = view.findViewById(R.id.list_balances_placeholder)

        val balances = model.balances.value!!

        balancesAdapter = WalletBalanceAdapter(balances, model)

        view.findViewById<RecyclerView>(R.id.list_balances).apply {
            val myLayoutManager = LinearLayoutManager(context)
            val myItemDecoration = DividerItemDecoration(context, myLayoutManager.orientation)
            layoutManager = myLayoutManager
            adapter = balancesAdapter
            addItemDecoration(myItemDecoration)
        }

        updateBalances(balances)

        model.balances.observe(viewLifecycleOwner, Observer {
            triggerLoading()
            updateBalances(it)
        })


        val withdrawTestkudosButton = view.findViewById<Button>(R.id.button_withdraw_testkudos)
        withdrawTestkudosButton.setOnClickListener {
            model.withdrawTestkudos()
        }

        model.testWithdrawalInProgress.observe(viewLifecycleOwner, Observer { loading ->
            Log.v("taler-wallet", "observing balance loading ${loading} in show balance")
            withdrawTestkudosButton.isEnabled = !loading
            triggerLoading()
        })

        pendingAdapter = PendingOperationsAdapter(PendingOperations(listOf()))
        pendingAdapter.setPendingOperationClickListener(this)

        this.pendingOperationsLabel = view.findViewById<View>(R.id.pending_operations_label)

        view.findViewById<RecyclerView>(R.id.list_pending).apply {
            val myLayoutManager = LinearLayoutManager(context)
            val myItemDecoration = DividerItemDecoration(context, myLayoutManager.orientation)
            layoutManager = myLayoutManager
            adapter = pendingAdapter
            addItemDecoration(myItemDecoration)
        }

        model.pendingOperations.observe(viewLifecycleOwner, Observer {
            updatePending(it)
        })

        return view
    }

    override fun onPendingOperationClick(type: String, detail: JSONObject) {
        val v = view ?: return
        when (type) {
            else -> {
                val bar = Snackbar.make(
                    v,
                    "No detail view for ${type} implemented yet.",
                    Snackbar.LENGTH_SHORT
                )
                bar.show()
            }
        }
    }

    override fun onPendingOperationActionClick(type: String, detail: JSONObject) {
        when (type) {
            "proposal-choice" -> {
                Log.v(TAG, "got action click on proposal-choice")
                val proposalId = detail.optString("proposalId", "")
                if (proposalId == "") {
                    return
                }
                model.paymentManager.abortProposal(proposalId)
            }
        }
    }
}