Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 앨범 저장 취소 시 이미지 파일 삭제 #262

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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 @@ -268,8 +268,7 @@ class SynchronizationWorker @AssistedInject constructor(

val valueList = fileNameToLabelUid.values.toList()
val findAlbumData = valueList.find { value -> value.second == photo.fileName }
val uri = makeFileToUri(photo.uri, photo.fileName)

val uri = ImageConvert.makeFileToUri(photo.uri, photo.fileName, true)

val labelId = findAlbumData?.first
?: fileNameToLabelUid[photo.label]?.first
Expand Down Expand Up @@ -383,32 +382,6 @@ class SynchronizationWorker @AssistedInject constructor(
}
}

private fun makeFileToUri(photoUri: String, fileName: String): String {
val context = applicationContext
val storage = context.filesDir
val imageFile = File(storage, fileName)
imageFile.createNewFile()

FileOutputStream(imageFile).use { fos ->
BitmapFactory.decodeStream(URL(photoUri).openStream()).apply {
if (Build.VERSION.SDK_INT >= 30) {
compress(Bitmap.CompressFormat.WEBP_LOSSY, 100, fos)
} else {
compress(Bitmap.CompressFormat.JPEG, 100, fos)
}

recycle()
}
fos.flush()
}

return FileProvider.getUriForFile(
context,
"${context.packageName}.fileprovider",
imageFile
).toString()
}

