From 46533e4b454e0872e47b8f2625fce1f9dc5e59f9 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 18 Jun 2022 14:35:04 +0530 Subject: [PATCH 1/3] Minor code improvements --- .../gallery/pro/extensions/Context.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 37cf10e26..774c1d90c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -373,18 +373,16 @@ fun Context.rescanFolderMedia(path: String) { } fun Context.rescanFolderMediaSync(path: String) { - getCachedMedia(path) { - val cached = it - GetMediaAsynctask(applicationContext, path, false, false, false) { + getCachedMedia(path) { cached -> + GetMediaAsynctask(applicationContext, path, isPickImage = false, isPickVideo = false, showAll = false) { newMedia -> ensureBackgroundThread { - val newMedia = it - val media = newMedia.filter { it is Medium } as ArrayList + val media = newMedia.filterIsInstance() as ArrayList try { mediaDB.insertAll(media) - cached.forEach { - if (!newMedia.contains(it)) { - val mediumPath = (it as? Medium)?.path + cached.forEach { thumbnailItem -> + if (!newMedia.contains(thumbnailItem)) { + val mediumPath = (thumbnailItem as? Medium)?.path if (mediumPath != null) { deleteDBPath(mediumPath) } From ccb9b60a12ab4c11ef9ed5cd7792442d97b994e7 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sat, 18 Jun 2022 14:36:59 +0530 Subject: [PATCH 2/3] Remove unnecessary call to `getCachedMedia` --- .../pro/activities/ViewPagerActivity.kt | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) 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 574e61df1..ff922e2e7 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 @@ -59,6 +59,7 @@ import kotlinx.android.synthetic.main.bottom_actions.* import java.io.File import java.io.OutputStream +@Suppress("UNCHECKED_CAST") class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener { private val REQUEST_VIEW_VIDEO = 1 @@ -133,24 +134,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View window.attributes = attributes } - // show the selected image asap, while loading the rest in the background to allow swiping between them. Needed at third party intents - if (mMediaFiles.isEmpty() && mPath.isNotEmpty() && mDirectory != FAVORITES) { - getCachedMedia(mPath.getParentPath(), false, false) { thumbnailItems -> - // if we have nothing cached from the given folder, at least show the selected image asap - if (thumbnailItems.isEmpty()) { - val filename = mPath.getFilenameFromPath() - val folder = mPath.getParentPath() - val type = getTypeFromPath(mPath) - val medium = Medium(null, filename, mPath, folder, 0, 0, 0, type, 0, false, 0L, 0L) - thumbnailItems.add(medium) - } - - runOnUiThread { - gotMedia(thumbnailItems) - } - } - } - setupOrientation() invalidateOptionsMenu() @@ -388,7 +371,17 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } - refreshViewPager() + // show the selected image asap, while loading the rest in the background to allow swiping between them. Might be needed at third party intents + if (mMediaFiles.isEmpty() && mPath.isNotEmpty() && mDirectory != FAVORITES) { + val filename = mPath.getFilenameFromPath() + val folder = mPath.getParentPath() + val type = getTypeFromPath(mPath) + val medium = Medium(null, filename, mPath, folder, 0, 0, 0, type, 0, false, 0L, 0L) + mMediaFiles.add(medium) + gotMedia(mMediaFiles as ArrayList, refetchViewPagerPosition = true) + } + + refreshViewPager(true) view_pager.offscreenPageLimit = 2 if (config.blackBackground) { @@ -1233,10 +1226,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } - private fun refreshViewPager() { + private fun refreshViewPager(refetchPosition: Boolean = false) { if (config.getFolderSorting(mDirectory) and SORT_BY_RANDOM == 0) { - GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) { - gotMedia(it) + GetMediaAsynctask(applicationContext, mDirectory, isPickImage = false, isPickVideo = false, showAll = mShowAll) { + gotMedia(it, refetchViewPagerPosition = refetchPosition) }.execute() } } @@ -1270,6 +1263,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View updateActionbarTitle() updatePagerItems(mMediaFiles.toMutableList()) + invalidateOptionsMenu() checkOrientation() initBottomActions() From e6039d07cb7d65ad370b6b2d416c482972474e80 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 19 Jun 2022 17:19:35 +0530 Subject: [PATCH 3/3] Remove page change listener before changing adapter --- .../gallery/pro/activities/ViewPagerActivity.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 ff922e2e7..988a2fecf 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 @@ -462,11 +462,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (!isDestroyed) { pagerAdapter.shouldInitFragment = mPos < 5 view_pager.apply { + // must remove the listener before changing adapter, otherwise it might cause `mPos` to be set to 0 + removeOnPageChangeListener(this@ViewPagerActivity) adapter = pagerAdapter pagerAdapter.shouldInitFragment = true - currentItem = mPos - removeOnPageChangeListener(this@ViewPagerActivity) addOnPageChangeListener(this@ViewPagerActivity) + currentItem = mPos } } }