ImageContextMenuModifier.swift (4805B)
1 // 2 // ImageContextMenuModifier.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-03-23. 6 // 7 8 import Foundation 9 import SwiftUI 10 import UIKit 11 12 struct ImageContextMenuModifier: ViewModifier { 13 let url: URL? 14 let image: UIImage? 15 let settings: UserSettingsStore 16 17 @State var qrCodeValue: String = "" 18 @State var open_link_confirm: Bool = false 19 @State var open_wallet_confirm: Bool = false 20 @State var not_found: Bool = false 21 22 @Binding var showShareSheet: Bool 23 24 @Environment(\.openURL) var openURL 25 26 func body(content: Content) -> some View { 27 return content.contextMenu { 28 Button { 29 UIPasteboard.general.url = url 30 } label: { 31 Label(NSLocalizedString("Copy Image URL", comment: "Context menu option to copy the URL of an image into clipboard."), image: "copy2") 32 } 33 if let someImage = image { 34 Button { 35 UIPasteboard.general.image = someImage 36 } label: { 37 Label(NSLocalizedString("Copy Image", comment: "Context menu option to copy an image into clipboard."), image: "copy2.fill") 38 } 39 Button { 40 UIImageWriteToSavedPhotosAlbum(someImage, nil, nil, nil) 41 } label: { 42 Label(NSLocalizedString("Save Image", comment: "Context menu option to save an image."), image: "download") 43 } 44 Button { 45 qrCodeValue = "" 46 guard let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh]) else { 47 return 48 } 49 guard let ciImage = CIImage(image:someImage) else { 50 return 51 } 52 let features = detector.features(in: ciImage) 53 if let qrfeatures = features as? [CIQRCodeFeature] { 54 for feature in qrfeatures { 55 if let msgStr = feature.messageString { 56 qrCodeValue = msgStr 57 } 58 } 59 } 60 61 if qrCodeValue == "" { 62 not_found.toggle() 63 } else { 64 if qrCodeValue.localizedCaseInsensitiveContains("lnurl") || qrCodeValue.localizedCaseInsensitiveContains("lnbc") { 65 open_wallet_confirm.toggle() 66 open_link_confirm.toggle() 67 } else if let _ = URL(string: qrCodeValue) { 68 open_link_confirm.toggle() 69 } 70 } 71 } label: { 72 Label(NSLocalizedString("Scan for QR Code", comment: "Context menu option to scan image for a QR Code."), image: "qr-code.fill") 73 } 74 } 75 Button { 76 showShareSheet = true 77 } label: { 78 Label(NSLocalizedString("Share", comment: "Button to share an image."), image: "upload") 79 } 80 } 81 .alert(NSLocalizedString("Found\n \(qrCodeValue)", comment: "Alert message asking if the user wants to open the link.").truncate(maxLength: 50), isPresented: $open_link_confirm) { 82 if open_wallet_confirm { 83 Button(NSLocalizedString("Open in wallet", comment: "Button to open the value found in browser."), role: .none) { 84 do { 85 try open_with_wallet(wallet: settings.default_wallet.model, invoice: qrCodeValue) 86 } 87 catch { 88 present_sheet(.select_wallet(invoice: qrCodeValue)) 89 } 90 } 91 } else { 92 Button(NSLocalizedString("Open in browser", comment: "Button to open the value found in browser."), role: .none) { 93 if let url = URL(string: qrCodeValue) { 94 openURL(url) 95 } 96 } 97 } 98 Button(NSLocalizedString("Copy", comment: "Button to copy the value found."), role: .none) { 99 UIPasteboard.general.string = qrCodeValue 100 } 101 Button(NSLocalizedString("Cancel", comment: "Button to cancel any interaction with the QRCode link."), role: .cancel) {} 102 } 103 .alert(NSLocalizedString("Unable to find a QR Code", comment: "Alert message letting user know a QR Code was not found."), isPresented: $not_found) { 104 Button(NSLocalizedString("Dismiss", comment: "Button to dismiss alert"), role: .cancel) {} 105 } 106 } 107 }