Files
damus/damus/Views/Wallet/NWCScannerView.swift
Daniel D’Aquino 50ef6600a8 Make QR code scanning more robust
1. Removed the dependency on finding the profile event for displaying actions to the user, even if the full profile couldn't be loaded. This allowed showing useful options such as the option to follow that pubkey.
2. Opened a profile preview sheet instead of navigating to the full profile page, enabling quick actions and saving bandwidth by not loading their timeline immediately.
3. Refactored most of that view to simplify state management and make it less prone to errors.
4. Improved error handling and management.
5. Ensured the view truly reflected the internal state of the scanner to the user.

Changelog-Fixed: Fixed some issues where QR code would not work, and improved UX
Closes: https://github.com/damus-io/damus/issues/2032
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2024-11-15 15:41:04 -08:00

82 lines
1.9 KiB
Swift

//
// QRScannerView.swift
// damus
//
// Created by William Casarin on 2023-05-09.
//
import SwiftUI
import CodeScanner
enum WalletScanResult: Equatable {
static func == (lhs: WalletScanResult, rhs: WalletScanResult) -> Bool {
switch lhs {
case .success(let a):
switch rhs {
case .success(let b):
return a == b
case .failed:
return false
case .scanning:
return false
}
case .failed:
switch rhs {
case .success:
return false
case .failed:
return true
case .scanning:
return false
}
case .scanning:
switch rhs {
case .success:
return false
case .failed:
return false
case .scanning:
return true
}
}
}
case success(WalletConnectURL)
case failed
case scanning
}
struct WalletScannerView: View {
@Binding var result: WalletScanResult
@Environment(\.dismiss) var dismiss
var body: some View {
VStack {
CodeScannerView(codeTypes: [.qr]) { res in
switch res {
case .success(let success):
guard let url = WalletConnectURL(str: success.string) else {
result = .failed
dismiss()
return
}
result = .success(url)
case .failure:
result = .failed
}
dismiss()
}
}
}
}
struct QRScannerView_Previews: PreviewProvider {
@State static var result: WalletScanResult = .scanning
static var previews: some View {
WalletScannerView(result: $result)
}
}