Skip to content

Commit

Permalink
fix: handle gif import and retain original properties of gif
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY committed Sep 4, 2024
1 parent eaa0463 commit 420b34d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

private lateinit var selectedImageOptions: ImageOptions

enum class ImageType {
SVG, GIF, OTHER
}

/**
* Launches an activity to pick an image from the device's gallery.
* This launcher is registered using `ActivityResultContracts.StartActivityForResult()`.
Expand Down Expand Up @@ -433,21 +437,16 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
}
}

private fun handleSelectImageIntent(imageUri: Uri?) {
val mimeType = imageUri?.let { context?.contentResolver?.getType(it) }
if (mimeType == "image/svg+xml") {
Timber.i("Selected image is an SVG.")
view?.findViewById<TextView>(R.id.no_image_textview)?.apply {
text = resources.getString(R.string.multimedia_editor_svg_preview)
visibility = View.VISIBLE
}
} else {
// reset the no preview text
view?.findViewById<TextView>(R.id.no_image_textview)?.apply {
text = null
visibility = View.GONE
}
fun getImageType(mimeType: String?): ImageType {
return when (mimeType) {
"image/svg+xml" -> ImageType.SVG
"image/gif" -> ImageType.GIF
else -> ImageType.OTHER
}
}

private fun handleSelectImageIntent(imageUri: Uri?) {
val imageType = getImageType(imageUri?.let { context?.contentResolver?.getType(it) })

if (imageUri == null) {
Timber.w("handleSelectImageIntent() selectedImage was null")
Expand All @@ -467,6 +466,37 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_

val imagePath = internalizedPick.absolutePath

when (imageType) {
ImageType.SVG -> {
Timber.i("Selected image is an SVG.")
view?.findViewById<TextView>(R.id.no_image_textview)?.apply {
text = resources.getString(R.string.multimedia_editor_svg_preview)
visibility = View.VISIBLE
}
}
ImageType.GIF -> {
view?.findViewById<TextView>(R.id.no_image_textview)?.apply {
text = null
visibility = View.GONE
}

Timber.d("Selected image is a GIF, skipping compression.")
// Directly use the internalizedPick path and size
viewModel.updateCurrentMultimediaUri(imageUri)
viewModel.updateCurrentMultimediaPath(imagePath)
imagePreview.setImageURI(imageUri)
viewModel.selectedMediaFileSize = internalizedPick.length()
updateAndDisplayImageSize(imagePath)
return
}
ImageType.OTHER -> {
view?.findViewById<TextView>(R.id.no_image_textview)?.apply {
text = null
visibility = View.GONE
}
}
}

if (!rotateAndCompress(imagePath)) {
Timber.d("Unable to compress the clicked image")
showErrorDialog(errorMessage = resources.getString(R.string.multimedia_editor_image_compression_failed))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Ashish Yadav <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki.multimedia

import kotlin.test.Test
import kotlin.test.assertEquals

class MultimediaImageFragmentTest {
private val multimediaImageFragment = MultimediaImageFragment()

@Test
fun testGetImageType_SVG() {
val mimeType = "image/svg+xml"
val result = multimediaImageFragment.getImageType(mimeType)
assertEquals(MultimediaImageFragment.ImageType.SVG, result)
}

@Test
fun testGetImageType_GIF() {
val mimeType = "image/gif"
val result = multimediaImageFragment.getImageType(mimeType)
assertEquals(MultimediaImageFragment.ImageType.GIF, result)
}

@Test
fun testGetImageType_Other() {
val mimeType = "image/jpeg"
val result = multimediaImageFragment.getImageType(mimeType)
assertEquals(MultimediaImageFragment.ImageType.OTHER, result)
}
}

0 comments on commit 420b34d

Please sign in to comment.