Skip to content

Commit 18a03a1

Browse files
Pratham2305ashishkumar468
authored andcommitted
Fixes commons-app#2815 - Nominating for deletion is cancelled on leaving the media details (commons-app#4295)
* fix issue with nominating for deletion * Fix UI issue * minor improvements * fix App crash * Added Javadoc and other minor improvements * Updated string name Co-authored-by: Pratham2305 <Pratham2305@users.noreply.github.com>
1 parent 3e51cd8 commit 18a03a1

11 files changed

+224
-38
lines changed

app/src/main/java/fr/free/nrw/commons/bookmarks/BookmarkListRootFragment.java

+16
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ public Integer getContributionStateAt(int position) {
169169
return null;
170170
}
171171

172+
/**
173+
* Reload media detail fragment once media is nominated
174+
*
175+
* @param index item position that has been nominated
176+
*/
177+
@Override
178+
public void refreshNominatedMedia(int index) {
179+
if(mediaDetails != null && !listFragment.isVisible()) {
180+
removeFragment(mediaDetails);
181+
mediaDetails = new MediaDetailPagerFragment(false, true);
182+
((BookmarkFragment) getParentFragment()).setScroll(false);
183+
setFragment(mediaDetails, listFragment);
184+
mediaDetails.showImage(index);
185+
}
186+
}
187+
172188
/**
173189
* This method is called on success of API call for featured images or mobile uploads. The
174190
* viewpager will notified that number of items have changed.

app/src/main/java/fr/free/nrw/commons/category/CategoryDetailsActivity.java

+13
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public Integer getContributionStateAt(int position) {
165165
return null;
166166
}
167167

168+
/**
169+
* Reload media detail fragment once media is nominated
170+
*
171+
* @param index item position that has been nominated
172+
*/
173+
@Override
174+
public void refreshNominatedMedia(int index) {
175+
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
176+
onBackPressed();
177+
onMediaClicked(index);
178+
}
179+
}
180+
168181
/**
169182
* This method inflates the menu in the toolbar
170183
*/

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.java

+25
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,16 @@ public void pauseUpload(Contribution contribution) {
565565
contributionsPresenter.saveContribution(contribution);
566566
}
567567

568+
/**
569+
* Notify the viewpager that number of items have changed.
570+
*/
571+
@Override
572+
public void viewPagerNotifyDataSetChanged() {
573+
if (mediaDetailPagerFragment != null) {
574+
mediaDetailPagerFragment.notifyDataSetChanged();
575+
}
576+
}
577+
568578
/**
569579
* Replace whatever is in the current contributionsFragmentContainer view with
570580
* mediaDetailPagerFragment, and preserve previous state in back stack. Called when user selects a
@@ -620,6 +630,21 @@ public MediaDetailPagerFragment getMediaDetailPagerFragment() {
620630
return mediaDetailPagerFragment;
621631
}
622632

633+
/**
634+
* Reload media detail fragment once media is nominated
635+
*
636+
* @param index item position that has been nominated
637+
*/
638+
@Override
639+
public void refreshNominatedMedia(int index) {
640+
if(mediaDetailPagerFragment != null && !contributionsListFragment.isVisible()) {
641+
removeFragment(mediaDetailPagerFragment);
642+
mediaDetailPagerFragment = new MediaDetailPagerFragment(false, true);
643+
mediaDetailPagerFragment.showImage(index);
644+
showMediaDetailPagerFragment();
645+
}
646+
}
647+
623648
// click listener to toggle description that means uses can press the limited connection
624649
// banner and description will hide. Tap again to show description.
625650
private View.OnClickListener toggleDescriptionListener = new View.OnClickListener() {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsListFragment.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ public void onItemRangeInserted(int positionStart, int itemCount) {
157157
}
158158
}
159159
}
160+
161+
/**
162+
* Called whenever items in the list have changed
163+
* Calls viewPagerNotifyDataSetChanged() that will notify the viewpager
164+
*/
165+
@Override
166+
public void onItemRangeChanged(final int positionStart, final int itemCount) {
167+
super.onItemRangeChanged(positionStart, itemCount);
168+
callback.viewPagerNotifyDataSetChanged();
169+
}
160170
});
161171

