diff options
Diffstat (limited to 'Hutch/Extensions/SRHTShareUI.swift')
| -rw-r--r-- | Hutch/Extensions/SRHTShareUI.swift | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Hutch/Extensions/SRHTShareUI.swift b/Hutch/Extensions/SRHTShareUI.swift new file mode 100644 index 0000000..9d949c9 --- /dev/null +++ b/Hutch/Extensions/SRHTShareUI.swift @@ -0,0 +1,57 @@ +import SwiftUI +import UIKit + +enum SRHTShareTarget: String { + case repository = "repository" + case commit = "commit" + case file = "file" + case build = "build" + case tracker = "tracker" + case ticket = "ticket" + case profile = "profile" + + var fallbackMessage: String { + "This \(rawValue) does not have a valid web URL to share." + } +} + +struct SRHTShareButton<Label: View>: View { + let url: URL? + let target: SRHTShareTarget + @ViewBuilder let label: () -> Label + + @State private var isShowingShareSheet = false + @State private var isShowingFallbackAlert = false + + var body: some View { + Button { + if url != nil { + isShowingShareSheet = true + } else { + isShowingFallbackAlert = true + } + } label: { + label() + } + .sheet(isPresented: $isShowingShareSheet) { + if let url { + ShareSheet(activityItems: [url]) + } + } + .alert("Share Unavailable", isPresented: $isShowingFallbackAlert) { + Button("OK", role: .cancel) {} + } message: { + Text(target.fallbackMessage) + } + } +} + +private struct ShareSheet: UIViewControllerRepresentable { + let activityItems: [Any] + + func makeUIViewController(context: Context) -> UIActivityViewController { + UIActivityViewController(activityItems: activityItems, applicationActivities: nil) + } + + func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {} +} |
