Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LocalFileProviderImpl @Inject constructor(
var outputStream: FileOutputStream? = null
try {
outputStream = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
outputStream.flush()
} catch (e: IOException) {
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ internal class ImageGenerationRepositoryImpl @Inject constructor(
}

override suspend fun saveImage(imageBitmap: Bitmap): Uri {
val cacheFile = localFileProvider.createCacheFile("shared_image_${UUID.randomUUID()}.jpg")
val cacheFile = localFileProvider.createCacheFile("shared_image_${UUID.randomUUID()}.png")
localFileProvider.saveBitmapToFile(imageBitmap, cacheFile)
return localFileProvider.sharingUriForFile(cacheFile)
}

override suspend fun saveImageToExternalStorage(imageBitmap: Bitmap): Uri {
val cacheFile = localFileProvider.createCacheFile("androidify_image_result_${UUID.randomUUID()}.jpg")
val cacheFile = localFileProvider.createCacheFile("androidify_image_result_${UUID.randomUUID()}.png")
localFileProvider.saveBitmapToFile(imageBitmap, cacheFile)
return localFileProvider.saveToSharedStorage(cacheFile, cacheFile.name, "image/jpeg")
return localFileProvider.saveToSharedStorage(cacheFile, cacheFile.name, "image/png")
}

override suspend fun saveImageToExternalStorage(imageUri: Uri): Uri {
return localFileProvider.saveUriToSharedStorage(
imageUri,
"androidify_image_original_${UUID.randomUUID()}.jpg",
"image/jpeg",
"androidify_image_original_${UUID.randomUUID()}.png",
"image/png",
)
Comment on lines +110 to 126
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid repeating string literals, it would be beneficial to define constants for the file extension, MIME type, and filename prefixes. This will make future changes (e.g., switching to another image format) much easier.

You could add a companion object to the ImageGenerationRepositoryImpl class to hold these constants:

companion object {
    private const val IMAGE_EXTENSION = ".png"
    private const val IMAGE_MIME_TYPE = "image/png"
    private const val SHARED_IMAGE_PREFIX = "shared_image_"
    private const val RESULT_IMAGE_PREFIX = "androidify_image_result_"
    private const val ORIGINAL_IMAGE_PREFIX = "androidify_image_original_"
}

Then you can use these constants to construct the filenames and specify the MIME type across the class, for example:

  • Line 110: localFileProvider.createCacheFile("$SHARED_IMAGE_PREFIX${UUID.randomUUID()}$IMAGE_EXTENSION")
  • Line 116: localFileProvider.createCacheFile("$RESULT_IMAGE_PREFIX${UUID.randomUUID()}$IMAGE_EXTENSION")
  • Line 118: localFileProvider.saveToSharedStorage(cacheFile, cacheFile.name, IMAGE_MIME_TYPE)
  • Line 124: "$ORIGINAL_IMAGE_PREFIX${UUID.randomUUID()}$IMAGE_EXTENSION"
  • Line 125: IMAGE_MIME_TYPE

This change would centralize the image format configuration, making the code cleaner and easier to maintain.

Note: I noticed that test files like FakeImageGenerationRepository.kt and ResultsViewModelTest.kt still use .jpg extensions in hardcoded URIs. You might want to update them as part of this PR to ensure tests are consistent with this change.

}

Expand Down