16
16
package com.android.developers.androidify.customize
17
17
18
18
import android.app.Application
19
+ import android.graphics.Bitmap
19
20
import android.net.Uri
20
21
import android.util.Log
21
22
import androidx.compose.foundation.layout.fillMaxSize
@@ -29,13 +30,11 @@ import dagger.assisted.Assisted
29
30
import dagger.assisted.AssistedFactory
30
31
import dagger.assisted.AssistedInject
31
32
import dagger.hilt.android.lifecycle.HiltViewModel
32
- import kotlinx.coroutines.Dispatchers
33
33
import kotlinx.coroutines.flow.MutableStateFlow
34
34
import kotlinx.coroutines.flow.StateFlow
35
35
import kotlinx.coroutines.flow.asStateFlow
36
36
import kotlinx.coroutines.flow.update
37
37
import kotlinx.coroutines.launch
38
- import javax.inject.Inject
39
38
40
39
@HiltViewModel(assistedFactory = CustomizeExportViewModel .Factory ::class )
41
40
class CustomizeExportViewModel @AssistedInject constructor(
@@ -65,11 +64,12 @@ class CustomizeExportViewModel @AssistedInject constructor(
65
64
66
65
init {
67
66
_state .update {
68
- CustomizeExportState (
67
+ it.copy (
69
68
originalImageUrl = originalImageUrl,
70
69
exportImageCanvas = it.exportImageCanvas.copy(imageUri = resultImageUrl),
71
70
)
72
71
}
72
+ loadInitialBitmap(resultImageUrl)
73
73
}
74
74
75
75
@@ -221,8 +221,7 @@ class CustomizeExportViewModel @AssistedInject constructor(
221
221
}
222
222
return @launch
223
223
}
224
-
225
- val image = state.value.exportImageCanvas.imageUri?.let { uri -> localFileProvider.loadBitmapFromUri(uri) }
224
+ val image = state.value.exportImageCanvas.imageBitmap
226
225
if (image == null ) {
227
226
return @launch
228
227
}
@@ -278,4 +277,19 @@ class CustomizeExportViewModel @AssistedInject constructor(
278
277
it.copy(selectedTool = tool)
279
278
}
280
279
}
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
+ }
281
295
}
0 commit comments