diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java index ee485fcf1e..d5caa800b1 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java @@ -1300,12 +1300,7 @@ private void onDeleteClicked(Spinner spinner) { resultSingle .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(s -> { - if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) { - applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl())); - callback.nominatingForDeletion(index); - } - }); + .subscribe(this::handleDeletionResult, this::handleDeletionError); } @SuppressLint("CheckResult") @@ -1317,12 +1312,7 @@ private void onDeleteClickeddialogtext(String reason) { resultSingletext .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(s -> { - if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) { - applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl())); - callback.nominatingForDeletion(index); - } - }); + .subscribe(this::handleDeletionResult, this::handleDeletionError); } @OnClick(R.id.seeMore) @@ -1356,6 +1346,47 @@ private void enableProgressBar() { isDeleted = true; } + /** + * Disables Progress Bar and Update delete button text. + */ + private void disableProgressBar() { + if (getActivity() == null) { + return; // Prevent NullPointerException when fragment is not attached to activity + } + getActivity().runOnUiThread(() -> { + if (progressBarDeletion != null) { + progressBarDeletion.setVisibility(GONE); + } + }); + } + + private void handleDeletionResult(final boolean success) { + if (success) { + Timber.d("Nominated for Deletion : Success"); + delete.setText("Nominated for Deletion"); + ViewUtil.showLongSnackbar(requireView(), + "Nominating for deletion : Success"); + disableProgressBar(); + checkAndClearDeletionFlag(); + } else { + disableProgressBar(); + } + } + + private void handleDeletionError(final Throwable throwable) { + // Log error or show error message to the user + Timber.e(throwable.getLocalizedMessage()); + disableProgressBar(); + checkAndClearDeletionFlag(); + } + + private void checkAndClearDeletionFlag() { + if (applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) { + applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl())); + callback.nominatingForDeletion(index); // Notify that nomination for deletion has proceeded + } + } + private void rebuildCatList(List categories) { categoryContainer.removeAllViews(); for (String category : categories) {