diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 46f1b3b..aff477e 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -1,37 +1,27 @@ import SwiftUI struct ContentView: View { @ObservedObject var postStore: PostStore @State private var selectedCollection: PostCollection? = allPostsCollection var body: some View { NavigationView { CollectionSidebar(selectedCollection: $selectedCollection) PostList( title: selectedCollection?.title ?? allPostsCollection.title, posts: showPosts(for: selectedCollection ?? allPostsCollection) ) Text("Select a post, or create a new draft.") .foregroundColor(.secondary) } .environmentObject(postStore) } - - func showPosts(for collection: PostCollection) -> [Post] { - if collection == allPostsCollection { - return postStore.posts - } else { - return postStore.posts.filter { - $0.collection.title == collection.title - } - } - } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView(postStore: testPostStore) } } diff --git a/Shared/Post/PostList.swift b/Shared/Post/PostList.swift index 97559f0..d805cb6 100644 --- a/Shared/Post/PostList.swift +++ b/Shared/Post/PostList.swift @@ -1,41 +1,51 @@ import SwiftUI struct PostList: View { @EnvironmentObject var postStore: PostStore var title: String var posts: [Post] var body: some View { List { Text(pluralizedPostCount(for: posts)) .foregroundColor(.secondary) ForEach(posts) { post in PostCell(post: post) } } .navigationTitle(title) .toolbar { Button(action: { let post = Post() postStore.add(post) }, label: { Image(systemName: "square.and.pencil") }) } } func pluralizedPostCount(for posts: [Post]) -> String { if posts.count == 1 { return "1 post" } else { return "\(posts.count) posts" } } + + private func showPosts(for collection: PostCollection) -> [Post] { + if collection == allPostsCollection { + return postStore.posts + } else { + return postStore.posts.filter { + $0.collection.title == collection.title + } + } + } } struct PostList_Previews: PreviewProvider { static var previews: some View { PostList(title: "Posts", posts: testPostData) .environmentObject(testPostStore) } }