-
Notifications
You must be signed in to change notification settings - Fork 21
Refactors detail view to use ViewModel #37
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31b1757
Merge pull request #36 from maiatoday/work/better-safe-area
maiatoday 1e1a0f3
Refactor WizardScreen to use ViewModel
maiatoday ab3ab28
Refactor dependencies and update versions
maiatoday 32ce4a2
Refactor PatchableEditor composable parameters
maiatoday f669619
Add `reset` functionality to `WizardViewModel` and integrate with `Ma…
maiatoday d8ba64a
Remove unused `setReducedResult` function from `WizardViewModel`
maiatoday b312724
Refactor embroidery creation flow and update `WizardViewModel`
maiatoday 45f1c63
Refactor `WizardViewModel` to use `MutableStateFlow.update` for state…
maiatoday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
app/src/main/java/de/berlindroid/zepatch/WizardViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package de.berlindroid.zepatch | ||
|
|
||
| import android.app.Application | ||
| import android.graphics.Bitmap | ||
| import android.graphics.BitmapFactory | ||
| import androidx.compose.ui.graphics.ImageBitmap | ||
| import androidx.compose.ui.graphics.asAndroidBitmap | ||
| import androidx.compose.ui.graphics.asImageBitmap | ||
| import androidx.core.graphics.scale | ||
| import androidx.lifecycle.AndroidViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.embroidermodder.punching.Histogram | ||
| import com.embroidermodder.punching.reduceColors | ||
| import de.berlindroid.zepatch.PatchablePreviewMode.COMPOSABLE | ||
| import de.berlindroid.zepatch.stiches.StitchToPES | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| /** | ||
| * ViewModel to manage the Wizard screen state. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧙♀️👍 |
||
| */ | ||
|
|
||
| class WizardViewModel(application: Application) : AndroidViewModel(application) { | ||
|
|
||
| data class UIState( | ||
| val imageBitmap: ImageBitmap? = null, | ||
| val reducedImageBitmap: ImageBitmap? = null, | ||
| val reducedHistogram: Histogram? = null, | ||
| val colorCount: Int = 3, | ||
| val embroideryData: ByteArray? = null, | ||
| val embroideryPreviewImage: ImageBitmap? = null, | ||
| val creatingEmbroidery: Boolean = false, | ||
| val error: String? = null, | ||
| val previewMode: PatchablePreviewMode = COMPOSABLE, | ||
| ) | ||
|
|
||
| private val _uiState = MutableStateFlow(UIState()) | ||
| val uiState: StateFlow<UIState> = _uiState.asStateFlow() | ||
|
|
||
| /** Resets the whole wizard state back to initial defaults. */ | ||
| fun reset() { | ||
| _uiState.update { UIState() } | ||
| } | ||
|
|
||
| fun setPreviewMode(mode: PatchablePreviewMode) { | ||
| _uiState.update { it.copy(previewMode = mode) } | ||
| } | ||
|
|
||
| fun updateColorCount(count: Int) { | ||
| _uiState.update { it.copy(colorCount = count) } | ||
| } | ||
|
|
||
| fun updateBitmap(bitmap: ImageBitmap) { | ||
| _uiState.update { | ||
| it.copy( | ||
| imageBitmap = bitmap, | ||
| // Reset downstream results when source bitmap changes | ||
| reducedImageBitmap = null, | ||
| reducedHistogram = null, | ||
| embroideryData = null, | ||
| embroideryPreviewImage = null, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fun computeReducedBitmap() { | ||
| val state = _uiState.value | ||
| val image = state.imageBitmap ?: return | ||
| val colorCount = state.colorCount | ||
| viewModelScope.launch(Dispatchers.IO) { | ||
| val aspect = image.width / image.height.toFloat() | ||
| val (reducedBmp, histogram) = image.asAndroidBitmap() | ||
| .copy(Bitmap.Config.ARGB_8888, false) | ||
| .scale((512 * aspect).toInt(), 512, false) | ||
| .reduceColors(colorCount) | ||
|
|
||
| _uiState.update { | ||
| it.copy( | ||
| reducedImageBitmap = reducedBmp.asImageBitmap(), | ||
| reducedHistogram = histogram, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun createEmbroidery( | ||
| name: String, | ||
| bitmap: ImageBitmap, | ||
| histogram: Histogram, | ||
| ) { | ||
| viewModelScope.launch(Dispatchers.IO) { | ||
| try { | ||
| _uiState.update { it.copy(creatingEmbroidery = true, error = null) } | ||
|
|
||
| val aspect = bitmap.width / bitmap.height.toFloat() | ||
| val embroidery = StitchToPES.createEmbroideryFromBitmap( | ||
| name = name, | ||
| bitmap = bitmap.asAndroidBitmap(), | ||
| histogram = histogram, | ||
| mmWidth = 500f * aspect, | ||
| mmHeight = 500f, | ||
| mmDensityX = 4f, | ||
| mmDensityY = 2f, | ||
| ) | ||
|
|
||
| val context = getApplication<Application>().applicationContext | ||
| val pes = StitchToPES.convert(context, embroidery) | ||
| val png = StitchToPES.convert(context, embroidery, "png") | ||
|
|
||
| val previewImage = if (png == null || png.isEmpty()) { | ||
| null | ||
| } else { | ||
| val decoded = BitmapFactory.decodeByteArray(png, 0, png.size) | ||
| decoded.scale(decoded.width * 2, decoded.height * 2).asImageBitmap() | ||
| } | ||
|
|
||
| _uiState.update { | ||
| it.copy( | ||
| embroideryData = pes, | ||
| embroideryPreviewImage = previewImage, | ||
| creatingEmbroidery = false, | ||
| ) | ||
| } | ||
| } catch (t: Throwable) { | ||
| _uiState.update { | ||
| it.copy( | ||
| creatingEmbroidery = false, | ||
| error = t.message ?: t.toString(), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick: should that also be
onComputeReducedBitmap? Consistency with the other names, or does work differently, hence the difference in naming?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for
onBitmapUpdatedandonColorCountUpdatedwe ask the composable to do something and it calls these methods when it completes the task.on the other hand the
computeReducedBitmapis an imperative action which we are asking and external function to perform, in this case the ViewModel.BUT I see a nit myself, I am not consistently using color vs colour. very irritating, I will fix it.Fake news it is ok