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