taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

CustomDialogFragment.kt (2916B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2023 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.merchantpos.order
     18 
     19 import android.os.Bundle
     20 import android.view.LayoutInflater
     21 import android.view.View
     22 import android.view.ViewGroup
     23 import android.widget.Toast
     24 import android.widget.Toast.LENGTH_LONG
     25 import androidx.fragment.app.DialogFragment
     26 import androidx.fragment.app.activityViewModels
     27 import net.taler.common.Amount
     28 import net.taler.common.AmountParserException
     29 import net.taler.merchantpos.MainViewModel
     30 import net.taler.merchantpos.R
     31 import net.taler.merchantpos.config.ConfigProduct
     32 import net.taler.merchantpos.databinding.FragmentCustomDialogBinding
     33 
     34 class CustomDialogFragment : DialogFragment() {
     35 
     36     companion object {
     37         const val TAG = "CustomDialogFragment"
     38     }
     39 
     40     private val viewModel: MainViewModel by activityViewModels()
     41 
     42     private lateinit var ui: FragmentCustomDialogBinding
     43 
     44     override fun onCreateView(
     45         inflater: LayoutInflater,
     46         container: ViewGroup?,
     47         savedInstanceState: Bundle?,
     48     ): View {
     49         ui = FragmentCustomDialogBinding.inflate(inflater, container, false)
     50         return ui.root
     51     }
     52 
     53     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     54         val currency = viewModel.configManager.currency ?: error("No currency")
     55         ui.currencyView.text = currency
     56         ui.addButton.setOnClickListener {
     57             val currentOrderId =
     58                 viewModel.orderManager.currentOrderId.value ?: return@setOnClickListener
     59             val amount = try {
     60                 Amount.fromString(currency, ui.amountLayout.editText!!.text.toString())
     61             } catch (e: AmountParserException) {
     62                 Toast.makeText(requireContext(), R.string.refund_error_invalid_amount, LENGTH_LONG)
     63                     .show()
     64                 return@setOnClickListener
     65             }
     66             val product = ConfigProduct(
     67                 description = ui.productNameLayout.editText!!.text.toString(),
     68                 price = amount,
     69                 categories = listOf(Int.MIN_VALUE),
     70             )
     71             viewModel.orderManager.addProduct(currentOrderId, product)
     72             dismiss()
     73         }
     74         ui.cancelButton.setOnClickListener {
     75             dismiss()
     76         }
     77     }
     78 }