CopyShare.swift (12495B)
1 /* 2 * This file is part of GNU Taler, ©2022-26 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import UniformTypeIdentifiers 9 import SwiftUI 10 import SymLog 11 12 @MainActor 13 struct FeedbackButton: View { 14 private let symLog = SymLogV(0) 15 let title: String? 16 let image: UIImage? 17 let isDisabled: Bool 18 let haptic: Bool 19 let action: () -> Void 20 21 @EnvironmentObject private var controller: Controller 22 @State private var scale: CGFloat = 1.0 23 24 public init(_ title: String? = nil, 25 image: UIImage? = nil, 26 disabled: Bool = false, 27 haptic: Bool = true, 28 action: @escaping @MainActor () -> Void) { 29 self.title = title 30 self.image = image 31 self.isDisabled = disabled 32 self.haptic = haptic 33 self.action = action 34 } 35 36 private func triggerPulse() { 37 // First scale down quickly 38 withAnimation(.easeIn(duration: 0.1)) { 39 scale = 0.8 40 } 41 // Then bounce back bigger 42 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 43 withAnimation(.easeOut(duration: 0.25)) { 44 scale = 1.15 45 } 46 } 47 // Finally settle to normal 48 DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { 49 withAnimation(.easeInOut(duration: 0.25)) { 50 scale = 1.0 51 } 52 } 53 } 54 55 var body: some View { 56 Button(title ?? EMPTYSTRING) { 57 symLog.log(title ?? EMPTYSTRING) 58 if haptic { 59 controller.hapticFeedback(.medium) 60 } 61 action() 62 triggerPulse() 63 } 64 .buttonStyle(TalerButtonStyle(type: .bordered, disabled: isDisabled)) 65 } 66 } 67 // MARK: - 68 @MainActor 69 struct CopyButton: View { 70 private let symLog = SymLogV(0) 71 let textToCopy: String 72 @Binding var isCopied: Bool? 73 let image: UIImage? 74 let vertical: Bool 75 let title: String? 76 let a11yID: String? // unique ID for UI-testing 77 78 @Environment(\.isEnabled) private var isEnabled: Bool 79 @EnvironmentObject private var controller: Controller 80 81 @State private var scale: CGFloat = 1.0 82 83 init(_ textToCopy: String, isCopied: Binding<Bool?>? = nil, 84 vertical: Bool, image: UIImage? = nil, a11yID: String? = nil) { 85 self.textToCopy = textToCopy 86 self._isCopied = isCopied ?? Binding.constant(nil) 87 self.image = image 88 self.vertical = vertical 89 self.title = nil 90 self.a11yID = a11yID 91 } 92 93 init(_ textToCopy: String, isCopied: Binding<Bool?>? = nil, 94 title: String, image: UIImage? = nil, a11yID: String? = nil) { 95 self.textToCopy = textToCopy 96 self._isCopied = isCopied ?? Binding.constant(nil) 97 self.image = image 98 self.vertical = false 99 self.title = title 100 self.a11yID = a11yID 101 } 102 103 func copyAction() -> Void { 104 symLog.log(textToCopy) 105 #if DEBUG 106 print("CopyButton: \(textToCopy)") 107 #endif 108 triggerPulse() 109 controller.hapticFeedback(.medium) 110 let pasteboard = UIPasteboard.general 111 if let image { 112 // pasteboard.image = image 113 let strItem = [UTType.plainText.identifier : textToCopy] 114 let imgItem = [UTType.image.identifier : image] 115 pasteboard.items = [imgItem, strItem] // iOS27 Notes.app crashes if text comes first! 116 } else { 117 pasteboard.string = textToCopy 118 } 119 if isCopied != nil { 120 isCopied = true 121 } 122 } 123 124 private func triggerPulse() { 125 // First scale down quickly 126 withAnimation(.easeIn(duration: 0.1)) { 127 scale = 0.8 128 } 129 // Then bounce back bigger 130 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 131 withAnimation(.easeOut(duration: 0.25)) { 132 scale = 1.15 133 } 134 } 135 // Finally settle to normal 136 DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { 137 withAnimation(.easeInOut(duration: 0.25)) { 138 scale = 1.0 139 } 140 } 141 } 142 143 var body: some View { 144 Button(action: copyAction) { 145 let image = Image(systemName: COPY1) // 146 .accessibility(hidden: true) 147 148 if vertical { 149 VStack { 150 let shortCopy = String(localized: "Copy.short", defaultValue: "Copy", comment: "5 letters max, else abbreviate") 151 image 152 Text(shortCopy) 153 } 154 } else { 155 let longCopy = String(localized: "Copy.long", defaultValue: "Copy", comment: "may be a bit longer") 156 HStack { 157 image 158 Text(title ?? longCopy) 159 } 160 } 161 } 162 .tint(WalletColors().primaryAccent) 163 .talerFont(.body) 164 .scaleEffect(scale) 165 .disabled(!isEnabled) 166 .accessibilityIdentifier(a11yID ?? A11yID.copyButton) 167 } 168 } 169 // MARK: - 170 struct ShareType: Hashable { 171 let textToShare: String 172 let image: UIImage? 173 } 174 // MARK: - 175 /// The Share function can NOT be called from a sheet. 176 /// Thus we need to know whether we were called from the .sheet view hierarchy 177 /// If yes, then we must first dismiss the sheet before posting the .ShareAction notification 178 extension EnvironmentValues { 179 @Entry var fromSheet: Bool = false 180 } 181 // MARK: - 182 @MainActor 183 struct ShareButton: View { 184 private let symLog = SymLogV(0) 185 let textToShare: String 186 let image: UIImage? 187 let title: String 188 let a11yID: String? 189 190 init(_ textToShare: String, image: UIImage? = nil, a11yID: String? = nil) { 191 self.textToShare = textToShare 192 self.image = image 193 self.title = String(localized: "Share") 194 self.a11yID = a11yID 195 } 196 init(_ textToShare: String, title: String, image: UIImage? = nil, 197 a11yID: String? = nil) { 198 self.textToShare = textToShare 199 self.image = image 200 self.title = title 201 self.a11yID = a11yID 202 } 203 204 @Environment(\.fromSheet) private var fromSheet 205 @Environment(\.isEnabled) private var isEnabled: Bool 206 @EnvironmentObject private var controller: Controller 207 208 @State private var scale: CGFloat = 1.0 209 210 @MainActor 211 func dismissAndPost() { 212 if fromSheet { 213 dismissTop() 214 } 215 let shareType = ShareType(textToShare: textToShare, image: image) 216 let userInfo = [NOTIFICATIONSHARE: shareType] 217 NotificationCenter.default.post(name: .ShareAction, object: nil, userInfo: userInfo) // will trigger NavigationLink 218 } 219 220 func shareAction() -> Void { 221 symLog.log(textToShare) 222 controller.hapticFeedback(.soft) 223 Task { 224 // First scale down quickly 225 withAnimation(.easeIn(duration: 0.1)) { 226 scale = 0.8 227 } 228 // Then bounce back bigger 229 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 230 withAnimation(.easeOut(duration: 0.25)) { 231 scale = 1.15 232 } 233 dismissAndPost() 234 } 235 // Finally settle to normal 236 DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { 237 withAnimation(.easeInOut(duration: 0.25)) { 238 scale = 1.0 239 } 240 } 241 } 242 } 243 244 var body: some View { 245 Button(action: shareAction) { 246 HStack { 247 Image(systemName: SHARE) // 248 .accessibility(hidden: true) 249 Text(title) 250 } 251 } 252 .tint(WalletColors().primaryAccent) 253 .talerFont(.body) 254 .scaleEffect(scale) 255 .disabled(!isEnabled) 256 .accessibilityIdentifier(a11yID ?? A11yID.shareButton) 257 } 258 } 259 // MARK: - 260 #if GNU_TALER || TALER_NIGHTLY 261 @MainActor 262 struct ContactsButton: View { 263 private let symLog = SymLogV(0) 264 let stack: CallStack 265 let talerUri: String? 266 let subject: String? 267 let title: String 268 269 init(_ stack: CallStack, talerUri: String?, subject: String? = nil, title: String? = nil) { 270 self.stack = stack 271 self.talerUri = talerUri 272 self.title = title ?? String(localized: "Contacts") 273 self.subject = subject 274 } 275 276 @Environment(\.isEnabled) private var isEnabled: Bool 277 @EnvironmentObject private var controller: Controller 278 279 @State private var scale: CGFloat = 1.0 280 @State private var showContacts: Bool = false 281 282 func shareAction() -> Void { 283 symLog.log(talerUri) 284 controller.hapticFeedback(.soft) 285 Task { 286 // First scale down quickly 287 withAnimation(.easeIn(duration: 0.1)) { 288 scale = 0.8 289 } 290 // Then bounce back bigger 291 DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 292 withAnimation(.easeOut(duration: 0.25)) { 293 scale = 1.15 294 } 295 showContacts = true 296 } 297 // Finally settle to normal 298 DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) { 299 withAnimation(.easeInOut(duration: 0.25)) { 300 scale = 1.0 301 } 302 } 303 } 304 } 305 306 var body: some View { 307 if let talerUri { 308 let contactDest = ContactsView(stack: stack.push(), talerURI: talerUri, subject: subject) 309 let actions = Group { 310 NavLink($showContacts) { contactDest } 311 } 312 Button(action: shareAction) { 313 HStack { 314 Image(systemName: ICONNAME_PERSON3) // 315 .accessibility(hidden: true) 316 Text(title) 317 } 318 } 319 .background(actions) 320 .tint(WalletColors().primaryAccent) 321 .talerFont(.body) 322 .scaleEffect(scale) 323 .disabled(!isEnabled) 324 } 325 } 326 } 327 #endif 328 // MARK: - 329 struct CopyShare: View { 330 let stack: CallStack 331 let textToCopy: String // either talerURI, or report 332 let image: UIImage? // either QRcode of talerURI, or nil (report) 333 let subject: String? 334 let talerURI: String? 335 336 @Environment(\.isEnabled) private var isEnabled: Bool 337 338 init(_ stack: CallStack, _ string: String, image: UIImage? = nil, 339 subject: String? = nil, talerURI: String? = nil 340 ) { 341 self.stack = stack 342 self.textToCopy = string 343 self.image = image 344 self.subject = subject 345 #if GNU_TALER || TALER_NIGHTLY 346 self.talerURI = talerURI 347 #else 348 self.talerURI = nil 349 #endif 350 } 351 352 var body: some View { 353 let withContacts: Bool = talerURI != nil 354 let copyB = CopyButton(textToCopy, vertical: false, image: image) 355 .buttonStyle(TalerButtonStyle(type: .bordered)) 356 let share = ShareButton(textToCopy, image: image) 357 .buttonStyle(TalerButtonStyle(type: .bordered)) 358 #if GNU_TALER || TALER_NIGHTLY 359 let contacts = ContactsButton(stack.push(), talerUri: talerURI, subject: subject) 360 .buttonStyle(TalerButtonStyle(type: .bordered)) 361 #else 362 let contacts = EmptyView() 363 #endif 364 365 let vLayout = VStack { 366 copyB 367 share 368 if withContacts { 369 contacts 370 } 371 } 372 373 if #available(iOS 16.0, *) { 374 let hLayout = HStack(spacing: HSPACING) { 375 copyB 376 share 377 if withContacts { 378 contacts 379 } 380 } 381 382 if withContacts { 383 let mLayout = VStack { 384 HStack(spacing: HSPACING) { 385 copyB 386 share 387 } 388 contacts 389 } 390 391 ViewThatFits(in: .horizontal) { 392 hLayout 393 mLayout 394 vLayout 395 } 396 } else { 397 ViewThatFits(in: .horizontal) { 398 hLayout 399 vLayout 400 } 401 } 402 } else { 403 vLayout 404 } 405 } 406 } 407 // MARK: - 408 struct CopyShare_Previews: PreviewProvider { 409 static var previews: some View { 410 CopyShare(CallStack("Preview"), "Hallö", image: nil) 411 } 412 }