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)
|
.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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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(
|
||||||
|
@ -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) {
|
||||||
|
@ -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
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user