|
5 | 5 | // Created by Matthew Ramsden on 9/15/23.
|
6 | 6 | //
|
7 | 7 |
|
| 8 | +import AVFoundation |
8 | 9 | import BitcoinUI
|
9 | 10 | import CodeScanner
|
10 | 11 | import SwiftUI
|
@@ -63,10 +64,10 @@ struct AddressView: View {
|
63 | 64 | .foregroundColor(Color(UIColor.label))
|
64 | 65 | .padding(.top)
|
65 | 66 | .sheet(isPresented: $isShowingScanner) {
|
66 |
| - CodeScannerView( |
| 67 | + CustomScannerView( |
67 | 68 | codeTypes: [.qr],
|
68 |
| - simulatedData: "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", |
69 |
| - completion: handleScan |
| 69 | + completion: handleScan, |
| 70 | + pasteAction: pasteAddress |
70 | 71 | )
|
71 | 72 | }
|
72 | 73 |
|
@@ -115,6 +116,13 @@ struct AddressView: View {
|
115 | 116 | }
|
116 | 117 | .padding()
|
117 | 118 | .navigationTitle("Address")
|
| 119 | + .alert(isPresented: $isShowingAlert) { |
| 120 | + Alert( |
| 121 | + title: Text("Error"), |
| 122 | + message: Text(alertMessage), |
| 123 | + dismissButton: .default(Text("OK")) |
| 124 | + ) |
| 125 | + } |
118 | 126 |
|
119 | 127 | }
|
120 | 128 |
|
@@ -143,6 +151,44 @@ extension AddressView {
|
143 | 151 | isShowingAlert = true
|
144 | 152 | }
|
145 | 153 | }
|
| 154 | + |
| 155 | + private func pasteAddress() { |
| 156 | + if pasteboard.hasStrings { |
| 157 | + if let string = pasteboard.string { |
| 158 | + let lowercaseAddress = string.lowercased() |
| 159 | + address = lowercaseAddress |
| 160 | + isShowingScanner = false |
| 161 | + } else { |
| 162 | + alertMessage = "Unable to get the string from the pasteboard." |
| 163 | + isShowingAlert = true |
| 164 | + } |
| 165 | + } else { |
| 166 | + alertMessage = "No strings found in the pasteboard." |
| 167 | + isShowingAlert = true |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +struct CustomScannerView: View { |
| 173 | + let codeTypes: [AVMetadataObject.ObjectType] |
| 174 | + let completion: (Result<ScanResult, ScanError>) -> Void |
| 175 | + let pasteAction: () -> Void |
| 176 | + |
| 177 | + var body: some View { |
| 178 | + ZStack(alignment: .bottom) { |
| 179 | + CodeScannerView(codeTypes: codeTypes, completion: completion) |
| 180 | + |
| 181 | + Button(action: pasteAction) { |
| 182 | + Text("Paste Address") |
| 183 | + .padding() |
| 184 | + .foregroundColor(.primary) |
| 185 | + .background(Color.white.opacity(0.5)) |
| 186 | + .clipShape(Capsule()) |
| 187 | + |
| 188 | + } |
| 189 | + .padding(.bottom, 20) |
| 190 | + } |
| 191 | + } |
146 | 192 | }
|
147 | 193 |
|
148 | 194 | #if DEBUG
|
|
0 commit comments