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