From de967340adc63c02c9a96d902f903ddd6ffebd8e Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 5 Dec 2016 18:50:23 +0100 Subject: [PATCH] fix #74, properly set the viewpager position after delete --- .../gallery/activities/ViewPagerActivity.kt | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index 27a9f09f3..099198594 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -38,7 +38,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private var mDirectory = "" private var mIsFullScreen = false - private var mPos = 0 + private var mPos = -1 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -73,8 +73,6 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View return } - mPos = 0 - mIsFullScreen = false mMedia = ArrayList() showSystemUI() @@ -246,13 +244,10 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View if (isDirEmpty()) return@GetMediaAsynctask - var j = 0 - for (medium in it) { - if (medium.path == mPath) { - mPos = j - break - } - j++ + if (mPos == -1) { + mPos = getProperPosition() + } else { + mPos = Math.min(mPos, mMedia.size - 1) } updateActionbarTitle() @@ -261,6 +256,18 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View }.execute() } + private fun getProperPosition(): Int { + mPos = 0 + var i = 0 + for (medium in mMedia) { + if (medium.path == mPath) { + return i + } + i++ + } + return mPos + } + private fun deleteDirectoryIfEmpty() { val file = File(mDirectory) if (file.isDirectory && file.listFiles().isEmpty()) {