5.2 KiB
Sheet Presentation Fix
Problems Fixed
Issue 1: Editor Not Opening After App Restart ✅
Symptom: After restarting the app, clicking "Edit Animation" showed logs but editor sheet didn't appear.
Root Cause: The sheet was using .sheet(isPresented: $showingEditor) with an if let url = editorSourceURL wrapper inside. When state variables were updated, SwiftUI's sheet didn't properly re-evaluate the if let condition, causing the sheet content to be nil even though isPresented was true.
Fix: Changed to item-based sheet presentation:
// Before (BROKEN)
.sheet(isPresented: $showingEditor) {
if let url = editorSourceURL {
AnimationEditorView(...)
}
}
// After (WORKING)
.sheet(item: Binding(
get: { showingEditor ? EditorState(...) : nil },
set: { newValue in showingEditor = (newValue != nil) }
)) { state in
AnimationEditorView(...)
}
This ensures the sheet content is always evaluated when showingEditor changes.
Issue 2: Imports Not Showing Editor ✅
Symptom: When importing new animations, the editor didn't open, animations were imported with default transforms.
Root Cause: Same as Issue 1 - the sheet state wasn't being properly synchronized.
Fix: Added delays and logging:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
showingEditor = true
print("📥 [Import] Opening editor for new import")
}
Issue 3: Edit Button Clicks Not Registering After Restart ✅
Symptom: Logs showed edit was being triggered, but sheet didn't appear.
Root Cause: State updates were happening synchronously, and SwiftUI sheet wasn't detecting the change.
Fix: Added small delay before setting showingEditor = true:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
showingEditor = true
print("🔧 [Edit] Sheet should now be visible")
}
Technical Details
EditorState Struct
Created a new identifiable struct to properly manage sheet state:
private struct EditorState: Identifiable {
let id = UUID()
let url: URL?
let isRemote: Bool
let existingAnimation: CustomIdleAnimation?
}
This makes the sheet's item parameter work correctly and ensures proper state management.
State Flow
- User clicks "Edit" or imports file
- State variables set:
editorSourceURL,editorIsRemote,editingExistingAnimation - After 0.05-0.1s delay:
showingEditor = true - Sheet's
itemgetter evaluates and createsEditorState - Sheet presents with correct
AnimationEditorView - On dismiss: Sheet's
itemsetter cleans up state
Benefits of Item-Based Presentation
- More reliable: Item changes always trigger sheet updates
- Better cleanup: Setter can clean up state when sheet dismisses
- Type safety: Editor state is encapsulated in a struct
- Debugging: State changes are more explicit and trackable
Testing
Test 1: Edit After Restart
- Launch app
- Right-click any animation
- Click "Edit Animation"
- Check console:
🔧 [Edit] Attempting to edit animation: Orange Cat Peeping 🔧 [Edit] Animation source: lottieFile(...) 🔧 [Edit] Lottie file URL: file://... 🔧 [Edit] File exists: true 🔧 [Edit] Sheet should now be visible - Editor should open ✅
Test 2: Import New Animation
- Click "+" button
- Select a Lottie JSON file
- Check console:
📥 [Import] Opening editor for new import - Editor should open with preview and controls ✅
- Adjust transforms, save
- Animation should appear in grid with transforms applied ✅
Test 3: Import from URL
- Click "+" → "From URL"
- Enter URL and name
- Click "Import"
- Check console:
📥 [Import] Opening editor for URL import - Editor should open ✅
Test 4: Rapid Edits
- Edit an animation
- Save
- Immediately edit again
- Editor should open both times ✅
Console Logs to Watch For
Successful Edit Flow:
🔧 [Edit] Attempting to edit animation: [name]
🔧 [Edit] Animation source: [source type]
🔧 [Edit] Sheet should now be visible
✅ [AnimationEditor] Saved transform override for: [name]
📋 [CustomIdleAnimation] Found override for '[name]': AnimationTransformConfig(...)
🎨 [IdleAnimationView] Rendering animation: [name]
Successful Import Flow:
📥 [Import] Opening editor for new import
✅ [AnimationEditor] Imported animation: [name]
🎨 [IdleAnimationView] Rendering animation: [name]
If Issues Persist
- Check Console: Look for the "Sheet should now be visible" message
- Verify State: Add breakpoint in sheet's
itemgetter to see if it's being called - Check Delays: If sheet still doesn't open, increase delay to 0.2s
- Clean Build: Sometimes Xcode needs a clean build (Cmd+Shift+K)
- Check Logs: All state changes should be logged now
Architecture Benefits
The new item-based sheet presentation is more robust because:
- ✅ SwiftUI always evaluates item changes
- ✅ No nested
if letinside sheet content - ✅ State cleanup happens automatically in setter
- ✅ Better for async state updates
- ✅ More predictable behavior after app restarts
- ✅ Easier to debug with explicit state object