mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Pass Post into PostEditor, split title/body into TextField/TextEditor
This commit is contained in:
parent
896b5f73f7
commit
7f920e54dc
@ -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")
|
||||
}
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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) {
|
||||
|
@ -8,16 +8,25 @@
|
||||
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())
|
||||
VStack {
|
||||
TextField(post.title, text: $post.title)
|
||||
.font(.title)
|
||||
.multilineTextAlignment(.center)
|
||||
.padding(.bottom)
|
||||
.onChange(of: post.title) { _ in
|
||||
if post.status == .published {
|
||||
hasUnpublishedChanges = true
|
||||
}
|
||||
}
|
||||
TextEditor(text: $post.body)
|
||||
.font(.body)
|
||||
.padding()
|
||||
.onChange(of: textString) { _ in
|
||||
if postStatus == .published {
|
||||
.padding(.leading)
|
||||
.onChange(of: post.body) { _ in
|
||||
if post.status == .published {
|
||||
hasUnpublishedChanges = true
|
||||
}
|
||||
}
|
||||
@ -25,7 +34,8 @@ struct PostEditor: View {
|
||||
if hasUnpublishedChanges {
|
||||
PostStatusBadge(postStatus: .edited)
|
||||
} else {
|
||||
PostStatusBadge(postStatus: postStatus)
|
||||
PostStatusBadge(postStatus: post.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -33,9 +43,6 @@ struct PostEditor: View {
|
||||
|
||||
struct PostEditor_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PostEditor(
|
||||
textString: testPost.editableText,
|
||||
postStatus: testPost.status
|
||||
)
|
||||
PostEditor(post: testPost)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user