Use selectedCollection, NavLinks in PostList, split view for iOS/macOS

This commit is contained in:
Angelo Stavrow 2020-08-07 10:13:36 -04:00
parent d4e3cf5f95
commit 9e87bff6a5
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 64 additions and 30 deletions

View File

@ -2,16 +2,12 @@ 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)

View File

@ -5,21 +5,18 @@ struct PostCell: View {
@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)
}

View File

@ -2,18 +2,55 @@ 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()
@ -22,9 +59,10 @@ struct PostList: View {
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 {
@ -45,7 +83,9 @@ struct PostList: View {
struct PostList_Previews: PreviewProvider {
static var previews: some View {
PostList(title: "Posts", posts: testPostData)
.environmentObject(testPostStore)
Group {
PostList(selectedCollection: allPostsCollection)
.environmentObject(testPostStore)
}
}
}

View File

@ -7,7 +7,8 @@ struct CollectionSidebar: 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)
}
}