Merge pull request #2522 from Naveen3Singh/position_glitch

Fix position snap glitch
This commit is contained in:
Tibor Kaputa 2022-06-20 21:27:11 +02:00 committed by GitHub
commit aa68cc92f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 32 deletions

View File

@ -59,6 +59,7 @@ import kotlinx.android.synthetic.main.bottom_actions.*
import java.io.File import java.io.File
import java.io.OutputStream import java.io.OutputStream
@Suppress("UNCHECKED_CAST")
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener { class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, ViewPagerFragment.FragmentListener {
private val REQUEST_VIEW_VIDEO = 1 private val REQUEST_VIEW_VIDEO = 1
@ -133,24 +134,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
window.attributes = attributes 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() setupOrientation()
invalidateOptionsMenu() 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<ThumbnailItem>, refetchViewPagerPosition = true)
}
refreshViewPager(true)
view_pager.offscreenPageLimit = 2 view_pager.offscreenPageLimit = 2
if (config.blackBackground) { if (config.blackBackground) {
@ -469,11 +462,12 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (!isDestroyed) { if (!isDestroyed) {
pagerAdapter.shouldInitFragment = mPos < 5 pagerAdapter.shouldInitFragment = mPos < 5
view_pager.apply { 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 adapter = pagerAdapter
pagerAdapter.shouldInitFragment = true pagerAdapter.shouldInitFragment = true
currentItem = mPos
removeOnPageChangeListener(this@ViewPagerActivity)
addOnPageChangeListener(this@ViewPagerActivity) addOnPageChangeListener(this@ViewPagerActivity)
currentItem = mPos
} }
} }
} }
@ -1233,10 +1227,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) { if (config.getFolderSorting(mDirectory) and SORT_BY_RANDOM == 0) {
GetMediaAsynctask(applicationContext, mDirectory, false, false, mShowAll) { GetMediaAsynctask(applicationContext, mDirectory, isPickImage = false, isPickVideo = false, showAll = mShowAll) {
gotMedia(it) gotMedia(it, refetchViewPagerPosition = refetchPosition)
}.execute() }.execute()
} }
} }
@ -1270,6 +1264,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
updateActionbarTitle() updateActionbarTitle()
updatePagerItems(mMediaFiles.toMutableList()) updatePagerItems(mMediaFiles.toMutableList())
invalidateOptionsMenu() invalidateOptionsMenu()
checkOrientation() checkOrientation()
initBottomActions() initBottomActions()

View File

@ -373,18 +373,16 @@ fun Context.rescanFolderMedia(path: String) {
} }
fun Context.rescanFolderMediaSync(path: String) { fun Context.rescanFolderMediaSync(path: String) {
getCachedMedia(path) { getCachedMedia(path) { cached ->
val cached = it GetMediaAsynctask(applicationContext, path, isPickImage = false, isPickVideo = false, showAll = false) { newMedia ->
GetMediaAsynctask(applicationContext, path, false, false, false) {
ensureBackgroundThread { ensureBackgroundThread {
val newMedia = it val media = newMedia.filterIsInstance<Medium>() as ArrayList<Medium>
val media = newMedia.filter { it is Medium } as ArrayList<Medium>
try { try {
mediaDB.insertAll(media) mediaDB.insertAll(media)
cached.forEach { cached.forEach { thumbnailItem ->
if (!newMedia.contains(it)) { if (!newMedia.contains(thumbnailItem)) {
val mediumPath = (it as? Medium)?.path val mediumPath = (thumbnailItem as? Medium)?.path
if (mediumPath != null) { if (mediumPath != null) {
deleteDBPath(mediumPath) deleteDBPath(mediumPath)
} }