Skip to content

Commit 51ff0a4

Browse files
committed
[Refactor] 저장 전 이미지 캐시로 변경
1 parent fe2aef0 commit 51ff0a4

File tree

8 files changed

+90
-100
lines changed

8 files changed

+90
-100
lines changed

app/src/main/java/com/and04/naturealbum/background/workmanager/SynchronizationWorker.kt

+1-28
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ class SynchronizationWorker @AssistedInject constructor(
268268

269269
val valueList = fileNameToLabelUid.values.toList()
270270
val findAlbumData = valueList.find { value -> value.second == photo.fileName }
271-
val uri = makeFileToUri(photo.uri, photo.fileName)
272-
271+
val uri = ImageConvert.makeFileToUri(photo.uri, photo.fileName, true)
273272

274273
val labelId = findAlbumData?.first
275274
?: fileNameToLabelUid[photo.label]?.first
@@ -383,32 +382,6 @@ class SynchronizationWorker @AssistedInject constructor(
383382
}
384383
}
385384

386-
private fun makeFileToUri(photoUri: String, fileName: String): String {
387-
val context = applicationContext
388-
val storage = context.filesDir
389-
val imageFile = File(storage, fileName)
390-
imageFile.createNewFile()
391-
392-
FileOutputStream(imageFile).use { fos ->
393-
BitmapFactory.decodeStream(URL(photoUri).openStream()).apply {
394-
if (Build.VERSION.SDK_INT >= 30) {
395-
compress(Bitmap.CompressFormat.WEBP_LOSSY, 100, fos)
396-
} else {
397-
compress(Bitmap.CompressFormat.JPEG, 100, fos)
398-
}
399-
400-
recycle()
401-
}
402-
fos.flush()
403-
}
404-
405-
return FileProvider.getUriForFile(
406-
context,
407-
"${context.packageName}.fileprovider",
408-
imageFile
409-
).toString()
410-
}
411-
412385
private fun isUnSyncLabel(label: SyncAlbumsDto, firebaseLabel: FirebaseLabelResponse): Boolean {
413386
return (firebaseLabel.labelName == label.labelName) &&
414387
((firebaseLabel.fileName != label.fileName)

app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreen.kt

+4-23
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import android.content.Context
55
import android.content.Intent
66
import android.content.res.Configuration.UI_MODE_NIGHT_NO
77
import android.content.res.Configuration.UI_MODE_NIGHT_YES
8-
import android.graphics.Bitmap
9-
import android.graphics.BitmapFactory
108
import android.location.Location
119
import android.net.Uri
1210
import androidx.activity.compose.BackHandler
@@ -80,7 +78,6 @@ import com.and04.naturealbum.utils.network.NetworkState
8078
import com.and04.naturealbum.utils.network.NetworkState.DISCONNECTED
8179
import com.google.firebase.auth.ktx.auth
8280
import com.google.firebase.ktx.Firebase
83-
import java.io.IOException
8481
import java.time.LocalDateTime
8582
import java.time.format.DateTimeFormatter
8683

@@ -89,7 +86,6 @@ fun SavePhotoScreen(
8986
locationHandler: LocationHandler,
9087
location: Location?,
9188
model: Uri,
92-
fileName: String,
9389
onBack: () -> Unit,
9490
onSave: () -> Unit,
9591
onCancel: () -> Unit,
@@ -141,7 +137,6 @@ fun SavePhotoScreen(
141137

142138
SavePhotoScreen(
143139
model = model,
144-
fileName = fileName,
145140
location = newLocation,
146141
photoSaveState = photoSaveState,
147142
rememberDescription = rememberDescription,
@@ -165,7 +160,6 @@ fun SavePhotoScreen(
165160
@Composable
166161
fun SavePhotoScreen(
167162
model: Uri,
168-
fileName: String,
169163
location: State<Location?>,
170164
rememberDescription: State<String>,
171165
onDescriptionChange: (String) -> Unit,
@@ -200,8 +194,7 @@ fun SavePhotoScreen(
200194
if (context.isPortrait()) {
201195
SavePhotoScreenPortrait(
202196
innerPadding = innerPadding,
203-
model = model,
204-
fileName = fileName,
197+
uri = model,
205198
label = label,
206199
location = location.value!!,
207200
rememberDescription = rememberDescription,
@@ -216,8 +209,7 @@ fun SavePhotoScreen(
216209
} else {
217210
SavePhotoScreenLandscape(
218211
innerPadding = innerPadding,
219-
model = model,
220-
fileName = fileName,
212+
uri = model,
221213
label = label,
222214
location = location.value!!,
223215
rememberDescription = rememberDescription,
@@ -381,19 +373,9 @@ fun Description(
381373
}
382374
}
383375

384-
private fun loadImageFromUri(context: Context, uri: Uri): Bitmap? {
385-
return try {
386-
context.contentResolver.openInputStream(uri)?.use { inputStream ->
387-
BitmapFactory.decodeStream(inputStream)
388-
}
389-
} catch (e: IOException) {
390-
null
391-
}
392-
}
393-
394376
fun insertFirebaseService(
395377
context: Context,
396-
model: Uri,
378+
uri: String,
397379
fileName: String,
398380
label: Label,
399381
location: Location,
@@ -403,7 +385,7 @@ fun insertFirebaseService(
403385
if (Firebase.auth.currentUser == null || NetworkState.getNetWorkCode() == DISCONNECTED) return
404386
val newTime = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
405387
val intent = Intent(context, FirebaseInsertService::class.java).apply {
406-
putExtra(SERVICE_URI, model.toString())
388+
putExtra(SERVICE_URI, uri)
407389
putExtra(SERVICE_FILENAME, fileName)
408390
putExtra(SERVICE_LABEL, label)
409391
putExtra(SERVICE_LOCATION_LATITUDE, location.latitude)
@@ -428,7 +410,6 @@ private fun ScreenPreview() {
428410
SavePhotoScreen(
429411
model = "".toUri(),
430412
location = location,
431-
fileName = "fileName.jpg",
432413
rememberDescription = rememberDescription,
433414
onDescriptionChange = { },
434415
isRepresented = isRepresented,

app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenLandscape.kt

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import coil3.request.crossfade
2626
import com.and04.naturealbum.R
2727
import com.and04.naturealbum.data.localdata.room.Label
2828
import com.and04.naturealbum.ui.utils.UiState
29+
import com.and04.naturealbum.utils.image.ImageConvert
2930
import java.time.LocalDateTime
3031
import java.time.ZoneId
3132

3233
@Composable
3334
fun SavePhotoScreenLandscape(
3435
innerPadding: PaddingValues,
35-
model: Uri,
36-
fileName: String,
36+
uri: Uri,
3737
label: Label?,
3838
location: Location,
3939
rememberDescription: State<String>,
@@ -55,7 +55,7 @@ fun SavePhotoScreenLandscape(
5555
) {
5656
AsyncImage(
5757
model = ImageRequest.Builder(context)
58-
.data(model)
58+
.data(uri)
5959
.crossfade(true)
6060
.build(),
6161
contentDescription = stringResource(R.string.save_photo_screen_image_description),
@@ -108,8 +108,10 @@ fun SavePhotoScreenLandscape(
108108
stringRes = R.string.save_photo_screen_save,
109109
onClick = {
110110
val time = LocalDateTime.now(ZoneId.of("UTC"))
111+
val fileName = "${System.currentTimeMillis()}.jpg"
112+
val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName)
111113
savePhoto(
112-
model.toString(),
114+
fileUri,
113115
fileName,
114116
label!!,
115117
location,
@@ -120,7 +122,7 @@ fun SavePhotoScreenLandscape(
120122

121123
insertFirebaseService(
122124
context = context,
123-
model = model,
125+
uri = fileUri,
124126
fileName = fileName,
125127
label = label,
126128
location = location,

app/src/main/java/com/and04/naturealbum/ui/add/savephoto/SavePhotoScreenPortrait.kt

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import coil3.request.crossfade
2626
import com.and04.naturealbum.R
2727
import com.and04.naturealbum.data.localdata.room.Label
2828
import com.and04.naturealbum.ui.utils.UiState
29+
import com.and04.naturealbum.utils.image.ImageConvert
2930
import java.time.LocalDateTime
3031
import java.time.ZoneId
3132

3233
@Composable
3334
fun SavePhotoScreenPortrait(
3435
innerPadding: PaddingValues,
35-
model: Uri,
36-
fileName: String,
36+
uri: Uri,
3737
label: Label?,
3838
location: Location,
3939
rememberDescription: State<String>,
@@ -51,7 +51,7 @@ fun SavePhotoScreenPortrait(
5151
) {
5252
AsyncImage(
5353
model = ImageRequest.Builder(context)
54-
.data(model)
54+
.data(uri)
5555
.crossfade(true)
5656
.build(),
5757
contentDescription = stringResource(R.string.save_photo_screen_image_description),
@@ -98,8 +98,10 @@ fun SavePhotoScreenPortrait(
9898
stringRes = R.string.save_photo_screen_save,
9999
onClick = {
100100
val time = LocalDateTime.now(ZoneId.of("UTC"))
101+
val fileName = "${System.currentTimeMillis()}.jpg"
102+
val fileUri = ImageConvert.makeFileToUri(uri.toString(), fileName)
101103
savePhoto(
102-
model.toString(),
104+
fileUri,
103105
fileName,
104106
label!!,
105107
location,
@@ -110,7 +112,7 @@ fun SavePhotoScreenPortrait(
110112

111113
insertFirebaseService(
112114
context = context,
113-
model = model,
115+
uri = fileUri,
114116
fileName = fileName,
115117
label = label,
116118
location = location,

app/src/main/java/com/and04/naturealbum/ui/add/savephoto/navigation/SaveAlbumNavigation.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ fun NavGraphBuilder.saveAlbumNavGraph(
2525
locationHandler = state.locationHandler.value,
2626
location = state.lastLocation.value,
2727
model = state.imageUri.value,
28-
fileName = state.fileName.value,
2928
onBack = {
30-
state.deleteFilePhoto()
29+
state.deleteCachePhoto()
3130
state.takePicture(takePictureLauncher)
32-
},
31+
},
3332
onSave = {
3433
navigator.navigateSavePhotoToAlbum()
3534
state.selectedLabel.value = null
35+
state.deleteCachePhoto()
3636
},
3737
onCancel = { navigator.navigateToHome() },
3838
label = state.selectedLabel.value,

app/src/main/java/com/and04/naturealbum/ui/navigation/NatureAlbumState.kt

+11-16
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,20 @@ class NatureAlbumState(
3030
var lastLocation = mutableStateOf<Location?>(null)
3131
var locationHandler = mutableStateOf(LocationHandler(context))
3232
var imageUri = mutableStateOf(Uri.EMPTY)
33-
var fileName = mutableStateOf("")
3433
var selectedLabel = mutableStateOf<Label?>(null)
35-
private var imageFile = mutableStateOf<File?>(null)
34+
private var imageCache = mutableStateOf<File?>(null)
3635
var currentUid = mutableStateOf("")
3736

3837
fun handleLauncher(
3938
result: ActivityResult,
4039
navigator: NatureAlbumNavigator
4140
) {
4241
if (result.resultCode == RESULT_OK) {
43-
val resizePicture = ImageConvert.resizeImage(imageUri.value) { file ->
44-
imageFile.value?.delete()
45-
imageFile.value = file
46-
}!!
47-
48-
imageUri.value = resizePicture.uri
49-
fileName.value = resizePicture.fileName
42+
ImageConvert.resizeImage(imageUri.value) { file, uri ->
43+
imageCache.value?.delete()
44+
imageCache.value = file
45+
imageUri.value = uri
46+
}
5047

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

@@ -59,13 +56,13 @@ class NatureAlbumState(
5956
}
6057

6158
fun takePicture(launcher: ManagedActivityResultLauncher<Intent, ActivityResult>) {
62-
fileName.value = "temp_${System.currentTimeMillis()}.jpg"
63-
imageFile.value = File(context.filesDir, fileName.value)
59+
val fileName = "temp_${System.currentTimeMillis()}"
60+
imageCache.value = File.createTempFile(fileName, ".jpg", context.cacheDir)
6461
imageUri.value =
6562
FileProvider.getUriForFile(
6663
context,
6764
"${context.packageName}.fileprovider",
68-
imageFile.value!!
65+
imageCache.value!!
6966
)
7067

7168
Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
@@ -78,8 +75,8 @@ class NatureAlbumState(
7875
}
7976
}
8077

81-
fun deleteFilePhoto() {
82-
imageFile.value?.delete()
78+
fun deleteCachePhoto() {
79+
imageCache.value?.delete()
8380
}
8481
}
8582

@@ -91,7 +88,6 @@ fun rememberNatureAlbumState(
9188
save = { state ->
9289
mapOf(
9390
"imageUri" to state.imageUri.value.toString(),
94-
"fileName" to state.fileName.value,
9591
"lastLocation" to state.lastLocation.value,
9692
)
9793
},
@@ -100,7 +96,6 @@ fun rememberNatureAlbumState(
10096
context = context,
10197
).apply {
10298
imageUri.value = Uri.parse(restoredMap["imageUri"] as String)
103-
fileName.value = restoredMap["fileName"] as String
10499
lastLocation.value = restoredMap["lastLocation"] as Location?
105100
}
106101
}

0 commit comments

Comments
 (0)