Skip to content

Commit 5b438c7

Browse files
committed
feat: custom scanner view
1 parent 0ebac66 commit 5b438c7

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

BDKSwiftExampleWallet/Resources/Localizable.xcstrings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@
340340
}
341341
}
342342
}
343+
},
344+
"Error" : {
345+
343346
},
344347
"Esplora Server" : {
345348
"localizations" : {
@@ -465,6 +468,9 @@
465468
},
466469
"Outputs" : {
467470

471+
},
472+
"Paste Address" : {
473+
468474
},
469475
"Receive" : {
470476
"localizations" : {

BDKSwiftExampleWallet/View/Send/AddressView.swift

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Matthew Ramsden on 9/15/23.
66
//
77

8+
import AVFoundation
89
import BitcoinUI
910
import CodeScanner
1011
import SwiftUI
@@ -63,10 +64,10 @@ struct AddressView: View {
6364
.foregroundColor(Color(UIColor.label))
6465
.padding(.top)
6566
.sheet(isPresented: $isShowingScanner) {
66-
CodeScannerView(
67+
CustomScannerView(
6768
codeTypes: [.qr],
68-
simulatedData: "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2",
69-
completion: handleScan
69+
completion: handleScan,
70+
pasteAction: pasteAddress
7071
)
7172
}
7273

@@ -115,6 +116,13 @@ struct AddressView: View {
115116
}
116117
.padding()
117118
.navigationTitle("Address")
119+
.alert(isPresented: $isShowingAlert) {
120+
Alert(
121+
title: Text("Error"),
122+
message: Text(alertMessage),
123+
dismissButton: .default(Text("OK"))
124+
)
125+
}
118126

119127
}
120128

@@ -143,6 +151,44 @@ extension AddressView {
143151
isShowingAlert = true
144152
}
145153
}
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+
}
146192
}
147193

148194
#if DEBUG

0 commit comments

Comments
 (0)