mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Merge pull request #155 from writeas/add-share-feature
Add share feature
This commit is contained in:
commit
a22c93ad36
@ -23,6 +23,7 @@
|
||||
173E19D1254318F600440F0F /* RemoteChangePromptView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 173E19D0254318F600440F0F /* RemoteChangePromptView.swift */; };
|
||||
173E19E3254329CC00440F0F /* PostTextEditingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 173E19E2254329CC00440F0F /* PostTextEditingView.swift */; };
|
||||
17466626256C0D0600629997 /* MacEditorTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17466625256C0D0600629997 /* MacEditorTextView.swift */; };
|
||||
17479F152583D8E40072B7FB /* PostEditorSharingPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17479F142583D8E40072B7FB /* PostEditorSharingPicker.swift */; };
|
||||
17480CA5251272EE00EB7765 /* Bundle+AppVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17480CA4251272EE00EB7765 /* Bundle+AppVersion.swift */; };
|
||||
17480CA6251272EE00EB7765 /* Bundle+AppVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17480CA4251272EE00EB7765 /* Bundle+AppVersion.swift */; };
|
||||
174D313224EC2831006CA9EE /* WriteFreelyModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 174D313124EC2831006CA9EE /* WriteFreelyModel.swift */; };
|
||||
@ -126,6 +127,7 @@
|
||||
173E19D0254318F600440F0F /* RemoteChangePromptView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteChangePromptView.swift; sourceTree = "<group>"; };
|
||||
173E19E2254329CC00440F0F /* PostTextEditingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostTextEditingView.swift; sourceTree = "<group>"; };
|
||||
17466625256C0D0600629997 /* MacEditorTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacEditorTextView.swift; sourceTree = "<group>"; };
|
||||
17479F142583D8E40072B7FB /* PostEditorSharingPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostEditorSharingPicker.swift; sourceTree = "<group>"; };
|
||||
17480CA4251272EE00EB7765 /* Bundle+AppVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+AppVersion.swift"; sourceTree = "<group>"; };
|
||||
174D313124EC2831006CA9EE /* WriteFreelyModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteFreelyModel.swift; sourceTree = "<group>"; };
|
||||
1753F6AB24E431CC00309365 /* MacPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacPreferencesView.swift; sourceTree = "<group>"; };
|
||||
@ -311,6 +313,7 @@
|
||||
17A67CAC251A5D8D002F163D /* PostEditor */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
17479F142583D8E40072B7FB /* PostEditorSharingPicker.swift */,
|
||||
17A67CAE251A5DD7002F163D /* PostEditorView.swift */,
|
||||
17E5DF892543610700DCDC9B /* PostTextEditingView.swift */,
|
||||
17466625256C0D0600629997 /* MacEditorTextView.swift */,
|
||||
@ -755,6 +758,7 @@
|
||||
17D435E924E3128F0036B539 /* PreferencesModel.swift in Sources */,
|
||||
17120DAA24E1B2F5002B9F6C /* AccountLogoutView.swift in Sources */,
|
||||
17DF32D624C8CA3400BCE2E3 /* PostStatusBadgeView.swift in Sources */,
|
||||
17479F152583D8E40072B7FB /* PostEditorSharingPicker.swift in Sources */,
|
||||
17480CA6251272EE00EB7765 /* Bundle+AppVersion.swift in Sources */,
|
||||
17C42E662509237800072984 /* PostListFilteredView.swift in Sources */,
|
||||
17120DAD24E1B99F002B9F6C /* AccountLoginView.swift in Sources */,
|
||||
|
@ -3,19 +3,41 @@ import SwiftUI
|
||||
struct ActivePostToolbarView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@ObservedObject var activePost: WFAPost
|
||||
@State private var isPresentingSharingServicePicker: Bool = false
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 16) {
|
||||
PostEditorStatusToolbarView(post: activePost)
|
||||
HStack(spacing: 4) {
|
||||
Button(action: {}, label: { Image(systemName: "square.and.arrow.up") })
|
||||
.disabled(activePost.status == PostStatus.local.rawValue)
|
||||
Button(
|
||||
action: {
|
||||
self.isPresentingSharingServicePicker = true
|
||||
},
|
||||
label: { Image(systemName: "square.and.arrow.up") }
|
||||
)
|
||||
.disabled(activePost.status == PostStatus.local.rawValue)
|
||||
.popover(isPresented: $isPresentingSharingServicePicker) {
|
||||
PostEditorSharingPicker(
|
||||
isPresented: $isPresentingSharingServicePicker,
|
||||
sharingItems: createPostUrl()
|
||||
)
|
||||
.frame(width: .zero, height: .zero)
|
||||
}
|
||||
Button(action: { publishPost(activePost) }, label: { Image(systemName: "paperplane") })
|
||||
.disabled(activePost.body.isEmpty || activePost.status == PostStatus.published.rawValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func createPostUrl() -> [Any] {
|
||||
guard let postId = activePost.postId else { return [] }
|
||||
guard let urlString = activePost.slug != nil ?
|
||||
"\(model.account.server)/\((activePost.collectionAlias)!)/\((activePost.slug)!)" :
|
||||
"\(model.account.server)/\((postId))" else { return [] }
|
||||
guard let data = URL(string: urlString) else { return [] }
|
||||
return [data as NSURL]
|
||||
}
|
||||
|
||||
private func publishPost(_ post: WFAPost) {
|
||||
DispatchQueue.main.async {
|
||||
LocalStorageManager().saveContext()
|
||||
|
39
macOS/PostEditor/PostEditorSharingPicker.swift
Normal file
39
macOS/PostEditor/PostEditorSharingPicker.swift
Normal file
@ -0,0 +1,39 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostEditorSharingPicker: NSViewRepresentable {
|
||||
@Binding var isPresented: Bool
|
||||
var sharingItems: [Any] = []
|
||||
|
||||
func makeNSView(context: Context) -> some NSView {
|
||||
let view = NSView()
|
||||
let picker = NSSharingServicePicker(items: sharingItems)
|
||||
picker.delegate = context.coordinator
|
||||
|
||||
DispatchQueue.main.async {
|
||||
picker.show(relativeTo: .zero, of: view, preferredEdge: .minY)
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
func updateNSView(_ nsView: NSViewType, context: Context) {
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(owner: self)
|
||||
}
|
||||
|
||||
class Coordinator: NSObject, NSSharingServicePickerDelegate {
|
||||
let owner: PostEditorSharingPicker
|
||||
init(owner: PostEditorSharingPicker) {
|
||||
self.owner = owner
|
||||
}
|
||||
|
||||
func sharingServicePicker(
|
||||
_ sharingServicePicker: NSSharingServicePicker,
|
||||
didChoose service: NSSharingService?
|
||||
) {
|
||||
sharingServicePicker.delegate = nil
|
||||
self.owner.isPresented = false
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user