diff --git a/Shared/PostEditor/PostEditorStatusToolbarView.swift b/Shared/PostEditor/PostEditorStatusToolbarView.swift index 7f19f7a..85c5e65 100644 --- a/Shared/PostEditor/PostEditorStatusToolbarView.swift +++ b/Shared/PostEditor/PostEditorStatusToolbarView.swift @@ -1,136 +1,136 @@ import SwiftUI struct PostEditorStatusToolbarView: View { #if os(iOS) @Environment(\.horizontalSizeClass) var horizontalSizeClass #endif @EnvironmentObject var model: WriteFreelyModel @ObservedObject var post: WFAPost var body: some View { if post.hasNewerRemoteCopy { #if os(iOS) if horizontalSizeClass == .compact { VStack { PostStatusBadgeView(post: post) HStack { Text("⚠️ Newer copy on server. Replace local copy?") .font(.caption) .foregroundColor(.secondary) Button(action: { - model.updateFromServer(post: post) // FIXME: This shouldn't change post status after update + model.updateFromServer(post: post) }, label: { Image(systemName: "square.and.arrow.down") }) } .padding(.bottom) } .padding(.top) } else { HStack { PostStatusBadgeView(post: post) .padding(.trailing) Text("⚠️ Newer copy on server. Replace local copy?") .font(.callout) .foregroundColor(.secondary) Button(action: { model.updateFromServer(post: post) }, label: { Image(systemName: "square.and.arrow.down") }) } } #else HStack { PostStatusBadgeView(post: post) .padding(.trailing) Text("⚠️ Newer copy on server. Replace local copy?") .font(.callout) .foregroundColor(.secondary) Button(action: { model.updateFromServer(post: post) }, label: { Image(systemName: "square.and.arrow.down") }) } #endif } else { PostStatusBadgeView(post: post) } } } //#if DEBUG //let testPost = Post( // title: "Test Post Title", // body: """ // Here's some cool sample body text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ultrices \ // posuere dignissim. Vestibulum a libero tempor, lacinia nulla vitae, congue purus. Nunc ac nulla quam. Duis \ // tincidunt eros augue, et volutpat tortor pulvinar ut. Nullam sit amet maximus urna. Phasellus non dignissim \ // lacus. Nulla ac posuere ex. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus \ // mus. Donec non molestie mauris. Suspendisse potenti. Vivamus at erat turpis. // // Pellentesque porttitor gravida tincidunt. Sed vitae eros non metus aliquam hendrerit. Aliquam sed risus suscipit \ // turpis dictum dictum. Duis lacus lectus, dictum vel felis in, rhoncus fringilla felis. Nunc id dolor nisl. \ // Aliquam euismod purus elit. Nullam egestas neque leo, sed aliquet ligula ultrices nec. // """, // createdDate: Date() //) //#endif // //struct ToolbarView_LocalPreviews: PreviewProvider { // static var previews: some View { // let model = WriteFreelyModel() // let post = testPost // return PostEditorStatusToolbarView(post: post) // .environmentObject(model) // } //} // //struct ToolbarView_RemotePreviews: PreviewProvider { // static var previews: some View { // let model = WriteFreelyModel() // let newerRemotePost = Post( // title: testPost.wfPost.title ?? "", // body: testPost.wfPost.body, // createdDate: testPost.wfPost.createdDate ?? Date(), // status: testPost.status, // collection: testPost.collection // ) // newerRemotePost.hasNewerRemoteCopy = true // return PostEditorStatusToolbarView(post: newerRemotePost) // .environmentObject(model) // } //} // //#if os(iOS) //struct ToolbarView_CompactLocalPreviews: PreviewProvider { // static var previews: some View { // let model = WriteFreelyModel() // let post = testPost // return PostEditorStatusToolbarView(post: post) // .environmentObject(model) // .environment(\.horizontalSizeClass, .compact) // } //} //#endif // //#if os(iOS) //struct ToolbarView_CompactRemotePreviews: PreviewProvider { // static var previews: some View { // let model = WriteFreelyModel() // let newerRemotePost = Post( // title: testPost.wfPost.title ?? "", // body: testPost.wfPost.body, // createdDate: testPost.wfPost.createdDate ?? Date(), // status: testPost.status, // collection: testPost.collection // ) // newerRemotePost.hasNewerRemoteCopy = true // return PostEditorStatusToolbarView(post: newerRemotePost) // .environmentObject(model) // .environment(\.horizontalSizeClass, .compact) // } //} //#endif diff --git a/Shared/PostEditor/PostEditorView.swift b/Shared/PostEditor/PostEditorView.swift index b638e01..983aa54 100644 --- a/Shared/PostEditor/PostEditorView.swift +++ b/Shared/PostEditor/PostEditorView.swift @@ -1,77 +1,82 @@ import SwiftUI struct PostEditorView: View { @EnvironmentObject var model: WriteFreelyModel @ObservedObject var post: WFAPost var body: some View { VStack { TextEditor(text: $post.title) .font(.title) .frame(height: 100) .onChange(of: post.title) { _ in if post.status == PostStatus.published.rawValue { post.status = PostStatus.edited.rawValue } } TextEditor(text: $post.body) .font(.body) .onChange(of: post.body) { _ in if post.status == PostStatus.published.rawValue { post.status = PostStatus.edited.rawValue } } } .padding() .toolbar { ToolbarItem(placement: .status) { PostEditorStatusToolbarView(post: post) } ToolbarItem(placement: .primaryAction) { Button(action: { model.publish(post: post) post.status = PostStatus.published.rawValue }, label: { Image(systemName: "paperplane") }) } } + .onChange(of: post.hasNewerRemoteCopy, perform: { _ in + if post.status == PostStatus.edited.rawValue && !post.hasNewerRemoteCopy { + post.status = PostStatus.published.rawValue + } + }) .onDisappear(perform: { if post.status == PostStatus.edited.rawValue { DispatchQueue.main.async { PersistenceManager().saveContext() } } }) } } //struct PostEditorView_NewLocalDraftPreviews: PreviewProvider { // static var previews: some View { // PostEditorView(post: Post()) // .environmentObject(WriteFreelyModel()) // } //} // //struct PostEditorView_NewerLocalPostPreviews: PreviewProvider { // static var previews: some View { // return PostEditorView(post: testPost) // .environmentObject(WriteFreelyModel()) // } //} // //struct PostEditorView_NewerRemotePostPreviews: PreviewProvider { // static var previews: some View { // let newerRemotePost = Post( // title: testPost.wfPost.title ?? "", // body: testPost.wfPost.body, // createdDate: testPost.wfPost.createdDate ?? Date(), // status: testPost.status, // collection: testPost.collection // ) // newerRemotePost.hasNewerRemoteCopy = true // return PostEditorView(post: newerRemotePost) // .environmentObject(WriteFreelyModel()) // } //}