diff --git a/app/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.kt b/app/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.kt index d919ed52af6e..a26666970524 100644 --- a/app/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.kt +++ b/app/src/main/java/com/nextcloud/client/onboarding/FirstRunActivity.kt @@ -18,7 +18,7 @@ import androidx.activity.OnBackPressedCallback import androidx.activity.result.ActivityResult import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts -import androidx.viewpager.widget.ViewPager +import androidx.viewpager2.widget.ViewPager2 import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.client.account.UserAccountManager import com.nextcloud.client.appinfo.AppInfo @@ -39,7 +39,7 @@ import javax.inject.Inject /** * Activity displaying general feature after a fresh install. */ -class FirstRunActivity : BaseActivity(), ViewPager.OnPageChangeListener, Injectable { +class FirstRunActivity : BaseActivity(), Injectable { @JvmField @Inject @@ -171,10 +171,14 @@ class FirstRunActivity : BaseActivity(), ViewPager.OnPageChangeListener, Injecta @Suppress("SpreadOperator") private fun setupFeaturesViewAdapter() { - val featuresViewAdapter = FeaturesViewAdapter(supportFragmentManager, *firstRun) - binding.progressIndicator.setNumberOfSteps(featuresViewAdapter.count) + val featuresViewAdapter = FeaturesViewAdapter(this, *firstRun) + binding.progressIndicator.setNumberOfSteps(featuresViewAdapter.itemCount) binding.contentPanel.adapter = featuresViewAdapter - binding.contentPanel.addOnPageChangeListener(this) + binding.contentPanel.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + binding.progressIndicator.animateToStep(position + 1) + } + }) } private fun handleOnBackPressed() { @@ -236,18 +240,6 @@ class FirstRunActivity : BaseActivity(), ViewPager.OnPageChangeListener, Injecta super.onStop() } - override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { - // unused but to be implemented due to abstract parent - } - - override fun onPageSelected(position: Int) { - binding.progressIndicator.animateToStep(position + 1) - } - - override fun onPageScrollStateChanged(state: Int) { - // unused but to be implemented due to abstract parent - } - companion object { const val EXTRA_ALLOW_CLOSE = "ALLOW_CLOSE" const val EXTRA_EXIT = "EXIT" diff --git a/app/src/main/java/com/nextcloud/client/onboarding/WhatsNewActivity.kt b/app/src/main/java/com/nextcloud/client/onboarding/WhatsNewActivity.kt index 669587658bb0..b1fdf9e23410 100644 --- a/app/src/main/java/com/nextcloud/client/onboarding/WhatsNewActivity.kt +++ b/app/src/main/java/com/nextcloud/client/onboarding/WhatsNewActivity.kt @@ -13,7 +13,7 @@ import android.os.Bundle import android.view.View import androidx.activity.OnBackPressedCallback import androidx.fragment.app.FragmentActivity -import androidx.viewpager.widget.ViewPager +import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.client.appinfo.AppInfo import com.nextcloud.client.di.Injectable @@ -29,7 +29,7 @@ import javax.inject.Inject /** * Activity displaying new features after an update. */ -class WhatsNewActivity : FragmentActivity(), ViewPager.OnPageChangeListener, Injectable { +class WhatsNewActivity : FragmentActivity(), Injectable { @JvmField @Inject @@ -64,7 +64,11 @@ class WhatsNewActivity : FragmentActivity(), ViewPager.OnPageChangeListener, Inj val showWebView = urls.isNotEmpty() setupFeatureViewAdapter(showWebView, urls) - binding.contentPanel.addOnPageChangeListener(this) + binding.contentPanel.registerOnPageChangeCallback(object : OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + controlPanelOnPageSelected(position) + } + }) setupForwardImageButton() setupSkipImageButton() setupWelcomeText(showWebView) @@ -75,15 +79,15 @@ class WhatsNewActivity : FragmentActivity(), ViewPager.OnPageChangeListener, Inj @Suppress("SpreadOperator") private fun setupFeatureViewAdapter(showWebView: Boolean, urls: Array) { val adapter = if (showWebView) { - FeaturesWebViewAdapter(supportFragmentManager, *urls) + FeaturesWebViewAdapter(this, *urls) } else { onboarding?.let { - FeaturesViewAdapter(supportFragmentManager, *it.whatsNew) + FeaturesViewAdapter(this, *it.whatsNew) } } adapter?.let { - binding.progressIndicator.setNumberOfSteps(it.count) + binding.progressIndicator.setNumberOfSteps(it.itemCount) binding.contentPanel.adapter = it } } @@ -142,14 +146,8 @@ class WhatsNewActivity : FragmentActivity(), ViewPager.OnPageChangeListener, Inj preferences?.lastSeenVersionCode = BuildConfig.VERSION_CODE } - override fun onPageSelected(position: Int) { + private fun controlPanelOnPageSelected(position: Int) { binding.progressIndicator.animateToStep(position + 1) updateNextButtonIfNeeded() } - - @Suppress("EmptyFunctionBlock") - override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {} - - @Suppress("EmptyFunctionBlock") - override fun onPageScrollStateChanged(state: Int) {} } diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesViewAdapter.java index 2731d9fc8847..f4536999af48 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesViewAdapter.java @@ -9,26 +9,28 @@ import com.owncloud.android.features.FeatureItem; import com.owncloud.android.ui.fragment.FeatureFragment; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; +import androidx.fragment.app.FragmentActivity; +import androidx.viewpager2.adapter.FragmentStateAdapter; -public class FeaturesViewAdapter extends FragmentPagerAdapter { +public class FeaturesViewAdapter extends FragmentStateAdapter { - private FeatureItem[] mFeatures; + private final FeatureItem[] mFeatures; - public FeaturesViewAdapter(FragmentManager fm, FeatureItem... features) { - super(fm); + public FeaturesViewAdapter(FragmentActivity fragmentActivity, FeatureItem... features) { + super(fragmentActivity); mFeatures = features; } + @NonNull @Override - public Fragment getItem(int position) { + public Fragment createFragment(int position) { return FeatureFragment.newInstance(mFeatures[position]); } @Override - public int getCount() { + public int getItemCount() { return mFeatures.length; } } diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesWebViewAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesWebViewAdapter.java index 1cf0c9075938..2de789b24ce1 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesWebViewAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/FeaturesWebViewAdapter.java @@ -8,25 +8,27 @@ import com.owncloud.android.ui.fragment.FeatureWebFragment; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentPagerAdapter; +import androidx.fragment.app.FragmentActivity; +import androidx.viewpager2.adapter.FragmentStateAdapter; -public class FeaturesWebViewAdapter extends FragmentPagerAdapter { +public class FeaturesWebViewAdapter extends FragmentStateAdapter { private String[] mWebUrls; - public FeaturesWebViewAdapter(FragmentManager fm, String... webUrls) { - super(fm); + public FeaturesWebViewAdapter(FragmentActivity fragmentActivity, String... webUrls) { + super(fragmentActivity); mWebUrls = webUrls; } + @NonNull @Override - public Fragment getItem(int position) { + public Fragment createFragment(int position) { return FeatureWebFragment.newInstance(mWebUrls[position]); } @Override - public int getCount() { + public int getItemCount() { return mWebUrls.length; } } diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/FileDetailTabAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/FileDetailTabAdapter.java index 0e53c247457d..3c20828d1950 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/FileDetailTabAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/FileDetailTabAdapter.java @@ -15,13 +15,13 @@ import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentStatePagerAdapter; +import androidx.fragment.app.FragmentActivity; +import androidx.viewpager2.adapter.FragmentStateAdapter; /** * File details pager adapter. */ -public class FileDetailTabAdapter extends FragmentStatePagerAdapter { +public class FileDetailTabAdapter extends FragmentStateAdapter { private final OCFile file; private final User user; private final boolean showSharingTab; @@ -30,33 +30,16 @@ public class FileDetailTabAdapter extends FragmentStatePagerAdapter { private FileDetailActivitiesFragment fileDetailActivitiesFragment; private ImageDetailFragment imageDetailFragment; - public FileDetailTabAdapter(FragmentManager fm, + public FileDetailTabAdapter(FragmentActivity fragmentActivity, OCFile file, User user, boolean showSharingTab) { - super(fm); + super(fragmentActivity); this.file = file; this.user = user; this.showSharingTab = showSharingTab; } - @NonNull - @Override - public Fragment getItem(int position) { - switch (position) { - case 0: - default: - fileDetailActivitiesFragment = FileDetailActivitiesFragment.newInstance(file, user); - return fileDetailActivitiesFragment; - case 1: - fileDetailSharingFragment = FileDetailSharingFragment.newInstance(file, user); - return fileDetailSharingFragment; - case 2: - imageDetailFragment = ImageDetailFragment.newInstance(file, user); - return imageDetailFragment; - } - } - public FileDetailSharingFragment getFileDetailSharingFragment() { return fileDetailSharingFragment; } @@ -69,8 +52,27 @@ public ImageDetailFragment getImageDetailFragment() { return imageDetailFragment; } + @NonNull + @Override + public Fragment createFragment(int position) { + return switch (position) { + default -> { + fileDetailActivitiesFragment = FileDetailActivitiesFragment.newInstance(file, user); + yield fileDetailActivitiesFragment; + } + case 1 -> { + fileDetailSharingFragment = FileDetailSharingFragment.newInstance(file, user); + yield fileDetailSharingFragment; + } + case 2 -> { + imageDetailFragment = ImageDetailFragment.newInstance(file, user); + yield imageDetailFragment; + } + }; + } + @Override - public int getCount() { + public int getItemCount() { if (showSharingTab) { if (MimeTypeUtil.isImage(file)) { return 3; diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java index 3e76c8d1dfba..048b26b6cf01 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -75,6 +75,7 @@ import androidx.annotation.Nullable; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.FragmentManager; +import androidx.viewpager2.widget.ViewPager2; /** * This Fragment is used to display the details about a file. @@ -166,7 +167,12 @@ public FileDetailSharingFragment getFileDetailSharingFragment() { if (binding == null) { return null; } - return ((FileDetailTabAdapter) binding.pager.getAdapter()).getFileDetailSharingFragment(); + + if (binding.pager.getAdapter() instanceof FileDetailTabAdapter adapter) { + return adapter.getFileDetailSharingFragment(); + } + + return null; } /** @@ -175,7 +181,11 @@ public FileDetailSharingFragment getFileDetailSharingFragment() { * @return reference to the {@link FileDetailActivitiesFragment} */ public FileDetailActivitiesFragment getFileDetailActivitiesFragment() { - return ((FileDetailTabAdapter) binding.pager.getAdapter()).getFileDetailActivitiesFragment(); + if (binding.pager.getAdapter() instanceof FileDetailTabAdapter adapter) { + return adapter.getFileDetailActivitiesFragment(); + } + + return null; } public void goBackToOCFileListFragment() { @@ -296,12 +306,13 @@ private void setupViewPager() { viewThemeUtils.material.themeTabLayout(binding.tabLayout); - final FileDetailTabAdapter adapter = new FileDetailTabAdapter(getFragmentManager(), + final FileDetailTabAdapter adapter = new FileDetailTabAdapter(requireActivity(), getFile(), user, showSharingTab()); binding.pager.setAdapter(adapter); - binding.pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabLayout) { + + binding.pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { final FileDetailActivitiesFragment fragment = getFileDetailActivitiesFragment(); @@ -334,10 +345,11 @@ public void onTabReselected(TabLayout.Tab tab) { } }); - TabLayout.Tab tab = binding.tabLayout.getTabAt(activeTab); - if (tab != null) { - tab.select(); - } + binding.tabLayout.post(() -> { + TabLayout.Tab tab1 = binding.tabLayout.getTabAt(activeTab); + if (tab1 == null) return; + tab1.select(); + }); } @Override diff --git a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java index 1ca7f2dfaedf..417f9f060daf 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java @@ -79,9 +79,7 @@ import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentStatePagerAdapter; import androidx.fragment.app.FragmentTransaction; -import androidx.viewpager2.adapter.FragmentStateAdapter; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import pl.droidsonroids.gif.GifDrawable; @@ -128,7 +126,7 @@ public class PreviewImageFragment extends FileFragment implements Injectable { * This method hides to client objects the need of doing the construction in two steps. * * @param imageFile An {@link OCFile} to preview as an image in the fragment - * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStateAdapter} ; + * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of { FragmentStateAdapter } ; * TODO better solution */ public static PreviewImageFragment newInstance(@NonNull OCFile imageFile, diff --git a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt index 0132567a02ec..38f350999b32 100644 --- a/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt +++ b/app/src/main/java/com/owncloud/android/ui/preview/PreviewImagePagerAdapter.kt @@ -135,10 +135,6 @@ class PreviewImagePagerAdapter : FragmentStateAdapter { } } - override fun getItemId(position: Int): Long { - return imageFiles[position].hashCode().toLong() - } - private fun addVideoOfLivePhoto(file: OCFile) { file.livePhotoVideo = selectedFile } diff --git a/app/src/main/res/layout/file_details_fragment.xml b/app/src/main/res/layout/file_details_fragment.xml index 8efdd4478502..c00d53999a43 100644 --- a/app/src/main/res/layout/file_details_fragment.xml +++ b/app/src/main/res/layout/file_details_fragment.xml @@ -185,7 +185,7 @@ app:tabTextColor="@color/text_color" app:tabInlineLabel="true" /> - diff --git a/app/src/main/res/layout/first_run_activity.xml b/app/src/main/res/layout/first_run_activity.xml index bff1c49a4b3a..a9a44b69226b 100644 --- a/app/src/main/res/layout/first_run_activity.xml +++ b/app/src/main/res/layout/first_run_activity.xml @@ -20,7 +20,7 @@ android:layout_marginBottom="@dimen/standard_margin" android:layout_marginTop="@dimen/standard_margin"> - - - - - - - - - - + + + + + + + +