Skip to content

Commit

Permalink
fix: recreation in case don't keep activity is on
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY authored and david-allison committed Dec 5, 2024
1 parent 399fbe7 commit 914a015
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ class MultimediaActivity : AnkiActivity(), BaseSnackbarBuilderProvider {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_multimedia)
setTransparentStatusBar()

val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

// avoid recreating the fragment on configuration changes
if (savedInstanceState != null) {
return
}

val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)

val fragmentClassName =
requireNotNull(intent.getStringExtra(MULTIMEDIA_FRAGMENT_NAME_EXTRA)) {
"'$MULTIMEDIA_FRAGMENT_NAME_EXTRA' extra should be provided"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,16 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

private lateinit var selectedImageOptions: ImageOptions

/** Keeps track of the process in case `Don't keep activities` in turned on*/
private var hasStartedImageSelection = false

/**
* Launches an activity to pick an image from the device's gallery.
* This launcher is registered using `ActivityResultContracts.StartActivityForResult()`.
*/
private val pickImageLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
hasStartedImageSelection = false
when (result.resultCode) {
Activity.RESULT_CANCELED -> {
if (viewModel.currentMultimediaUri.value == null) {
Expand Down Expand Up @@ -144,6 +148,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
@NeedsTest("Works fine without permission as we use Camera as feature")
private val cameraLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) { isPictureTaken ->
hasStartedImageSelection = false
when {
!isPictureTaken && viewModel.currentMultimediaUri.value == null -> {
val resultData = Intent().apply {
Expand All @@ -169,6 +174,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
/** Launches an activity to crop the image, using the [ImageCropper] */
private val imageCropperLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
hasStartedImageSelection = false
when (result.resultCode) {
Activity.RESULT_OK -> {
result.data?.let {
Expand Down Expand Up @@ -251,6 +257,8 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
hasStartedImageSelection = savedInstanceState?.getBoolean("HAS_STARTED_IMAGE_SELECTION", false) ?: false

ankiCacheDirectory = FileUtil.getAnkiCacheDirectory(requireContext(), "temp-photos")
if (ankiCacheDirectory == null) {
Timber.e("createUI() failed to get cache directory")
Expand All @@ -273,6 +281,11 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
setupDoneButton()
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean("HAS_STARTED_IMAGE_SELECTION", hasStartedImageSelection)
}

private fun handleImageUri() {
fun processExternalImage(uri: Uri): Uri? = internalizeUri(uri)?.let { Uri.fromFile(it) }

Expand All @@ -287,6 +300,12 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
}

private fun handleSelectedImageOptions() {
if (hasStartedImageSelection) {
Timber.d("Image selection already in progress, skipping.")
return
}
hasStartedImageSelection = true

when (selectedImageOptions) {
ImageOptions.GALLERY -> {
Timber.d("MultimediaImageFragment:: Opening gallery")
Expand Down Expand Up @@ -521,6 +540,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

private fun requestCrop() {
val imageUri = viewModel.currentMultimediaUri.value ?: return
hasStartedImageSelection = true
val intent = com.ichi2.imagecropper.ImageCropperLauncher.ImageUri(imageUri).getIntent(requireContext())
imageCropperLauncher.launch(intent)
}
Expand Down

0 comments on commit 914a015

Please sign in to comment.