Merge pull request #28 from writeas/consolidate-post-properties

Consolidate redundant Post properties
This commit is contained in:
Angelo Stavrow 2020-08-27 13:18:03 -04:00 committed by GitHub
commit 0b6b0c395d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 38 deletions

View File

@ -8,12 +8,9 @@ enum PostStatus {
}
class Post: Identifiable, ObservableObject {
@Published var title: String
@Published var body: String
@Published var createdDate: Date
@Published var wfPost: WFPost
@Published var status: PostStatus
@Published var collection: PostCollection
@Published var wfPost: WFPost?
let id = UUID()
@ -24,9 +21,7 @@ class Post: Identifiable, ObservableObject {
status: PostStatus = .draft,
collection: PostCollection = draftsCollection
) {
self.title = title
self.body = body
self.createdDate = createdDate
self.wfPost = WFPost(body: body, title: title, createdDate: createdDate)
self.status = status
self.collection = collection
}

View File

@ -66,30 +66,17 @@ extension WriteFreelyModel {
func publish(post: Post) {
guard let loggedInClient = client else { return }
if post.wfPost == nil {
// This is a new local draft.
post.wfPost = WFPost(
body: post.body,
title: post.title,
createdDate: post.createdDate
)
loggedInClient.createPost(
post: post.wfPost!, in: post.collection.wfCollection?.alias, completion: publishHandler
if let existingPostId = post.wfPost.postId {
// This is an existing post.
loggedInClient.updatePost(
postId: existingPostId,
updatedPost: post.wfPost,
completion: publishHandler
)
} else {
// This is an existing post.
// 1. Update Post.wfPost properties from (redundant) Post properties
// FIXME: https://github.com/writeas/writefreely-swiftui-multiplatform/issues/27
guard var publishedPost = post.wfPost else { return }
publishedPost.title = post.title
publishedPost.body = post.body
publishedPost.createdDate = post.createdDate
// 2. Update the post on the server and call the handler
loggedInClient.updatePost(
postId: publishedPost.postId!,
updatedPost: publishedPost,
completion: publishHandler
// This is a new local draft.
loggedInClient.createPost(
post: post.wfPost, in: post.collection.wfCollection?.alias, completion: publishHandler
)
}
}
@ -212,7 +199,7 @@ private extension WriteFreelyModel {
do {
let wfPost = try result.get()
let foundPostIndex = store.posts.firstIndex(where: {
$0.title == wfPost.title && $0.body == wfPost.body
$0.wfPost.title == wfPost.title && $0.wfPost.body == wfPost.body
})
guard let index = foundPostIndex else { return }
DispatchQueue.main.async {

View File

@ -6,20 +6,21 @@ struct PostEditorView: View {
@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
}
@ -40,6 +41,7 @@ struct PostEditorView: View {
}
}
.onAppear(perform: {
title = post.wfPost.title ?? ""
checkIfNewPost()
if self.isNewPost {
addNewPostToStore()

View File

@ -6,10 +6,10 @@ struct PostCellView: View {
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)
}