Use same code path to set iOS and Mac app state on launch

This commit is contained in:
Angelo Stavrow 2021-01-26 09:23:47 -05:00
parent 34b14ba46c
commit 900ef269ba
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 14 additions and 57 deletions

View File

@ -75,21 +75,6 @@ struct CollectionListView: View {
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
) )
.listStyle(SidebarListStyle()) .listStyle(SidebarListStyle())
.onAppear(perform: {
#if os(iOS)
if model.editor.showAllPostsFlag {
DispatchQueue.main.async {
self.model.selectedCollection = nil
self.model.showAllPosts = true
}
} else {
DispatchQueue.main.async {
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage()
self.model.showAllPosts = false
}
}
#endif
})
.onChange(of: model.selectedCollection) { collection in .onChange(of: model.selectedCollection) { collection in
if collection != model.editor.fetchSelectedCollectionFromAppStorage() { if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation() self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()

View File

@ -10,8 +10,7 @@ enum PostAppearance: String {
struct PostEditorModel { struct PostEditorModel {
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false @AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL? @AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
@AppStorage("selectedPostURL") var selectedPostURL: URL? @AppStorage("lastDraftURL") var lastDraftURL: URL?
@AppStorage("lastDraftURL") private var lastDraftURL: URL?
func saveLastDraft(_ post: WFAPost) { func saveLastDraft(_ post: WFAPost) {
self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil
@ -21,15 +20,9 @@ struct PostEditorModel {
self.lastDraftURL = nil self.lastDraftURL = nil
} }
func fetchLastDraftFromUserDefaults() -> WFAPost? { func fetchLastDraftFromAppStorage() -> WFAPost? {
guard let postURL = lastDraftURL else { return nil } guard let postURL = lastDraftURL else { return nil }
guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil }
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
guard let postManagedObjectID = coordinator.managedObjectID(forURIRepresentation: postURL) else { return nil }
guard let post = LocalStorageManager.persistentContainer.viewContext.object(
with: postManagedObjectID
) as? WFAPost else { return nil }
return post return post
} }
@ -55,22 +48,16 @@ struct PostEditorModel {
return managedPost return managedPost
} }
func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
guard let collectionURL = selectedCollectionURL else { return nil }
guard let collection = fetchManagedObject(from: collectionURL) as? WFACollection else { return nil }
return collection
}
private func fetchManagedObject(from objectURL: URL) -> NSManagedObject? { private func fetchManagedObject(from objectURL: URL) -> NSManagedObject? {
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil } guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
let object = LocalStorageManager.persistentContainer.viewContext.object(with: managedObjectID) let object = LocalStorageManager.persistentContainer.viewContext.object(with: managedObjectID)
return object return object
} }
func fetchSelectedPostFromAppStorage() -> WFAPost? {
guard let postURL = selectedPostURL else { return nil }
guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil }
return post
}
func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
guard let collectionURL = selectedCollectionURL else { return nil }
guard let collection = fetchManagedObject(from: collectionURL) as? WFACollection else { return nil }
return collection
}
} }

View File

@ -61,18 +61,10 @@ struct PostListFilteredView: View {
} }
.onAppear(perform: { .onAppear(perform: {
self.postCount = fetchRequest.wrappedValue.count self.postCount = fetchRequest.wrappedValue.count
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage()
}
}) })
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in .onChange(of: fetchRequest.wrappedValue.count, perform: { value in
self.postCount = value self.postCount = value
}) })
.onChange(of: model.selectedPost) { post in
if post != model.editor.fetchSelectedPostFromAppStorage() {
saveSelectedPostURL(post)
}
}
#else #else
List(selection: $model.selectedPost) { List(selection: $model.selectedPost) {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in ForEach(fetchRequest.wrappedValue, id: \.self) { post in
@ -127,18 +119,9 @@ struct PostListFilteredView: View {
model.isPresentingDeleteAlert = true model.isPresentingDeleteAlert = true
} }
}) })
.onChange(of: model.selectedPost) { post in
if post != fetchSelectedPostFromAppStorage() {
saveSelectedPostURL(post)
}
}
#endif #endif
} }
private func saveSelectedPostURL(_ post: WFAPost?) {
self.model.editor.selectedPostURL = post?.objectID.uriRepresentation()
}
func delete(_ post: WFAPost) { func delete(_ post: WFAPost) {
DispatchQueue.main.async { DispatchQueue.main.async {
if post == model.selectedPost { if post == model.selectedPost {

View File

@ -33,7 +33,6 @@ struct WriteFreely_MultiPlatformApp: App {
WindowGroup { WindowGroup {
ContentView() ContentView()
.onAppear(perform: { .onAppear(perform: {
#if os(macOS)
if model.editor.showAllPostsFlag { if model.editor.showAllPostsFlag {
DispatchQueue.main.async { DispatchQueue.main.async {
self.model.selectedCollection = nil self.model.selectedCollection = nil
@ -46,9 +45,12 @@ struct WriteFreely_MultiPlatformApp: App {
} }
} }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage() if model.editor.lastDraftURL != nil {
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
} else {
createNewLocalPost()
}
} }
#endif
}) })
.environmentObject(model) .environmentObject(model)
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext) .environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)