diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 3bb8a26..ec5b8f4 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -1,27 +1,31 @@ import SwiftUI struct ContentView: View { @ObservedObject var postStore: PostStore var body: some View { NavigationView { PostList(postStore: postStore) .frame(maxHeight: .infinity) .navigationTitle("Posts") .toolbar { - NavigationLink(destination: PostEditor()) { + NavigationLink( + destination: PostEditor( + post: Post(title: "Title", body: "Write your post here...", createdDate: Date()) + ) + ) { Image(systemName: "plus") } } Text("Select a post, or create a new draft.") .foregroundColor(.secondary) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView(postStore: testPostStore) } } diff --git a/Shared/Post/Post.swift b/Shared/Post/Post.swift index e1bf6e3..52a5e20 100644 --- a/Shared/Post/Post.swift +++ b/Shared/Post/Post.swift @@ -1,53 +1,46 @@ import Foundation import WriteFreely struct Post: Identifiable { var id = UUID() var title: String var body: String var createdDate: Date var status: PostStatus = .draft - var editableText: String { - return """ - # \(self.title) - - \(self.body) - """ - } } let testPost = Post( title: "Test Post Title", body: """ Here's some cool sample body text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultrices \ posuere dignissim. Vestibulum a libero tempor, lacinia nulla vitae, congue purus. Nunc ac nulla quam. Duis \ tincidunt eros augue, et volutpat tortor pulvinar ut. Nullam sit amet maximus urna. Phasellus non dignissim lacus.\ Nulla ac posuere ex. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec \ non molestie mauris. Suspendisse potenti. Vivamus at erat turpis. Pellentesque porttitor gravida tincidunt. Sed vitae eros non metus aliquam hendrerit. Aliquam sed risus suscipit \ turpis dictum dictum. Duis lacus lectus, dictum vel felis in, rhoncus fringilla felis. Nunc id dolor nisl. Aliquam \ euismod purus elit. Nullam egestas neque leo, sed aliquet ligula ultrices nec. """, createdDate: Date(), status: .published) let testPostData = [ Post( title: "My First Post", body: "Look at me, creating a first post! That's cool.", createdDate: Date(timeIntervalSince1970: 1595429452), status: .published ), Post( title: "Post 2: The Quickening", body: "See, here's the rule about Highlander jokes: _there can be only one_.", createdDate: Date(timeIntervalSince1970: 1595514125), status: .edited ), Post( title: "The Post Revolutions", body: "I can never keep the Matrix movie order straight. Why not just call them part 2 and part 3?", createdDate: Date(timeIntervalSince1970: 1595600006) ) ] diff --git a/Shared/Post/PostCell.swift b/Shared/Post/PostCell.swift index 1a2a0cd..19d49e6 100644 --- a/Shared/Post/PostCell.swift +++ b/Shared/Post/PostCell.swift @@ -1,41 +1,38 @@ import SwiftUI struct PostCell: View { var post: Post var body: some View { NavigationLink( - destination: PostEditor( - textString: post.editableText, - postStatus: post.status - ) + destination: PostEditor(post: post) ) { HStack { VStack(alignment: .leading) { Text(post.title) .font(.headline) .lineLimit(1) Text(buildDateString(from: post.createdDate)) .font(.caption) .foregroundColor(.secondary) .lineLimit(1) } Spacer() PostStatusBadge(postStatus: post.status) } } } 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/PostEditor.swift b/Shared/Post/PostEditor.swift index b47dc99..6199196 100644 --- a/Shared/Post/PostEditor.swift +++ b/Shared/Post/PostEditor.swift @@ -1,41 +1,48 @@ // // PostEditor.swift // WriteFreely-MultiPlatform // // Created by Angelo Stavrow on 2020-07-24. // import SwiftUI struct PostEditor: View { - @State var textString: String = "" + @State var post: Post @State private var hasUnpublishedChanges: Bool = false - var postStatus: PostStatus = .draft var body: some View { - TextEditor(text: $textString.animation()) - .font(.body) - .padding() - .onChange(of: textString) { _ in - if postStatus == .published { - hasUnpublishedChanges = true + VStack { + TextField(post.title, text: $post.title) + .font(.title) + .multilineTextAlignment(.center) + .padding(.bottom) + .onChange(of: post.title) { _ in + if post.status == .published { + hasUnpublishedChanges = true + } } - } - .toolbar { - if hasUnpublishedChanges { - PostStatusBadge(postStatus: .edited) - } else { - PostStatusBadge(postStatus: postStatus) + TextEditor(text: $post.body) + .font(.body) + .padding(.leading) + .onChange(of: post.body) { _ in + if post.status == .published { + hasUnpublishedChanges = true + } } - } + .toolbar { + if hasUnpublishedChanges { + PostStatusBadge(postStatus: .edited) + } else { + PostStatusBadge(postStatus: post.status) + } + } + } } } struct PostEditor_Previews: PreviewProvider { static var previews: some View { - PostEditor( - textString: testPost.editableText, - postStatus: testPost.status - ) + PostEditor(post: testPost) } }