162172
//Fab close on touch outside (Scrolling or taping on item triggers this action).
@@ -370,11 +380,17 @@ private void showAddImageToWikipediaInstructions(Contribution contribution) {
370380

371381

372382
public Media getMediaAtPosition(final int i) {
373-
return adapter.getContributionForPosition(i).getMedia();
383+
if(adapter.getContributionForPosition(i) != null) {
384+
return adapter.getContributionForPosition(i).getMedia();
385+
}
386+
return null;
374387
}
375388

376389
public int getTotalMediaCount() {
377-
return adapter.getItemCount();
390+
if(adapter != null) {
391+
return adapter.getItemCount();
392+
}
393+
return 0;
378394
}
379395

380396
/**
@@ -406,5 +422,8 @@ public interface Callback {
406422
void showDetail(int position, boolean isWikipediaButtonDisplayed);
407423

408424
void pauseUpload(Contribution contribution);
425+
426+
// Notify the viewpager that number of items have changed.
427+
void viewPagerNotifyDataSetChanged();
409428
}
410429
}

app/src/main/java/fr/free/nrw/commons/explore/ExploreListRootFragment.java

+13
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,19 @@ public Integer getContributionStateAt(int position) {
153153
return null;
154154
}
155155

156+
/**
157+
* Reload media detail fragment once media is nominated
158+
*
159+
* @param index item position that has been nominated
160+
*/
161+
@Override
162+
public void refreshNominatedMedia(int index) {
163+
if(mediaDetails != null && !listFragment.isVisible()) {
164+
removeFragment(mediaDetails);
165+
onMediaClicked(index);
166+
}
167+
}
168+
156169
/**
157170
* This method is called on success of API call for featured images or mobile uploads. The
158171
* viewpager will notified that number of items have changed.

app/src/main/java/fr/free/nrw/commons/explore/SearchActivity.java

+13
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ public Integer getContributionStateAt(int position) {
183183
return null;
184184
}
185185

186+
/**
187+
* Reload media detail fragment once media is nominated
188+
*
189+
* @param index item position that has been nominated
190+
*/
191+
@Override
192+
public void refreshNominatedMedia(int index) {
193+
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
194+
onBackPressed();
195+
onMediaClicked(index);
196+
}
197+
}
198+
186199
/**
187200
* This method is called on success of API call for image Search.
188201
* The viewpager will notified that number of items have changed.

app/src/main/java/fr/free/nrw/commons/explore/depictions/WikidataItemDetailsActivity.java

+13
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ public Integer getContributionStateAt(int position) {
191191
return null;
192192
}
193193

194+
/**
195+
* Reload media detail fragment once media is nominated
196+
*
197+
* @param index item position that has been nominated
198+
*/
199+
@Override
200+
public void refreshNominatedMedia(int index) {
201+
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
202+
onBackPressed();
203+
onMediaClicked(index);
204+
}
205+
}
206+
194207
/**
195208
* Consumers should be simply using this method to use this activity.
196209
*

app/src/main/java/fr/free/nrw/commons/explore/media/PageableMediaFragment.kt

+10
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailP
5555
override fun getTotalMediaCount(): Int = pagedListAdapter.itemCount
5656

5757
override fun getContributionStateAt(position: Int) = null
58+
59+
/**
60+
* Reload media detail fragment once media is nominated
61+
*
62+
* @param index item position that has been nominated
63+
*/
64+
override fun refreshNominatedMedia(index: Int) {
65+
activity?.onBackPressed()
66+
categoryImagesCallback.onMediaClicked(index)
67+
}
5868
}

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

+60-29
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
9494
private int index;
9595
private boolean isDeleted = false;
9696
private boolean isWikipediaButtonDisplayed;
97+
private Callback callback;
9798

9899

