Pass Post into PostEditor, split title/body into TextField/TextEditor

This commit is contained in:
Angelo Stavrow 2020-07-28 11:17:05 -04:00
parent 896b5f73f7
commit 7f920e54dc
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 32 additions and 31 deletions

View File

@ -9,7 +9,11 @@ struct ContentView: View {
.frame(maxHeight: .infinity) .frame(maxHeight: .infinity)
.navigationTitle("Posts") .navigationTitle("Posts")
.toolbar { .toolbar {
NavigationLink(destination: PostEditor()) { NavigationLink(
destination: PostEditor(
post: Post(title: "Title", body: "Write your post here...", createdDate: Date())
)
) {
Image(systemName: "plus") Image(systemName: "plus")
} }
} }

View File

@ -7,13 +7,6 @@ struct Post: Identifiable {
var body: String var body: String
var createdDate: Date var createdDate: Date
var status: PostStatus = .draft var status: PostStatus = .draft
var editableText: String {
return """
# \(self.title)
\(self.body)
"""
}
} }
let testPost = Post( let testPost = Post(

View File

@ -4,10 +4,7 @@ struct PostCell: View {
var post: Post var post: Post
var body: some View { var body: some View {
NavigationLink( NavigationLink(
destination: PostEditor( destination: PostEditor(post: post)
textString: post.editableText,
postStatus: post.status
)
) { ) {
HStack { HStack {
VStack(alignment: .leading) { VStack(alignment: .leading) {

View File

@ -8,34 +8,41 @@
import SwiftUI import SwiftUI
struct PostEditor: View { struct PostEditor: View {
@State var textString: String = "" @State var post: Post
@State private var hasUnpublishedChanges: Bool = false @State private var hasUnpublishedChanges: Bool = false
var postStatus: PostStatus = .draft
var body: some View { var body: some View {
TextEditor(text: $textString.animation()) VStack {
.font(.body) TextField(post.title, text: $post.title)
.padding() .font(.title)
.onChange(of: textString) { _ in .multilineTextAlignment(.center)
if postStatus == .published { .padding(.bottom)
hasUnpublishedChanges = true .onChange(of: post.title) { _ in
if post.status == .published {
hasUnpublishedChanges = true
}
} }
} TextEditor(text: $post.body)
.toolbar { .font(.body)
if hasUnpublishedChanges { .padding(.leading)
PostStatusBadge(postStatus: .edited) .onChange(of: post.body) { _ in
} else { if post.status == .published {
PostStatusBadge(postStatus: postStatus) hasUnpublishedChanges = true
}
} }
} .toolbar {
if hasUnpublishedChanges {
PostStatusBadge(postStatus: .edited)
} else {
PostStatusBadge(postStatus: post.status)
}
}
}
} }
} }
struct PostEditor_Previews: PreviewProvider { struct PostEditor_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
PostEditor( PostEditor(post: testPost)
textString: testPost.editableText,
postStatus: testPost.status
)
} }
} }