From 06682d89b68239a373716ed8cf69681aff8cbb8f Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sat, 3 Aug 2024 07:45:40 -0400 Subject: [PATCH] Replace WFModel nav state published vars with model.navState for Mac app --- .../Extensions/WriteFreelyModel+APIHandlers.swift | 2 +- Shared/Navigation/ContentView.swift | 13 ++++++++----- Shared/PostEditor/PostEditorStatusToolbarView.swift | 4 ++-- Shared/PostList/PostListFilteredView.swift | 4 ++-- Shared/PostList/PostListView.swift | 8 ++++---- macOS/Navigation/ActivePostToolbarView.swift | 12 ++++++------ macOS/Navigation/HelpCommands.swift | 8 ++++---- macOS/Navigation/PostCommands.swift | 6 +++--- 8 files changed, 30 insertions(+), 27 deletions(-) diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index beb186b..ec1521d 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -242,7 +242,7 @@ extension WriteFreelyModel { DispatchQueue.main.async { LocalStorageManager.standard.saveContext() #if os(macOS) - self.selectedPost = cachedPost + self.navState.selectedPost = cachedPost #endif cachedPost.status = PostStatus.published.rawValue } diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index e67e145..6c26e4f 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -28,14 +28,14 @@ struct ContentView: View { Button(action: { withAnimation { // Un-set the currently selected post - self.model.selectedPost = nil + self.model.navState.selectedPost = nil } // Create the new-post managed object let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) withAnimation { DispatchQueue.main.async { // Load the new post in the editor - self.model.selectedPost = managedPost + self.model.navState.selectedPost = managedPost } } }, label: { Image(systemName: "square.and.pencil") }) @@ -45,9 +45,12 @@ struct ContentView: View { }, postList: { ZStack { - PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts) - .withErrorHandling() - .frame(width: 300) + PostListView( + selectedCollection: model.navState.selectedCollection, + showAllPosts: model.navState.showAllPosts + ) + .withErrorHandling() + .frame(width: 300) if model.isProcessingRequest { ZStack { Color(NSColor.controlBackgroundColor).opacity(0.75) diff --git a/Shared/PostEditor/PostEditorStatusToolbarView.swift b/Shared/PostEditor/PostEditorStatusToolbarView.swift index 8d98614..27b12ad 100644 --- a/Shared/PostEditor/PostEditorStatusToolbarView.swift +++ b/Shared/PostEditor/PostEditorStatusToolbarView.swift @@ -19,7 +19,7 @@ struct PostEditorStatusToolbarView: View { model.editor.postToUpdate = post model.updateFromServer(post: post) DispatchQueue.main.async { - model.selectedPost = nil + model.navState.selectedPost = nil } }, label: { Image(systemName: "square.and.arrow.down") @@ -44,7 +44,7 @@ struct PostEditorStatusToolbarView: View { .font(.callout) .foregroundColor(.secondary) Button(action: { - model.selectedPost = nil + model.navState.selectedPost = nil DispatchQueue.main.async { model.posts.remove(post) } diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index 3af723a..0bbdab4 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -100,7 +100,7 @@ struct PostListFilteredView: View { }, secondaryButton: .destructive(Text("Delete"), action: { if let postToDelete = model.postToDelete { - model.selectedPost = nil + model.navState.selectedPost = nil DispatchQueue.main.async { model.editor.clearLastDraft() model.posts.remove(postToDelete) @@ -111,7 +111,7 @@ struct PostListFilteredView: View { ) } .onDeleteCommand(perform: { - guard let selectedPost = model.selectedPost else { return } + guard let selectedPost = model.navState.selectedPost else { return } if selectedPost.status == PostStatus.local.rawValue { model.postToDelete = selectedPost model.isPresentingDeleteAlert = true diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index d47cafa..e1c4ac6 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -157,8 +157,8 @@ struct PostListView: View { ) .toolbar { ToolbarItemGroup(placement: .primaryAction) { - if model.selectedPost != nil { - ActivePostToolbarView(activePost: model.selectedPost!) + if model.navState.selectedPost != nil { + ActivePostToolbarView(activePost: model.navState.selectedPost!) } } } @@ -168,8 +168,8 @@ struct PostListView: View { ) ) .onAppear { - model.selectedCollection = selectedCollection - model.showAllPosts = showAllPosts + model.navState.selectedCollection = selectedCollection + model.navState.showAllPosts = showAllPosts } .onChange(of: model.hasError) { value in if value { diff --git a/macOS/Navigation/ActivePostToolbarView.swift b/macOS/Navigation/ActivePostToolbarView.swift index 911689d..bc454fe 100644 --- a/macOS/Navigation/ActivePostToolbarView.swift +++ b/macOS/Navigation/ActivePostToolbarView.swift @@ -35,7 +35,7 @@ struct ActivePostToolbarView: View { Button(action: { model.editor.postToUpdate = activePost model.updateFromServer(post: activePost) - model.selectedPost = nil + model.navState.selectedPost = nil }, label: { Image(systemName: "clock.arrow.circlepath") .accessibilityLabel(Text("Revert post")) @@ -75,7 +75,7 @@ struct ActivePostToolbarView: View { }, label: { Label("Publish…", systemImage: "paperplane") }) - .disabled(model.selectedPost?.body.isEmpty ?? true) + .disabled(model.navState.selectedPost?.body.isEmpty ?? true) .help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length } else { HStack(spacing: 4) { @@ -115,12 +115,12 @@ struct ActivePostToolbarView: View { } private func createPostUrl() -> [NSURL] { - guard let postId = model.selectedPost?.postId else { return [] } + guard let postId = model.navState.selectedPost?.postId else { return [] } var urlString: String - if let postSlug = model.selectedPost?.slug, - let postCollectionAlias = model.selectedPost?.collectionAlias { + if let postSlug = model.navState.selectedPost?.slug, + let postCollectionAlias = model.navState.selectedPost?.collectionAlias { // This post is in a collection, so share the URL as baseURL/postSlug let urls = collections.filter { $0.alias == postCollectionAlias } let baseURL = urls.first?.url ?? "\(model.account.server)/\(postCollectionAlias)/" @@ -135,7 +135,7 @@ struct ActivePostToolbarView: View { } private func publishPost(_ post: WFAPost) { - if post != model.selectedPost { + if post != model.navState.selectedPost { return } DispatchQueue.main.async { diff --git a/macOS/Navigation/HelpCommands.swift b/macOS/Navigation/HelpCommands.swift index c465aeb..d2cff07 100644 --- a/macOS/Navigation/HelpCommands.swift +++ b/macOS/Navigation/HelpCommands.swift @@ -22,9 +22,9 @@ struct HelpCommands: Commands { DispatchQueue.main.asyncAfter(deadline: .now()) { // Unset selected post and collection and navigate to local drafts. - self.model.selectedPost = nil - self.model.selectedCollection = nil - self.model.showAllPosts = false + self.model.navState.selectedPost = nil + self.model.navState.selectedCollection = nil + self.model.navState.showAllPosts = false // Create the new log post. let newLogPost = model.editor.generateNewLocalPost(withFont: 2) @@ -39,7 +39,7 @@ struct HelpCommands: Commands { // Hide the spinner in the post list and set the log post as active self.model.isProcessingRequest = false - self.model.selectedPost = newLogPost + self.model.navState.selectedPost = newLogPost logger.log("Generated local log post.") } diff --git a/macOS/Navigation/PostCommands.swift b/macOS/Navigation/PostCommands.swift index 1a2b7b6..0c8bc32 100644 --- a/macOS/Navigation/PostCommands.swift +++ b/macOS/Navigation/PostCommands.swift @@ -17,14 +17,14 @@ struct PostCommands: Commands { Group { Button(action: sendPostUrlToPasteboard, label: { Text("Copy Link To Published Post") }) - .disabled(model.selectedPost?.status == PostStatus.local.rawValue) + .disabled(model.navState.selectedPost?.status == PostStatus.local.rawValue) } - .disabled(model.selectedPost == nil || !model.account.isLoggedIn) + .disabled(model.navState.selectedPost == nil || !model.account.isLoggedIn) } } private func sendPostUrlToPasteboard() { - guard let activePost = model.selectedPost else { return } + guard let activePost = model.navState.selectedPost else { return } guard let postId = activePost.postId else { return } guard let urlString = activePost.slug != nil ? "\(model.account.server)/\((activePost.collectionAlias)!)/\((activePost.slug)!)" :