Fix crash upload wizard (#5466)

* Upload Wizard Crash Fix

* Upload Wizard Crash Fix 2

* Fixes

---------

Co-authored-by: shashankkumar <shashankkumar45556@gmail.com>
This commit is contained in:
Shashank Kumar 2024-01-24 15:00:49 +05:30 committed by GitHub
parent 8b8eb84fae
commit e8e87b1d1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 178 additions and 28 deletions

View file

@ -18,6 +18,8 @@ import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.View;
@ -54,6 +56,7 @@ import fr.free.nrw.commons.mwapi.UserClient;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.upload.UploadBaseFragment.Callback;
import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment;
import fr.free.nrw.commons.upload.depicts.DepictsFragment;
import fr.free.nrw.commons.upload.license.MediaLicenseFragment;
@ -171,6 +174,19 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
setContentView(R.layout.activity_upload);
/*
If Configuration of device is changed then get the new fragments
created by the system and populate the fragments ArrayList
*/
if (savedInstanceState != null) {
List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
fragments = new ArrayList<>();
for (Fragment fragment : fragmentList) {
fragments.add((UploadBaseFragment) fragment);
}
}
ButterKnife.bind(this);
compositeDisposable = new CompositeDisposable();
init();
@ -459,7 +475,12 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
tvTopCardTitle.setText(getResources()
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
fragments = new ArrayList<>();
if(fragments==null){
fragments = new ArrayList<>();
}
/* Suggest users to turn battery optimisation off when uploading more than a few files.
That's because we have noticed that many-files uploads have
a much higher probability of failing than uploads with less files.
@ -521,7 +542,8 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
}
uploadMediaDetailFragment.setImageTobeUploaded(uploadableFile, place, currLocation);
locationManager.unregisterLocationManager();
uploadMediaDetailFragment.setCallback(new UploadMediaDetailFragmentCallback() {
UploadMediaDetailFragmentCallback uploadMediaDetailFragmentCallback = new UploadMediaDetailFragmentCallback() {
@Override
public void deletePictureAtIndex(int index) {
presenter.deletePictureAtIndex(index);
@ -573,33 +595,99 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
public boolean isWLMUpload() {
return place!=null && place.isMonument();
}
});
fragments.add(uploadMediaDetailFragment);
};
if(fragments.size()==0){
uploadMediaDetailFragment.setCallback(uploadMediaDetailFragmentCallback);
fragments.add(uploadMediaDetailFragment);
}else{
UploadMediaDetailFragment fragment = (UploadMediaDetailFragment) fragments.get(0);
fragment.setCallback(uploadMediaDetailFragmentCallback);
}
}
uploadCategoriesFragment = new UploadCategoriesFragment();
if (place != null) {
Bundle categoryBundle = new Bundle();
categoryBundle.putString(SELECTED_NEARBY_PLACE_CATEGORY, place.getCategory());
uploadCategoriesFragment.setArguments(categoryBundle);
//If fragments are not created, create them and add them to the fragments ArrayList
if(!(fragments.size()>1)){
uploadCategoriesFragment = new UploadCategoriesFragment();
if (place != null) {
Bundle categoryBundle = new Bundle();
categoryBundle.putString(SELECTED_NEARBY_PLACE_CATEGORY, place.getCategory());
uploadCategoriesFragment.setArguments(categoryBundle);
}
uploadCategoriesFragment.setCallback(this);
depictsFragment = new DepictsFragment();
Bundle placeBundle = new Bundle();
placeBundle.putParcelable(SELECTED_NEARBY_PLACE, place);
depictsFragment.setArguments(placeBundle);
depictsFragment.setCallback(this);
mediaLicenseFragment = new MediaLicenseFragment();
mediaLicenseFragment.setCallback(this);
fragments.add(depictsFragment);
fragments.add(uploadCategoriesFragment);
fragments.add(mediaLicenseFragment);
}else{
for(int i=1;i<fragments.size();i++){
fragments.get(i).setCallback(new Callback() {
@Override
public void onNextButtonClicked(int index) {
if (index < fragments.size() - 1) {
vpUpload.setCurrentItem(index + 1, false);
fragments.get(index + 1).onBecameVisible();
((LinearLayoutManager) rvThumbnails.getLayoutManager())
.scrollToPositionWithOffset((index > 0) ? index-1 : 0, 0);
} else {
presenter.handleSubmit();
}
}
@Override
public void onPreviousButtonClicked(int index) {
if (index != 0) {
vpUpload.setCurrentItem(index - 1, true);
fragments.get(index - 1).onBecameVisible();
((LinearLayoutManager) rvThumbnails.getLayoutManager())
.scrollToPositionWithOffset((index > 3) ? index-2 : 0, 0);
}
}
@Override
public void showProgress(boolean shouldShow) {
if (shouldShow) {
if (!progressDialog.isShowing()) {
progressDialog.show();
}
} else {
if (progressDialog != null && !isFinishing()) {
progressDialog.dismiss();
}
}
}
@Override
public int getIndexInViewFlipper(UploadBaseFragment fragment) {
return fragments.indexOf(fragment);
}
@Override
public int getTotalNumberOfSteps() {
return fragments.size();
}
@Override
public boolean isWLMUpload() {
return place!=null && place.isMonument();
}
});
}
}
uploadCategoriesFragment.setCallback(this);
depictsFragment = new DepictsFragment();
Bundle placeBundle = new Bundle();
placeBundle.putParcelable(SELECTED_NEARBY_PLACE, place);
depictsFragment.setArguments(placeBundle);
depictsFragment.setCallback(this);
mediaLicenseFragment = new MediaLicenseFragment();
mediaLicenseFragment.setCallback(this);
fragments.add(depictsFragment);
fragments.add(uploadCategoriesFragment);
fragments.add(mediaLicenseFragment);
uploadImagesAdapter.setFragments(fragments);
vpUpload.setOffscreenPageLimit(fragments.size());
}
}
@ -712,6 +800,7 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
* The adapter used to show image upload intermediate fragments
*/
private class UploadImageAdapter extends FragmentStatePagerAdapter {
List<UploadBaseFragment> fragments;
@ -754,12 +843,15 @@ public class UploadActivity extends BaseActivity implements UploadContract.View,
super.onDestroy();
presenter.onDetachView();
compositeDisposable.clear();
fragments = null;
uploadImagesAdapter = null;
if (mediaLicenseFragment != null) {
mediaLicenseFragment.setCallback(null);
}
if (uploadCategoriesFragment != null) {
uploadCategoriesFragment.setCallback(null);
}
}
/**

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.upload;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.annotation.Nullable;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
@ -9,9 +10,10 @@ import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
/**
* The base fragment of the fragments in upload
*/
public class UploadBaseFragment extends CommonsDaggerSupportFragment {
public class UploadBaseFragment extends CommonsDaggerSupportFragment {
public Callback callback;
public static final String CALLBACK = "callback";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {

View file

@ -102,8 +102,11 @@ public class UploadCategoriesFragment extends UploadBaseFragment implements Cate
wikiText = bundle.getString("WikiText");
nearbyPlaceCategory = bundle.getString(SELECTED_NEARBY_PLACE_CATEGORY);
}
init();
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
if(callback!=null) {
init();
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
}
}
private void init() {

View file

@ -106,8 +106,10 @@ public class DepictsFragment extends UploadBaseFragment implements DepictsContra
nearbyPlace = bundle.getParcelable(SELECTED_NEARBY_PLACE);
}
init();
presenter.getDepictedItems().observe(getViewLifecycleOwner(), this::setDepictsList);
if(callback!=null){
init();
presenter.getDepictedItems().observe(getViewLifecycleOwner(), this::setDepictsList);
}
}
/**

View file

@ -60,6 +60,7 @@ import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.lang3.StringUtils;
import timber.log.Timber;
import android.os.Parcelable;
public class UploadMediaDetailFragment extends UploadBaseFragment implements
UploadMediaDetailsContract.View, UploadMediaDetailAdapter.EventListener {
@ -74,6 +75,11 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
*/
public static final String LAST_LOCATION = "last_location_while_uploading";
public static final String LAST_ZOOM = "last_zoom_level_while_uploading";
public static final String UPLOADABLE_FILE = "uploadable_file";
public static final String UPLOAD_MEDIA_DETAILS = "upload_media_detail_adapter";
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.location_image_view)
@ -154,8 +160,16 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!=null && uploadableFile==null) {
uploadableFile = savedInstanceState.getParcelable(UPLOADABLE_FILE);
}
}
public void setImageTobeUploaded(UploadableFile uploadableFile, Place place,
LatLng inAppPictureLocation) {
this.uploadableFile = uploadableFile;
@ -168,15 +182,25 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_upload_media_detail_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
if (callback != null) {
init();
}
if(savedInstanceState!=null){
if(uploadMediaDetailAdapter.getItems().size()==0){
uploadMediaDetailAdapter.setItems(savedInstanceState.getParcelableArrayList(UPLOAD_MEDIA_DETAILS));
presenter.setUploadMediaDetails(uploadMediaDetailAdapter.getItems(), callback.getIndexInViewFlipper(this));
}
}
}
private void init() {
@ -735,4 +759,18 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
Toast.makeText(getContext(), getResources().getString(R.string.copied_successfully), Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if(uploadableFile!=null){
outState.putParcelable(UPLOADABLE_FILE,uploadableFile);
}
if(uploadMediaDetailAdapter!=null){
outState.putParcelableArrayList(UPLOAD_MEDIA_DETAILS,
(ArrayList<? extends Parcelable>) uploadMediaDetailAdapter.getItems());
}
}
}

View file

@ -48,6 +48,8 @@ public interface UploadMediaDetailsContract {
void receiveImage(UploadableFile uploadableFile, Place place, LatLng inAppPictureLocation);
void setUploadMediaDetails(List<UploadMediaDetail> uploadMediaDetails, int uploadItemIndex);
boolean verifyImageQuality(int uploadItemIndex, LatLng inAppPictureLocation);
void copyTitleAndDescriptionToSubsequentMedia(int indexInViewFlipper);

View file

@ -81,6 +81,17 @@ public class UploadMediaPresenter implements UserActionListener, SimilarImageInt
compositeDisposable.clear();
}
/**
* Sets the Upload Media Details for the corresponding upload item
*
* @param uploadMediaDetails
* @param uploadItemIndex
*/
@Override
public void setUploadMediaDetails(List<UploadMediaDetail> uploadMediaDetails, int uploadItemIndex) {
repository.getUploads().get(uploadItemIndex).setMediaDetails(uploadMediaDetails);
}
/**
* Receives the corresponding uploadable file, processes it and return the view with and uplaod item
* @param uploadableFile