TwoRowButtons.swift (6158B)
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 import taler_swift 10 11 let LINELIMIT = 5 12 13 struct TypeButton: View { 14 let stack: CallStack 15 let title: String 16 let a11y: String 17 var type: TransactionType 18 let disabled: Bool 19 let action: NSNotification.Name 20 21 @AppStorage("minimalistic") var minimalistic: Bool = false 22 23 @MainActor 24 func dismissAndPost() { 25 dismissTop(stack.push()) 26 NotificationCenter.default.post(name: action, object: nil) // will trigger NavigationLink 27 } 28 29 var body: some View { 30 #if DEBUG 31 let debug = 1==0 32 let red = debug ? Color.red : Color.clear 33 let green = debug ? Color.green : Color.clear 34 let blue = debug ? Color.blue : Color.clear 35 let orange = debug ? Color.orange : Color.clear 36 #endif 37 let disabledColor = WalletColors().gray2 38 let talerColor = WalletColors().talerColor 39 let badge = ButtonIconBadge(type: type, 40 foreColor: disabled ? disabledColor : talerColor, 41 done: false) 42 .talerFont(.title2) 43 let hLayout = HStack { 44 badge 45 Spacer() 46 Text(title) 47 .fixedSize(horizontal: true, vertical: false) 48 Spacer() 49 } 50 #if DEBUG 51 .border(red) 52 #endif 53 54 let vLayout = VStack { 55 let fragments = title.components(separatedBy: "\n") 56 let fragCount = fragments.count 57 if fragCount > 1 { 58 Text(fragments[0]) 59 HStack { 60 badge 61 Spacer() 62 Text(fragments[1]) 63 .fixedSize(horizontal: true, vertical: false) 64 Spacer() 65 } 66 #if DEBUG 67 .border(orange) 68 #endif 69 if fragCount > 2 { 70 Text(fragments[2]) 71 // .fixedSize(horizontal: true, vertical: false) 72 #if DEBUG 73 .border(green) 74 #endif 75 } 76 } else { 77 hLayout 78 } 79 } 80 #if DEBUG 81 .border(red) 82 #endif 83 84 Button(action: dismissAndPost) { 85 if minimalistic { 86 badge 87 } else { 88 if #available(iOS 16.4, *) { 89 ViewThatFits(in: .horizontal) { 90 hLayout 91 vLayout 92 } 93 } else { vLayout } // view for iOS 15 94 } 95 } 96 .accessibilityLabel(Text(a11y)) 97 .lineLimit(LINELIMIT) 98 .disabled(disabled) 99 .buttonStyle(TalerButtonStyle(type: .bordered, 100 dimmed: false, 101 disabled: disabled, 102 aligned: .center)) 103 } 104 } 105 // MARK: - 106 struct TwoRowButtons: View { 107 let stack: CallStack 108 let sendTitle: String 109 var sendType: TransactionType 110 let sendA11y: String 111 let recvTitle: String 112 var recvType: TransactionType 113 let recvA11y: String 114 let sendDisabled: Bool // can't send/deposit if wallet has no coins at all 115 let sendAction: NSNotification.Name 116 let recvDisabled: Bool // can't receive/withdraw if wallet has no payments services 117 let recvAction: NSNotification.Name 118 119 func sendButton(_ title: String) -> TypeButton { 120 TypeButton(stack: stack.push(), 121 title: title, 122 a11y: sendA11y, 123 type: sendType, 124 disabled: sendDisabled, 125 action: sendAction) 126 } 127 128 func recvButton(_ title: String) -> TypeButton { 129 TypeButton(stack: stack.push(), 130 title: title, 131 a11y: recvA11y, 132 type: recvType, 133 disabled: recvDisabled, 134 action: recvAction) 135 } 136 137 var body: some View { 138 let hLayout = HStack(spacing: HSPACING) { 139 // side by side, text in 1+ lines (\t -> \n) 140 sendButton(sendTitle.tabbed(oneLine: false)) 141 recvButton(recvTitle.tabbed(oneLine: false)) 142 } 143 let vLayout = VStack(alignment: .leading) { 144 // one below the other, text in one line (\t -> " ") 145 sendButton(sendTitle.tabbed(oneLine: true)) 146 recvButton(recvTitle.tabbed(oneLine: true)) 147 } 148 149 if #available(iOS 16.4, *) { 150 ViewThatFits(in: .horizontal) { 151 hLayout 152 // .border(.green) 153 vLayout 154 // .border(.red) 155 } 156 } else { vLayout } // iOS 15 has no ViewThatFits 157 } 158 } 159 // MARK: - 160 #if DEBUG 161 struct TwoRowButtons_Previews: PreviewProvider { 162 static var previews: some View { 163 List { 164 TwoRowButtons(stack: CallStack("Preview"), 165 sendTitle: "Send " + TESTCURRENCY, 166 sendType: .peerPushDebit, 167 sendA11y: "Send " + TESTCURRENCY, 168 recvTitle: "Request " + LONGCURRENCY, 169 recvType: .peerPullCredit, 170 recvA11y: "Request " + LONGCURRENCY, 171 sendDisabled: true, 172 sendAction: .SendAction, 173 recvDisabled: false, 174 recvAction: .RequestAction) 175 .listRowSeparator(.hidden) 176 TwoRowButtons(stack: CallStack("Preview"), 177 sendTitle: "Send " + DEMOCURRENCY, 178 sendType: .peerPushDebit, 179 sendA11y: "Send " + DEMOCURRENCY, 180 recvTitle: "Request " + DEMOCURRENCY, 181 recvType: .peerPullCredit, 182 recvA11y: "Request " + DEMOCURRENCY, 183 sendDisabled: true, 184 sendAction: .SendAction, 185 recvDisabled: false, 186 recvAction: .RequestAction) 187 .listRowSeparator(.hidden) 188 } 189 } 190 } 191 #endif