Skip to content

Conversation

AngeloStavrow
Copy link
Collaborator

Closes #16.

This is A Big One™ —local storage for your user collections and posts— which gets us much closer to a usable client, and it touches nearly every file in the app (plus adds a few new ones).

I'm a model, you know what I mean

We start by replacing the Post and PostCollection custom types with WFAPost and WFACollection managed objects — CoreData model objects for posts and collections, respectively. These are created in LocalStorage.xcdatamodeld and the extension classes are generated manually (Editor > Create NSManagedObject Subclass...).

Because NSManagedObjects conform to ObservableObject, we can use these directly in our SwiftUI views, and rather than maintaining an in-memory store (e.g., PostStore's posts array), use FetchedResults to populate (and update) our lists of collections and posts.

I insist we persist

We continue by adding a shared, top-level LocalStorageManager. This instantiates and initializes our persistent container as a (static) class property, adds an observer for notifications sent when the app resigns its active state, and adds a helper method for saving changes to the managed object context.

1-800-RIP-IT-OUT

We finally go through the codebase, replacing anything related to the Post and PostCollection types with WFAPost and WFACollections. There are two main concerns here:

  1. Maintaining state through the views: Rather than passing in State / Binding objects, we can —when necessary— tap into the managedObjectContext Environment property wrapper in our views, which lets us create, for example, FetchRequests for populating lists from FetchedResults. As a result, we don't need a PostStore type anymore, so this gets converted into a PostListModel type.
  2. Translation in the WriteFreelyModel: Previously, the Post type contained a WFPost property — a custom type from the WriteFreely Swift package, which handles requests to the WriteFreely API; without this, the WriteFreelyModel public API must create a WFPost object from the (local) WFAPost's property to hand over to the Swift package.

@AngeloStavrow AngeloStavrow added this to the 1.0.0-beta milestone Sep 9, 2020
@AngeloStavrow AngeloStavrow self-assigned this Sep 9, 2020
Move sheet modifier for SettingsView outside of NavigationView
@AngeloStavrow AngeloStavrow merged commit 353c9e6 into main Sep 11, 2020
@AngeloStavrow AngeloStavrow deleted the persist-content-locally branch September 11, 2020 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

The PostStore/CollectionListModel must be persisted locally
2 participants