|
18 | 18 | import android.os.Build.VERSION;
|
19 | 19 | import android.os.Build.VERSION_CODES;
|
20 | 20 | import android.os.Bundle;
|
| 21 | +import android.os.Parcel; |
| 22 | +import android.os.Parcelable; |
21 | 23 | import android.provider.Settings;
|
22 | 24 | import android.util.DisplayMetrics;
|
23 | 25 | import android.view.View;
|
|
54 | 56 | import fr.free.nrw.commons.nearby.Place;
|
55 | 57 | import fr.free.nrw.commons.settings.Prefs;
|
56 | 58 | import fr.free.nrw.commons.theme.BaseActivity;
|
| 59 | +import fr.free.nrw.commons.upload.UploadBaseFragment.Callback; |
57 | 60 | import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment;
|
58 | 61 | import fr.free.nrw.commons.upload.depicts.DepictsFragment;
|
59 | 62 | import fr.free.nrw.commons.upload.license.MediaLicenseFragment;
|
@@ -171,6 +174,19 @@ protected void onCreate(Bundle savedInstanceState) {
|
171 | 174 |
|
172 | 175 | setContentView(R.layout.activity_upload);
|
173 | 176 |
|
| 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 | + |
174 | 190 | ButterKnife.bind(this);
|
175 | 191 | compositeDisposable = new CompositeDisposable();
|
176 | 192 | init();
|
@@ -459,7 +475,12 @@ private void receiveSharedItems() {
|
459 | 475 | tvTopCardTitle.setText(getResources()
|
460 | 476 | .getQuantityString(R.plurals.upload_count_title, uploadableFiles.size(), uploadableFiles.size()));
|
461 | 477 |
|
462 |
| - fragments = new ArrayList<>(); |
| 478 | + |
| 479 | + if(fragments==null){ |
| 480 | + fragments = new ArrayList<>(); |
| 481 | + } |
| 482 | + |
| 483 | + |
463 | 484 | /* Suggest users to turn battery optimisation off when uploading more than a few files.
|
464 | 485 | That's because we have noticed that many-files uploads have
|
465 | 486 | a much higher probability of failing than uploads with less files.
|
@@ -521,7 +542,8 @@ private void receiveSharedItems() {
|
521 | 542 | }
|
522 | 543 | uploadMediaDetailFragment.setImageTobeUploaded(uploadableFile, place, currLocation);
|
523 | 544 | locationManager.unregisterLocationManager();
|
524 |
| - uploadMediaDetailFragment.setCallback(new UploadMediaDetailFragmentCallback() { |
| 545 | + |
| 546 | + UploadMediaDetailFragmentCallback uploadMediaDetailFragmentCallback = new UploadMediaDetailFragmentCallback() { |
525 | 547 | @Override
|
526 | 548 | public void deletePictureAtIndex(int index) {
|
527 | 549 | presenter.deletePictureAtIndex(index);
|
@@ -573,33 +595,99 @@ public int getTotalNumberOfSteps() {
|
573 | 595 | public boolean isWLMUpload() {
|
574 | 596 | return place!=null && place.isMonument();
|
575 | 597 | }
|
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 | + } |
579 | 607 |
|
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); |
585 | 608 | }
|
586 |
| - uploadCategoriesFragment.setCallback(this); |
587 | 609 |
|
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 | + } |
593 | 674 |
|
594 |
| - mediaLicenseFragment = new MediaLicenseFragment(); |
595 |
| - mediaLicenseFragment.setCallback(this); |
| 675 | + @Override |
| 676 | + public int getTotalNumberOfSteps() { |
| 677 | + return fragments.size(); |
| 678 | + } |
596 | 679 |
|
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 | + } |
600 | 687 |
|
601 | 688 | uploadImagesAdapter.setFragments(fragments);
|
602 | 689 | vpUpload.setOffscreenPageLimit(fragments.size());
|
| 690 | + |
603 | 691 | }
|
604 | 692 | }
|
605 | 693 |
|
@@ -712,6 +800,7 @@ public void onPreviousButtonClicked(int index) {
|
712 | 800 | * The adapter used to show image upload intermediate fragments
|
713 | 801 | */
|
714 | 802 |
|
| 803 | + |
715 | 804 | private class UploadImageAdapter extends FragmentStatePagerAdapter {
|
716 | 805 | List<UploadBaseFragment> fragments;
|
717 | 806 |
|
@@ -754,12 +843,15 @@ protected void onDestroy() {
|
754 | 843 | super.onDestroy();
|
755 | 844 | presenter.onDetachView();
|
756 | 845 | compositeDisposable.clear();
|
| 846 | + fragments = null; |
| 847 | + uploadImagesAdapter = null; |
757 | 848 | if (mediaLicenseFragment != null) {
|
758 | 849 | mediaLicenseFragment.setCallback(null);
|
759 | 850 | }
|
760 | 851 | if (uploadCategoriesFragment != null) {
|
761 | 852 | uploadCategoriesFragment.setCallback(null);
|
762 | 853 | }
|
| 854 | + |
763 | 855 | }
|
764 | 856 |
|
765 | 857 | /**
|
|
0 commit comments