2020-07-25 11:02:11 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2020-08-17 18:16:42 +00:00
|
|
|
struct PostEditorView: View {
|
2020-08-18 18:06:02 +00:00
|
|
|
@EnvironmentObject var model: WriteFreelyModel
|
2022-07-28 11:38:19 +00:00
|
|
|
@EnvironmentObject var errorHandling: ErrorHandling
|
2020-08-11 21:03:30 +00:00
|
|
|
|
2020-09-08 20:17:58 +00:00
|
|
|
@ObservedObject var post: WFAPost
|
2020-09-22 20:18:00 +00:00
|
|
|
@State private var isHovering: Bool = false
|
2020-11-23 19:13:15 +00:00
|
|
|
@State private var updatingFromServer: Bool = false
|
2020-09-08 20:17:58 +00:00
|
|
|
|
2020-07-25 11:02:11 +00:00
|
|
|
var body: some View {
|
2023-10-23 21:15:41 +00:00
|
|
|
VStack {
|
|
|
|
if !model.hasNetworkConnection {
|
|
|
|
Label("You are not connected to the internet", systemImage: "wifi.exclamationmark")
|
|
|
|
.font(.caption)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.padding(.top, 8)
|
2020-11-26 20:53:17 +00:00
|
|
|
}
|
2023-10-23 21:15:41 +00:00
|
|
|
PostTextEditingView(
|
|
|
|
post: post,
|
|
|
|
updatingFromServer: $updatingFromServer
|
|
|
|
)
|
|
|
|
.background(Color(NSColor.controlBackgroundColor))
|
|
|
|
.onAppear(perform: {
|
|
|
|
model.editor.setInitialValues(for: post)
|
|
|
|
if post.status != PostStatus.published.rawValue {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.model.editor.saveLastDraft(post)
|
|
|
|
}
|
2022-07-28 11:38:19 +00:00
|
|
|
} else {
|
2023-10-23 21:15:41 +00:00
|
|
|
self.model.editor.clearLastDraft()
|
2022-07-28 11:38:19 +00:00
|
|
|
}
|
2023-10-23 21:15:41 +00:00
|
|
|
})
|
|
|
|
.onChange(of: post.hasNewerRemoteCopy, perform: { _ in
|
|
|
|
if !post.hasNewerRemoteCopy {
|
|
|
|
self.updatingFromServer = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.onChange(of: post.status, perform: { value in
|
|
|
|
if value != PostStatus.published.rawValue {
|
|
|
|
self.model.editor.saveLastDraft(post)
|
|
|
|
} else {
|
|
|
|
self.model.editor.clearLastDraft()
|
2020-08-27 21:40:05 +00:00
|
|
|
}
|
2020-09-24 18:13:32 +00:00
|
|
|
DispatchQueue.main.async {
|
2021-10-08 21:07:06 +00:00
|
|
|
LocalStorageManager.standard.saveContext()
|
2020-09-24 18:13:32 +00:00
|
|
|
}
|
2023-10-23 21:15:41 +00:00
|
|
|
})
|
|
|
|
.onChange(of: model.hasError) { value in
|
|
|
|
if value {
|
|
|
|
if let error = model.currentError {
|
|
|
|
self.errorHandling.handle(error: error)
|
|
|
|
} else {
|
|
|
|
self.errorHandling.handle(error: AppError.genericError())
|
|
|
|
}
|
|
|
|
model.hasError = false
|
|
|
|
}
|
2020-08-27 21:40:05 +00:00
|
|
|
}
|
2023-10-23 21:15:41 +00:00
|
|
|
.onDisappear(perform: {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
model.editor.clearLastDraft()
|
|
|
|
}
|
|
|
|
if post.title.count == 0
|
|
|
|
&& post.body.count == 0
|
|
|
|
&& post.status == PostStatus.local.rawValue
|
|
|
|
&& post.updatedDate == nil
|
|
|
|
&& post.postId == nil {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
model.posts.remove(post)
|
|
|
|
}
|
|
|
|
} else if post.status != PostStatus.published.rawValue {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
LocalStorageManager.standard.saveContext()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-07-28 21:11:01 +00:00
|
|
|
}
|
2020-07-25 11:02:11 +00:00
|
|
|
}
|
2020-07-31 17:49:53 +00:00
|
|
|
|
2020-09-22 15:52:07 +00:00
|
|
|
struct PostEditorView_EmptyPostPreviews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-11-05 18:18:36 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-09-22 15:52:07 +00:00
|
|
|
let testPost = WFAPost(context: context)
|
|
|
|
testPost.createdDate = Date()
|
|
|
|
testPost.appearance = "norm"
|
|
|
|
|
|
|
|
let model = WriteFreelyModel()
|
|
|
|
|
|
|
|
return PostEditorView(post: testPost)
|
|
|
|
.environment(\.managedObjectContext, context)
|
|
|
|
.environmentObject(model)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PostEditorView_ExistingPostPreviews: PreviewProvider {
|
2020-09-09 16:12:19 +00:00
|
|
|
static var previews: some View {
|
2021-11-05 18:18:36 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-09-09 16:12:19 +00:00
|
|
|
let testPost = WFAPost(context: context)
|
|
|
|
testPost.title = "Test Post Title"
|
|
|
|
testPost.body = "Here's some cool sample body text."
|
|
|
|
testPost.createdDate = Date()
|
2020-09-18 16:10:01 +00:00
|
|
|
testPost.appearance = "code"
|
2020-09-09 16:12:19 +00:00
|
|
|
|
|
|
|
let model = WriteFreelyModel()
|
|
|
|
|
|
|
|
return PostEditorView(post: testPost)
|
|
|
|
.environment(\.managedObjectContext, context)
|
|
|
|
.environmentObject(model)
|
|
|
|
}
|
|
|
|
}
|