CustomDialogFragment.kt (3024B)
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.AmountParserException 28 import net.taler.common.Amount 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 val currencySpec = viewModel.configManager.currencySpec 56 ui.currencyView.text = currency 57 ui.addButton.setOnClickListener { 58 val currentOrderId = 59 viewModel.orderManager.currentOrderId.value ?: return@setOnClickListener 60 val amount = try { 61 Amount.fromString(currency, ui.amountLayout.editText!!.text.toString()) 62 .withSpec(currencySpec) 63 } catch (e: AmountParserException) { 64 Toast.makeText(requireContext(), R.string.refund_error_invalid_amount, LENGTH_LONG) 65 .show() 66 return@setOnClickListener 67 } 68 val product = ConfigProduct( 69 description = ui.productNameLayout.editText!!.text.toString(), 70 price = amount, 71 categories = listOf(Int.MIN_VALUE), 72 ) 73 viewModel.orderManager.addProduct(currentOrderId, product) 74 dismiss() 75 } 76 ui.cancelButton.setOnClickListener { 77 dismiss() 78 } 79 } 80 }