Open History to see the last commit
1import SwiftUI
2import UIKit
3
4final class T3ShareViewController: UIViewController {
5 private var hostingController: UIHostingController<T3ShareExtensionView>?
6
7 override func viewDidLoad() {
8 super.viewDidLoad()
9 view.backgroundColor = .systemBackground
10
11 let content = T3ShareExtensionView(
12 save: { [weak self] in
13 let inputItems = self?.extensionContext?.inputItems ?? []
14 let payload = await T3SharePayloadLoader.load(from: inputItems)
15 return try await Task.detached {
16 try T3IncomingShareStore.write(
17 textFragments: payload.textFragments,
18 images: payload.images,
19 files: payload.files,
20 warnings: payload.warnings
21 )
22 }.value
23 },
24 cancel: { [weak self] in
25 self?.extensionContext?.cancelRequest(withError: CocoaError(.userCancelled))
26 },
27 complete: { [weak self] in
28 self?.extensionContext?.completeRequest(returningItems: nil)
29 }
30 )
31 let hostingController = UIHostingController(rootView: content)
32 hostingController.view.backgroundColor = .systemBackground
33 addChild(hostingController)
34 hostingController.view.translatesAutoresizingMaskIntoConstraints = false
35 view.addSubview(hostingController.view)
36 NSLayoutConstraint.activate([
37 hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
38 hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
39 hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
40 hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
41 ])
42 hostingController.didMove(toParent: self)
43 self.hostingController = hostingController
44 }
45}
46
47struct T3ShareExtensionView: View {
48 enum Phase: Equatable {
49 case ready
50 case saving
51 case saved(imageCount: Int)
52 case failed(message: String)
53 }
54
55 let save: () async throws -> T3IncomingShareEnvelope
56 let cancel: () -> Void
57 let complete: () -> Void
58
59 @State private var phase = Phase.ready
60
61 var body: some View {
62 VStack(spacing: 0) {
63 HStack {
64 Button("Cancel", action: cancel)
65 .foregroundStyle(.secondary)
66 .disabled(isSaving)
67 Spacer()
68 Text("T3 Code")
69 .font(.system(size: 17, weight: .semibold))
70 .foregroundStyle(.primary)
71 Spacer()
72 Color.clear.frame(width: 52, height: 1)
73 }
74 .padding(.horizontal, 18)
75 .padding(.vertical, 15)
76
77 Divider()
78
79 VStack(spacing: 14) {
80 Image(systemName: phaseSymbol)
81 .font(.system(size: 32, weight: .medium))
82 .foregroundStyle(phaseTint)
83 .accessibilityHidden(true)
84 Text(title)
85 .font(.system(size: 22, weight: .bold))
86 .foregroundStyle(.primary)
87 .multilineTextAlignment(.center)
88 Text(message)
89 .font(.system(size: 15, weight: .medium))
90 .foregroundStyle(.secondary)
91 .multilineTextAlignment(.center)
92 .lineSpacing(3)
93 }
94 .frame(maxWidth: .infinity, maxHeight: .infinity)
95 .padding(.horizontal, 28)
96 .padding(.vertical, 24)
97
98 Button(action: primaryAction) {
99 Text(primaryTitle)
100 .font(.system(size: 16, weight: .semibold))
101 .foregroundStyle(Color(uiColor: .systemBackground))
102 .frame(maxWidth: .infinity)
103 .frame(height: 50)
104 .background(Color(uiColor: .label), in: RoundedRectangle(cornerRadius: 13))
105 }
106 .buttonStyle(.plain)
107 .disabled(isSaving)
108 .opacity(isSaving ? 0.55 : 1)
109 .padding(.horizontal, 18)
110 .padding(.bottom, 18)
111 }
112 .background(Color(uiColor: .systemBackground).ignoresSafeArea())
113 }
114
115 private var isSaving: Bool {
116 phase == .saving
117 }
118
119 private var title: String {
120 switch phase {
121 case .ready: "Add to a new task"
122 case .saving: "Saving shared content"
123 case .saved: "Ready in T3 Code"
124 case .failed: "Could not add this"
125 }
126 }
127
128 private var message: String {
129 switch phase {
130 case .ready:
131 "Text, links, and up to eight files will be waiting in the native composer."
132 case .saving:
133 "Keeping a durable copy so nothing gets lost."
134 case let .saved(imageCount):
135 imageCount == 0
136 ? "Open T3 Code to choose a project and send it."
137 : "Saved \(imageCount) image\(imageCount == 1 ? "" : "s"). Open T3 Code to choose a project."
138 case let .failed(message):
139 message
140 }
141 }
142
143 private var phaseSymbol: String {
144 switch phase {
145 case .ready: "square.and.arrow.up"
146 case .saving: "arrow.down.doc"
147 case .saved: "checkmark.circle.fill"
148 case .failed: "exclamationmark.triangle.fill"
149 }
150 }
151
152 private var phaseTint: Color {
153 switch phase {
154 case .saved: Color(uiColor: .systemGreen)
155 case .failed: Color(uiColor: .systemRed)
156 default: Color(uiColor: .label)
157 }
158 }
159
160 private var primaryTitle: String {
161 switch phase {
162 case .ready: "Add to T3 Code"
163 case .saving: "Saving…"
164 case .saved: "Done"
165 case .failed: "Try again"
166 }
167 }
168
169 private func primaryAction() {
170 switch phase {
171 case .ready, .failed:
172 phase = .saving
173 Task {
174 do {
175 let envelope = try await save()
176 phase = .saved(imageCount: envelope.images.count + envelope.files.count)
177 } catch {
178 phase = .failed(
179 message: (error as? LocalizedError)?.errorDescription
180 ?? "The shared content could not be saved."
181 )
182 }
183 }
184 case .saved:
185 complete()
186 case .saving:
187 break
188 }
189 }
190}