Skip to content

Commit e8e87b1

Browse files
Fix crash upload wizard (commons-app#5466)
* Upload Wizard Crash Fix * Upload Wizard Crash Fix 2 * Fixes --------- Co-authored-by: shashankkumar <shashankkumar45556@gmail.com>
1 parent 8b8eb84 commit e8e87b1

File tree

7 files changed

+176
-26
lines changed

7 files changed

+176
-26
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java

+113-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import android.os.Build.VERSION;
1919
import android.os.Build.VERSION_CODES;
2020
import android.os.Bundle;
21+
import android.os.Parcel;
22+
import android.os.Parcelable;
2123
import android.provider.Settings;
2224
import android.util.DisplayMetrics;
2325
import android.view.View;
@@ -54,6 +56,7 @@
5456
import fr.free.nrw.commons.nearby.Place;
5557
import fr.free.nrw.commons.settings.Prefs;
5658
import fr.free.nrw.commons.theme.BaseActivity;
59+
import fr.free.nrw.commons.upload.UploadBaseFragment.Callback;
5760
import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment;
5861
import fr.free.nrw.commons.upload.depicts.DepictsFragment;
5962
import fr.free.nrw.commons.upload.license.MediaLicenseFragment;
@@ -171,6 +174,19 @@ protected void onCreate(Bundle savedInstanceState) {
171174

172175
setContentView(R.layout.activity_upload);
173176

177+
/*
178+
If Configuration of device is changed then get the new fragments
179+
created by the system and populate the fragments ArrayList
180+
*/
181+
if (savedInstanceState != null) {
182+
List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
183+
fragments = new ArrayList<>();
184+
for (Fragment fragment : fragmentList) {
185+
fragments.add((UploadBaseFragment) fragment);
186+
}
187+
}
188+
189+
174190
ButterKnife.bind(this);
175191
compositeDisposable = new CompositeDisposable();
176192
init();
@@ -459,7 +475,12 @@ private void receiveSharedItems() {
459475
tvTopCardTitle.setText(getResources()
460476
.getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
461477

462-
fragments = new ArrayList<>();
478+
479+
if(fragments==null){
480+
fragments = new ArrayList<>();
481+
}
482+
483+
463484
/* Suggest users to turn battery optimisation off when uploading more than a few files.
464485
That's because we have noticed that many-files uploads have
465486
a much higher probability of failing than uploads with less files.
@@ -521,7 +542,8 @@ private void receiveSharedItems() {
521542
}
522543
uploadMediaDetailFragment.setImageTobeUploaded(uploadableFile, place, currLocation);
523544
locationManager.unregisterLocationManager();
524-
uploadMediaDetailFragment.setCallback(new UploadMediaDetailFragmentCallback() {
545+
546+
UploadMediaDetailFragmentCallback uploadMediaDetailFragmentCallback = new UploadMediaDetailFragmentCallback() {
525547
@Override
526548
public void deletePictureAtIndex(int index) {
527549
presenter.deletePictureAtIndex(index);
@@ -573,33 +595,99 @@ public int getTotalNumberOfSteps() {
573595
public boolean isWLMUpload() {
574596
return place!=null && place.isMonument();
575597
}
576-
});
577-
fragments.add(uploadMediaDetailFragment);
578-
}
598+
};
599+
600+
if(fragments.size()==0){
601+
uploadMediaDetailFragment.setCallback(uploadMediaDetailFragmentCallback);
602+
fragments.add(uploadMediaDetailFragment);
603+
}else{
604+
UploadMediaDetailFragment fragment = (UploadMediaDetailFragment) fragments.get(0);
605+
fragment.setCallback(uploadMediaDetailFragmentCallback);
606+
}
579607

580-
uploadCategoriesFragment = new UploadCategoriesFragment();
581-
if (place != null) {
582-
Bundle categoryBundle = new Bundle();
583-
categoryBundle.putString(SELECTED_NEARBY_PLACE_CATEGORY, place.getCategory());
584-
uploadCategoriesFragment.setArguments(categoryBundle);
585608
}
586-
uploadCategoriesFragment.setCallback(this);
587609

588-
depictsFragment = new DepictsFragment();
589-
Bundle placeBundle = new Bundle();
590-
placeBundle.putParcelable(SELECTED_NEARBY_PLACE, place);
591-
depictsFragment.setArguments(placeBundle);
592-
depictsFragment.setCallback(this);
610+
//If fragments are not created, create them and add them to the fragments ArrayList
611+
if(!(fragments.size()>1)){
612+
uploadCategoriesFragment = new UploadCategoriesFragment();
613+
if (place != null) {
614+
Bundle categoryBundle = new Bundle();
615+
categoryBundle.putString(SELECTED_NEARBY_PLACE_CATEGORY, place.getCategory());
616+
uploadCategoriesFragment.setArguments(categoryBundle);
617+
}
618+
619+
uploadCategoriesFragment.setCallback(this);
620+
621+
depictsFragment = new DepictsFragment();
622+
Bundle placeBundle = new Bundle();
623+
placeBundle.putParcelable(SELECTED_NEARBY_PLACE, place);
624+
depictsFragment.setArguments(placeBundle);
625+
depictsFragment.setCallback(this);
626+
627+
mediaLicenseFragment = new MediaLicenseFragment();
628+
mediaLicenseFragment.setCallback(this);
629+
630+
fragments.add(depictsFragment);
631+
fragments.add(uploadCategoriesFragment);
632+
fragments.add(mediaLicenseFragment);
633+
634+
}else{
635+
for(int i=1;i<fragments.size();i++){
636+
fragments.get(i).setCallback(new Callback() {
637+
@Override
638+
public void onNextButtonClicked(int index) {
639+
if (index < fragments.size() - 1) {
640+
vpUpload.setCurrentItem(index + 1, false);
641+
fragments.get(index + 1).onBecameVisible();
642+
((LinearLayoutManager) rvThumbnails.getLayoutManager())
643+
.scrollToPositionWithOffset((index > 0) ? index-1 : 0, 0);
644+
} else {
645+
presenter.handleSubmit();
646+
}
647+
648+
}
649+
@Override
650+
public void onPreviousButtonClicked(int index) {
651+
if (index != 0) {
652+
vpUpload.setCurrentItem(index - 1, true);
653+
fragments.get(index - 1).onBecameVisible();
654+
((LinearLayoutManager) rvThumbnails.getLayoutManager())
655+
.scrollToPositionWithOffset((index > 3) ? index-2 : 0, 0);
656+
}
657+
}
658+
@Override
659+
public void showProgress(boolean shouldShow) {
660+
if (shouldShow) {
661+
if (!progressDialog.isShowing()) {
662+
progressDialog.show();
663+
}
664+
} else {
665+
if (progressDialog != null && !isFinishing()) {
666+
progressDialog.dismiss();
667+
}
668+
}
669+
}
670+
@Override
671+
public int getIndexInViewFlipper(UploadBaseFragment fragment) {
672+
return fragments.indexOf(fragment);
673+
}
593674

594-
mediaLicenseFragment = new MediaLicenseFragment();
595-
mediaLicenseFragment.setCallback(this);
675+
@Override
676+
public int getTotalNumberOfSteps() {
677+
return fragments.size();
678+
}
596679

597-
fragments.add(depictsFragment);
598-
fragments.add(uploadCategoriesFragment);
599-
fragments.add(mediaLicenseFragment);
680+
@Override
681+
public boolean isWLMUpload() {
682+
return place!=null && place.isMonument();
683+
}
684+
});
685+
}
686+
}
600687

601688
uploadImagesAdapter.setFragments(fragments);
602689
vpUpload.setOffscreenPageLimit(fragments.size());
690+
603691
}
604692
}
605693

@@ -712,6 +800,7 @@ public void onPreviousButtonClicked(int index) {
712800
* The adapter used to show image upload intermediate fragments
713801
*/
714802

803+
715804
private class UploadImageAdapter extends FragmentStatePagerAdapter {
716805
List<UploadBaseFragment> fragments;
717806

@@ -754,12 +843,15 @@ protected void onDestroy() {
754843
super.onDestroy();
755844
presenter.onDetachView();
756845
compositeDisposable.clear();
846+
fragments = null;
847+
uploadImagesAdapter = null;
757848
if (mediaLicenseFragment != null) {
758849
mediaLicenseFragment.setCallback(null);
759850
}
760851
if (uploadCategoriesFragment != null) {
761852
uploadCategoriesFragment.setCallback(null);
762853
}
854+
763855
}
764856

765857
/**

app/src/main/java/fr/free/nrw/commons/upload/UploadBaseFragment.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import android.os.Bundle;
44

5+
import android.os.Parcelable;
56
import androidx.annotation.Nullable;
67

78
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
89

910
/**
1011
* The base fragment of the fragments in upload
1112
*/
12-
public class UploadBaseFragment extends CommonsDaggerSupportFragment {
13+
public class UploadBaseFragment extends CommonsDaggerSupportFragment {
1314

1415
public Callback callback;
16+
public static final String CALLBACK = "callback";
1517

1618
@Override
1719
public void onCreate(@Nullable Bundle savedInstanceState) {

app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoriesFragment.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
102102
wikiText = bundle.getString("WikiText");
103103
nearbyPlaceCategory = bundle.getString(SELECTED_NEARBY_PLACE_CATEGORY);
104104
}
105-
init();
106-
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
105+
if(callback!=null) {
106+
init();
107+
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
108+
}
109+
107110
}
108111

109112
private void init() {

app/src/main/java/fr/free/nrw/commons/upload/depicts/DepictsFragment.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ public void onViewCreated(@NonNull android.view.View view, @Nullable Bundle save
106106
nearbyPlace = bundle.getParcelable(SELECTED_NEARBY_PLACE);
107107
}
108108

109-
init();
110-
presenter.getDepictedItems().observe(getViewLifecycleOwner(), this::setDepictsList);
109+
if(callback!=null){
110+
init();
111+
presenter.getDepictedItems().observe(getViewLifecycleOwner(), this::setDepictsList);
112+
}
111113
}
112114

113115
/**

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.java

+38
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import javax.inject.Named;
6161
import org.apache.commons.lang3.StringUtils;
6262
import timber.log.Timber;
63+
import android.os.Parcelable;
6364

6465
public class UploadMediaDetailFragment extends UploadBaseFragment implements
6566
UploadMediaDetailsContract.View, UploadMediaDetailAdapter.EventListener {
@@ -74,6 +75,11 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
7475
*/
7576
public static final String LAST_LOCATION = "last_location_while_uploading";
7677
public static final String LAST_ZOOM = "last_zoom_level_while_uploading";
78+
79+
80+
public static final String UPLOADABLE_FILE = "uploadable_file";
81+
82+
public static final String UPLOAD_MEDIA_DETAILS = "upload_media_detail_adapter";
7783
@BindView(R.id.tv_title)
7884
TextView tvTitle;
7985
@BindView(R.id.location_image_view)
@@ -154,8 +160,16 @@ public void setCallback(UploadMediaDetailFragmentCallback callback) {
154160
@Override
155161
public void onCreate(@Nullable Bundle savedInstanceState) {
156162
super.onCreate(savedInstanceState);
163+
164+
165+
if(savedInstanceState!=null && uploadableFile==null) {
166+
uploadableFile = savedInstanceState.getParcelable(UPLOADABLE_FILE);
167+
}
168+
157169
}
158170

171+
172+
159173
public void setImageTobeUploaded(UploadableFile uploadableFile, Place place,
160174
LatLng inAppPictureLocation) {
161175
this.uploadableFile = uploadableFile;
@@ -168,15 +182,25 @@ public void setImageTobeUploaded(UploadableFile uploadableFile, Place place,
168182
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
169183
@Nullable Bundle savedInstanceState) {
170184
return inflater.inflate(R.layout.fragment_upload_media_detail_fragment, container, false);
185+
171186
}
172187

173188
@Override
174189
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
175190
super.onViewCreated(view, savedInstanceState);
176191
ButterKnife.bind(this, view);
192+
177193
if (callback != null) {
178194
init();
179195
}
196+
197+
if(savedInstanceState!=null){
198+
if(uploadMediaDetailAdapter.getItems().size()==0){
199+
uploadMediaDetailAdapter.setItems(savedInstanceState.getParcelableArrayList(UPLOAD_MEDIA_DETAILS));
200+
presenter.setUploadMediaDetails(uploadMediaDetailAdapter.getItems(), callback.getIndexInViewFlipper(this));
201+
}
202+
}
203+
180204
}
181205

182206
private void init() {
@@ -735,4 +759,18 @@ public void onButtonCopyTitleDescToSubsequentMedia(){
735759
Toast.makeText(getContext(), getResources().getString(R.string.copied_successfully), Toast.LENGTH_SHORT).show();
736760
}
737761

762+
@Override
763+
public void onSaveInstanceState(final Bundle outState) {
764+
super.onSaveInstanceState(outState);
765+
766+
if(uploadableFile!=null){
767+
outState.putParcelable(UPLOADABLE_FILE,uploadableFile);
768+
}
769+
if(uploadMediaDetailAdapter!=null){
770+
outState.putParcelableArrayList(UPLOAD_MEDIA_DETAILS,
771+
(ArrayList<? extends Parcelable>) uploadMediaDetailAdapter.getItems());
772+
}
773+
}
774+
775+
738776
}

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailsContract.java

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ interface UserActionListener extends BasePresenter<View> {
4848

4949
void receiveImage(UploadableFile uploadableFile, Place place, LatLng inAppPictureLocation);
5050

51+
void setUploadMediaDetails(List<UploadMediaDetail> uploadMediaDetails, int uploadItemIndex);
52+
5153
boolean verifyImageQuality(int uploadItemIndex, LatLng inAppPictureLocation);
5254

5355
void copyTitleAndDescriptionToSubsequentMedia(int indexInViewFlipper);

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaPresenter.java

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public void onDetachView() {
8181
compositeDisposable.clear();
8282
}
8383

84+
/**
85+
* Sets the Upload Media Details for the corresponding upload item
86+
*
87+
* @param uploadMediaDetails
88+
* @param uploadItemIndex
89+
*/
90+
@Override
91+
public void setUploadMediaDetails(List<UploadMediaDetail> uploadMediaDetails, int uploadItemIndex) {
92+
repository.getUploads().get(uploadItemIndex).setMediaDetails(uploadMediaDetails);
93+
}
94+
8495
/**
8596
* Receives the corresponding uploadable file, processes it and return the view with and uplaod item
8697
* @param uploadableFile

0 commit comments

Comments
 (0)