99100
public static MediaDetailFragment forMedia(int index, boolean editable, boolean isCategoryImage, boolean isWikipediaButtonDisplayed) {
@@ -195,6 +196,8 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
195196
TextView existingCategories;
196197
@BindView(R.id.no_results_found)
197198
TextView noResultsFound;
199+
@BindView(R.id.progressBarDeletion)
200+
ProgressBar progressBarDeletion;
198201

199202
private ArrayList<String> categoryNames = new ArrayList<>();
200203
private String categorySearchQuery;
@@ -228,6 +231,8 @@ public static MediaDetailFragment forMedia(int index, boolean editable, boolean
228231
*/
229232
private int minimumHeightOfMetadata = 200;
230233

234+
final static String NOMINATING_FOR_DELETION_MEDIA = "Nominating for deletion %s";
235+
231236
@Override
232237
public void onSaveInstanceState(Bundle outState) {
233238
super.onSaveInstanceState(outState);
@@ -305,6 +310,14 @@ public void run() {
305310
return view;
306311
}
307312

313+
@Override
314+
public void onAttach(final Context context) {
315+
super.onAttach(context);
316+
if (getParentFragment() != null) {
317+
callback = (Callback) getParentFragment();
318+
}
319+
}
320+
308321
@OnClick(R.id.mediaDetailImageViewSpacer)
309322
public void launchZoomActivity(View view) {
310323
if (media.getImageUrl() != null) {
@@ -339,6 +352,12 @@ public void onResume() {
339352
media = getArguments().getParcelable("media");
340353
}
341354

355+
media = detailProvider.getMediaAtPosition(index);
356+
357+
if(media != null && applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
358+
enableProgressBar();
359+
}
360+
342361
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(
343362
new OnGlobalLayoutListener() {
344363
@Override
@@ -348,7 +367,9 @@ public void onGlobalLayout() {
348367
}
349368
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
350369
oldWidthOfImageView = scrollView.getWidth();
351-
displayMediaDetails();
370+
if(media != null) {
371+
displayMediaDetails();
372+
}
352373
}
353374
}
354375
);
@@ -431,6 +452,10 @@ private void onDiscussionLoaded(String discussion) {
431452

432453
private void onDeletionPageExists(Boolean deletionPageExists) {
433454
if (deletionPageExists){
455+
if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
456+
applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
457+
progressBarDeletion.setVisibility(GONE);
458+
}
434459
delete.setVisibility(GONE);
435460
nominatedForDeletion.setVisibility(VISIBLE);
436461
} else if (!isCategoryImage) {
@@ -804,7 +829,7 @@ public void onDeleteButtonClicked(){
804829
input.addTextChangedListener(new TextWatcher() {
805830
private void handleText() {
806831
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
807-
if (input.getText().length() == 0) {
832+
if (input.getText().length() == 0 || isDeleted) {
808833
okButton.setEnabled(false);
809834
} else {
810835
okButton.setEnabled(true);
@@ -831,35 +856,37 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
831856

832857
@SuppressLint("CheckResult")
833858
private void onDeleteClicked(Spinner spinner) {
859+
applicationKvStore.putBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), true);
860+
enableProgressBar();
834861
String reason = spinner.getSelectedItem().toString();
835862
Single<Boolean> resultSingle = reasonBuilder.getReason(media, reason)
836863
.flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason));
837-
compositeDisposable.add(resultSingle
838-
.subscribeOn(Schedulers.io())
839-
.observeOn(AndroidSchedulers.mainThread())
840-
.subscribe(s -> {
841-
if (getActivity() != null) {
842-
isDeleted = true;
843-
enableDeleteButton(false);
844-
}
845-
}));
846-
864+
resultSingle
865+
.subscribeOn(Schedulers.io())
866+
.observeOn(AndroidSchedulers.mainThread())
867+
.subscribe(s -> {
868+
if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
869+
applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
870+
callback.nominatingForDeletion(index);
871+
}
872+
});
847873
}
848874

849875
@SuppressLint("CheckResult")
850876
private void onDeleteClickeddialogtext(String reason) {
877+
applicationKvStore.putBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), true);
878+
enableProgressBar();
851879
Single<Boolean> resultSingletext = reasonBuilder.getReason(media, reason)
852880
.flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason));
853-
compositeDisposable.add(resultSingletext
854-
.subscribeOn(Schedulers.io())
855-
.observeOn(AndroidSchedulers.mainThread())
856-
.subscribe(s -> {
857-
if (getActivity() != null) {
858-
isDeleted = true;
859-
enableDeleteButton(false);
860-
}
861-
}));
862-
881+
resultSingletext
882+
.subscribeOn(Schedulers.io())
883+
.observeOn(AndroidSchedulers.mainThread())
884+
.subscribe(s -> {
885+
if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
886+
applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
887+
callback.nominatingForDeletion(index);
888+
}
889+
});
863890
}
864891

865892
@OnClick(R.id.seeMore)
@@ -869,13 +896,13 @@ public void onSeeMoreClicked(){
869896
}
870897
}
871898

872-
private void enableDeleteButton(boolean visibility) {
873-
delete.setEnabled(visibility);
874-
if (visibility) {
875-
delete.setTextColor(getResources().getColor(R.color.primaryTextColor));
876-
} else {
877-
delete.setTextColor(getResources().getColor(R.color.deleteButtonLight));
878-
}
899+
/**
900+
* Enable Progress Bar and Update delete button text.
901+
*/
902+
private void enableProgressBar() {
903+
progressBarDeletion.setVisibility(VISIBLE);
904+
delete.setText("Nominating for Deletion");
905+
isDeleted = true;
879906
}
880907

881908
private void rebuildCatList(List<String> categories) {
@@ -1005,4 +1032,8 @@ public boolean updateCategoryDisplay(List<String> categories) {
10051032
return true;
10061033
}
10071034
}
1035+
1036+
public interface Callback {
1037+
void nominatingForDeletion(int index);
1038+
}
10081039
}

0 commit comments

Comments
 (0)