diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index 1b7fbf4..6848ce6 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -33,7 +33,7 @@ struct PostListFilteredView: View { var body: some View { #if os(iOS) - List { + List(selection: $model.selectedPost) { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), @@ -67,10 +67,12 @@ struct PostListFilteredView: View { self.postCount = value }) .onChange(of: model.selectedPost) { post in - saveSelectedPostURL(post) + if post != fetchSelectedPostFromAppStorage() { + saveSelectedPostURL(post) + } } #else - List { + List(selection: $model.selectedPost) { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), @@ -124,7 +126,9 @@ struct PostListFilteredView: View { } }) .onChange(of: model.selectedPost) { post in - saveSelectedPostURL(post) + if post != fetchSelectedPostFromAppStorage() { + saveSelectedPostURL(post) + } } #endif } @@ -133,6 +137,16 @@ struct PostListFilteredView: View { self.selectedPostURL = post?.objectID.uriRepresentation() } + private func fetchSelectedPostFromAppStorage() -> WFAPost? { + guard let objectURL = selectedPostURL else { return nil } + let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator + guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil } + guard let object = LocalStorageManager.persistentContainer.viewContext.object( + with: managedObjectID + ) as? WFAPost else { return nil } + return object + } + func delete(_ post: WFAPost) { DispatchQueue.main.async { if post == model.selectedPost { diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index 47d610d..9171cf3 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -36,10 +36,8 @@ struct WriteFreely_MultiPlatformApp: App { WindowGroup { ContentView() .onAppear(perform: { - DispatchQueue.main.async { - self.model.showAllPosts = showAllPostsFlag - self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() - } + self.model.showAllPosts = showAllPostsFlag + self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() DispatchQueue.main.asyncAfter(deadline: .now()) { self.model.selectedPost = fetchSelectedPostFromAppStorage() }