From b21afb66e583914499b679269df13e5677c780e2 Mon Sep 17 00:00:00 2001 From: Ste Prescott Date: Thu, 27 Oct 2022 20:39:51 +0100 Subject: [PATCH] Add support for SwiftUI --- Sources/Share/Link.swift | 8 ++++---- Sources/Share/Sheet.swift | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Sources/Share/Link.swift b/Sources/Share/Link.swift index 1d742f5..e4fb2e2 100644 --- a/Sources/Share/Link.swift +++ b/Sources/Share/Link.swift @@ -57,22 +57,22 @@ extension Share { self.shareValue = shareValue } - public func title(_ title: String) -> Link { + public func title(_ title: String?) -> Link { self.title = title return self } - public func icon(_ url: URL) -> Link { + public func icon(_ url: URL?) -> Link { icon = url return self } - public func image(_ url: URL) -> Link { + public func image(_ url: URL?) -> Link { self.image = url return self } - public func video(_ url: URL) -> Link { + public func video(_ url: URL?) -> Link { self.video = url return self } diff --git a/Sources/Share/Sheet.swift b/Sources/Share/Sheet.swift index 2a545d2..b591969 100644 --- a/Sources/Share/Sheet.swift +++ b/Sources/Share/Sheet.swift @@ -83,3 +83,38 @@ extension Share { } } #endif + +#if canImport(SwiftUI) +import SwiftUI + +@available(iOS 13.0, *) +extension Share.Sheet { + + public struct View: UIViewControllerRepresentable, ShareDelegate { + public typealias Action = ((Share.Service.Status) -> ()) + + let sharable: Sharable + + var onStatusChange: Action = { _ in } + + public func didChange(status: Share.Service.Status) { + onStatusChange(status) + } + + public func onStatusChange(_ action: @escaping Action) -> View { + View(sharable: sharable, onStatusChange: action) + } + + public func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIActivityViewController { + return Share.Sheet(for: sharable, delegate: self) + } + + public func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext) {} + } + + public func view() -> View { + .init(sharable: sharable) + } +} + +#endif