From 64dfdd63ebcf38cc64c9da8679a68679175fc4fb Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Sat, 3 Aug 2024 07:29:10 -0400 Subject: [PATCH] Replace WFModel nav state published vars with model.navState --- Shared/Extensions/WriteFreelyModel+API.swift | 4 ++-- .../WriteFreelyModel+APIHandlers.swift | 8 +++---- Shared/Navigation/ContentView.swift | 7 ++++-- .../PostCollection/CollectionListView.swift | 10 ++++---- Shared/PostEditor/PostEditorModel.swift | 2 +- Shared/PostList/DeprecatedListView.swift | 6 ++--- Shared/PostList/PostCellView.swift | 4 ++-- Shared/PostList/PostListFilteredView.swift | 10 ++++---- Shared/PostList/PostListView.swift | 8 +++---- .../SearchablePostListFilteredView.swift | 2 +- Shared/WriteFreely_MultiPlatformApp.swift | 23 ++++++++++--------- iOS/Settings/SettingsView.swift | 6 ++--- 12 files changed, 47 insertions(+), 43 deletions(-) diff --git a/Shared/Extensions/WriteFreelyModel+API.swift b/Shared/Extensions/WriteFreelyModel+API.swift index 1a574ab..47ec63e 100644 --- a/Shared/Extensions/WriteFreelyModel+API.swift +++ b/Shared/Extensions/WriteFreelyModel+API.swift @@ -150,7 +150,7 @@ extension WriteFreelyModel { // We're starting the network request. DispatchQueue.main.async { #if os(iOS) - self.selectedPost = post + self.navState.selectedPost = post #endif self.isProcessingRequest = true } @@ -175,7 +175,7 @@ extension WriteFreelyModel { self.isProcessingRequest = true } - selectedPost = post + navState.selectedPost = post post.collectionAlias = newCollection?.alias loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler) } diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index 8698977..beb186b 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -140,8 +140,8 @@ extension WriteFreelyModel { if managedPost.collectionAlias != fetchedPost.collectionAlias { // The post has been moved so we update the managed post's collectionAlias property. DispatchQueue.main.async { - if self.selectedPost == managedPost { - self.selectedPost = nil + if self.navState.selectedPost == managedPost { + self.navState.selectedPost = nil } managedPost.collectionAlias = fetchedPost.collectionAlias } @@ -233,7 +233,7 @@ extension WriteFreelyModel { do { let fetchedPost = try result.get() #if os(iOS) - guard let cachedPost = self.selectedPost else { return } + guard let cachedPost = self.navState.selectedPost else { return } #else guard let cachedPost = self.editor.postToUpdate else { return } #endif @@ -259,7 +259,7 @@ extension WriteFreelyModel { do { let succeeded = try result.get() if succeeded { - if let post = selectedPost { + if let post = navState.selectedPost { updateFromServer(post: post) } else { return diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 5b9d89a..e67e145 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -78,8 +78,11 @@ struct ContentView: View { .withErrorHandling() }, postList: { - PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts) - .withErrorHandling() + PostListView( + selectedCollection: model.navState.selectedCollection, + showAllPosts: model.navState.showAllPosts + ) + .withErrorHandling() }, postDetail: { NoSelectedPostView(isConnected: $model.hasNetworkConnection) diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index 2c3dab7..a5f9300 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -32,16 +32,16 @@ struct CollectionListView: View { model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" ) .listStyle(SidebarListStyle()) - .onChange(of: model.selectedCollection) { collection in - model.selectedPost = nil + .onChange(of: model.navState.selectedCollection) { collection in + model.navState.selectedPost = nil if collection != model.editor.fetchSelectedCollectionFromAppStorage() { self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation() } } - .onChange(of: model.showAllPosts) { value in - model.selectedPost = nil + .onChange(of: model.navState.showAllPosts) { value in + model.navState.selectedPost = nil if value != model.editor.showAllPostsFlag { - self.model.editor.showAllPostsFlag = model.showAllPosts + self.model.editor.showAllPostsFlag = model.navState.showAllPosts } } .onChange(of: model.hasError) { value in diff --git a/Shared/PostEditor/PostEditorModel.swift b/Shared/PostEditor/PostEditorModel.swift index c6fda4c..e961b51 100644 --- a/Shared/PostEditor/PostEditorModel.swift +++ b/Shared/PostEditor/PostEditorModel.swift @@ -39,7 +39,7 @@ struct PostEditorModel { managedPost.title = "" managedPost.body = "" managedPost.status = PostStatus.local.rawValue - managedPost.collectionAlias = WriteFreelyModel.shared.selectedCollection?.alias + managedPost.collectionAlias = WriteFreelyModel.shared.navState.selectedCollection?.alias switch appearance { case 1: managedPost.appearance = "sans" diff --git a/Shared/PostList/DeprecatedListView.swift b/Shared/PostList/DeprecatedListView.swift index f2a6e5a..1c4fb10 100644 --- a/Shared/PostList/DeprecatedListView.swift +++ b/Shared/PostList/DeprecatedListView.swift @@ -10,7 +10,7 @@ struct DeprecatedListView: View { var onDelete: (WFAPost) -> Void var body: some View { - List(selection: $model.selectedPost) { + List(selection: $model.navState.selectedPost) { ForEach(fetchRequest.wrappedValue, id: \.self) { post in if !searchString.isEmpty && !post.title.localizedCaseInsensitiveContains(searchString) && @@ -20,9 +20,9 @@ struct DeprecatedListView: View { NavigationLink( destination: PostEditorView(post: post), tag: post, - selection: $model.selectedPost, + selection: $model.navState.selectedPost, label: { - if model.showAllPosts { + if model.navState.showAllPosts { if let collection = collections.filter({ $0.alias == post.collectionAlias }).first { PostCellView(post: post, collectionName: collection.title) } else { diff --git a/Shared/PostList/PostCellView.swift b/Shared/PostList/PostCellView.swift index 9f3a622..acbb49f 100644 --- a/Shared/PostList/PostCellView.swift +++ b/Shared/PostList/PostCellView.swift @@ -52,8 +52,8 @@ struct PostCellView: View { private func didTapDeleteContextMenuItem() { guard post.status == PostStatus.local.rawValue else { return } - if post === model.selectedPost { - model.selectedPost = nil + if post === model.navState.selectedPost { + model.navState.selectedPost = nil model.editor.clearLastDraft() } diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index 24b2453..3af723a 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -48,14 +48,14 @@ struct PostListFilteredView: View { self.postCount = value }) } else { - List(selection: $model.selectedPost) { + List(selection: $model.navState.selectedPost) { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), tag: post, - selection: $model.selectedPost, + selection: $model.navState.selectedPost, label: { - if model.showAllPosts { + if model.navState.showAllPosts { if let collection = collections.filter({ $0.alias == post.collectionAlias }).first { PostCellView(post: post, collectionName: collection.title) } else { @@ -122,8 +122,8 @@ struct PostListFilteredView: View { func delete(_ post: WFAPost) { DispatchQueue.main.async { - if post == model.selectedPost { - model.selectedPost = nil + if post == model.navState.selectedPost { + model.navState.selectedPost = nil model.editor.clearLastDraft() } model.posts.remove(post) diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index 06b4714..d47cafa 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -46,9 +46,9 @@ struct PostListView: View { Button(action: { let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) withAnimation { - self.model.showAllPosts = false + self.model.navState.showAllPosts = false DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - self.model.selectedPost = managedPost + self.model.navState.selectedPost = managedPost } } }, label: { @@ -130,8 +130,8 @@ struct PostListView: View { .ignoresSafeArea(.all, edges: .bottom) .onAppear { // Set the selected collection and whether or not we want to show all posts - model.selectedCollection = selectedCollection - model.showAllPosts = showAllPosts + model.navState.selectedCollection = selectedCollection + model.navState.showAllPosts = showAllPosts // We use this to invalidate and refresh the view, so that new posts created outside of the app (e.g., // in the action extension) show up. diff --git a/Shared/PostList/SearchablePostListFilteredView.swift b/Shared/PostList/SearchablePostListFilteredView.swift index 5279fea..8c79d5e 100644 --- a/Shared/PostList/SearchablePostListFilteredView.swift +++ b/Shared/PostList/SearchablePostListFilteredView.swift @@ -16,7 +16,7 @@ struct SearchablePostListFilteredView: View { var body: some View { if #available(iOS 16, macOS 13, *) { NavigationStack { - List(fetchRequest.wrappedValue, id: \.self, selection: $model.selectedPost) { post in + List(fetchRequest.wrappedValue, id: \.self, selection: $model.navState.selectedPost) { post in NavigationLink( "\(post.title.isEmpty ? "UNTITLED" : post.title)", destination: PostEditorView(post: post) diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index b121314..fae66c4 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -40,14 +40,15 @@ struct WriteFreely_MultiPlatformApp: App { .onAppear(perform: { if model.editor.showAllPostsFlag { DispatchQueue.main.async { - self.model.selectedCollection = nil - self.model.showAllPosts = true + self.model.navState.selectedCollection = nil + self.model.navState.showAllPosts = true showLastDraftOrCreateNewLocalPost() } } else { DispatchQueue.main.async { - self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage() - self.model.showAllPosts = false + self.model.navState.selectedCollection = + model.editor.fetchSelectedCollectionFromAppStorage() + self.model.navState.showAllPosts = false showLastDraftOrCreateNewLocalPost() } } @@ -146,7 +147,7 @@ struct WriteFreely_MultiPlatformApp: App { private func showLastDraftOrCreateNewLocalPost() { if model.editor.lastDraftURL != nil { DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { - self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage() + self.model.navState.selectedPost = model.editor.fetchLastDraftFromAppStorage() } } else { createNewLocalPost() @@ -156,14 +157,14 @@ struct WriteFreely_MultiPlatformApp: App { private func createNewLocalPost() { 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 { // Set it as the selectedPost DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { - self.model.selectedPost = managedPost + self.model.navState.selectedPost = managedPost } } } @@ -174,9 +175,9 @@ struct WriteFreely_MultiPlatformApp: App { 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) @@ -189,7 +190,7 @@ struct WriteFreely_MultiPlatformApp: App { postBody.append(contentsOf: logger.fetchLogs()) newLogPost.body = postBody.joined(separator: "\n") - self.model.selectedPost = newLogPost + self.model.navState.selectedPost = newLogPost } logger.log("Generated local log post.") diff --git a/iOS/Settings/SettingsView.swift b/iOS/Settings/SettingsView.swift index cfd0211..e75b2d2 100644 --- a/iOS/Settings/SettingsView.swift +++ b/iOS/Settings/SettingsView.swift @@ -75,9 +75,9 @@ struct SettingsView: View { 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)