From 92ab3a143f0cd3ba3463410607ecfa66539d3df7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 02:07:11 +0000 Subject: [PATCH 1/8] Create PR for #374 From 25a72de47a1d331db9c5baae7459b85373d0eb95 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 11:42:23 +0900 Subject: [PATCH 2/8] :zap: improve Image Uploading speed - utilization of coroutine - image compress - PNG -> JPEG --- .../presentation/core/util/ImageUtils.kt | 85 ++++++++++++++++--- .../making/RecipeMakingFragment.kt | 24 ++++-- .../making/step/StepMakingFragment.kt | 24 ++++-- .../setting/edit/EditProfileFragment.kt | 21 +++-- .../presentation/signup/SignUpFragment.kt | 21 +++-- 5 files changed, 142 insertions(+), 33 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt index 242b4ee0..4b117376 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt @@ -2,49 +2,57 @@ package net.pengcook.android.presentation.core.util import android.content.Context import android.content.pm.PackageManager +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.media.ExifInterface import android.net.Uri import androidx.core.content.ContextCompat import androidx.core.content.FileProvider +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.io.File +import java.io.FileOutputStream import java.io.IOException import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -class ImageUtils(private val context: Context) { +class ImageUtils( + private val context: Context, +) { private fun createTempImageFile(): File { val timeStamp: String = - SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) + SimpleDateFormat(DATA_FORMAT, Locale.getDefault()).format(Date()) val storageDir: File? = context.getExternalFilesDir(null) return File.createTempFile( "JPEG_${timeStamp}_", - ".jpg", + FILE_SUFFIX, storageDir, ) } fun createImageFile(): File { val timeStamp: String = - SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) + SimpleDateFormat(DATA_FORMAT, Locale.getDefault()).format(Date()) val storageDir: File? = context.getExternalFilesDir(null) return File .createTempFile( "JPEG_${timeStamp}_", - ".jpg", + FILE_SUFFIX, storageDir, ) } - fun getUriForFile(file: File): Uri { - return FileProvider.getUriForFile( + fun getUriForFile(file: File): Uri = + FileProvider.getUriForFile( context, "net.pengcook.android.fileprovider", file, ) - } - fun processImageUri(uri: Uri): String? { - return try { + fun processImageUri(uri: Uri): String? = + try { val inputStream = context.contentResolver.openInputStream(uri) if (inputStream != null) { val tempFile = createTempImageFile() @@ -59,11 +67,62 @@ class ImageUtils(private val context: Context) { e.printStackTrace() null } - } - fun isPermissionGranted(permissions: Array): Boolean { - return permissions.all { + fun isPermissionGranted(permissions: Array): Boolean = + permissions.all { ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED } + + suspend fun compressAndResizeImage(uri: Uri): File = + withContext(Dispatchers.IO) { + val inputStream = context.contentResolver.openInputStream(uri) + val originalBitmap = BitmapFactory.decodeStream(inputStream) + + // 이미지 리사이즈 + val resizedBitmap = + Bitmap.createScaledBitmap( + originalBitmap, + MAX_WIDTH, + MAX_HEIGHT, + true, + ) + + // 압축된 파일 생성 + val compressedFile = createTempImageFile() + val outputStream = FileOutputStream(compressedFile) + resizedBitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSED_QUALITY, outputStream) + outputStream.flush() + outputStream.close() + + // 메모리 정리 + originalBitmap.recycle() + resizedBitmap.recycle() + + compressedFile + } + + private fun adjustImageOrientation( + bitmap: Bitmap, + uri: Uri, + ): Bitmap { + val inputStream = context.contentResolver.openInputStream(uri) + val exif = ExifInterface(inputStream!!) + val orientation = + exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL) + val matrix = Matrix() + when (orientation) { + ExifInterface.ORIENTATION_ROTATE_90 -> matrix.postRotate(90f) + ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180f) + ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270f) + } + return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true) + } + + companion object { + private const val COMPRESSED_QUALITY = 80 + private const val MAX_WIDTH = 1080 + private const val DATA_FORMAT = "yyyyMMdd_HHmmss" + private const val FILE_SUFFIX = ".jpg" + private const val MAX_HEIGHT = 1080 } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt index bde767ed..c43c5e9a 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt @@ -9,11 +9,16 @@ import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import androidx.activity.result.contract.ActivityResultContracts +import androidx.datastore.core.IOException import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.pengcook.android.R import net.pengcook.android.databinding.FragmentRecipeMakingBinding import net.pengcook.android.presentation.core.util.AnalyticsLogging @@ -120,11 +125,20 @@ class RecipeMakingFragment : Fragment() { } private fun processImageUri(uri: Uri) { - currentPhotoPath = imageUtils.processImageUri(uri) - if (currentPhotoPath != null) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } else { - showSnackBar(getString(R.string.image_selection_failed)) + lifecycleScope.launch { + try { + val compressedFile = imageUtils.compressAndResizeImage(uri) + currentPhotoPath = compressedFile.absolutePath + + withContext(Dispatchers.Main) { + viewModel.fetchImageUri(File(currentPhotoPath!!).name) + } + } catch (e: IOException) { + e.printStackTrace() + withContext(Dispatchers.Main) { + showSnackBar(getString(R.string.image_selection_failed)) + } + } } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt index 273a8de8..4c0e6a0d 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt @@ -9,11 +9,16 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts +import androidx.datastore.core.IOException import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels +import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.pengcook.android.R import net.pengcook.android.databinding.FragmentMakingStepBinding import net.pengcook.android.presentation.core.util.AnalyticsLogging @@ -182,11 +187,20 @@ class StepMakingFragment : Fragment() { } private fun processImageUri(uri: Uri) { - currentPhotoPath = imageUtils.processImageUri(uri) - if (currentPhotoPath != null) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } else { - showToast(getString(R.string.image_selection_failed)) + lifecycleScope.launch { + try { + val compressedFile = imageUtils.compressAndResizeImage(uri) + currentPhotoPath = compressedFile.absolutePath + + withContext(Dispatchers.Main) { + viewModel.fetchImageUri(File(currentPhotoPath!!).name) + } + } catch (e: IOException) { + e.printStackTrace() + withContext(Dispatchers.Main) { + showToast(getString(R.string.image_selection_failed)) + } + } } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt index bf1123d0..34d39a86 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter import androidx.activity.result.contract.ActivityResultContracts +import androidx.datastore.core.IOException import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope @@ -15,6 +16,7 @@ import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.pengcook.android.R import net.pengcook.android.databinding.FragmentEditProfileBinding import net.pengcook.android.presentation.core.util.AnalyticsLogging @@ -87,11 +89,20 @@ class EditProfileFragment : Fragment() { } private fun processImageUri(uri: Uri) { - currentPhotoPath = imageUtils.processImageUri(uri) - if (currentPhotoPath != null) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } else { - showSnackBar(getString(R.string.image_selection_failed)) + lifecycleScope.launch { + try { + val compressedFile = imageUtils.compressAndResizeImage(uri) + currentPhotoPath = compressedFile.absolutePath + + withContext(Dispatchers.Main) { + viewModel.fetchImageUri(File(currentPhotoPath!!).name) + } + } catch (e: IOException) { + e.printStackTrace() + withContext(Dispatchers.Main) { + showSnackBar(getString(R.string.image_selection_failed)) + } + } } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt index 65ae44e8..2e72e4a6 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt @@ -20,6 +20,7 @@ import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import net.pengcook.android.R import net.pengcook.android.databinding.FragmentSignUpBinding import net.pengcook.android.presentation.core.util.AnalyticsLogging @@ -28,6 +29,7 @@ import net.pengcook.android.presentation.core.util.ImageUtils import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream +import java.io.IOException import javax.inject.Inject @AndroidEntryPoint @@ -101,11 +103,20 @@ class SignUpFragment : Fragment() { } private fun processImageUri(uri: Uri) { - currentPhotoPath = imageUtils.processImageUri(uri) - if (currentPhotoPath != null) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } else { - showSnackBar(getString(R.string.image_selection_failed)) + lifecycleScope.launch { + try { + val compressedFile = imageUtils.compressAndResizeImage(uri) + currentPhotoPath = compressedFile.absolutePath + + withContext(Dispatchers.Main) { + viewModel.fetchImageUri(File(currentPhotoPath!!).name) + } + } catch (e: IOException) { + e.printStackTrace() + withContext(Dispatchers.Main) { + showSnackBar(getString(R.string.image_selection_failed)) + } + } } } From f3dbd2a9fbfc58bc28a78f49182f19e1cdae4e83 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 13:13:51 +0900 Subject: [PATCH 3/8] :sparkles: adjust image orientation --- .../presentation/core/util/ImageUtils.kt | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt index 4b117376..be424d53 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt @@ -13,7 +13,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.File import java.io.FileOutputStream -import java.io.IOException import java.text.SimpleDateFormat import java.util.Date import java.util.Locale @@ -51,22 +50,22 @@ class ImageUtils( file, ) - fun processImageUri(uri: Uri): String? = - try { - val inputStream = context.contentResolver.openInputStream(uri) - if (inputStream != null) { - val tempFile = createTempImageFile() - tempFile.outputStream().use { outputStream -> - inputStream.copyTo(outputStream) - } - tempFile.absolutePath - } else { - null - } - } catch (e: IOException) { - e.printStackTrace() - null - } +// fun processImageUri(uri: Uri): String? = +// try { +// val inputStream = context.contentResolver.openInputStream(uri) +// if (inputStream != null) { +// val tempFile = createTempImageFile() +// tempFile.outputStream().use { outputStream -> +// inputStream.copyTo(outputStream) +// } +// tempFile.absolutePath +// } else { +// null +// } +// } catch (e: IOException) { +// e.printStackTrace() +// null +// } fun isPermissionGranted(permissions: Array): Boolean = permissions.all { @@ -78,23 +77,23 @@ class ImageUtils( val inputStream = context.contentResolver.openInputStream(uri) val originalBitmap = BitmapFactory.decodeStream(inputStream) - // 이미지 리사이즈 val resizedBitmap = - Bitmap.createScaledBitmap( - originalBitmap, - MAX_WIDTH, - MAX_HEIGHT, - true, + adjustImageOrientation( + Bitmap.createScaledBitmap( + originalBitmap, + MAX_WIDTH, + MAX_HEIGHT, + true, + ), + uri, ) - // 압축된 파일 생성 val compressedFile = createTempImageFile() val outputStream = FileOutputStream(compressedFile) resizedBitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSED_QUALITY, outputStream) outputStream.flush() outputStream.close() - // 메모리 정리 originalBitmap.recycle() resizedBitmap.recycle() From 21fc921055645333e365854d43b9867bba2f042b Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 16:42:58 +0900 Subject: [PATCH 4/8] :truck: processImageUri -> compressAndFetchPresignedUrl --- .../android/presentation/making/RecipeMakingFragment.kt | 4 ++-- .../android/presentation/making/step/StepMakingFragment.kt | 4 ++-- .../android/presentation/setting/edit/EditProfileFragment.kt | 4 ++-- .../pengcook/android/presentation/signup/SignUpFragment.kt | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt index c43c5e9a..b013e40d 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt @@ -70,7 +70,7 @@ class RecipeMakingFragment : Fragment() { if (currentPhotoPath != null) { viewModel.fetchImageUri(File(currentPhotoPath!!).name) } else { - processImageUri(photoUri) + compressAndFetchPresignedUrl(photoUri) } } ?: run { showSnackBar(getString(R.string.image_selection_failed)) @@ -124,7 +124,7 @@ class RecipeMakingFragment : Fragment() { takePictureLauncher.launch(photoUri) } - private fun processImageUri(uri: Uri) { + private fun compressAndFetchPresignedUrl(uri: Uri) { lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt index 4c0e6a0d..58ccf2df 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt @@ -77,7 +77,7 @@ class StepMakingFragment : Fragment() { if (currentPhotoPath != null) { viewModel.fetchImageUri(File(currentPhotoPath!!).name) } else { - processImageUri(photoUri) + compressAndFetchPresignedUrl(photoUri) } } ?: run { showToast(getString(R.string.image_selection_failed)) @@ -186,7 +186,7 @@ class StepMakingFragment : Fragment() { viewModel.uploadImageToS3(presignedUrl, file) } - private fun processImageUri(uri: Uri) { + private fun compressAndFetchPresignedUrl(uri: Uri) { lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt index 34d39a86..df77275a 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt @@ -45,7 +45,7 @@ class EditProfileFragment : Fragment() { if (currentPhotoPath != null) { viewModel.fetchImageUri(File(currentPhotoPath!!).name) } else { - processImageUri(photoUri) + compressAndFetchPresignedUrl(photoUri) } } ?: run { showSnackBar(getString(R.string.image_selection_failed)) @@ -88,7 +88,7 @@ class EditProfileFragment : Fragment() { viewModel.uploadImageToS3(presignedUrl, file) } - private fun processImageUri(uri: Uri) { + private fun compressAndFetchPresignedUrl(uri: Uri) { lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt index 2e72e4a6..304fa4da 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt @@ -59,7 +59,7 @@ class SignUpFragment : Fragment() { if (currentPhotoPath != null) { viewModel.fetchImageUri(File(currentPhotoPath!!).name) } else { - processImageUri(photoUri) + compressAndFetchPresignedUrl(photoUri) } } ?: run { showSnackBar(getString(R.string.image_selection_failed)) @@ -102,7 +102,7 @@ class SignUpFragment : Fragment() { viewModel.uploadImageToS3(presignedUrl, file) } - private fun processImageUri(uri: Uri) { + private fun compressAndFetchPresignedUrl(uri: Uri) { lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) From 881f8f6288070449f452cd37a259aa3bc0977ec7 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 16:46:55 +0900 Subject: [PATCH 5/8] :fire: recycle() --- .../net/pengcook/android/presentation/core/util/ImageUtils.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt index be424d53..7ee646c0 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt @@ -94,9 +94,6 @@ class ImageUtils( outputStream.flush() outputStream.close() - originalBitmap.recycle() - resizedBitmap.recycle() - compressedFile } From 8ba6bfb7e4a0bfc5bc4b752cdc38a03306a0ee03 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 17:02:35 +0900 Subject: [PATCH 6/8] :recycle: add viewLifeCycleOwner --- .../android/presentation/making/RecipeMakingFragment.kt | 2 +- .../android/presentation/making/step/StepMakingFragment.kt | 2 +- .../android/presentation/setting/edit/EditProfileFragment.kt | 2 +- .../net/pengcook/android/presentation/signup/SignUpFragment.kt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt index b013e40d..5ea1fca3 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt @@ -125,7 +125,7 @@ class RecipeMakingFragment : Fragment() { } private fun compressAndFetchPresignedUrl(uri: Uri) { - lifecycleScope.launch { + viewLifecycleOwner.lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt index 58ccf2df..e1aa62c4 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt @@ -187,7 +187,7 @@ class StepMakingFragment : Fragment() { } private fun compressAndFetchPresignedUrl(uri: Uri) { - lifecycleScope.launch { + viewLifecycleOwner.lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath diff --git a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt index df77275a..1797b530 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt @@ -89,7 +89,7 @@ class EditProfileFragment : Fragment() { } private fun compressAndFetchPresignedUrl(uri: Uri) { - lifecycleScope.launch { + viewLifecycleOwner.lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath diff --git a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt index 304fa4da..dc69f527 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt @@ -103,7 +103,7 @@ class SignUpFragment : Fragment() { } private fun compressAndFetchPresignedUrl(uri: Uri) { - lifecycleScope.launch { + viewLifecycleOwner.lifecycleScope.launch { try { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath From 832aedc8baf4671af21135bc59bf7bf0c9637c70 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 17:06:30 +0900 Subject: [PATCH 7/8] :fire: unused Dispatchers --- .../android/presentation/making/RecipeMakingFragment.kt | 4 +--- .../android/presentation/making/step/StepMakingFragment.kt | 5 +---- .../android/presentation/setting/edit/EditProfileFragment.kt | 4 +--- .../pengcook/android/presentation/signup/SignUpFragment.kt | 4 +--- 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt index 5ea1fca3..1b3d28e0 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingFragment.kt @@ -130,9 +130,7 @@ class RecipeMakingFragment : Fragment() { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath - withContext(Dispatchers.Main) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } + viewModel.fetchImageUri(File(currentPhotoPath!!).name) } catch (e: IOException) { e.printStackTrace() withContext(Dispatchers.Main) { diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt index e1aa62c4..0146ebea 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/step/StepMakingFragment.kt @@ -191,10 +191,7 @@ class StepMakingFragment : Fragment() { try { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath - - withContext(Dispatchers.Main) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } + viewModel.fetchImageUri(File(currentPhotoPath!!).name) } catch (e: IOException) { e.printStackTrace() withContext(Dispatchers.Main) { diff --git a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt index 1797b530..56ce6ccb 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/setting/edit/EditProfileFragment.kt @@ -94,9 +94,7 @@ class EditProfileFragment : Fragment() { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath - withContext(Dispatchers.Main) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } + viewModel.fetchImageUri(File(currentPhotoPath!!).name) } catch (e: IOException) { e.printStackTrace() withContext(Dispatchers.Main) { diff --git a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt index dc69f527..0bcfc1da 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/signup/SignUpFragment.kt @@ -108,9 +108,7 @@ class SignUpFragment : Fragment() { val compressedFile = imageUtils.compressAndResizeImage(uri) currentPhotoPath = compressedFile.absolutePath - withContext(Dispatchers.Main) { - viewModel.fetchImageUri(File(currentPhotoPath!!).name) - } + viewModel.fetchImageUri(File(currentPhotoPath!!).name) } catch (e: IOException) { e.printStackTrace() withContext(Dispatchers.Main) { From 85020cb302cf102a7c90089561e00e7e8407d501 Mon Sep 17 00:00:00 2001 From: ii2001 Date: Thu, 26 Sep 2024 17:07:57 +0900 Subject: [PATCH 8/8] :fire: comment --- .../presentation/core/util/ImageUtils.kt | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt index 7ee646c0..e486621e 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/core/util/ImageUtils.kt @@ -50,23 +50,6 @@ class ImageUtils( file, ) -// fun processImageUri(uri: Uri): String? = -// try { -// val inputStream = context.contentResolver.openInputStream(uri) -// if (inputStream != null) { -// val tempFile = createTempImageFile() -// tempFile.outputStream().use { outputStream -> -// inputStream.copyTo(outputStream) -// } -// tempFile.absolutePath -// } else { -// null -// } -// } catch (e: IOException) { -// e.printStackTrace() -// null -// } - fun isPermissionGranted(permissions: Array): Boolean = permissions.all { ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED