diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index 79d7047..c835613 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -75,21 +75,6 @@ struct CollectionListView: View { model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" ) .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 if collection != model.editor.fetchSelectedCollectionFromAppStorage() { self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation() diff --git a/Shared/PostEditor/PostEditorModel.swift b/Shared/PostEditor/PostEditorModel.swift index c07c703..014681f 100644 --- a/Shared/PostEditor/PostEditorModel.swift +++ b/Shared/PostEditor/PostEditorModel.swift @@ -10,8 +10,7 @@ enum PostAppearance: String { struct PostEditorModel { @AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false @AppStorage("selectedCollectionURL") var selectedCollectionURL: URL? - @AppStorage("selectedPostURL") var selectedPostURL: URL? - @AppStorage("lastDraftURL") private var lastDraftURL: URL? + @AppStorage("lastDraftURL") var lastDraftURL: URL? func saveLastDraft(_ post: WFAPost) { self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil @@ -21,15 +20,9 @@ struct PostEditorModel { self.lastDraftURL = nil } - func fetchLastDraftFromUserDefaults() -> WFAPost? { + func fetchLastDraftFromAppStorage() -> WFAPost? { guard let postURL = lastDraftURL 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 } - + guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil } return post } @@ -55,22 +48,16 @@ struct PostEditorModel { 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? { let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil } let object = LocalStorageManager.persistentContainer.viewContext.object(with: managedObjectID) 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 - } } diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index dd11691..7826be9 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -61,18 +61,10 @@ struct PostListFilteredView: View { } .onAppear(perform: { 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 self.postCount = value }) - .onChange(of: model.selectedPost) { post in - if post != model.editor.fetchSelectedPostFromAppStorage() { - saveSelectedPostURL(post) - } - } #else List(selection: $model.selectedPost) { ForEach(fetchRequest.wrappedValue, id: \.self) { post in @@ -127,18 +119,9 @@ struct PostListFilteredView: View { model.isPresentingDeleteAlert = true } }) - .onChange(of: model.selectedPost) { post in - if post != fetchSelectedPostFromAppStorage() { - saveSelectedPostURL(post) - } - } #endif } - private func saveSelectedPostURL(_ post: WFAPost?) { - self.model.editor.selectedPostURL = post?.objectID.uriRepresentation() - } - func delete(_ post: WFAPost) { DispatchQueue.main.async { if post == model.selectedPost { diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index 25dfe78..e6aaa58 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -33,7 +33,6 @@ struct WriteFreely_MultiPlatformApp: App { WindowGroup { ContentView() .onAppear(perform: { - #if os(macOS) if model.editor.showAllPostsFlag { DispatchQueue.main.async { self.model.selectedCollection = nil @@ -46,9 +45,12 @@ struct WriteFreely_MultiPlatformApp: App { } } 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) .environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)