Fixes #2815 - Nominating for deletion is cancelled on leaving the media details (#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>
This commit is contained in:
Pratham Pahariya 2021-06-04 07:45:39 +05:30 committed by GitHub
parent 76b30678da
commit ca18763e4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 224 additions and 38 deletions

View file

@ -169,6 +169,22 @@ public class BookmarkListRootFragment extends CommonsDaggerSupportFragment imple
return null; return null;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if(mediaDetails != null && !listFragment.isVisible()) {
removeFragment(mediaDetails);
mediaDetails = new MediaDetailPagerFragment(false, true);
((BookmarkFragment) getParentFragment()).setScroll(false);
setFragment(mediaDetails, listFragment);
mediaDetails.showImage(index);
}
}
/** /**
* This method is called on success of API call for featured images or mobile uploads. The * This method is called on success of API call for featured images or mobile uploads. The
* viewpager will notified that number of items have changed. * viewpager will notified that number of items have changed.

View file

@ -165,6 +165,19 @@ public class CategoryDetailsActivity extends BaseActivity
return null; return null;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
onBackPressed();
onMediaClicked(index);
}
}
/** /**
* This method inflates the menu in the toolbar * This method inflates the menu in the toolbar
*/ */

View file

@ -565,6 +565,16 @@ public class ContributionsFragment
contributionsPresenter.saveContribution(contribution); contributionsPresenter.saveContribution(contribution);
} }
/**
* Notify the viewpager that number of items have changed.
*/
@Override
public void viewPagerNotifyDataSetChanged() {
if (mediaDetailPagerFragment != null) {
mediaDetailPagerFragment.notifyDataSetChanged();
}
}
/** /**
* Replace whatever is in the current contributionsFragmentContainer view with * Replace whatever is in the current contributionsFragmentContainer view with
* mediaDetailPagerFragment, and preserve previous state in back stack. Called when user selects a * mediaDetailPagerFragment, and preserve previous state in back stack. Called when user selects a
@ -620,6 +630,21 @@ public class ContributionsFragment
return mediaDetailPagerFragment; return mediaDetailPagerFragment;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if(mediaDetailPagerFragment != null && !contributionsListFragment.isVisible()) {
removeFragment(mediaDetailPagerFragment);
mediaDetailPagerFragment = new MediaDetailPagerFragment(false, true);
mediaDetailPagerFragment.showImage(index);
showMediaDetailPagerFragment();
}
}
// click listener to toggle description that means uses can press the limited connection // click listener to toggle description that means uses can press the limited connection
// banner and description will hide. Tap again to show description. // banner and description will hide. Tap again to show description.
private View.OnClickListener toggleDescriptionListener = new View.OnClickListener() { private View.OnClickListener toggleDescriptionListener = new View.OnClickListener() {

View file

@ -157,6 +157,16 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
} }
} }
} }
/**
* Called whenever items in the list have changed
* Calls viewPagerNotifyDataSetChanged() that will notify the viewpager
*/
@Override
public void onItemRangeChanged(final int positionStart, final int itemCount) {
super.onItemRangeChanged(positionStart, itemCount);
callback.viewPagerNotifyDataSetChanged();
}
}); });
//Fab close on touch outside (Scrolling or taping on item triggers this action). //Fab close on touch outside (Scrolling or taping on item triggers this action).
@ -370,11 +380,17 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
public Media getMediaAtPosition(final int i) { public Media getMediaAtPosition(final int i) {
return adapter.getContributionForPosition(i).getMedia(); if(adapter.getContributionForPosition(i) != null) {
return adapter.getContributionForPosition(i).getMedia();
}
return null;
} }
public int getTotalMediaCount() { public int getTotalMediaCount() {
return adapter.getItemCount(); if(adapter != null) {
return adapter.getItemCount();
}
return 0;
} }
/** /**
@ -406,5 +422,8 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
void showDetail(int position, boolean isWikipediaButtonDisplayed); void showDetail(int position, boolean isWikipediaButtonDisplayed);
void pauseUpload(Contribution contribution); void pauseUpload(Contribution contribution);
// Notify the viewpager that number of items have changed.
void viewPagerNotifyDataSetChanged();
} }
} }

View file

@ -153,6 +153,19 @@ public class ExploreListRootFragment extends CommonsDaggerSupportFragment implem
return null; return null;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if(mediaDetails != null && !listFragment.isVisible()) {
removeFragment(mediaDetails);
onMediaClicked(index);
}
}
/** /**
* This method is called on success of API call for featured images or mobile uploads. The * This method is called on success of API call for featured images or mobile uploads. The
* viewpager will notified that number of items have changed. * viewpager will notified that number of items have changed.

View file

@ -183,6 +183,19 @@ public class SearchActivity extends BaseActivity
return null; return null;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
onBackPressed();
onMediaClicked(index);
}
}
/** /**
* This method is called on success of API call for image Search. * This method is called on success of API call for image Search.
* The viewpager will notified that number of items have changed. * The viewpager will notified that number of items have changed.

View file

@ -191,6 +191,19 @@ public class WikidataItemDetailsActivity extends BaseActivity implements MediaDe
return null; return null;
} }
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
@Override
public void refreshNominatedMedia(int index) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
onBackPressed();
onMediaClicked(index);
}
}
/** /**
* Consumers should be simply using this method to use this activity. * Consumers should be simply using this method to use this activity.
* *

View file

@ -55,4 +55,14 @@ abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailP
override fun getTotalMediaCount(): Int = pagedListAdapter.itemCount override fun getTotalMediaCount(): Int = pagedListAdapter.itemCount
override fun getContributionStateAt(position: Int) = null override fun getContributionStateAt(position: Int) = null
/**
* Reload media detail fragment once media is nominated
*
* @param index item position that has been nominated
*/
override fun refreshNominatedMedia(index: Int) {
activity?.onBackPressed()
categoryImagesCallback.onMediaClicked(index)
}
} }

