diff options
| author | 2024-02-12 16:54:19 -0500 | |
|---|---|---|
| committer | 2024-02-12 16:54:19 -0500 | |
| commit | fbc1b61bff841badd76bdc55050b36c31fa70374 (patch) | |
| tree | e2243bd5369e753e3d32a38325f94894a4bcf6f1 | |
| parent | Merge pull request #12756 from liamwhite/applet-multiprocess-hwc (diff) | |
| download | yuzu-fbc1b61bff841badd76bdc55050b36c31fa70374.tar.gz yuzu-fbc1b61bff841badd76bdc55050b36c31fa70374.tar.xz yuzu-fbc1b61bff841badd76bdc55050b36c31fa70374.zip | |
android: Extend MessageDialogFragment to support a negative action and button titles
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/MessageDialogFragment.kt | 124 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/model/MessageDialogViewModel.kt | 2 |
2 files changed, 104 insertions, 22 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/MessageDialogFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/MessageDialogFragment.kt index 22b084b9a..685df0d59 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/MessageDialogFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/MessageDialogFragment.kt | |||
| @@ -4,7 +4,6 @@ | |||
| 4 | package org.yuzu.yuzu_emu.fragments | 4 | package org.yuzu.yuzu_emu.fragments |
| 5 | 5 | ||
| 6 | import android.app.Dialog | 6 | import android.app.Dialog |
| 7 | import android.content.DialogInterface | ||
| 8 | import android.content.Intent | 7 | import android.content.Intent |
| 9 | import android.net.Uri | 8 | import android.net.Uri |
| 10 | import android.os.Bundle | 9 | import android.os.Bundle |
| @@ -16,18 +15,52 @@ import androidx.lifecycle.ViewModelProvider | |||
| 16 | import com.google.android.material.dialog.MaterialAlertDialogBuilder | 15 | import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 17 | import org.yuzu.yuzu_emu.R | 16 | import org.yuzu.yuzu_emu.R |
| 18 | import org.yuzu.yuzu_emu.model.MessageDialogViewModel | 17 | import org.yuzu.yuzu_emu.model.MessageDialogViewModel |
| 18 | import org.yuzu.yuzu_emu.utils.Log | ||
| 19 | 19 | ||
| 20 | class MessageDialogFragment : DialogFragment() { | 20 | class MessageDialogFragment : DialogFragment() { |
| 21 | private val messageDialogViewModel: MessageDialogViewModel by activityViewModels() | 21 | private val messageDialogViewModel: MessageDialogViewModel by activityViewModels() |
| 22 | 22 | ||
| 23 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | 23 | override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { |
| 24 | val titleId = requireArguments().getInt(TITLE_ID) | 24 | val titleId = requireArguments().getInt(TITLE_ID) |
| 25 | val titleString = requireArguments().getString(TITLE_STRING)!! | 25 | val title = if (titleId != 0) { |
| 26 | getString(titleId) | ||
| 27 | } else { | ||
| 28 | requireArguments().getString(TITLE_STRING)!! | ||
| 29 | } | ||
| 30 | |||
| 26 | val descriptionId = requireArguments().getInt(DESCRIPTION_ID) | 31 | val descriptionId = requireArguments().getInt(DESCRIPTION_ID) |
| 27 | val descriptionString = requireArguments().getString(DESCRIPTION_STRING)!! | 32 | val description = if (descriptionId != 0) { |
| 33 | getString(descriptionId) | ||
| 34 | } else { | ||
| 35 | requireArguments().getString(DESCRIPTION_STRING)!! | ||
| 36 | } | ||
| 37 | |||
| 38 | val positiveButtonId = requireArguments().getInt(POSITIVE_BUTTON_TITLE_ID) | ||
| 39 | val positiveButtonString = requireArguments().getString(POSITIVE_BUTTON_TITLE_STRING)!! | ||
| 40 | val positiveButton = if (positiveButtonId != 0) { | ||
| 41 | getString(positiveButtonId) | ||
| 42 | } else if (positiveButtonString.isNotEmpty()) { | ||
| 43 | positiveButtonString | ||
| 44 | } else if (messageDialogViewModel.positiveAction != null) { | ||
| 45 | getString(R.string.close) | ||
| 46 | } else { | ||
| 47 | getString(android.R.string.ok) | ||
| 48 | } | ||
| 49 | |||
| 50 | val negativeButtonId = requireArguments().getInt(NEGATIVE_BUTTON_TITLE_ID) | ||
| 51 | val negativeButtonString = requireArguments().getString(NEGATIVE_BUTTON_TITLE_STRING)!! | ||
| 52 | val negativeButton = if (negativeButtonId != 0) { | ||
| 53 | getString(negativeButtonId) | ||
| 54 | } else if (negativeButtonString.isNotEmpty()) { | ||
| 55 | negativeButtonString | ||
| 56 | } else { | ||
| 57 | getString(android.R.string.cancel) | ||
| 58 | } | ||
| 59 | |||
| 28 | val helpLinkId = requireArguments().getInt(HELP_LINK) | 60 | val helpLinkId = requireArguments().getInt(HELP_LINK) |
| 29 | val dismissible = requireArguments().getBoolean(DISMISSIBLE) | 61 | val dismissible = requireArguments().getBoolean(DISMISSIBLE) |
| 30 | val clearPositiveAction = requireArguments().getBoolean(CLEAR_POSITIVE_ACTION) | 62 | val clearPositiveAction = requireArguments().getBoolean(CLEAR_ACTIONS) |
| 63 | val showNegativeButton = requireArguments().getBoolean(SHOW_NEGATIVE_BUTTON) | ||
| 31 | 64 | ||
| 32 | val builder = MaterialAlertDialogBuilder(requireContext()) | 65 | val builder = MaterialAlertDialogBuilder(requireContext()) |
| 33 | 66 | ||
| @@ -35,21 +68,19 @@ class MessageDialogFragment : DialogFragment() { | |||
| 35 | messageDialogViewModel.positiveAction = null | 68 | messageDialogViewModel.positiveAction = null |
| 36 | } | 69 | } |
| 37 | 70 | ||
| 38 | if (messageDialogViewModel.positiveAction == null) { | 71 | builder.setPositiveButton(positiveButton) { _, _ -> |
| 39 | builder.setPositiveButton(R.string.close, null) | 72 | messageDialogViewModel.positiveAction?.invoke() |
| 40 | } else { | 73 | } |
| 41 | builder.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> | 74 | if (messageDialogViewModel.negativeAction != null || showNegativeButton) { |
| 42 | messageDialogViewModel.positiveAction?.invoke() | 75 | builder.setNegativeButton(negativeButton) { _, _ -> |
| 43 | }.setNegativeButton(android.R.string.cancel, null) | 76 | messageDialogViewModel.negativeAction?.invoke() |
| 77 | } | ||
| 44 | } | 78 | } |
| 45 | 79 | ||
| 46 | if (titleId != 0) builder.setTitle(titleId) | 80 | if (title.isNotEmpty()) builder.setTitle(title) |
| 47 | if (titleString.isNotEmpty()) builder.setTitle(titleString) | 81 | if (description.isNotEmpty()) { |
| 48 | 82 | builder.setMessage(Html.fromHtml(description, Html.FROM_HTML_MODE_LEGACY)) | |
| 49 | if (descriptionId != 0) { | ||
| 50 | builder.setMessage(Html.fromHtml(getString(descriptionId), Html.FROM_HTML_MODE_LEGACY)) | ||
| 51 | } | 83 | } |
| 52 | if (descriptionString.isNotEmpty()) builder.setMessage(descriptionString) | ||
| 53 | 84 | ||
| 54 | if (helpLinkId != 0) { | 85 | if (helpLinkId != 0) { |
| 55 | builder.setNeutralButton(R.string.learn_more) { _, _ -> | 86 | builder.setNeutralButton(R.string.learn_more) { _, _ -> |
| @@ -76,8 +107,41 @@ class MessageDialogFragment : DialogFragment() { | |||
| 76 | private const val DESCRIPTION_STRING = "DescriptionString" | 107 | private const val DESCRIPTION_STRING = "DescriptionString" |
| 77 | private const val HELP_LINK = "Link" | 108 | private const val HELP_LINK = "Link" |
| 78 | private const val DISMISSIBLE = "Dismissible" | 109 | private const val DISMISSIBLE = "Dismissible" |
| 79 | private const val CLEAR_POSITIVE_ACTION = "ClearPositiveAction" | 110 | private const val CLEAR_ACTIONS = "ClearActions" |
| 80 | 111 | private const val POSITIVE_BUTTON_TITLE_ID = "PositiveButtonTitleId" | |
| 112 | private const val POSITIVE_BUTTON_TITLE_STRING = "PositiveButtonTitleString" | ||
| 113 | private const val SHOW_NEGATIVE_BUTTON = "ShowNegativeButton" | ||
| 114 | private const val NEGATIVE_BUTTON_TITLE_ID = "NegativeButtonTitleId" | ||
| 115 | private const val NEGATIVE_BUTTON_TITLE_STRING = "NegativeButtonTitleString" | ||
| 116 | |||
| 117 | /** | ||
| 118 | * Creates a new [MessageDialogFragment] instance. | ||
| 119 | * @param activity Activity that will hold a [MessageDialogViewModel] instance if using | ||
| 120 | * [positiveAction] or [negativeAction]. | ||
| 121 | * @param titleId String resource ID that will be used for the title. [titleString] used if 0. | ||
| 122 | * @param titleString String that will be used for the title. No title is set if empty. | ||
| 123 | * @param descriptionId String resource ID that will be used for the description. | ||
| 124 | * [descriptionString] used if 0. | ||
| 125 | * @param descriptionString String that will be used for the description. | ||
| 126 | * No description is set if empty. | ||
| 127 | * @param helpLinkId String resource ID that contains a help link. Will be added as a neutral | ||
| 128 | * button with the title R.string.help. | ||
| 129 | * @param dismissible Whether the dialog is dismissible or not. Typically used to ensure that | ||
| 130 | * the user clicks on one of the dialog buttons before closing. | ||
| 131 | * @param positiveButtonTitleId String resource ID that will be used for the positive button. | ||
| 132 | * [positiveButtonTitleString] used if 0. | ||
| 133 | * @param positiveButtonTitleString String that will be used for the positive button. | ||
| 134 | * android.R.string.ok used if empty. android.R.string.close will be used if [positiveAction] | ||
| 135 | * is not null. | ||
| 136 | * @param positiveAction Lambda to run when the positive button is clicked. | ||
| 137 | * @param showNegativeButton Normally the negative button isn't shown if there is no | ||
| 138 | * [negativeAction] set. This can override that behavior to always show a button. | ||
| 139 | * @param negativeButtonTitleId String resource ID that will be used for the negative button. | ||
| 140 | * [negativeButtonTitleString] used if 0. | ||
| 141 | * @param negativeButtonTitleString String that will be used for the negative button. | ||
| 142 | * android.R.string.cancel used if empty. | ||
| 143 | * @param negativeAction Lambda to run when the negative button is clicked | ||
| 144 | */ | ||
| 81 | fun newInstance( | 145 | fun newInstance( |
| 82 | activity: FragmentActivity? = null, | 146 | activity: FragmentActivity? = null, |
| 83 | titleId: Int = 0, | 147 | titleId: Int = 0, |
| @@ -86,16 +150,27 @@ class MessageDialogFragment : DialogFragment() { | |||
| 86 | descriptionString: String = "", | 150 | descriptionString: String = "", |
| 87 | helpLinkId: Int = 0, | 151 | helpLinkId: Int = 0, |
| 88 | dismissible: Boolean = true, | 152 | dismissible: Boolean = true, |
| 89 | positiveAction: (() -> Unit)? = null | 153 | positiveButtonTitleId: Int = 0, |
| 154 | positiveButtonTitleString: String = "", | ||
| 155 | positiveAction: (() -> Unit)? = null, | ||
| 156 | showNegativeButton: Boolean = false, | ||
| 157 | negativeButtonTitleId: Int = 0, | ||
| 158 | negativeButtonTitleString: String = "", | ||
| 159 | negativeAction: (() -> Unit)? = null | ||
| 90 | ): MessageDialogFragment { | 160 | ): MessageDialogFragment { |
| 91 | var clearPositiveAction = false | 161 | var clearActions = false |
| 92 | if (activity != null) { | 162 | if (activity != null) { |
| 93 | ViewModelProvider(activity)[MessageDialogViewModel::class.java].apply { | 163 | ViewModelProvider(activity)[MessageDialogViewModel::class.java].apply { |
| 94 | clear() | 164 | clear() |
| 95 | this.positiveAction = positiveAction | 165 | this.positiveAction = positiveAction |
| 166 | this.negativeAction = negativeAction | ||
| 96 | } | 167 | } |
| 97 | } else { | 168 | } else { |
| 98 | clearPositiveAction = true | 169 | clearActions = true |
| 170 | } | ||
| 171 | |||
| 172 | if (activity == null && (positiveAction == null || negativeAction == null)) { | ||
| 173 | Log.warning("[$TAG] Tried to set action with no activity!") | ||
| 99 | } | 174 | } |
| 100 | 175 | ||
| 101 | val dialog = MessageDialogFragment() | 176 | val dialog = MessageDialogFragment() |
| @@ -106,7 +181,12 @@ class MessageDialogFragment : DialogFragment() { | |||
| 106 | putString(DESCRIPTION_STRING, descriptionString) | 181 | putString(DESCRIPTION_STRING, descriptionString) |
| 107 | putInt(HELP_LINK, helpLinkId) | 182 | putInt(HELP_LINK, helpLinkId) |
| 108 | putBoolean(DISMISSIBLE, dismissible) | 183 | putBoolean(DISMISSIBLE, dismissible) |
| 109 | putBoolean(CLEAR_POSITIVE_ACTION, clearPositiveAction) | 184 | putBoolean(CLEAR_ACTIONS, clearActions) |
| 185 | putInt(POSITIVE_BUTTON_TITLE_ID, positiveButtonTitleId) | ||
| 186 | putString(POSITIVE_BUTTON_TITLE_STRING, positiveButtonTitleString) | ||
| 187 | putBoolean(SHOW_NEGATIVE_BUTTON, showNegativeButton) | ||
| 188 | putInt(NEGATIVE_BUTTON_TITLE_ID, negativeButtonTitleId) | ||
| 189 | putString(NEGATIVE_BUTTON_TITLE_STRING, negativeButtonTitleString) | ||
| 110 | } | 190 | } |
| 111 | dialog.arguments = bundle | 191 | dialog.arguments = bundle |
| 112 | return dialog | 192 | return dialog |
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/MessageDialogViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/MessageDialogViewModel.kt index 641c5cb17..2db005e49 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/MessageDialogViewModel.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/MessageDialogViewModel.kt | |||
| @@ -7,8 +7,10 @@ import androidx.lifecycle.ViewModel | |||
| 7 | 7 | ||
| 8 | class MessageDialogViewModel : ViewModel() { | 8 | class MessageDialogViewModel : ViewModel() { |
| 9 | var positiveAction: (() -> Unit)? = null | 9 | var positiveAction: (() -> Unit)? = null |
| 10 | var negativeAction: (() -> Unit)? = null | ||
| 10 | 11 | ||
| 11 | fun clear() { | 12 | fun clear() { |
| 12 | positiveAction = null | 13 | positiveAction = null |
| 14 | negativeAction = null | ||
| 13 | } | 15 | } |
| 14 | } | 16 | } |