PosErrorDialog.kt (3007B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2026 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 18 19 import android.app.Dialog 20 import android.os.Bundle 21 import androidx.annotation.StringRes 22 import androidx.fragment.app.DialogFragment 23 import androidx.fragment.app.Fragment 24 import androidx.fragment.app.FragmentActivity 25 import com.google.android.material.dialog.MaterialAlertDialogBuilder 26 27 private const val POS_ERROR_DIALOG_TAG = "POS_ERROR_DIALOG" 28 29 class PosErrorDialogFragment : DialogFragment() { 30 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 31 val args = requireArguments() 32 val mainText = args.getString(ARG_MAIN_TEXT).orEmpty() 33 val detailText = args.getString(ARG_DETAIL_TEXT).orEmpty() 34 val titleText = if (detailText.isBlank()) { 35 getString(R.string.app_name_short) 36 } else { 37 mainText 38 } 39 val messageText = if (detailText.isBlank()) { 40 mainText 41 } else { 42 detailText 43 } 44 return MaterialAlertDialogBuilder(requireContext()) 45 .setTitle(titleText) 46 .setMessage(messageText) 47 .setPositiveButton(android.R.string.ok, null) 48 .create() 49 } 50 51 companion object { 52 private const val ARG_MAIN_TEXT = "main_text" 53 private const val ARG_DETAIL_TEXT = "detail_text" 54 55 fun newInstance(mainText: String, detailText: String = "") = PosErrorDialogFragment().apply { 56 arguments = Bundle().apply { 57 putString(ARG_MAIN_TEXT, mainText) 58 putString(ARG_DETAIL_TEXT, detailText) 59 } 60 } 61 } 62 } 63 64 fun FragmentActivity.showPosError(mainText: String, detailText: String = "") { 65 (supportFragmentManager.findFragmentByTag(POS_ERROR_DIALOG_TAG) as? DialogFragment)?.dismissAllowingStateLoss() 66 PosErrorDialogFragment.newInstance(mainText, detailText) 67 .show(supportFragmentManager, POS_ERROR_DIALOG_TAG) 68 } 69 70 fun FragmentActivity.showPosError(@StringRes mainId: Int, detailText: String = "") { 71 showPosError(getString(mainId), detailText) 72 } 73 74 fun Fragment.showPosError(mainText: String, detailText: String = "") { 75 requireActivity().showPosError(mainText, detailText) 76 } 77 78 fun Fragment.showPosError(@StringRes mainId: Int, detailText: String = "") { 79 requireActivity().showPosError(mainId, detailText) 80 }