diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 04223a3..9fb9599 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -1,27 +1,23 @@ import SwiftUI struct ContentView: View { @ObservedObject var postStore: PostStore - @State private var selectedCollection: PostCollection? = allPostsCollection var body: some View { NavigationView { CollectionSidebar() - PostList( - title: selectedCollection?.title ?? allPostsCollection.title, - posts: showPosts(for: selectedCollection ?? allPostsCollection) - ) + PostList(selectedCollection: allPostsCollection) Text("Select a post, or create a new draft.") .foregroundColor(.secondary) } .environmentObject(postStore) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView(postStore: testPostStore) } } diff --git a/Shared/Post/PostCell.swift b/Shared/Post/PostCell.swift index bacce53..36d17d0 100644 --- a/Shared/Post/PostCell.swift +++ b/Shared/Post/PostCell.swift @@ -1,39 +1,36 @@ import SwiftUI struct PostCell: View { @EnvironmentObject var postStore: PostStore @ObservedObject var post: Post var body: some View { - NavigationLink( - destination: PostEditor(post: post) - ) { - HStack { - VStack(alignment: .leading) { - Text(post.title) - .font(.headline) - .lineLimit(1) - Text(buildDateString(from: post.createdDate)) - .font(.caption) - .lineLimit(1) - } - Spacer() - PostStatusBadge(post: post) + HStack { + VStack(alignment: .leading) { + Text(post.title) + .font(.headline) + .lineLimit(1) + Text(buildDateString(from: post.createdDate)) + .font(.caption) + .lineLimit(1) } + Spacer() + PostStatusBadge(post: post) + } .padding(5) } func buildDateString(from date: Date) -> String { let dateFormatter = DateFormatter() dateFormatter.dateStyle = .long dateFormatter.timeStyle = .short return dateFormatter.string(from: date) } } struct PostCell_Previews: PreviewProvider { static var previews: some View { PostCell(post: testPost) } } diff --git a/Shared/Post/PostList.swift b/Shared/Post/PostList.swift index d805cb6..8577636 100644 --- a/Shared/Post/PostList.swift +++ b/Shared/Post/PostList.swift @@ -1,51 +1,91 @@ import SwiftUI struct PostList: View { @EnvironmentObject var postStore: PostStore - var title: String - var posts: [Post] + @State var selectedCollection: PostCollection var body: some View { + #if os(iOS) List { - Text(pluralizedPostCount(for: posts)) - .foregroundColor(.secondary) - ForEach(posts) { post in - PostCell(post: post) + ForEach(showPosts(for: selectedCollection)) { post in + NavigationLink( + destination: PostEditor(post: post) + ) { + PostCell( + post: post + ) + } } } - .navigationTitle(title) + .navigationTitle(selectedCollection.title) + .toolbar { + ToolbarItem(placement: .primaryAction) { + Button(action: { + let post = Post() + postStore.add(post) + }, label: { + Image(systemName: "square.and.pencil") + }) + } + ToolbarItem(placement: .bottomBar) { + Spacer() + } + ToolbarItem(placement: .bottomBar) { + Text(pluralizedPostCount(for: showPosts(for: selectedCollection))) + } + ToolbarItem(placement: .bottomBar) { + Spacer() + } + } + #else //if os(macOS) + List { + ForEach(showPosts(for: selectedCollection)) { post in + NavigationLink( + destination: PostEditor(post: post) + ) { + PostCell( + post: post + ) + } + } + } + .navigationTitle(selectedCollection.title) + .navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection))) .toolbar { Button(action: { let post = Post() postStore.add(post) }, label: { Image(systemName: "square.and.pencil") }) } + #endif } - func pluralizedPostCount(for posts: [Post]) -> String { + private 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) + Group { + PostList(selectedCollection: allPostsCollection) + .environmentObject(testPostStore) + } } } diff --git a/Shared/PostCollection/CollectionSidebar.swift b/Shared/PostCollection/CollectionSidebar.swift index e3808d7..2e0f997 100644 --- a/Shared/PostCollection/CollectionSidebar.swift +++ b/Shared/PostCollection/CollectionSidebar.swift @@ -1,22 +1,23 @@ import SwiftUI struct CollectionSidebar: View { private let collections = postCollections var body: some View { List { ForEach(collections) { collection in NavigationLink( - destination: PostList(title: collection.title, posts: showPosts(for: collection)).tag(collection)) { + destination: PostList(selectedCollection: collection) + ) { Text(collection.title) } } } .listStyle(SidebarListStyle()) } struct CollectionSidebar_Previews: PreviewProvider { static var previews: some View { CollectionSidebar() } }