Set up UI for editor menu and changing collection

This commit is contained in:
Angelo Stavrow 2020-10-07 17:50:38 -04:00
parent 609a33067b
commit b216b576c8
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 76 additions and 25 deletions

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>

View File

@ -2,10 +2,19 @@ import SwiftUI
struct PostEditorView: View {
@EnvironmentObject var model: WriteFreelyModel
@Environment(\.managedObjectContext) var moc
@Environment(\.horizontalSizeClass) var horizontalSizeClass
@Environment(\.presentationMode) var presentationMode
@ObservedObject var post: WFAPost
@State private var selectedCollection: WFACollection?
@FetchRequest(
entity: WFACollection.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)]
) var collections: FetchedResults<WFACollection>
var body: some View {
VStack {
if post.hasNewerRemoteCopy {
@ -123,25 +132,51 @@ struct PostEditorView: View {
ToolbarItem(placement: .principal) {
PostEditorStatusToolbarView(post: post)
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(action: {
if model.account.isLoggedIn {
publishPost()
} else {
self.model.isPresentingSettingsView = true
ToolbarItem(placement: .primaryAction) {
Menu(content: {
Button(action: {
if model.account.isLoggedIn {
publishPost()
} else {
self.model.isPresentingSettingsView = true
}
}, label: {
Label(
post.status == PostStatus.local.rawValue ? "Publish…" : "Publish",
systemImage: "paperplane"
)
})
.disabled(
post.status ==
PostStatus.published.rawValue ||
!model.hasNetworkConnection ||
post.body.count == 0
)
Button(action: {
sharePost()
}, label: {
Label("Share", systemImage: "square.and.arrow.up")
})
.disabled(post.postId == nil)
Button(action: {
print("Tapped 'Delete...' button")
}, label: {
Label("Delete…", systemImage: "trash")
})
if model.account.isLoggedIn && post.status != PostStatus.local.rawValue {
Section(header: Text("Move To Collection")) {
Label("Move to:", systemImage: "arrowshape.zigzag.right")
Picker(selection: $selectedCollection, label: Text("Move to…")) {
Text(" Drafts").tag(nil as WFACollection?)
ForEach(collections) { collection in
Text(" \(collection.title)").tag(collection as WFACollection?)
}
}
}
}
}, label: {
Image(systemName: "paperplane")
Image(systemName: "ellipsis.circle")
})
.disabled(
post.status == PostStatus.published.rawValue || !model.hasNetworkConnection || post.body.count == 0
)
Button(action: {
sharePost()
}, label: {
Image(systemName: "square.and.arrow.up")
})
.disabled(post.postId == nil)
}
}
.onChange(of: post.hasNewerRemoteCopy, perform: { _ in
@ -160,6 +195,17 @@ struct PostEditorView: View {
}
}
})
.onChange(of: selectedCollection, perform: { newValue in
if post.collectionAlias != newValue?.alias {
post.status = PostStatus.edited.rawValue
post.collectionAlias = newValue?.alias
model.posts.loadCachedPosts()
LocalStorageManager().saveContext()
}
})
.onAppear(perform: {
self.selectedCollection = collections.first { $0.alias == post.collectionAlias }
})
.onDisappear(perform: {
if post.title.count == 0
&& post.body.count == 0
@ -179,14 +225,19 @@ struct PostEditorView: View {
}
private func publishPost() {
DispatchQueue.main.async {
LocalStorageManager().saveContext()
model.posts.loadCachedPosts()
model.publish(post: post)
if post.status == PostStatus.local.rawValue {
// If post is local, prompt user to choose where they want to publish.
} else {
// Otherwise, publish local changes to the server
DispatchQueue.main.async {
LocalStorageManager().saveContext()
model.posts.loadCachedPosts()
model.publish(post: post)
}
#if os(iOS)
self.hideKeyboard()
#endif
}
#if os(iOS)
self.hideKeyboard()
#endif
}
private func sharePost() {