From 7d9e9c6df0d249ad5f53e6dd5ed9ea9a8f294577 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 14 May 2019 15:30:02 +0200 Subject: [PATCH] avoid initializing the first 3 fragments unnecessarily --- app/build.gradle | 2 +- .../gallery/pro/activities/ViewPagerActivity.kt | 2 ++ .../gallery/pro/adapters/MyPagerAdapter.kt | 4 ++++ .../gallery/pro/fragments/PhotoFragment.kt | 12 +++++++----- .../gallery/pro/fragments/VideoFragment.kt | 9 +++++---- .../gallery/pro/helpers/Constants.kt | 1 + 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9b667c43e..ae02232cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,7 +61,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.12.17' + implementation 'com.simplemobiletools:commons:5.12.22' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'it.sephiroth.android.exif:library:1.0.1' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index abfcb9af6..e469e6571 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -366,8 +366,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun updatePagerItems(media: MutableList) { val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, media) if (!isDestroyed) { + pagerAdapter.shouldInitFragment = mPos < 5 view_pager.apply { adapter = pagerAdapter + pagerAdapter.shouldInitFragment = true currentItem = mPos removeOnPageChangeListener(this@ViewPagerActivity) addOnPageChangeListener(this@ViewPagerActivity) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MyPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MyPagerAdapter.kt index dad510f72..02be99de0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MyPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MyPagerAdapter.kt @@ -12,16 +12,20 @@ import com.simplemobiletools.gallery.pro.fragments.PhotoFragment import com.simplemobiletools.gallery.pro.fragments.VideoFragment import com.simplemobiletools.gallery.pro.fragments.ViewPagerFragment import com.simplemobiletools.gallery.pro.helpers.MEDIUM +import com.simplemobiletools.gallery.pro.helpers.SHOULD_INIT_FRAGMENT import com.simplemobiletools.gallery.pro.models.Medium class MyPagerAdapter(val activity: ViewPagerActivity, fm: FragmentManager, val media: MutableList) : FragmentStatePagerAdapter(fm) { private val fragments = HashMap() + var shouldInitFragment = true + override fun getCount() = media.size override fun getItem(position: Int): Fragment { val medium = media[position] val bundle = Bundle() bundle.putSerializable(MEDIUM, medium) + bundle.putBoolean(SHOULD_INIT_FRAGMENT, shouldInitFragment) val fragment = if (medium.isVideo()) { VideoFragment() } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt index 1160f5761..ae2c3b998 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt @@ -40,10 +40,7 @@ import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.PanoramaPhotoActivity import com.simplemobiletools.gallery.pro.activities.PhotoActivity import com.simplemobiletools.gallery.pro.extensions.* -import com.simplemobiletools.gallery.pro.helpers.MEDIUM -import com.simplemobiletools.gallery.pro.helpers.PATH -import com.simplemobiletools.gallery.pro.helpers.PicassoDecoder -import com.simplemobiletools.gallery.pro.helpers.PicassoRegionDecoder +import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.svg.SvgSoftwareLayerSetter import com.squareup.picasso.Callback @@ -90,7 +87,12 @@ class PhotoFragment : ViewPagerFragment() { private lateinit var mMedium: Medium override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - mView = (inflater.inflate(R.layout.pager_photo_item, container, false) as ViewGroup).apply { + mView = (inflater.inflate(R.layout.pager_photo_item, container, false) as ViewGroup) + if (!arguments!!.getBoolean(SHOULD_INIT_FRAGMENT)) { + return mView + } + + mView.apply { subsampling_view.setOnClickListener { photoClicked() } gestures_view.setOnClickListener { photoClicked() } gif_view.setOnClickListener { photoClicked() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index 8bf276b80..3b619feab 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -28,10 +28,7 @@ import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.PanoramaVideoActivity import com.simplemobiletools.gallery.pro.activities.VideoActivity import com.simplemobiletools.gallery.pro.extensions.* -import com.simplemobiletools.gallery.pro.helpers.Config -import com.simplemobiletools.gallery.pro.helpers.MEDIUM -import com.simplemobiletools.gallery.pro.helpers.MIN_SKIP_LENGTH -import com.simplemobiletools.gallery.pro.helpers.PATH +import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.views.MediaSideScroll import kotlinx.android.synthetic.main.bottom_video_time_holder.view.* @@ -125,6 +122,10 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S } } + if (!arguments!!.getBoolean(SHOULD_INIT_FRAGMENT)) { + return mView + } + storeStateVariables() mMedium = arguments!!.getSerializable(MEDIUM) as Medium Glide.with(context!!).load(mMedium.path).into(mView.video_preview) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt index e3af08f5c..b9ba9581d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt @@ -119,6 +119,7 @@ const val GET_ANY_INTENT = "get_any_intent" const val SET_WALLPAPER_INTENT = "set_wallpaper_intent" const val IS_VIEW_INTENT = "is_view_intent" const val PICKED_PATHS = "picked_paths" +const val SHOULD_INIT_FRAGMENT = "should_init_fragment" // rotations const val ROTATE_BY_SYSTEM_SETTING = 0