From 4b4608709ee8c36eff91822ded2c8004acd259e1 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Tue, 28 Jul 2020 12:45:11 -0400 Subject: [PATCH] Make Post object conform to ObservableObject --- Shared/Post/Post.swift | 25 +++++++++++++++++++------ Shared/Post/PostEditor.swift | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Shared/Post/Post.swift b/Shared/Post/Post.swift index 53dc52c..289b7bf 100644 --- a/Shared/Post/Post.swift +++ b/Shared/Post/Post.swift @@ -1,12 +1,25 @@ import Foundation import WriteFreely -struct Post: Identifiable { - var id = UUID() - var title: String = "Title" - var body: String = "Write your post here..." - var createdDate: Date = Date() - var status: PostStatus = .draft +class Post: Identifiable, ObservableObject { + @Published var title: String + @Published var body: String + @Published var createdDate: Date + @Published var status: PostStatus + + let id = UUID() + + init( + title: String = "Title", + body: String = "Write your post here...", + createdDate: Date = Date(), + status: PostStatus = .draft + ) { + self.title = title + self.body = body + self.createdDate = createdDate + self.status = status + } } let testPost = Post( diff --git a/Shared/Post/PostEditor.swift b/Shared/Post/PostEditor.swift index c689aaa..a3dbcfe 100644 --- a/Shared/Post/PostEditor.swift +++ b/Shared/Post/PostEditor.swift @@ -1,7 +1,7 @@ import SwiftUI struct PostEditor: View { - @State var post: Post + @ObservedObject var post: Post @State private var hasUnpublishedChanges: Bool = false var body: some View {