private fun isUnSyncLabel(label: SyncAlbumsDto, firebaseLabel: FirebaseLabelResponse): Boolean {
return (firebaseLabel.labelName == label.labelName) &&
((firebaseLabel.fileName != label.fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.content.Context
import android.content.Intent
import android.content.res.Configuration.UI_MODE_NIGHT_NO
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.location.Location
import android.net.Uri
import androidx.activity.compose.BackHandler
Expand Down Expand Up @@ -80,7 +78,6 @@ import com.and04.naturealbum.utils.network.NetworkState
import com.and04.naturealbum.utils.network.NetworkState.DISCONNECTED
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import java.io.IOException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

Expand All @@ -89,7 +86,6 @@ fun SavePhotoScreen(
locationHandler: LocationHandler,
location: Location?,
model: Uri,
fileName: String,
onBack: () -> Unit,
onSave: () -> Unit,
onCancel: () -> Unit,
Expand Down Expand Up @@ -141,7 +137,6 @@ fun SavePhotoScreen(

SavePhotoScreen(
model = model,
fileName = fileName,
location = newLocation,
photoSaveState = photoSaveState,
rememberDescription = rememberDescription,
Expand All @@ -165,7 +160,6 @@ fun SavePhotoScreen(
@Composable
fun SavePhotoScreen(
model: Uri,
fileName: String,
location: State<Location?>,
rememberDescription: State<String>,
onDescriptionChange: (String) -> Unit,
Expand Down Expand Up @@ -200,8 +194,7 @@ fun SavePhotoScreen(
if (context.isPortrait()) {
SavePhotoScreenPortrait(
innerPadding = innerPadding,
model = model,
fileName = fileName,
uri = model,
label = label,
location = location.value!!,
rememberDescription = rememberDescription,
Expand All @@ -216,8 +209,7 @@ fun SavePhotoScreen(
} else {
SavePhotoScreenLandscape(
innerPadding = innerPadding,
model = model,
fileName = fileName,
uri = model,
label = label,
location = location.value!!,
rememberDescription = rememberDescription,
Expand Down Expand Up @@ -381,19 +373,9 @@ fun Description(
}
}

private fun loadImageFromUri(context: Context, uri: Uri): Bitmap? {
return try {
context.contentResolver.openInputStream(uri)?.use { inputStream ->
BitmapFactory.decodeStream(inputStream)
}
} catch (e: IOException) {
null
}
}

fun insertFirebaseService(
context: Context,
model: Uri,
uri: String,
fileName: String,
label: Label,
location: Location,
Expand All @@ -403,7 +385,7 @@ fun insertFirebaseService(
if (Firebase.auth.currentUser == null || NetworkState.getNetWorkCode() == DISCONNECTED) return
val newTime = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val intent = Intent(context, FirebaseInsertService::class.java).apply {
putExtra(SERVICE_URI, model.toString())
putExtra(SERVICE_URI, uri)
putExtra(SERVICE_FILENAME, fileName)
putExtra(SERVICE_LABEL, label)
putExtra(SERVICE_LOCATION_LATITUDE, location.latitude)
Expand All @@ -428,7 +410,6 @@ private fun ScreenPreview() {
SavePhotoScreen(
model = "".toUri(),
location = location,
fileName = "fileName.jpg",
rememberDescription = rememberDescription,
onDescriptionChange = { },
isRepresented = isRepresented,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import coil3.request.crossfade
import com.and04.naturealbum.R
import com.and04.naturealbum.data.localdata.room.Label
import com.and04.naturealbum.ui.utils.UiState
import com.and04.naturealbum.utils.image.ImageConvert
import java.time.LocalDateTime
import java.time.ZoneId

@Composable
fun SavePhotoScreenLandscape(
innerPadding: PaddingValues,
model: Uri,
fileName: String,
uri: Uri,
label: Label?,
location: Location,
rememberDescription: State<String>,
Expand All @@ -55,7 +55,7 @@ fun SavePhotoScreenLandscape(
) {
AsyncImage(
model = ImageRequest.Builder(context)
.data(model)
.data(uri)
.crossfade(true)
.build(),
contentDescription = stringResource(R.string.save_photo_screen_image_description),
Expand Down Expand Up @@ -108,8 +108,10 @@ fun SavePhotoScreenLandscape(
stringRes = R.string.save_photo_screen_save,
onClick = {
val time = LocalDateTime.now(ZoneId.of("UTC"))
val fileName = "${System.currentTimeMillis()}.jpg"
val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName)
savePhoto(
model.toString(),
fileUri,
fileName,
label!!,
location,
Expand All @@ -120,7 +122,7 @@ fun SavePhotoScreenLandscape(

insertFirebaseService(
context = context,
model = model,
uri = fileUri,
fileName = fileName,
label = label,
location = location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import coil3.request.crossfade
import com.and04.naturealbum.R
import com.and04.naturealbum.data.localdata.room.Label
import com.and04.naturealbum.ui.utils.UiState
import com.and04.naturealbum.utils.image.ImageConvert
import java.time.LocalDateTime
import java.time.ZoneId

@Composable
fun SavePhotoScreenPortrait(
innerPadding: PaddingValues,
model: Uri,
fileName: String,
uri: Uri,
label: Label?,
location: Location,
rememberDescription: State<String>,
Expand All @@ -51,7 +51,7 @@ fun SavePhotoScreenPortrait(
) {
AsyncImage(
model = ImageRequest.Builder(context)
.data(model)
.data(uri)
.crossfade(true)
.build(),
contentDescription = stringResource(R.string.save_photo_screen_image_description),
Expand Down Expand Up @@ -98,8 +98,10 @@ fun SavePhotoScreenPortrait(
stringRes = R.string.save_photo_screen_save,
onClick = {
val time = LocalDateTime.now(ZoneId.of("UTC"))
val fileName = "${System.currentTimeMillis()}.jpg"
val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName)
savePhoto(
model.toString(),
fileUri,
fileName,
label!!,
location,
Expand All @@ -110,7 +112,7 @@ fun SavePhotoScreenPortrait(

insertFirebaseService(
context = context,
model = model,
uri = fileUri,
fileName = fileName,
label = label,
location = location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ fun NavGraphBuilder.saveAlbumNavGraph(
locationHandler = state.locationHandler.value,
location = state.lastLocation.value,
model = state.imageUri.value,
fileName = state.fileName.value,
onBack = { state.takePicture(takePictureLauncher) },
onBack = {
state.deleteCachePhoto()
state.takePicture(takePictureLauncher)
},
onSave = {
navigator.navigateSavePhotoToAlbum()
state.selectedLabel.value = null
state.deleteCachePhoto()
},
onCancel = { navigator.navigateToHome() },
label = state.selectedLabel.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ class NatureAlbumState(
var lastLocation = mutableStateOf<Location?>(null)
var locationHandler = mutableStateOf(LocationHandler(context))
var imageUri = mutableStateOf(Uri.EMPTY)
var fileName = mutableStateOf("")
var selectedLabel = mutableStateOf<Label?>(null)
private var imageFile = mutableStateOf<File?>(null)
private var imageCache = mutableStateOf<File?>(null)
var currentUid = mutableStateOf("")

fun handleLauncher(
result: ActivityResult,
navigator: NatureAlbumNavigator
) {
if (result.resultCode == RESULT_OK) {
val resizePicture = ImageConvert.resizeImage(imageUri.value)!!
imageFile.value?.delete()
imageUri.value = resizePicture.uri
fileName.value = resizePicture.fileName
ImageConvert.resizeImage(imageUri.value) { file, uri ->
imageCache.value?.delete()
imageCache.value = file
imageUri.value = uri
}

locationHandler.value.getLocation { location -> lastLocation.value = location }

Expand All @@ -56,14 +56,13 @@ class NatureAlbumState(
}

fun takePicture(launcher: ManagedActivityResultLauncher<Intent, ActivityResult>) {
// TODO: imageUri가 EMPTY가 아닐때 해당 파일 삭제
fileName.value = "temp_${System.currentTimeMillis()}.jpg"
imageFile.value = File(context.filesDir, fileName.value)
val fileName = "temp_${System.currentTimeMillis()}"
imageCache.value = File.createTempFile(fileName, ".jpg", context.cacheDir)
imageUri.value =
FileProvider.getUriForFile(
context,
"${context.packageName}.fileprovider",
imageFile.value!!
imageCache.value!!
)

Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
Expand All @@ -75,6 +74,10 @@ class NatureAlbumState(
}
}
}

fun deleteCachePhoto() {
imageCache.value?.delete()
}
}

@Composable
Expand All @@ -85,7 +88,6 @@ fun rememberNatureAlbumState(
save = { state ->
mapOf(
"imageUri" to state.imageUri.value.toString(),
"fileName" to state.fileName.value,
"lastLocation" to state.lastLocation.value,
)
},
Expand All @@ -94,7 +96,6 @@ fun rememberNatureAlbumState(
context = context,
).apply {
imageUri.value = Uri.parse(restoredMap["imageUri"] as String)
fileName.value = restoredMap["fileName"] as String
lastLocation.value = restoredMap["lastLocation"] as Location?
}
}
Expand Down
Loading