View file

@ -94,6 +94,7 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
private int index; private int index;
private boolean isDeleted = false; private boolean isDeleted = false;
private boolean isWikipediaButtonDisplayed; private boolean isWikipediaButtonDisplayed;
private Callback callback;
public static MediaDetailFragment forMedia(int index, boolean editable, boolean isCategoryImage, boolean isWikipediaButtonDisplayed) { public static MediaDetailFragment forMedia(int index, boolean editable, boolean isCategoryImage, boolean isWikipediaButtonDisplayed) {
@ -195,6 +196,8 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
TextView existingCategories; TextView existingCategories;
@BindView(R.id.no_results_found) @BindView(R.id.no_results_found)
TextView noResultsFound; TextView noResultsFound;
@BindView(R.id.progressBarDeletion)
ProgressBar progressBarDeletion;
private ArrayList<String> categoryNames = new ArrayList<>(); private ArrayList<String> categoryNames = new ArrayList<>();
private String categorySearchQuery; private String categorySearchQuery;
@ -228,6 +231,8 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
*/ */
private int minimumHeightOfMetadata = 200; private int minimumHeightOfMetadata = 200;
final static String NOMINATING_FOR_DELETION_MEDIA = "Nominating for deletion %s";
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -305,6 +310,14 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
return view; return view;
} }
@Override
public void onAttach(final Context context) {
super.onAttach(context);
if (getParentFragment() != null) {
callback = (Callback) getParentFragment();
}
}
@OnClick(R.id.mediaDetailImageViewSpacer) @OnClick(R.id.mediaDetailImageViewSpacer)
public void launchZoomActivity(View view) { public void launchZoomActivity(View view) {
if (media.getImageUrl() != null) { if (media.getImageUrl() != null) {
@ -339,6 +352,12 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
media = getArguments().getParcelable("media"); media = getArguments().getParcelable("media");
} }
media = detailProvider.getMediaAtPosition(index);
if(media != null && applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
enableProgressBar();
}
scrollView.getViewTreeObserver().addOnGlobalLayoutListener( scrollView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() { new OnGlobalLayoutListener() {
@Override @Override
@ -348,7 +367,9 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
} }
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this); scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
oldWidthOfImageView = scrollView.getWidth(); oldWidthOfImageView = scrollView.getWidth();
displayMediaDetails(); if(media != null) {
displayMediaDetails();
}
} }
} }
); );
@ -431,6 +452,10 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
private void onDeletionPageExists(Boolean deletionPageExists) { private void onDeletionPageExists(Boolean deletionPageExists) {
if (deletionPageExists){ if (deletionPageExists){
if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
progressBarDeletion.setVisibility(GONE);
}
delete.setVisibility(GONE); delete.setVisibility(GONE);
nominatedForDeletion.setVisibility(VISIBLE); nominatedForDeletion.setVisibility(VISIBLE);
} else if (!isCategoryImage) { } else if (!isCategoryImage) {
@ -804,7 +829,7 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
input.addTextChangedListener(new TextWatcher() { input.addTextChangedListener(new TextWatcher() {
private void handleText() { private void handleText() {
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
if (input.getText().length() == 0) { if (input.getText().length() == 0 || isDeleted) {
okButton.setEnabled(false); okButton.setEnabled(false);
} else { } else {
okButton.setEnabled(true); okButton.setEnabled(true);
@ -831,35 +856,37 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
private void onDeleteClicked(Spinner spinner) { private void onDeleteClicked(Spinner spinner) {
applicationKvStore.putBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), true);
enableProgressBar();
String reason = spinner.getSelectedItem().toString(); String reason = spinner.getSelectedItem().toString();
Single<Boolean> resultSingle = reasonBuilder.getReason(media, reason) Single<Boolean> resultSingle = reasonBuilder.getReason(media, reason)
.flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason)); .flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason));
compositeDisposable.add(resultSingle resultSingle
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> { .subscribe(s -> {
if (getActivity() != null) { if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
isDeleted = true; applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
enableDeleteButton(false); callback.nominatingForDeletion(index);
} }
})); });
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
private void onDeleteClickeddialogtext(String reason) { private void onDeleteClickeddialogtext(String reason) {
applicationKvStore.putBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), true);
enableProgressBar();
Single<Boolean> resultSingletext = reasonBuilder.getReason(media, reason) Single<Boolean> resultSingletext = reasonBuilder.getReason(media, reason)
.flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason)); .flatMap(reasonString -> deleteHelper.makeDeletion(getContext(), media, reason));
compositeDisposable.add(resultSingletext resultSingletext
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> { .subscribe(s -> {
if (getActivity() != null) { if(applicationKvStore.getBoolean(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()), false)) {
isDeleted = true; applicationKvStore.remove(String.format(NOMINATING_FOR_DELETION_MEDIA, media.getImageUrl()));
enableDeleteButton(false); callback.nominatingForDeletion(index);
} }
})); });
} }
@OnClick(R.id.seeMore) @OnClick(R.id.seeMore)
@ -869,13 +896,13 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
} }
} }
private void enableDeleteButton(boolean visibility) { /**
delete.setEnabled(visibility); * Enable Progress Bar and Update delete button text.
if (visibility) { */
delete.setTextColor(getResources().getColor(R.color.primaryTextColor)); private void enableProgressBar() {
} else { progressBarDeletion.setVisibility(VISIBLE);
delete.setTextColor(getResources().getColor(R.color.deleteButtonLight)); delete.setText("Nominating for Deletion");
} isDeleted = true;
} }
private void rebuildCatList(List<String> categories) { private void rebuildCatList(List<String> categories) {
@ -1005,4 +1032,8 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
return true; return true;
} }
} }
public interface Callback {
void nominatingForDeletion(int index);
}
} }

