SwiftDataTCA icon indicating copy to clipboard operation
SwiftDataTCA copied to clipboard

UndoManager POC

Open DandyLyons opened this issue 1 year ago • 0 comments

I'm not sure if you're interested, but I would like to get a proof of concept made for implementing SwiftData's undo/redo functionality in TCA.

I've explored it some so far but it is not easy. Two major problems I've found so far:

  1. Registering the UndoManager with SwiftData:
  • Undo/Redo functionality is off by default, and so far the only way I've seen how to enable it is:
    1. pass isUndoEnabled: true into modelContainer(for:inMemory:isAutosaveEnabled:isUndoEnabled:onSetup:)
    2. then grab the undo manager from @Environment(\.undoManager)
    3. then call undoManager.undo()
  • But, right now, we are creating our ModelContainer manually in our Dependency client and none of the initializers for ModelContainer, ModelConfiguration, nor ModelContext have a isUndoEnabled param.
  • Perhaps, we can just create our own UndoManager and attach it to our context, but I've tried that in various ways and have yet to get it working.
  1. Observability:
  • Currently, we're calling send(.onAppear) to refresh our data, any time that we ask SwiftData to make a change. But the problem is 1) we have to remember to always call send(.onAppear), and 2) If something else modifies SwiftData (e.g. a sync from CloudKit) then we are unaware and we now have stale data.
  • If we were using vanilla @Query it would handle this for us. I discovered a way to incorporate an @Query into TCA. Here's a PR. I'm still learning the pros/cons of this approach.
  • Also, in a few weeks pointfree.co will release their solution for implementing Observable into TCA. So hopefully that should solve observability.

DandyLyons avatar Nov 08 '23 16:11 DandyLyons