Skip to content

Comments

🔴 Fix: Critical State Management Issue in PromptRow (#19)#23

Open
pj4533 wants to merge 1 commit intomainfrom
fix/prompt-row-state-management-binding
Open

🔴 Fix: Critical State Management Issue in PromptRow (#19)#23
pj4533 wants to merge 1 commit intomainfrom
fix/prompt-row-state-management-binding

Conversation

@pj4533
Copy link
Owner

@pj4533 pj4533 commented Oct 11, 2025

Summary

Fixes critical state management bug in PromptRow where @State initialized from parameter created stale state issues.

Changes

  • ✅ Refactor PromptRow to use @Binding instead of @State
  • ✅ Remove intermediate editedPrompt state variable
  • ✅ Remove custom init method
  • ✅ Update PromptLibraryView to pass bindings using $viewModel.prompts
  • ✅ Ensure prompt updates persist correctly

Why This Fixes the Issue

The previous implementation used @State private var editedPrompt initialized from a parameter in init. This meant:

  1. Stale UI State - SwiftUI only initializes @State once during view creation
  2. Data Loss Risk - Parent updates wouldn't reflect in child views
  3. Confusing Behavior - UI showed outdated information

The new implementation uses @Binding var prompt which:

  • Creates a direct two-way binding to the parent's data
  • Eliminates stale state completely
  • Ensures single source of truth (viewModel.prompts)
  • Provides predictable, automatic updates

Test Plan

  • Build succeeds with no compilation errors
  • Verify prompt editing works correctly
  • Test rapid edits to multiple prompts
  • Confirm no data loss during concurrent editing
  • Verify auto-save functionality still works
  • Test that edits reflect immediately in UI

Benefits

  • ✅ No stale state
  • ✅ Single source of truth
  • ✅ Predictable behavior
  • ✅ Automatic persistence
  • ✅ Simpler, cleaner code (removed 8 lines)

Closes #19

🤖 Generated with Claude Code

Resolves #19

## Changes
- Refactor PromptRow to use @binding instead of @State
- Remove intermediate editedPrompt state variable
- Remove custom init method
- Update PromptLibraryView to pass bindings using $viewModel.prompts
- Direct binding eliminates stale state issues

## Benefits
✅ No stale state - changes reflect immediately
✅ Single source of truth - viewModel.prompts
✅ Predictable behavior - UI always in sync
✅ Automatic persistence - onChange handlers work correctly
✅ Simpler code - no init or state copying

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🔴 CRITICAL: State Management Issues in PromptRow

1 participant