taler-ios

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

IconBadge.swift (4766B)


      1 /*
      2  * This file is part of GNU Taler, ©2022-25 Taler Systems S.A.
      3  * See LICENSE.md
      4  */
      5 /**
      6  * @author Marc Stibane
      7  */
      8 import SwiftUI
      9 
     10 struct PendingIconBadge: View {
     11     let foreColor:Color
     12     let done: Bool
     13     let incoming: Bool
     14     let shouldConfirm: Bool
     15     let needsKYC: Bool
     16 
     17     var body: some View {
     18         let image = incoming && done ? Image(systemName: DONE_INCOMING)         // "plus.circle.fill"
     19                           : incoming ? Image(systemName: PENDING_INCOMING)      // "plus"
     20         // since outgoing money already left the wallet, show DONE_ and not PENDING_OUTGOING
     21                                      : Image(systemName: DONE_OUTGOING)         // "minus.circle"
     22         IconBadge(image: image,
     23                    done: false,
     24               foreColor: foreColor,
     25           shouldConfirm: shouldConfirm,
     26                needsKYC: needsKYC,
     27                wideIcon: nil)
     28     }
     29 }
     30 // MARK: -
     31 struct TransactionIconBadge: View {
     32     var type: TransactionType
     33     var foreColor: Color
     34     let done: Bool
     35     let incoming: Bool
     36     let shouldConfirm: Bool
     37     let needsKYC: Bool
     38 
     39     init(type: TransactionType, foreColor: Color, done: Bool, incoming: Bool, shouldConfirm: Bool, needsKYC: Bool) {
     40         self.type = type
     41         self.foreColor = foreColor
     42         self.done = done
     43         self.incoming = incoming
     44         self.shouldConfirm = shouldConfirm
     45         self.needsKYC = needsKYC
     46     }
     47 
     48     init(from transaction: TalerTransaction, isDark: Bool, _ increasedContrast: Bool) {
     49         let common = transaction.common
     50         self.type = common.type
     51         let done = transaction.isDone
     52         self.done = done
     53         let incoming = common.isIncoming
     54         self.incoming = incoming
     55         let needsKYC = transaction.isPendingKYC || transaction.isPendingKYCauth
     56         self.needsKYC = needsKYC
     57         self.shouldConfirm = transaction.shouldConfirm
     58         let pending = transaction.isPending || transaction.common.isFinalizing
     59         self.foreColor = .primary
     60 
     61         let doneOrPending = done || pending
     62         let isZero = common.amountEffective.isZero
     63         let refreshZero = common.type.isRefresh && isZero
     64         let textColor = doneOrPending ? .primary
     65                              : isDark ? .secondary
     66                   : increasedContrast ? Color(.darkGray)
     67                                       : .secondary  // Color(.tertiaryLabel)
     68         let foreColor = refreshZero ? textColor
     69                           : pending ? WalletColors().pendingColor(incoming)
     70                              : done ? WalletColors().transactionColor(incoming)
     71                                     : WalletColors().uncompletedColor
     72         self.foreColor = foreColor
     73     }
     74 
     75     var body: some View {
     76         IconBadge(image: type.icon(done),
     77                    done: true,
     78               foreColor: foreColor,
     79           shouldConfirm: shouldConfirm,
     80                needsKYC: needsKYC,
     81                wideIcon: TransactionType.refund.icon())
     82                       // "arrowshape.turn.up.backward" is wider than all others
     83     }
     84 }
     85 // MARK: -
     86 struct ButtonIconBadge: View {
     87     var type: TransactionType
     88     let foreColor:Color
     89     let done: Bool
     90 
     91     var body: some View {
     92         IconBadge(image: type.icon(done),
     93                    done: true,
     94               foreColor: foreColor,
     95           shouldConfirm: false,
     96                needsKYC: false,
     97                wideIcon: TransactionType.peerPushDebit.icon())
     98                       // button is send/receive/withdraw/deposit, never payment or refund
     99     }
    100 }
    101 // MARK: -
    102 struct IconBadge: View {
    103     let image: Image
    104     let done: Bool
    105     let foreColor:Color
    106     let shouldConfirm: Bool
    107     let needsKYC: Bool
    108     let wideIcon: Image?        // cheating: ZStack with widest icon to ensure all have the same width
    109                                 // TODO: EqualIconWidth...
    110 
    111     @ScaledMetric var spacing = 6       // relative to fontSize
    112 
    113     var body: some View {
    114         HStack(alignment: .top, spacing: -spacing) {
    115             ZStack {
    116                 if let wideIcon {
    117                     wideIcon.foregroundColor(.clear)
    118                 }
    119                 image.foregroundColor(foreColor)
    120             }
    121             // ZStack centers the main icon, so the badge will always be at the same position
    122             let badgeName = needsKYC ? NEEDS_KYC
    123                                      : CONFIRM_BANK
    124             Image(systemName: badgeName)
    125                 .talerFont(.badge)
    126                 .foregroundColor(needsKYC ? WalletColors().attention
    127                           : shouldConfirm ? WalletColors().confirm
    128                                           : .clear)
    129                 .padding(.top, -2)
    130         }.accessibilityHidden(true)
    131     }
    132 }
    133 // MARK: -
    134 //#Preview {
    135 //    IconBadge()
    136 //}