Add "All Posts" grouping back to CollectionListView

This commit is contained in:
Angelo Stavrow 2020-09-04 14:53:53 -04:00
parent 0887638841
commit e8301b7eb4
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 22 additions and 26 deletions

View File

@ -7,7 +7,7 @@ struct ContentView: View {
NavigationView { NavigationView {
SidebarView() SidebarView()
PostListView(selectedCollection: nil) PostListView(selectedCollection: nil, showAllPosts: true)
Text("Select a post, or create a new local draft.") Text("Select a post, or create a new local draft.")
.foregroundColor(.secondary) .foregroundColor(.secondary)

View File

@ -11,16 +11,16 @@ struct CollectionListView: View {
var body: some View { var body: some View {
List { List {
// NavigationLink(destination: PostListView(selectedCollection: CollectionListModel.allPostsCollection)) { NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: true)) {
// Text(CollectionListModel.allPostsCollection.title) Text("All Posts")
// } }
NavigationLink(destination: PostListView(selectedCollection: nil)) { NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) {
Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts") Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")
} }
Section(header: Text("Your Blogs")) { Section(header: Text("Your Blogs")) {
ForEach(collections, id: \.alias) { collection in ForEach(collections, id: \.alias) { collection in
NavigationLink( NavigationLink(
destination: PostListView(selectedCollection: collection) destination: PostListView(selectedCollection: collection, showAllPosts: false)
) { ) {
Text(collection.title) Text(collection.title)
} }

View File

@ -3,6 +3,7 @@ import SwiftUI
struct PostListView: View { struct PostListView: View {
@EnvironmentObject var model: WriteFreelyModel @EnvironmentObject var model: WriteFreelyModel
@State var selectedCollection: WFACollection? @State var selectedCollection: WFACollection?
@State var showAllPosts: Bool = false
#if os(iOS) #if os(iOS)
@State private var isPresentingSettings = false @State private var isPresentingSettings = false
@ -24,7 +25,9 @@ struct PostListView: View {
} }
.environmentObject(model) .environmentObject(model)
.navigationTitle( .navigationTitle(
selectedCollection?.title ?? (model.account.server == "https://write.as" ? "Anonymous" : "Drafts") showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
)
) )
.toolbar { .toolbar {
ToolbarItem(placement: .primaryAction) { ToolbarItem(placement: .primaryAction) {
@ -80,7 +83,9 @@ struct PostListView: View {
} }
} }
.navigationTitle( .navigationTitle(
selectedCollection?.title ?? (model.account.server == "https://write.as" ? "Anonymous" : "Drafts") showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
)
) )
.navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection))) .navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection)))
.toolbar { .toolbar {
@ -109,26 +114,17 @@ struct PostListView: View {
} }
private func showPosts(for collection: WFACollection?) -> [Post] { private func showPosts(for collection: WFACollection?) -> [Post] {
var posts: [Post] if showAllPosts {
return model.store.posts
if let selectedCollection = collection {
posts = model.store.posts.filter { $0.wfPost.collectionAlias == selectedCollection.alias }
} else { } else {
posts = model.store.posts.filter { $0.wfPost.collectionAlias == nil } var posts: [Post]
if let selectedCollection = collection {
posts = model.store.posts.filter { $0.wfPost.collectionAlias == selectedCollection.alias }
} else {
posts = model.store.posts.filter { $0.wfPost.collectionAlias == nil }
}
return posts
} }
// for post in model.store.posts {
// print("Post '\(post.wfPost.title ?? "Untitled")' in \(post.collection?.title ?? "No collection")")
// }
// if collection == CollectionListModel.allPostsCollection {
// posts = model.store.posts
// } else if collection == CollectionListModel.draftsCollection {
// posts = model.store.posts.filter { $0.collection == nil }
// } else {
// posts = model.store.posts.filter { $0.collection == collection }
// }
return posts
} }
private func reloadFromServer() { private func reloadFromServer() {