krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.7.0: Hutch/Extensions/SRHTShareUI.swift · raw

 1import SwiftUI
 2import UIKit
 3
 4enum SRHTShareTarget: String {
 5    case repository = "repository"
 6    case commit = "commit"
 7    case file = "file"
 8    case build = "build"
 9    case tracker = "tracker"
10    case ticket = "ticket"
11    case profile = "profile"
12    case paste = "paste"
13
14    var fallbackMessage: String {
15        "This \(rawValue) does not have a valid web URL to share."
16    }
17}
18
19struct SRHTShareButton<Label: View>: View {
20    let url: URL?
21    let target: SRHTShareTarget
22    @ViewBuilder let label: () -> Label
23
24    @State private var isShowingShareSheet = false
25    @State private var isShowingFallbackAlert = false
26
27    var body: some View {
28        Button {
29            if url != nil {
30                isShowingShareSheet = true
31            } else {
32                isShowingFallbackAlert = true
33            }
34        } label: {
35            label()
36        }
37        .sheet(isPresented: $isShowingShareSheet) {
38            if let url {
39                ShareSheet(activityItems: [url])
40            }
41        }
42        .alert("Share Unavailable", isPresented: $isShowingFallbackAlert) {
43            Button("OK", role: .cancel) {
44                // Alert dismissal is implicit; no additional action required.
45            }
46        } message: {
47            Text(target.fallbackMessage)
48        }
49    }
50}
51
52private struct ShareSheet: UIViewControllerRepresentable {
53    let activityItems: [Any]
54
55    func makeUIViewController(context _: Context) -> UIActivityViewController {
56        UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
57    }
58
59    func updateUIViewController(_ : UIActivityViewController, context _: Context) {
60        // UIActivityViewController is fully configured in makeUIViewController.
61        // No state-driven updates are required.
62    }
63}