Skip to content

Commit 0429a83

Browse files
MHShettythestinger
authored andcommitted
Fix in-app gallery resetting to first image
(on rotation/any config changes)
1 parent 63db69d commit 0429a83

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

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

+46-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package app.grapheneos.camera.ui.activities
33
import android.animation.ArgbEvaluator
44
import android.animation.ValueAnimator
55
import android.annotation.SuppressLint
6-
import android.app.Activity
76
import android.app.AlertDialog
87
import android.content.ActivityNotFoundException
98
import android.content.Context
@@ -12,6 +11,7 @@ import android.graphics.Color
1211
import android.graphics.drawable.ColorDrawable
1312
import android.media.MediaMetadataRetriever
1413
import android.net.Uri
14+
import android.os.Build
1515
import android.os.Bundle
1616
import android.os.Handler
1717
import android.provider.DocumentsContract
@@ -23,9 +23,9 @@ import android.view.Menu
2323
import android.view.MenuItem
2424
import android.view.View
2525
import android.widget.Toast
26-
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
2726
import androidx.appcompat.app.AppCompatActivity
2827
import androidx.core.content.ContextCompat
28+
import androidx.core.os.BundleCompat
2929
import androidx.viewpager2.widget.ViewPager2
3030
import androidxc.exifinterface.media.ExifInterface
3131
import app.grapheneos.camera.CapturedItem
@@ -39,7 +39,6 @@ import app.grapheneos.camera.util.getParcelableArrayListExtra
3939
import app.grapheneos.camera.util.getParcelableExtra
4040
import app.grapheneos.camera.util.storageLocationToUiString
4141
import com.google.android.material.snackbar.Snackbar
42-
import java.io.FileNotFoundException
4342
import java.text.SimpleDateFormat
4443
import java.util.Date
4544
import java.util.Locale
@@ -64,12 +63,16 @@ class InAppGallery : AppCompatActivity() {
6463

6564
private lateinit var rootView: View
6665

66+
private var lastViewedMediaItem : CapturedItem? = null
67+
6768
companion object {
6869
const val INTENT_KEY_SECURE_MODE = "is_secure_mode"
6970
const val INTENT_KEY_VIDEO_ONLY_MODE = "video_only_mode"
7071
const val INTENT_KEY_LIST_OF_SECURE_MODE_CAPTURED_ITEMS = "secure_mode_items"
7172
const val INTENT_KEY_LAST_CAPTURED_ITEM = "last_captured_item"
7273

74+
const val LAST_VIEWED_ITEM_KEY = "LAST_VIEWED_ITEM_KEY"
75+
7376
@SuppressLint("SimpleDateFormat")
7477
fun convertTime(time: Long, showTimeZone: Boolean = true): String {
7578
val date = Date(time)
@@ -431,6 +434,10 @@ class InAppGallery : AppCompatActivity() {
431434
snackBar = Snackbar.make(gallerySlider, "", Snackbar.LENGTH_LONG)
432435
gallerySlider.setPageTransformer(GSlideTransformer())
433436

437+
if (savedInstanceState != null) {
438+
lastViewedMediaItem = BundleCompat.getParcelable(savedInstanceState, LAST_VIEWED_ITEM_KEY, CapturedItem::class.java)
439+
}
440+
434441
val intent = this.intent
435442

436443
val showVideosOnly = intent.getBooleanExtra(INTENT_KEY_VIDEO_ONLY_MODE, false)
@@ -468,23 +475,25 @@ class InAppGallery : AppCompatActivity() {
468475
mainExecutor.execute { asyncResultReady(items) }
469476
}
470477

471-
val lastCapturedItem = getParcelableExtra<CapturedItem>(intent, INTENT_KEY_LAST_CAPTURED_ITEM)
478+
if (lastViewedMediaItem == null) {
479+
val lastCapturedItem = getParcelableExtra<CapturedItem>(intent, INTENT_KEY_LAST_CAPTURED_ITEM)
472480

473-
if (lastCapturedItem != null) {
474-
val list = ArrayList<CapturedItem>()
475-
list.add(lastCapturedItem)
476-
GallerySliderAdapter(this, list).let {
477-
gallerySliderAdapter = it
478-
gallerySlider.adapter = it
479-
}
480-
} else {
481-
Handler(mainLooper).postDelayed({
482-
if (gallerySliderAdapter == null) {
483-
binding.placeholderText.root.visibility = View.VISIBLE
481+
if (lastCapturedItem != null) {
482+
val list = ArrayList<CapturedItem>()
483+
list.add(lastCapturedItem)
484+
GallerySliderAdapter(this, list).let {
485+
gallerySliderAdapter = it
486+
gallerySlider.adapter = it
484487
}
485-
}, 500)
488+
} else {
489+
Handler(mainLooper).postDelayed({
490+
if (gallerySliderAdapter == null) {
491+
binding.placeholderText.root.visibility = View.VISIBLE
492+
}
493+
}, 500)
486494

487-
hideActionBar()
495+
hideActionBar()
496+
}
488497
}
489498
}
490499

@@ -499,6 +508,17 @@ class InAppGallery : AppCompatActivity() {
499508
return
500509
}
501510

511+
var capturedItemPosition = 0
512+
513+
if (lastViewedMediaItem != null) {
514+
for (i in 0..<items.size) {
515+
val capturedItem = items[i]
516+
if (capturedItem == lastViewedMediaItem) {
517+
capturedItemPosition = i
518+
}
519+
}
520+
}
521+
502522
binding.placeholderText.root.visibility = View.GONE
503523

504524
val existingAdapter = gallerySliderAdapter
@@ -523,6 +543,7 @@ class InAppGallery : AppCompatActivity() {
523543
}
524544
existingAdapter.notifyItemRangeInserted(1, items.size - 1)
525545
}
546+
gallerySlider.setCurrentItem(capturedItemPosition, false)
526547
showActionBar()
527548
}
528549

@@ -560,4 +581,12 @@ class InAppGallery : AppCompatActivity() {
560581
snackBar.setText(msg)
561582
snackBar.show()
562583
}
584+
585+
override fun onSaveInstanceState(outState: Bundle) {
586+
super.onSaveInstanceState(outState)
587+
588+
gallerySliderAdapter?.let {
589+
outState.putParcelable(LAST_VIEWED_ITEM_KEY, it.items[gallerySlider.currentItem])
590+
}
591+
}
563592
}

0 commit comments

Comments
 (0)