Skip to content

Commit b190a15

Browse files
committed
Persist last viewed item position on activity re-creation
1 parent c2f4feb commit b190a15

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

app/src/main/java/app/grapheneos/camera/ui/activities/InAppGallery.kt

+14
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ class InAppGallery : AppCompatActivity() {
6464

6565
private lateinit var rootView: View
6666

67+
private var lastViewedMediaPosition = 0
68+
6769
companion object {
6870
const val INTENT_KEY_SECURE_MODE = "is_secure_mode"
6971
const val INTENT_KEY_VIDEO_ONLY_MODE = "video_only_mode"
7072
const val INTENT_KEY_LIST_OF_SECURE_MODE_CAPTURED_ITEMS = "secure_mode_items"
7173
const val INTENT_KEY_LAST_CAPTURED_ITEM = "last_captured_item"
7274

75+
const val LAST_VIEWED_ITEM_POSITION_KEY = "LAST_VIEWED_ITEM_POSITION_KEY"
76+
7377
@SuppressLint("SimpleDateFormat")
7478
fun convertTime(time: Long, showTimeZone: Boolean = true): String {
7579
val date = Date(time)
@@ -431,6 +435,10 @@ class InAppGallery : AppCompatActivity() {
431435
snackBar = Snackbar.make(gallerySlider, "", Snackbar.LENGTH_LONG)
432436
gallerySlider.setPageTransformer(GSlideTransformer())
433437

438+
if (savedInstanceState != null) {
439+
lastViewedMediaPosition = savedInstanceState.getInt(LAST_VIEWED_ITEM_POSITION_KEY, 0)
440+
}
441+
434442
val intent = this.intent
435443

436444
val showVideosOnly = intent.getBooleanExtra(INTENT_KEY_VIDEO_ONLY_MODE, false)
@@ -523,6 +531,7 @@ class InAppGallery : AppCompatActivity() {
523531
}
524532
existingAdapter.notifyItemRangeInserted(1, items.size - 1)
525533
}
534+
gallerySlider.setCurrentItem(lastViewedMediaPosition, false)
526535
showActionBar()
527536
}
528537

@@ -560,4 +569,9 @@ class InAppGallery : AppCompatActivity() {
560569
snackBar.setText(msg)
561570
snackBar.show()
562571
}
572+
573+
override fun onSaveInstanceState(outState: Bundle) {
574+
super.onSaveInstanceState(outState)
575+
outState.putInt(LAST_VIEWED_ITEM_POSITION_KEY, gallerySlider.currentItem)
576+
}
563577
}

0 commit comments

Comments
 (0)