From 975f9fc4d1e1db2d4475fa144f87aa1b3bf67fac Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 2 Jun 2023 22:05:34 +0930 Subject: [PATCH] Utilise dialog dsl in DeleteMediaDialog --- .../securesms/DeleteMediaDialog.kt | 28 +++++++++++ .../securesms/MediaOverviewActivity.java | 47 +++++++------------ 2 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/DeleteMediaDialog.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/DeleteMediaDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/DeleteMediaDialog.kt new file mode 100644 index 0000000000..af38c31ff3 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/DeleteMediaDialog.kt @@ -0,0 +1,28 @@ +package org.thoughtcrime.securesms + +import android.content.Context +import network.loki.messenger.R + +class DeleteMediaDialog { + companion object { + @JvmStatic + fun show(context: Context, recordCount: Int, doDelete: Runnable) = context.showSessionDialog { + iconAttribute(R.attr.dialog_alert_icon) + title( + context.resources.getQuantityString( + R.plurals.MediaOverviewActivity_Media_delete_confirm_title, + recordCount, + recordCount + ) + ) + text( + context.resources.getQuantityString(R.plurals.MediaOverviewActivity_Media_delete_confirm_message, + recordCount, + recordCount + ) + ) + button(R.string.delete) { doDelete.run() } + cancelButton() + } + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/MediaOverviewActivity.java b/app/src/main/java/org/thoughtcrime/securesms/MediaOverviewActivity.java index e8af4e5a8e..95ba15c82e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/MediaOverviewActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/MediaOverviewActivity.java @@ -376,41 +376,26 @@ public class MediaOverviewActivity extends PassphraseRequiredActionBarActivity { @SuppressLint("StaticFieldLeak") private void handleDeleteMedia(@NonNull Collection mediaRecords) { int recordCount = mediaRecords.size(); - Resources res = getContext().getResources(); - String confirmTitle = res.getQuantityString(R.plurals.MediaOverviewActivity_Media_delete_confirm_title, - recordCount, - recordCount); - String confirmMessage = res.getQuantityString(R.plurals.MediaOverviewActivity_Media_delete_confirm_message, - recordCount, - recordCount); - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - builder.setIconAttribute(R.attr.dialog_alert_icon); - builder.setTitle(confirmTitle); - builder.setMessage(confirmMessage); - builder.setCancelable(true); - - builder.setPositiveButton(R.string.delete, (dialogInterface, i) -> { - new ProgressDialogAsyncTask(getContext(), - R.string.MediaOverviewActivity_Media_delete_progress_title, - R.string.MediaOverviewActivity_Media_delete_progress_message) - { - @Override - protected Void doInBackground(MediaDatabase.MediaRecord... records) { - if (records == null || records.length == 0) { - return null; - } - - for (MediaDatabase.MediaRecord record : records) { - AttachmentUtil.deleteAttachment(getContext(), record.getAttachment()); - } + DeleteMediaDialog.show( + requireContext(), + recordCount, + () -> new ProgressDialogAsyncTask( + requireContext(), + R.string.MediaOverviewActivity_Media_delete_progress_title, + R.string.MediaOverviewActivity_Media_delete_progress_message) { + @Override + protected Void doInBackground(MediaDatabase.MediaRecord... records) { + if (records == null || records.length == 0) { return null; } - }.execute(mediaRecords.toArray(new MediaDatabase.MediaRecord[mediaRecords.size()])); - }); - builder.setNegativeButton(android.R.string.cancel, null); - builder.show(); + for (MediaDatabase.MediaRecord record : records) { + AttachmentUtil.deleteAttachment(getContext(), record.getAttachment()); + } + return null; + } + }.execute(mediaRecords.toArray(new MediaDatabase.MediaRecord[mediaRecords.size()]))); } private void handleSelectAllMedia() {