View file

@ -42,7 +42,7 @@ import java.util.Objects;
import javax.inject.Inject; import javax.inject.Inject;
import timber.log.Timber; import timber.log.Timber;
public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener { public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment implements ViewPager.OnPageChangeListener, MediaDetailFragment.Callback {
@Inject BookmarkPicturesDao bookmarkDao; @Inject BookmarkPicturesDao bookmarkDao;
@ -382,6 +382,16 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
} }
} }
/**
* Called after the media is nominated for deletion
*
* @param index item position that has been nominated
*/
@Override
public void nominatingForDeletion(int index) {
provider.refreshNominatedMedia(index);
}
/** /**
* backButtonClicked is called on a back event in the media details pager. * backButtonClicked is called on a back event in the media details pager.
* returns true after closing the categoryEditContainer if open, implying that event was handled. * returns true after closing the categoryEditContainer if open, implying that event was handled.
@ -398,6 +408,9 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
int getTotalMediaCount(); int getTotalMediaCount();
Integer getContributionStateAt(int position); Integer getContributionStateAt(int position);
// Reload media detail fragment once media is nominated
void refreshNominatedMedia(int index);
} }
//FragmentStatePagerAdapter allows user to swipe across collection of images (no. of images undetermined) //FragmentStatePagerAdapter allows user to swipe across collection of images (no. of images undetermined)

View file

@ -381,14 +381,34 @@
android:text="@string/copy_wikicode" android:text="@string/copy_wikicode"
android:textColor="@color/primaryTextColor" /> android:textColor="@color/primaryTextColor" />
<Button <FrameLayout
android:id="@+id/nominateDeletion"
android:background="@drawable/bg_delete_button"
android:textColor="@color/primaryTextColor"
android:layout_margin="@dimen/standard_gap"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/nominate_deletion"/> android:orientation="horizontal"
android:layout_margin="@dimen/standard_gap">
<Button
android:id="@+id/nominateDeletion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_delete_button"
android:text="@string/nominate_deletion"
android:textColor="@color/primaryTextColor" />
<ProgressBar
android:id="@+id/progressBarDeletion"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_horizontal|center_vertical"
android:elevation="4dp"
android:indeterminate="true"
android:indeterminateTint="@color/primaryTextColor"
android:indeterminateTintMode="src_atop"
android:visibility="gone"/>
</FrameLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>