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;
}
/**
* 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
* viewpager will notified that number of items have changed.

View file

@ -165,6 +165,19 @@ public class CategoryDetailsActivity extends BaseActivity
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
*/

View file

@ -565,6 +565,16 @@ public class ContributionsFragment
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
* mediaDetailPagerFragment, and preserve previous state in back stack. Called when user selects a
@ -620,6 +630,21 @@ public class ContributionsFragment
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
// banner and description will hide. Tap again to show description.
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).
@ -370,11 +380,17 @@ public class ContributionsListFragment extends CommonsDaggerSupportFragment impl
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() {
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 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;
}
/**
* 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
* viewpager will notified that number of items have changed.

View file

@ -183,6 +183,19 @@ public class SearchActivity extends BaseActivity
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.
* 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;
}
/**
* 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.
*

View file

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

View file

@ -42,7 +42,7 @@ import java.util.Objects;
import javax.inject.Inject;
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;
@ -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.
* 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();
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)

View file

@ -381,14 +381,34 @@
android:text="@string/copy_wikicode"
android:textColor="@color/primaryTextColor" />
<Button
android:id="@+id/nominateDeletion"
android:background="@drawable/bg_delete_button"
android:textColor="@color/primaryTextColor"
android:layout_margin="@dimen/standard_gap"
<FrameLayout
android:layout_width="match_parent"
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>
</ScrollView>