taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

QRcodesForPayto.swift (3838B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-26 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 import OrderedCollections
     10 import taler_swift
     11 
     12 struct QRcodeCopyShare: View {
     13     let stack: CallStack
     14     let option: TransferOption
     15     @State private var qrImage: UIImage? = nil
     16 
     17     func textToShare() -> String? {
     18         if let paytoUri = option.paytoUri {
     19             let payto = PayTo(paytoUri)
     20             if let receiverStr = payto.receiver {
     21                 if let amountStr = payto.amountStr {
     22                     if let messageStr = option.qrReferenceNumber ?? payto.messageStr {
     23                         return String(receiverStr + LINEFEED + amountStr + LINEFEED + messageStr)
     24                     }
     25                 }
     26             }
     27         }
     28         return nil
     29     }
     30 
     31     var body: some View {
     32         let textToShare = textToShare()
     33         if let qrCodes = option.qrCodes {
     34             ForEach(qrCodes, id: \.self) { qrCodeSpec in
     35                 let logo = qrCodeSpec.type == "spc" ? Image(SWISS_QR) : nil
     36                 let size = 276.0    // 46x46mm, swiss flag = 7x7mm plus border  276*9/46 = 54
     37                 let image = QRCode(from: qrCodeSpec.qrContent)
     38                 if let image {
     39                     QRGeneratorView(image: image, size: size,
     40                                     logo: logo, logoSize: size * 9 / 46)
     41                         .frame(maxWidth: .infinity, alignment: .center)
     42                         .listRowSeparator(.hidden)
     43                 }
     44                 if let textToShare {
     45                     CopyShare(stack.push(), textToShare, image: image)
     46                         .disabled(false)
     47                 }
     48             }
     49         } else if let textToShare {
     50             CopyShare(stack.push(), textToShare)
     51                 .disabled(false)
     52         }
     53     }
     54 }
     55 
     56 struct QRcodesForPayto: View {
     57     let stack: CallStack
     58     var transferOptions: [TransferOption]
     59 
     60     let navTitle = String(localized: "Wire transfer", comment: "ViewTitle of wire-transfer QR codes")
     61 
     62     @AppStorage("minimalistic") var minimalistic: Bool = false
     63 
     64     var body: some View {
     65         List {
     66             if !minimalistic {
     67                 Text("If your banking software runs on another device, you can scan one of these QR codes:")
     68                     .listRowSeparator(.hidden)
     69             }
     70             ForEach(transferOptions, id: \.self) { option in
     71                 QRcodeCopyShare(stack: stack.push(), option: option)
     72                     .listRowSeparator(.automatic)
     73             }
     74         }
     75         .navigationTitle(navTitle)
     76         .onAppear() {
     77 //            symLog.log("onAppear")
     78             DebugViewC.shared.setViewID(VIEW_WITHDRAW_QRCODES, stack: stack.push())
     79         }
     80     }
     81 }
     82 // MARK: -
     83 #if DEBUG
     84 //struct QRcodesForPayto_Previews: PreviewProvider {
     85 //    static var previews: some View {
     86 //        let common = TransactionCommon(type: .withdrawal,
     87 //                                    txState: TransactionState(major: .done),
     88 //                            amountEffective: Amount(currency: LONGCURRENCY, cent: 110),
     89 //                                  amountRaw: Amount(currency: LONGCURRENCY, cent: 220),
     90 //                              transactionId: "someTxID",
     91 //                                  timestamp: Timestamp(from: 1_666_666_000_000),
     92 //                                  txActions: [])
     93 //        let payto = "payto://iban/SANDBOXX/DE159593?receiver-name=Exchange+Company"
     94 //        let details = WithdrawalDetails(type: .manual, 
     95 //                                  reservePub: "ReSeRvEpUbLiC_KeY_FoR_WiThDrAwAl",
     96 //                              reserveIsReady: false,
     97 //                                   confirmed: false)
     98 //        List {
     99 //            QRcodesForPayto(stack: CallStack("Preview"), qrCodeSpecs: details)
    100 //        }
    101 //    }
    102 //}
    103 #endif