@@ -17,6 +17,7 @@ import kotlinx.coroutines.Dispatchers
1717import kotlinx.coroutines.flow.MutableStateFlow
1818import kotlinx.coroutines.flow.StateFlow
1919import kotlinx.coroutines.flow.asStateFlow
20+ import kotlinx.coroutines.flow.update
2021import kotlinx.coroutines.launch
2122
2223/* *
@@ -42,26 +43,28 @@ class WizardViewModel(application: Application) : AndroidViewModel(application)
4243
4344 /* * Resets the whole wizard state back to initial defaults. */
4445 fun reset () {
45- _uiState .value = UIState ()
46+ _uiState .update { UIState () }
4647 }
4748
4849 fun setPreviewMode (mode : PatchablePreviewMode ) {
49- _uiState .value = _uiState .value. copy(previewMode = mode)
50+ _uiState .update { it. copy(previewMode = mode) }
5051 }
5152
5253 fun updateColorCount (count : Int ) {
53- _uiState .value = _uiState .value. copy(colorCount = count)
54+ _uiState .update { it. copy(colorCount = count) }
5455 }
5556
5657 fun updateBitmap (bitmap : ImageBitmap ) {
57- _uiState .value = _uiState .value.copy(
58- imageBitmap = bitmap,
59- // Reset downstream results when source bitmap changes
60- reducedImageBitmap = null ,
61- reducedHistogram = null ,
62- embroideryData = null ,
63- embroideryPreviewImage = null ,
64- )
58+ _uiState .update {
59+ it.copy(
60+ imageBitmap = bitmap,
61+ // Reset downstream results when source bitmap changes
62+ reducedImageBitmap = null ,
63+ reducedHistogram = null ,
64+ embroideryData = null ,
65+ embroideryPreviewImage = null ,
66+ )
67+ }
6568 }
6669
6770 fun computeReducedBitmap () {
@@ -75,10 +78,12 @@ class WizardViewModel(application: Application) : AndroidViewModel(application)
7578 .scale((512 * aspect).toInt(), 512 , false )
7679 .reduceColors(colorCount)
7780
78- _uiState .value = _uiState .value.copy(
79- reducedImageBitmap = reducedBmp.asImageBitmap(),
80- reducedHistogram = histogram,
81- )
81+ _uiState .update {
82+ it.copy(
83+ reducedImageBitmap = reducedBmp.asImageBitmap(),
84+ reducedHistogram = histogram,
85+ )
86+ }
8287 }
8388 }
8489
@@ -89,7 +94,7 @@ class WizardViewModel(application: Application) : AndroidViewModel(application)
8994 ) {
9095 viewModelScope.launch(Dispatchers .IO ) {
9196 try {
92- _uiState .value = _uiState .value. copy(creatingEmbroidery = true , error = null )
97+ _uiState .update { it. copy(creatingEmbroidery = true , error = null ) }
9398
9499 val aspect = bitmap.width / bitmap.height.toFloat()
95100 val embroidery = StitchToPES .createEmbroideryFromBitmap(
@@ -113,16 +118,20 @@ class WizardViewModel(application: Application) : AndroidViewModel(application)
113118 decoded.scale(decoded.width * 2 , decoded.height * 2 ).asImageBitmap()
114119 }
115120
116- _uiState .value = _uiState .value.copy(
117- embroideryData = pes,
118- embroideryPreviewImage = previewImage,
119- creatingEmbroidery = false ,
120- )
121+ _uiState .update {
122+ it.copy(
123+ embroideryData = pes,
124+ embroideryPreviewImage = previewImage,
125+ creatingEmbroidery = false ,
126+ )
127+ }
121128 } catch (t: Throwable ) {
122- _uiState .value = _uiState .value.copy(
123- creatingEmbroidery = false ,
124- error = t.message ? : t.toString(),
125- )
129+ _uiState .update {
130+ it.copy(
131+ creatingEmbroidery = false ,
132+ error = t.message ? : t.toString(),
133+ )
134+ }
126135 }
127136 }
128137 }
0 commit comments