Fix: initialize currentHighlights @State from highlights parameter for first-render correctness#1
Open
R2kashyap wants to merge 1 commit into
Open
Fix: initialize currentHighlights @State from highlights parameter for first-render correctness#1R2kashyap wants to merge 1 commit into
R2kashyap wants to merge 1 commit into
Conversation
The @State currentHighlights was initialized to empty [:], causing the first Canvas render to draw without highlights. .onAppear would then set currentHighlights = highlights, but Canvas would not re-draw automatically from that @State change. Users saw an empty body view until they interacted with it (toggle front/back, tap a muscle) which forced re-instantiation. This fix initializes @State from the highlights parameter directly via State(initialValue:), so the first Canvas draw uses the correct data. .onChange(of: highlights) is preserved for subsequent updates.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a bug where
AnimatedBodyContainerdoesn't render highlights on first appearance. Users see an empty body view until they interact with it (tap a muscle, toggle front/back), which forces re-instantiation and a fresh render.The Bug
AnimatedBodyContainer.currentHighlightsis a@Stateproperty initialized to an empty dictionary:The rendering flow on first appearance:
highlightsparameter containing heatmap data@State currentHighlightsinitializes to[:](empty)bodyis evaluated — Canvas draws usingblendedHighlights(progress: 1.0)which returnscurrentHighlights(empty).onAppearfires, setscurrentHighlights = highlights@StatechangeThe Fix
Initialize
@Statedirectly from thehighlightsparameter using the standard SwiftUI pattern for external initial values:Now the first Canvas draw has correct data. The
.onAppearis no longer needed (removed). The.onChange(of: highlights)path is preserved for subsequent updates when highlights change after initial render.How to Reproduce
Before fix: Body renders without colors on first appearance. Toggle front/back to see colors.
After fix: Colors render immediately on first appearance.
Changes
Sources/MuscleMap/Views/AnimatedBodyView.swift_currentHighlightsviaState(initialValue: highlights).onAppear { currentHighlights = highlights }(no longer needed)@State private var currentHighlights: [Muscle: MuscleHighlight] = [:]to remove default initializerTesting
.onChange(of: highlights)still works for subsequent updates