From a0b12184732bde9b2903de15195b9aeadbde14ec Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Fri, 4 Dec 2020 17:08:32 -0500 Subject: [PATCH 1/2] Use published properties for selectedCollection and showAllPosts --- Shared/Models/WriteFreelyModel.swift | 2 + Shared/Navigation/ContentView.swift | 20 +--------- Shared/PostList/PostListFilteredView.swift | 28 ++++++++----- Shared/PostList/PostListView.swift | 40 +++++++++++++++---- .../xcschemes/xcschememanagement.plist | 4 +- 5 files changed, 55 insertions(+), 39 deletions(-) diff --git a/Shared/Models/WriteFreelyModel.swift b/Shared/Models/WriteFreelyModel.swift index 525f4ee..b53da10 100644 --- a/Shared/Models/WriteFreelyModel.swift +++ b/Shared/Models/WriteFreelyModel.swift @@ -14,6 +14,8 @@ class WriteFreelyModel: ObservableObject { @Published var isProcessingRequest: Bool = false @Published var hasNetworkConnection: Bool = true @Published var selectedPost: WFAPost? + @Published var selectedCollection: WFACollection? + @Published var showAllPosts: Bool = true @Published var isPresentingDeleteAlert: Bool = false @Published var isPresentingLoginErrorAlert: Bool = false @Published var isPresentingNetworkErrorAlert: Bool = false diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index ce8f253..42b036f 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -51,25 +51,7 @@ struct ContentView: View { #endif #if os(macOS) - PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn) - .toolbar { - ToolbarItemGroup(placement: .primaryAction) { - if let selectedPost = model.selectedPost { - ActivePostToolbarView(activePost: selectedPost) - .alert(isPresented: $model.isPresentingNetworkErrorAlert, content: { - Alert( - title: Text("Connection Error"), - message: Text(""" - There is no internet connection at the moment. Please reconnect or try again later. - """), - dismissButton: .default(Text("OK"), action: { - model.isPresentingNetworkErrorAlert = false - }) - ) - }) - } - } - } + PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts) #else PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn) #endif diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index cc65779..d528176 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -5,21 +5,33 @@ struct PostListFilteredView: View { @Binding var postCount: Int @FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults var fetchRequest: FetchRequest - var showAllPosts: Bool - init(filter: String?, showAllPosts: Bool, postCount: Binding) { + var showAllPosts: Bool { + didSet { + model.showAllPosts = showAllPosts + } + } + + var selectedCollection: WFACollection? { + didSet { + model.selectedCollection = selectedCollection + } + } + + init(collection: WFACollection?, showAllPosts: Bool, postCount: Binding) { self.showAllPosts = showAllPosts + self.selectedCollection = collection if showAllPosts { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)] ) } else { - if let filter = filter { + if let collectionAlias = collection?.alias { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], - predicate: NSPredicate(format: "collectionAlias == %@", filter) + predicate: NSPredicate(format: "collectionAlias == %@", collectionAlias) ) } else { fetchRequest = FetchRequest( @@ -105,12 +117,6 @@ struct PostListFilteredView: View { }) ) } - .onAppear(perform: { - self.postCount = fetchRequest.wrappedValue.count - }) - .onChange(of: fetchRequest.wrappedValue.count, perform: { value in - self.postCount = value - }) .onDeleteCommand(perform: { guard let selectedPost = model.selectedPost else { return } if selectedPost.status == PostStatus.local.rawValue { @@ -134,6 +140,6 @@ struct PostListFilteredView: View { struct PostListFilteredView_Previews: PreviewProvider { static var previews: some View { - return PostListFilteredView(filter: nil, showAllPosts: false, postCount: .constant(999)) + return PostListFilteredView(collection: nil, showAllPosts: false, postCount: .constant(999)) } } diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index ffc545b..acd1fbe 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -12,7 +12,7 @@ struct PostListView: View { var body: some View { #if os(iOS) GeometryReader { geometry in - PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount) + PostListFilteredView(collection: selectedCollection, showAllPosts: showAllPosts, postCount: $postCount) .navigationTitle( showAllPosts ? "All Posts" : selectedCollection?.title ?? ( model.account.server == "https://write.as" ? "Anonymous" : "Drafts" @@ -79,13 +79,39 @@ struct PostListView: View { } } #else //if os(macOS) - PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount) - .navigationTitle( - showAllPosts ? "All Posts" : selectedCollection?.title ?? ( - model.account.server == "https://write.as" ? "Anonymous" : "Drafts" - ) + PostListFilteredView( + collection: selectedCollection, + showAllPosts: showAllPosts, + postCount: $postCount + ) + .toolbar { + ToolbarItemGroup(placement: .primaryAction) { + if let selectedPost = model.selectedPost { + ActivePostToolbarView(activePost: selectedPost) + .alert(isPresented: $model.isPresentingNetworkErrorAlert, content: { + Alert( + title: Text("Connection Error"), + message: Text(""" + There is no internet connection at the moment. Please reconnect or try again later. + """), + dismissButton: .default(Text("OK"), action: { + model.isPresentingNetworkErrorAlert = false + }) + ) + }) + } + } + } + .onDisappear { + DispatchQueue.main.async { + model.selectedPost = nil + } + } + .navigationTitle( + showAllPosts ? "All Posts" : selectedCollection?.title ?? ( + model.account.server == "https://write.as" ? "Anonymous" : "Drafts" ) - .navigationSubtitle(postCount == 1 ? "\(postCount) post" : "\(postCount) posts") + ) #endif } } diff --git a/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist b/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist index 6cd8075..2723ebe 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_ orderHint - 1 + 0 WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_ orderHint - 0 + 1 From 709a654528cf0022caa80d42a39ea7511cd8876b Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Mon, 7 Dec 2020 12:03:45 -0500 Subject: [PATCH 2/2] Reset selectedCollection/showAllPosts when dismissing PostListView --- Shared/PostList/PostListView.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index acd1fbe..5413e88 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -104,6 +104,8 @@ struct PostListView: View { } .onDisappear { DispatchQueue.main.async { + model.selectedCollection = nil + model.showAllPosts = true model.selectedPost = nil } }