|
@@ -13,179 +13,146 @@
|
|
* You should have received a copy of the GNU General Public License along with this program. If not, see
|
|
* You should have received a copy of the GNU General Public License along with this program. If not, see
|
|
* <http://www.gnu.org/licenses/>.
|
|
* <http://www.gnu.org/licenses/>.
|
|
*/
|
|
*/
|
|
|
|
+package com.owncloud.android.ui.dialog
|
|
|
|
|
|
-package com.owncloud.android.ui.dialog;
|
|
|
|
|
|
+//noinspection SuspiciousImport
|
|
|
|
+import android.R
|
|
|
|
+import android.app.Dialog
|
|
|
|
+import android.content.DialogInterface
|
|
|
|
+import android.os.Bundle
|
|
|
|
+import androidx.appcompat.app.AlertDialog
|
|
|
|
+import androidx.fragment.app.DialogFragment
|
|
|
|
+import com.google.android.material.button.MaterialButton
|
|
|
|
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|
|
|
+import com.nextcloud.client.di.Injectable
|
|
|
|
+import com.owncloud.android.utils.theme.ViewThemeUtils
|
|
|
|
+import javax.inject.Inject
|
|
|
|
|
|
-import android.app.Activity;
|
|
|
|
-import android.app.Dialog;
|
|
|
|
-import android.os.Bundle;
|
|
|
|
|
|
+open class ConfirmationDialogFragment : DialogFragment(), Injectable {
|
|
|
|
|
|
-import com.google.android.material.button.MaterialButton;
|
|
|
|
-import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
|
|
|
-import com.nextcloud.client.di.Injectable;
|
|
|
|
-import com.owncloud.android.R;
|
|
|
|
-import com.owncloud.android.utils.theme.ViewThemeUtils;
|
|
|
|
|
|
+ @JvmField
|
|
|
|
+ @Inject
|
|
|
|
+ var viewThemeUtils: ViewThemeUtils? = null
|
|
|
|
|
|
-import javax.inject.Inject;
|
|
|
|
|
|
+ private var mListener: ConfirmationDialogFragmentListener? = null
|
|
|
|
|
|
-import androidx.annotation.NonNull;
|
|
|
|
-import androidx.appcompat.app.AlertDialog;
|
|
|
|
-import androidx.fragment.app.DialogFragment;
|
|
|
|
|
|
+ override fun onStart() {
|
|
|
|
+ super.onStart()
|
|
|
|
|
|
-
|
|
|
|
-public class ConfirmationDialogFragment extends DialogFragment implements Injectable {
|
|
|
|
-
|
|
|
|
- final static String ARG_MESSAGE_RESOURCE_ID = "resource_id";
|
|
|
|
- final static String ARG_MESSAGE_ARGUMENTS = "string_array";
|
|
|
|
- final static String ARG_TITLE_ID = "title_id";
|
|
|
|
-
|
|
|
|
- final static String ARG_POSITIVE_BTN_RES = "positive_btn_res";
|
|
|
|
- final static String ARG_NEUTRAL_BTN_RES = "neutral_btn_res";
|
|
|
|
- final static String ARG_NEGATIVE_BTN_RES = "negative_btn_res";
|
|
|
|
-
|
|
|
|
- public static final String FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT";
|
|
|
|
-
|
|
|
|
- @Inject ViewThemeUtils viewThemeUtils;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- private ConfirmationDialogFragmentListener mListener;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Public factory method to create new ConfirmationDialogFragment instances.
|
|
|
|
- *
|
|
|
|
- * @param messageResId Resource id for a message to show in the dialog.
|
|
|
|
- * @param messageArguments Arguments to complete the message, if it's a format string. May be null.
|
|
|
|
- * @param titleResId Resource id for a text to show in the title. 0 for default alert title, -1 for no
|
|
|
|
- * title.
|
|
|
|
- * @param positiveButtonTextId Resource id for the text of the positive button. -1 for no positive button.
|
|
|
|
- * @param neutralButtonTextId Resource id for the text of the neutral button. -1 for no neutral button.
|
|
|
|
- * @param negativeButtonTextId Resource id for the text of the negative button. -1 for no negative button.
|
|
|
|
- * @return Dialog ready to show.
|
|
|
|
- */
|
|
|
|
- public static ConfirmationDialogFragment newInstance(int messageResId, String[] messageArguments, int titleResId,
|
|
|
|
- int positiveButtonTextId, int negativeButtonTextId, int neutralButtonTextId) {
|
|
|
|
- if (messageResId == -1) {
|
|
|
|
- throw new IllegalStateException("Calling confirmation dialog without message resource");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ConfirmationDialogFragment frag = new ConfirmationDialogFragment();
|
|
|
|
- Bundle args = new Bundle();
|
|
|
|
-
|
|
|
|
- args.putInt(ARG_MESSAGE_RESOURCE_ID, messageResId);
|
|
|
|
- args.putStringArray(ARG_MESSAGE_ARGUMENTS, messageArguments);
|
|
|
|
- args.putInt(ARG_TITLE_ID, titleResId);
|
|
|
|
-
|
|
|
|
- args.putInt(ARG_POSITIVE_BTN_RES, positiveButtonTextId);
|
|
|
|
- args.putInt(ARG_NEGATIVE_BTN_RES, negativeButtonTextId);
|
|
|
|
- args.putInt(ARG_NEUTRAL_BTN_RES, neutralButtonTextId);
|
|
|
|
-
|
|
|
|
- frag.setArguments(args);
|
|
|
|
- return frag;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void onStart() {
|
|
|
|
- super.onStart();
|
|
|
|
-
|
|
|
|
- AlertDialog alertDialog = (AlertDialog) getDialog();
|
|
|
|
|
|
+ val alertDialog = dialog as AlertDialog?
|
|
|
|
|
|
if (alertDialog != null) {
|
|
if (alertDialog != null) {
|
|
- MaterialButton positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
|
|
|
+ val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as MaterialButton?
|
|
if (positiveButton != null) {
|
|
if (positiveButton != null) {
|
|
- viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton);
|
|
|
|
|
|
+ viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(positiveButton)
|
|
}
|
|
}
|
|
|
|
|
|
- MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
|
|
|
|
|
|
+ val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as MaterialButton?
|
|
if (negativeButton != null) {
|
|
if (negativeButton != null) {
|
|
- viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton);
|
|
|
|
|
|
+ viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(negativeButton)
|
|
}
|
|
}
|
|
|
|
|
|
- MaterialButton neutralButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
|
|
|
|
|
|
+ val neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL) as MaterialButton?
|
|
if (neutralButton != null) {
|
|
if (neutralButton != null) {
|
|
- viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(neutralButton);
|
|
|
|
|
|
+ viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(neutralButton)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public void setOnConfirmationListener(ConfirmationDialogFragmentListener listener) {
|
|
|
|
- mListener = listener;
|
|
|
|
|
|
+ fun setOnConfirmationListener(listener: ConfirmationDialogFragmentListener?) {
|
|
|
|
+ mListener = listener
|
|
}
|
|
}
|
|
|
|
|
|
- @NonNull
|
|
|
|
- @Override
|
|
|
|
- public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
|
|
- Bundle arguments = getArguments();
|
|
|
|
-
|
|
|
|
- if (arguments == null) {
|
|
|
|
- throw new IllegalArgumentException("Arguments may not be null");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Activity activity = getActivity();
|
|
|
|
-
|
|
|
|
- if (activity == null) {
|
|
|
|
- throw new IllegalArgumentException("Activity may not be null");
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Object[] messageArguments = arguments.getStringArray(ARG_MESSAGE_ARGUMENTS);
|
|
|
|
-
|
|
|
|
- int titleId = arguments.getInt(ARG_TITLE_ID, -1);
|
|
|
|
- int messageId = arguments.getInt(ARG_MESSAGE_RESOURCE_ID, -1);
|
|
|
|
-
|
|
|
|
- int positiveButtonTextId = arguments.getInt(ARG_POSITIVE_BTN_RES, -1);
|
|
|
|
- int negativeButtonTextId = arguments.getInt(ARG_NEGATIVE_BTN_RES, -1);
|
|
|
|
- int neutralButtonTextId = arguments.getInt(ARG_NEUTRAL_BTN_RES, -1);
|
|
|
|
-
|
|
|
|
|
|
+ override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
+ var messageArguments = requireArguments().getStringArray(ARG_MESSAGE_ARGUMENTS)
|
|
|
|
+ val titleId = requireArguments().getInt(ARG_TITLE_ID, -1)
|
|
|
|
+ val messageId = requireArguments().getInt(ARG_MESSAGE_RESOURCE_ID, -1)
|
|
|
|
+ val positiveButtonTextId = requireArguments().getInt(ARG_POSITIVE_BTN_RES, -1)
|
|
|
|
+ val negativeButtonTextId = requireArguments().getInt(ARG_NEGATIVE_BTN_RES, -1)
|
|
|
|
+ val neutralButtonTextId = requireArguments().getInt(ARG_NEUTRAL_BTN_RES, -1)
|
|
if (messageArguments == null) {
|
|
if (messageArguments == null) {
|
|
- messageArguments = new String[]{};
|
|
|
|
|
|
+ messageArguments = arrayOf<String?>()
|
|
}
|
|
}
|
|
-
|
|
|
|
- MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(activity)
|
|
|
|
- .setIcon(R.drawable.ic_warning)
|
|
|
|
- .setIconAttribute(android.R.attr.alertDialogIcon)
|
|
|
|
- .setMessage(String.format(getString(messageId), messageArguments));
|
|
|
|
-
|
|
|
|
|
|
+ val builder = MaterialAlertDialogBuilder(requireActivity())
|
|
|
|
+ .setIcon(com.owncloud.android.R.drawable.ic_warning)
|
|
|
|
+ .setIconAttribute(R.attr.alertDialogIcon)
|
|
|
|
+ .setMessage(String.format(getString(messageId), *messageArguments))
|
|
if (titleId == 0) {
|
|
if (titleId == 0) {
|
|
- builder.setTitle(android.R.string.dialog_alert_title);
|
|
|
|
|
|
+ builder.setTitle(R.string.dialog_alert_title)
|
|
} else if (titleId != -1) {
|
|
} else if (titleId != -1) {
|
|
- builder.setTitle(titleId);
|
|
|
|
|
|
+ builder.setTitle(titleId)
|
|
}
|
|
}
|
|
-
|
|
|
|
if (positiveButtonTextId != -1) {
|
|
if (positiveButtonTextId != -1) {
|
|
- builder.setPositiveButton(positiveButtonTextId, (dialog, whichButton) -> {
|
|
|
|
- if (mListener != null) {
|
|
|
|
- mListener.onConfirmation(getTag());
|
|
|
|
- }
|
|
|
|
- dialog.dismiss();
|
|
|
|
- });
|
|
|
|
|
|
+ builder.setPositiveButton(positiveButtonTextId) { dialog: DialogInterface, _: Int ->
|
|
|
|
+ mListener?.onConfirmation(tag)
|
|
|
|
+ dialog.dismiss()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
if (negativeButtonTextId != -1) {
|
|
if (negativeButtonTextId != -1) {
|
|
- builder.setNegativeButton(negativeButtonTextId, (dialog, which) -> {
|
|
|
|
- if (mListener != null) {
|
|
|
|
- mListener.onCancel(getTag());
|
|
|
|
- }
|
|
|
|
- dialog.dismiss();
|
|
|
|
- });
|
|
|
|
|
|
+ builder.setNegativeButton(negativeButtonTextId) { dialog: DialogInterface, _: Int ->
|
|
|
|
+ mListener?.onCancel(tag)
|
|
|
|
+ dialog.dismiss()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
if (neutralButtonTextId != -1) {
|
|
if (neutralButtonTextId != -1) {
|
|
- builder.setNeutralButton(neutralButtonTextId, (dialog, which) -> {
|
|
|
|
- if (mListener != null) {
|
|
|
|
- mListener.onNeutral(getTag());
|
|
|
|
- }
|
|
|
|
- dialog.dismiss();
|
|
|
|
- });
|
|
|
|
|
|
+ builder.setNeutralButton(neutralButtonTextId) { dialog: DialogInterface, _: Int ->
|
|
|
|
+ mListener?.onNeutral(tag)
|
|
|
|
+ dialog.dismiss()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- viewThemeUtils.dialog.colorMaterialAlertDialogBackground(activity, builder);
|
|
|
|
|
|
+ viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(requireActivity(), builder)
|
|
|
|
|
|
- return builder.create();
|
|
|
|
|
|
+ return builder.create()
|
|
}
|
|
}
|
|
|
|
|
|
- public interface ConfirmationDialogFragmentListener {
|
|
|
|
- void onConfirmation(String callerTag);
|
|
|
|
-
|
|
|
|
- void onNeutral(String callerTag);
|
|
|
|
|
|
+ interface ConfirmationDialogFragmentListener {
|
|
|
|
+ fun onConfirmation(callerTag: String?)
|
|
|
|
+ fun onNeutral(callerTag: String?)
|
|
|
|
+ fun onCancel(callerTag: String?)
|
|
|
|
+ }
|
|
|
|
|
|
- void onCancel(String callerTag);
|
|
|
|
|
|
+ companion object {
|
|
|
|
+ const val ARG_MESSAGE_RESOURCE_ID = "resource_id"
|
|
|
|
+ const val ARG_MESSAGE_ARGUMENTS = "string_array"
|
|
|
|
+ const val ARG_TITLE_ID = "title_id"
|
|
|
|
+ const val ARG_POSITIVE_BTN_RES = "positive_btn_res"
|
|
|
|
+ const val ARG_NEUTRAL_BTN_RES = "neutral_btn_res"
|
|
|
|
+ const val ARG_NEGATIVE_BTN_RES = "negative_btn_res"
|
|
|
|
+ const val FTAG_CONFIRMATION = "CONFIRMATION_FRAGMENT"
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Public factory method to create new ConfirmationDialogFragment instances.
|
|
|
|
+ *
|
|
|
|
+ * @param messageResId Resource id for a message to show in the dialog.
|
|
|
|
+ * @param messageArguments Arguments to complete the message, if it's a format string. May be null.
|
|
|
|
+ * @param titleResId Resource id for a text to show in the title. 0 for default alert title, -1 for no
|
|
|
|
+ * title.
|
|
|
|
+ * @param positiveButtonTextId Resource id for the text of the positive button. -1 for no positive button.
|
|
|
|
+ * @param neutralButtonTextId Resource id for the text of the neutral button. -1 for no neutral button.
|
|
|
|
+ * @param negativeButtonTextId Resource id for the text of the negative button. -1 for no negative button.
|
|
|
|
+ * @return Dialog ready to show.
|
|
|
|
+ */
|
|
|
|
+ @JvmStatic
|
|
|
|
+ fun newInstance(
|
|
|
|
+ messageResId: Int,
|
|
|
|
+ messageArguments: Array<String?>?,
|
|
|
|
+ titleResId: Int,
|
|
|
|
+ positiveButtonTextId: Int,
|
|
|
|
+ negativeButtonTextId: Int,
|
|
|
|
+ neutralButtonTextId: Int
|
|
|
|
+ ): ConfirmationDialogFragment {
|
|
|
|
+ check(messageResId != -1) { "Calling confirmation dialog without message resource" }
|
|
|
|
+ val frag = ConfirmationDialogFragment()
|
|
|
|
+ val args = Bundle()
|
|
|
|
+ args.putInt(ARG_MESSAGE_RESOURCE_ID, messageResId)
|
|
|
|
+ args.putStringArray(ARG_MESSAGE_ARGUMENTS, messageArguments)
|
|
|
|
+ args.putInt(ARG_TITLE_ID, titleResId)
|
|
|
|
+ args.putInt(ARG_POSITIVE_BTN_RES, positiveButtonTextId)
|
|
|
|
+ args.putInt(ARG_NEGATIVE_BTN_RES, negativeButtonTextId)
|
|
|
|
+ args.putInt(ARG_NEUTRAL_BTN_RES, neutralButtonTextId)
|
|
|
|
+ frag.arguments = args
|
|
|
|
+ return frag
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|