1
0
Fork 0
forked from suyu/suyu

android: Add option to make MessageDialogFragments non-dismissible

Additionally fixes an issue where its viewmodel could hold onto a stale positive action
This commit is contained in:
t895 2024-01-24 12:38:12 -05:00
parent 97ca160b08
commit bc317a9807

View file

@ -26,9 +26,15 @@ class MessageDialogFragment : DialogFragment() {
val descriptionId = requireArguments().getInt(DESCRIPTION_ID) val descriptionId = requireArguments().getInt(DESCRIPTION_ID)
val descriptionString = requireArguments().getString(DESCRIPTION_STRING)!! val descriptionString = requireArguments().getString(DESCRIPTION_STRING)!!
val helpLinkId = requireArguments().getInt(HELP_LINK) val helpLinkId = requireArguments().getInt(HELP_LINK)
val dismissible = requireArguments().getBoolean(DISMISSIBLE)
val clearPositiveAction = requireArguments().getBoolean(CLEAR_POSITIVE_ACTION)
val builder = MaterialAlertDialogBuilder(requireContext()) val builder = MaterialAlertDialogBuilder(requireContext())
if (clearPositiveAction) {
messageDialogViewModel.positiveAction = null
}
if (messageDialogViewModel.positiveAction == null) { if (messageDialogViewModel.positiveAction == null) {
builder.setPositiveButton(R.string.close, null) builder.setPositiveButton(R.string.close, null)
} else { } else {
@ -51,6 +57,8 @@ class MessageDialogFragment : DialogFragment() {
} }
} }
isCancelable = dismissible
return builder.show() return builder.show()
} }
@ -67,6 +75,8 @@ class MessageDialogFragment : DialogFragment() {
private const val DESCRIPTION_ID = "DescriptionId" private const val DESCRIPTION_ID = "DescriptionId"
private const val DESCRIPTION_STRING = "DescriptionString" private const val DESCRIPTION_STRING = "DescriptionString"
private const val HELP_LINK = "Link" private const val HELP_LINK = "Link"
private const val DISMISSIBLE = "Dismissible"
private const val CLEAR_POSITIVE_ACTION = "ClearPositiveAction"
fun newInstance( fun newInstance(
activity: FragmentActivity? = null, activity: FragmentActivity? = null,
@ -75,22 +85,28 @@ class MessageDialogFragment : DialogFragment() {
descriptionId: Int = 0, descriptionId: Int = 0,
descriptionString: String = "", descriptionString: String = "",
helpLinkId: Int = 0, helpLinkId: Int = 0,
dismissible: Boolean = true,
positiveAction: (() -> Unit)? = null positiveAction: (() -> Unit)? = null
): MessageDialogFragment { ): MessageDialogFragment {
val dialog = MessageDialogFragment() var clearPositiveAction = false
val bundle = Bundle()
bundle.apply {
putInt(TITLE_ID, titleId)
putString(TITLE_STRING, titleString)
putInt(DESCRIPTION_ID, descriptionId)
putString(DESCRIPTION_STRING, descriptionString)
putInt(HELP_LINK, helpLinkId)
}
if (activity != null) { if (activity != null) {
ViewModelProvider(activity)[MessageDialogViewModel::class.java].apply { ViewModelProvider(activity)[MessageDialogViewModel::class.java].apply {
clear() clear()
this.positiveAction = positiveAction this.positiveAction = positiveAction
} }
} else {
clearPositiveAction = true
}
val dialog = MessageDialogFragment()
val bundle = Bundle().apply {
putInt(TITLE_ID, titleId)
putString(TITLE_STRING, titleString)
putInt(DESCRIPTION_ID, descriptionId)
putString(DESCRIPTION_STRING, descriptionString)
putInt(HELP_LINK, helpLinkId)
putBoolean(DISMISSIBLE, dismissible)
putBoolean(CLEAR_POSITIVE_ACTION, clearPositiveAction)
} }
dialog.arguments = bundle dialog.arguments = bundle
return dialog return dialog