From 7f920e54dcbd93658f4540a84eecf60db3b931ec Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Tue, 28 Jul 2020 11:17:05 -0400 Subject: [PATCH] Pass Post into PostEditor, split title/body into TextField/TextEditor --- Shared/Navigation/ContentView.swift | 6 +++- Shared/Post/Post.swift | 7 ----- Shared/Post/PostCell.swift | 5 +--- Shared/Post/PostEditor.swift | 45 +++++++++++++++++------------ 4 files changed, 32 insertions(+), 31 deletions(-) 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 @@ -9,7 +9,11 @@ struct ContentView: View { .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") } } 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 @@ -7,13 +7,6 @@ struct Post: Identifiable { var body: String var createdDate: Date var status: PostStatus = .draft - var editableText: String { - return """ - # \(self.title) - - \(self.body) - """ - } } let testPost = Post( 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 @@ -4,10 +4,7 @@ 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) { 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 @@ -8,34 +8,41 @@ 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) } }