diff --git a/Shared/PostEditor/PostEditorView.swift b/Shared/PostEditor/PostEditorView.swift index e8a18f4..47f1c10 100644 --- a/Shared/PostEditor/PostEditorView.swift +++ b/Shared/PostEditor/PostEditorView.swift @@ -1,74 +1,76 @@ import SwiftUI struct PostEditorView: View { @EnvironmentObject var model: WriteFreelyModel @ObservedObject var post: Post @State private var isNewPost = false - + @State private var title = "" var body: some View { VStack { - TextEditor(text: $post.title) + TextEditor(text: $title) .font(.title) .frame(height: 100) - .onChange(of: post.title) { _ in - if post.status == .published { + .onChange(of: title) { _ in + if post.status == .published && post.wfPost.title != title { post.status = .edited } + post.wfPost.title = title } - TextEditor(text: $post.body) + TextEditor(text: $post.wfPost.body) .font(.body) - .onChange(of: post.body) { _ in + .onChange(of: post.wfPost.body) { _ in if post.status == .published { post.status = .edited } } } .padding() .toolbar { ToolbarItem(placement: .status) { PostStatusBadgeView(post: post) } ToolbarItem(placement: .primaryAction) { Button(action: { model.publish(post: post) post.status = .published }, label: { Image(systemName: "paperplane") }) } } .onAppear(perform: { + title = post.wfPost.title ?? "" checkIfNewPost() if self.isNewPost { addNewPostToStore() } }) } private func checkIfNewPost() { self.isNewPost = !model.store.posts.contains(where: { $0.id == post.id }) } private func addNewPostToStore() { withAnimation { model.store.add(post) self.isNewPost = false } } } struct PostEditorView_NewDraftPreviews: PreviewProvider { static var previews: some View { PostEditorView(post: Post()) .environmentObject(WriteFreelyModel()) } } struct PostEditorView_ExistingPostPreviews: PreviewProvider { static var previews: some View { PostEditorView(post: testPostData[0]) .environmentObject(WriteFreelyModel()) } } diff --git a/Shared/PostList/PostCellView.swift b/Shared/PostList/PostCellView.swift index fa103ed..8bb68cf 100644 --- a/Shared/PostList/PostCellView.swift +++ b/Shared/PostList/PostCellView.swift @@ -1,35 +1,35 @@ import SwiftUI struct PostCellView: View { @ObservedObject var post: Post var body: some View { HStack { VStack(alignment: .leading) { - Text(post.title) + Text(post.wfPost.title ?? "") .font(.headline) .lineLimit(1) - Text(buildDateString(from: post.createdDate)) + Text(buildDateString(from: post.wfPost.createdDate ?? Date())) .font(.caption) .lineLimit(1) } Spacer() PostStatusBadgeView(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 { PostCellView(post: testPost) } }