Skip to content

Commit 500f5dc

Browse files
Handling the process death scenario for Bitmap
1 parent 048c502 commit 500f5dc

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

feature/results/src/main/java/com/android/developers/androidify/customize/CustomizeExportViewModel.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.android.developers.androidify.customize
1717

1818
import android.app.Application
19+
import android.graphics.Bitmap
1920
import android.net.Uri
2021
import android.util.Log
2122
import androidx.compose.foundation.layout.fillMaxSize
@@ -29,13 +30,11 @@ import dagger.assisted.Assisted
2930
import dagger.assisted.AssistedFactory
3031
import dagger.assisted.AssistedInject
3132
import dagger.hilt.android.lifecycle.HiltViewModel
32-
import kotlinx.coroutines.Dispatchers
3333
import kotlinx.coroutines.flow.MutableStateFlow
3434
import kotlinx.coroutines.flow.StateFlow
3535
import kotlinx.coroutines.flow.asStateFlow
3636
import kotlinx.coroutines.flow.update
3737
import kotlinx.coroutines.launch
38-
import javax.inject.Inject
3938

4039
@HiltViewModel(assistedFactory = CustomizeExportViewModel.Factory::class)
4140
class CustomizeExportViewModel @AssistedInject constructor(
@@ -65,11 +64,12 @@ class CustomizeExportViewModel @AssistedInject constructor(
6564

6665
init {
6766
_state.update {
68-
CustomizeExportState(
67+
it.copy(
6968
originalImageUrl = originalImageUrl,
7069
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl),
7170
)
7271
}
72+
loadInitialBitmap(resultImageUrl)
7373
}
7474

7575

@@ -221,8 +221,7 @@ class CustomizeExportViewModel @AssistedInject constructor(
221221
}
222222
return@launch
223223
}
224-
225-
val image = state.value.exportImageCanvas.imageUri?.let { uri -> localFileProvider.loadBitmapFromUri(uri) }
224+
val image = state.value.exportImageCanvas.imageBitmap
226225
if (image == null) {
227226
return@launch
228227
}
@@ -278,4 +277,19 @@ class CustomizeExportViewModel @AssistedInject constructor(
278277
it.copy(selectedTool = tool)
279278
}
280279
}
280+
281+
private fun loadInitialBitmap(uri: Uri) {
282+
viewModelScope.launch {
283+
try {
284+
val bitmap = localFileProvider.loadBitmapFromUri(uri)
285+
_state.update {
286+
it.copy(
287+
exportImageCanvas = it.exportImageCanvas.copy(imageBitmap = bitmap)
288+
)
289+
}
290+
} catch (e: Exception) {
291+
_snackbarHostState.value.showSnackbar("Could not load image.")
292+
}
293+
}
294+
}
281295
}

feature/results/src/main/java/com/android/developers/androidify/customize/CustomizeState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ data class BackgroundToolState(
7676

7777
data class ExportImageCanvas(
7878
val imageUri: Uri? = null,
79+
val imageBitmap: Bitmap? = null,
7980
val imageBitmapRemovedBackground: Bitmap? = null,
8081
val aspectRatioOption: SizeOption = SizeOption.Square,
8182
val canvasSize: Size = Size(1000f, 1000f),

0 commit comments

Comments
 (0)