Skip to content

ui: reorder addressview amountview #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@
AE2381B82C61254B00F6B00C /* Send */ = {
isa = PBXGroup;
children = (
AE7839FC2AB4E18D005F0CBA /* AmountView.swift */,
AE783A022AB4ECC2005F0CBA /* AddressView.swift */,
AE7839FC2AB4E18D005F0CBA /* AmountView.swift */,
AE783A062AB4F7C7005F0CBA /* FeeView.swift */,
AE783A002AB4E5E1005F0CBA /* BuildTransactionView.swift */,
);
Expand Down
2 changes: 2 additions & 0 deletions BDKSwiftExampleWallet/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
}
},
"Address" : {
"extractionState" : "stale",
"localizations" : {
"fr" : {
"stringUnit" : {
Expand Down Expand Up @@ -332,6 +333,7 @@

},
"Enter address to send BTC to" : {
"extractionState" : "stale",
"localizations" : {
"fr" : {
"stringUnit" : {
Expand Down
4 changes: 2 additions & 2 deletions BDKSwiftExampleWallet/View/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ struct HomeView: View {
}

enum NavigationDestination: Hashable {
case amount
case address(amount: String)
case address
case amount(address: String)
case fee(amount: String, address: String)
case buildTransaction(amount: String, address: String, fee: Int)
}
Expand Down
147 changes: 53 additions & 94 deletions BDKSwiftExampleWallet/View/Send/AddressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import CodeScanner
import SwiftUI

struct AddressView: View {
let amount: String
@State var address: String = ""
@Binding var navigationPath: NavigationPath
let pasteboard = UIPasteboard.general
@State private var isShowingScanner = false
@State private var isShowingAlert = false
@State private var alertMessage = ""

Expand All @@ -24,78 +22,11 @@ struct AddressView: View {
ZStack {
Color(uiColor: .systemBackground)

VStack {

HStack {

Spacer()

Button {
isShowingScanner = true
} label: {
HStack {
Image(systemName: "qrcode.viewfinder")
.minimumScaleFactor(0.5)
}
}

}
.font(.largeTitle)
.foregroundColor(Color(UIColor.label))
.padding(.top)
.sheet(isPresented: $isShowingScanner) {
CustomScannerView(
codeTypes: [.qr],
completion: handleScan,
pasteAction: pasteAddress
)
}

Spacer()

VStack {
HStack {
Text("Address")
.bold()
Spacer()
}
.padding(.horizontal, 15.0)
TextField(
"Enter address to send BTC to",
text: $address
)
.truncationMode(.middle)
.submitLabel(.done)
.lineLimit(1)
.padding()
}

AddressFormattedView(
address: address,
columns: 4,
spacing: 20.0,
gridItemSize: 60.0
)
.padding()

Spacer()

Button {
navigationPath.append(
NavigationDestination.fee(amount: amount, address: address)
)
} label: {
Label(
title: { Text("Next") },
icon: { Image(systemName: "arrow.right") }
)
.labelStyle(.iconOnly)
}
.buttonStyle(BitcoinOutlined(width: 100, isCapsule: true))

}
.padding()
.navigationTitle("Address")
CustomScannerView(
codeTypes: [.qr],
completion: handleScan,
pasteAction: pasteAddress
)
.alert(isPresented: $isShowingAlert) {
Alert(
title: Text("Error"),
Expand All @@ -112,7 +43,6 @@ struct AddressView: View {

extension AddressView {
func handleScan(result: Result<ScanResult, ScanError>) {
isShowingScanner = false
switch result {
case .success(let result):
let scannedAddress = result.string.lowercased().replacingOccurrences(
Expand All @@ -122,6 +52,7 @@ extension AddressView {
let components = scannedAddress.components(separatedBy: "?")
if let bitcoinAddress = components.first {
address = bitcoinAddress
navigationPath.append(NavigationDestination.amount(address: bitcoinAddress))
} else {
alertMessage = "The scanned QR code did not contain a valid Bitcoin address."
isShowingAlert = true
Expand All @@ -133,17 +64,23 @@ extension AddressView {
}

private func pasteAddress() {
if pasteboard.hasStrings {
if let string = pasteboard.string {
let lowercaseAddress = string.lowercased()
address = lowercaseAddress
isShowingScanner = false
} else {
alertMessage = "Unable to get the string from the pasteboard."
if let pasteboardContent = UIPasteboard.general.string {
if pasteboardContent.isEmpty {
alertMessage = "The pasteboard is empty."
isShowingAlert = true
return
}
let trimmedContent = pasteboardContent.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedContent.isEmpty {
alertMessage = "The pasteboard contains only whitespace."
isShowingAlert = true
return
}
let lowercaseAddress = trimmedContent.lowercased()
address = lowercaseAddress
navigationPath.append(NavigationDestination.amount(address: address))
} else {
alertMessage = "No strings found in the pasteboard."
alertMessage = "Unable to access the pasteboard. Please try copying the address again."
isShowingAlert = true
}
}
Expand All @@ -153,35 +90,57 @@ struct CustomScannerView: View {
let codeTypes: [AVMetadataObject.ObjectType]
let completion: (Result<ScanResult, ScanError>) -> Void
let pasteAction: () -> Void
@Environment(\.dismiss) private var dismiss

var body: some View {
ZStack(alignment: .bottom) {
CodeScannerView(codeTypes: codeTypes, completion: completion)
GeometryReader { geometry in
ZStack(alignment: .top) {
CodeScannerView(codeTypes: codeTypes, completion: completion)
.edgesIgnoringSafeArea(.all)

Button(action: pasteAction) {
Text("Paste Address")
.padding()
.foregroundColor(.primary)
.background(Color.white.opacity(0.5))
.clipShape(Capsule())
VStack {
HStack {
Button(action: { dismiss() }) {
Image(systemName: "xmark")
.foregroundColor(.white)
.font(.system(size: 20, weight: .bold))
.frame(width: 44, height: 44)
.background(Color.black.opacity(0.6))
.clipShape(Circle())
}
.padding(.top, 50)
.padding(.leading, 20)

Spacer()
}

Spacer()

Button(action: pasteAction) {
Text("Paste Address")
.padding()
.foregroundColor(.primary)
.background(Color.white.opacity(0.8))
.clipShape(Capsule())
}
.padding(.bottom, geometry.safeAreaInsets.bottom + 20)
}
}
.padding(.bottom, 20)
}
.navigationBarHidden(true)
.edgesIgnoringSafeArea(.all)
}
}

#if DEBUG
#Preview {
AddressView(
amount: "200",
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
navigationPath: .constant(NavigationPath())
)
}
#Preview {
AddressView(
amount: "200",
address: "tb1pw6y0vtmsn46epvz0j8ddc46ketmp28t82p22hcrrkch3a0jhu40qe267dl",
navigationPath: .constant(NavigationPath())
)
Expand Down
6 changes: 4 additions & 2 deletions BDKSwiftExampleWallet/View/Send/AmountView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BitcoinUI
import SwiftUI

struct AmountView: View {
let address: String
@Bindable var viewModel: AmountViewModel
@State var numpadAmount = "0"
@Binding var navigationPath: NavigationPath
Expand Down Expand Up @@ -61,7 +62,7 @@ struct AmountView: View {

Button {
navigationPath.append(
NavigationDestination.address(amount: numpadAmount)
NavigationDestination.fee(amount: numpadAmount, address: address)
)
} label: {
Label(
Expand Down Expand Up @@ -140,16 +141,17 @@ struct NumpadButton: View {
#if DEBUG
#Preview {
AmountView(
address: "address",
viewModel: .init(bdkClient: .mock),
navigationPath: .constant(NavigationPath())
)
}

#Preview {
AmountView(
address: "address",
viewModel: .init(bdkClient: .mock),
navigationPath: .constant(NavigationPath())

)
.environment(\.dynamicTypeSize, .accessibility5)
}
Expand Down
14 changes: 9 additions & 5 deletions BDKSwiftExampleWallet/View/WalletView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct WalletView: View {
Spacer()

Button(action: {
sendNavigationPath.append(NavigationDestination.amount)
sendNavigationPath.append(NavigationDestination.address)
}) {
Image(systemName: "qrcode.viewfinder")
.font(.title)
Expand Down Expand Up @@ -220,10 +220,14 @@ struct WalletView: View {
}
.navigationDestination(for: NavigationDestination.self) { destination in
switch destination {
case .amount:
AmountView(viewModel: .init(), navigationPath: $sendNavigationPath)
case .address(let amount):
AddressView(amount: amount, navigationPath: $sendNavigationPath)
case .address:
AddressView(navigationPath: $sendNavigationPath)
case .amount(let address):
AmountView(
address: address,
viewModel: .init(),
navigationPath: $sendNavigationPath
)
case .fee(let amount, let address):
FeeView(
amount: amount,
Expand Down
Loading