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