damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

NWCScannerView.swift (1961B)


      1 //
      2 //  QRScannerView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-05-09.
      6 //
      7 
      8 import SwiftUI
      9 
     10 enum WalletScanResult: Equatable {
     11     static func == (lhs: WalletScanResult, rhs: WalletScanResult) -> Bool {
     12         switch lhs {
     13         case .success(let a):
     14             switch rhs {
     15             case .success(let b):
     16                 return a == b
     17             case .failed:
     18                 return false
     19             case .scanning:
     20                 return false
     21             }
     22         case .failed:
     23             switch rhs {
     24             case .success:
     25                 return false
     26             case .failed:
     27                 return true
     28             case .scanning:
     29                 return false
     30             }
     31         case .scanning:
     32             switch rhs {
     33             case .success:
     34                 return false
     35             case .failed:
     36                 return false
     37             case .scanning:
     38                 return true
     39             }
     40         }
     41     }
     42     
     43     case success(WalletConnectURL)
     44     case failed
     45     case scanning
     46 }
     47 
     48 struct WalletScannerView: View {
     49     @Binding var result: WalletScanResult
     50     
     51     @Environment(\.dismiss) var dismiss
     52     
     53     var body: some View {
     54         VStack {
     55             CodeScannerView(codeTypes: [.qr]) { res in
     56                 switch res {
     57                 case .success(let success):
     58                     guard let url = WalletConnectURL(str: success.string) else {
     59                         result = .failed
     60                         dismiss()
     61                         return
     62                     }
     63                     
     64                     result = .success(url)
     65                 case .failure:
     66                     result = .failed
     67                 }
     68                 
     69                 dismiss()
     70             }
     71         }
     72     }
     73 }
     74 
     75 struct QRScannerView_Previews: PreviewProvider {
     76     @State static var result: WalletScanResult = .scanning
     77     static var previews: some View {
     78         WalletScannerView(result: $result)
     79     }
     80 }