Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'package:app_finance/_classes/controller/exchange_controller.dart';
5 : import 'package:app_finance/_classes/herald/app_design.dart';
6 : import 'package:app_finance/_classes/herald/app_locale.dart';
7 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
8 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
9 : import 'package:app_finance/_classes/controller/focus_controller.dart';
10 : import 'package:app_finance/_classes/storage/app_data.dart';
11 : import 'package:app_finance/_configs/theme_helper.dart';
12 : import 'package:app_finance/_ext/build_context_ext.dart';
13 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
14 : import 'package:app_finance/pages/_interfaces/interface_page_inject.dart';
15 : import 'package:app_finance/design/form/currency_exchange_input.dart';
16 : import 'package:app_finance/design/form/date_time_input.dart';
17 : import 'package:app_finance/design/button/full_sized_button_widget.dart';
18 : import 'package:app_finance/design/wrapper/row_widget.dart';
19 : import 'package:app_finance/design/form/simple_input.dart';
20 : import 'package:app_finance/design/form/tags_input.dart';
21 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
22 : import 'package:material_ui/material_ui.dart';
23 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
24 :
25 : class TransferTab<T> extends StatefulWidget {
26 : final String? accountFrom;
27 : final String? accountTo;
28 : final double? amount;
29 : final String? description;
30 : final Currency? currency;
31 : final DateTime? createdAt;
32 : final List<String>? tags;
33 : final AppData state;
34 : final bool isLeft;
35 : final FnPageCallback callback;
36 :
37 2 : const TransferTab({
38 : super.key,
39 : required this.state,
40 : required this.callback,
41 : this.accountFrom,
42 : this.accountTo,
43 : this.amount,
44 : this.description,
45 : this.currency,
46 : this.createdAt,
47 : this.tags,
48 : this.isLeft = false,
49 : });
50 :
51 0 : @override
52 0 : TransferTabState createState() => TransferTabState();
53 : }
54 :
55 : class TransferTabState<T extends TransferTab> extends State<T> {
56 : late FocusController focus;
57 : String? accountFrom;
58 : String? accountTo;
59 : Currency? accountFromCurrency;
60 : Currency? accountToCurrency;
61 : late TextEditingController amount;
62 : late TextEditingController description;
63 : late ValueNotifier<List<String>> tags;
64 : late ExchangeController exchange;
65 : late DateTime createdAt;
66 : Currency? currency;
67 : bool hasErrors = false;
68 : bool isPushed = false;
69 :
70 0 : @override
71 : void initState() {
72 0 : focus = FocusController();
73 0 : accountFrom = widget.accountFrom;
74 0 : accountTo = widget.accountTo;
75 0 : createdAt = widget.createdAt ?? DateTime.now();
76 0 : amount = TextEditingController(text: widget.amount != null ? widget.amount.toString() : '');
77 0 : description = TextEditingController(text: widget.description);
78 0 : tags = ValueNotifier([...?widget.tags]);
79 0 : currency = widget.currency ?? Exchange.defaultCurrency;
80 0 : exchange = ExchangeController({}, store: widget.state, targetController: amount, target: currency, source: []);
81 :
82 0 : widget.callback((
83 0 : buildButton: buildButton,
84 0 : buttonName: getButtonName(),
85 0 : title: getTitle(),
86 : ));
87 0 : super.initState();
88 : }
89 :
90 0 : @override
91 : dispose() {
92 0 : isPushed = false;
93 0 : amount.dispose();
94 0 : description.dispose();
95 0 : tags.dispose();
96 0 : exchange.dispose();
97 0 : focus.dispose();
98 0 : super.dispose();
99 : }
100 :
101 0 : bool hasFormErrors() {
102 0 : setState(() => hasErrors = accountFrom == null || accountTo == null);
103 0 : return hasErrors;
104 : }
105 :
106 0 : String getTitle() => AppLocale.labels.createBillHeader;
107 :
108 0 : void updateStorage() {
109 0 : exchange.save();
110 0 : widget.state.add(getValues());
111 : }
112 :
113 0 : InvoiceAppData getValues() => InvoiceAppData(
114 0 : title: description.text,
115 0 : color: widget.state.getByUuid(accountFrom ?? '')?.color,
116 0 : account: accountTo ?? '',
117 0 : accountFrom: accountFrom ?? '',
118 0 : details: double.tryParse(amount.text) ?? 0.0,
119 0 : currency: currency,
120 0 : createdAt: createdAt,
121 0 : tags: tags.value,
122 : );
123 :
124 0 : String getButtonName() => AppLocale.labels.createTransferTooltip;
125 :
126 0 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
127 0 : final nav = Navigator.of(context);
128 0 : return FullSizedButtonWidget(
129 : constraints: constraints,
130 0 : controller: focus,
131 0 : onPressed: () => {
132 0 : setState(() {
133 0 : if (hasFormErrors()) {
134 : return;
135 : }
136 0 : updateStorage();
137 0 : nav.pop();
138 : })
139 : },
140 0 : title: getButtonName(),
141 : icon: Icons.save,
142 : );
143 : }
144 :
145 0 : @override
146 : Widget build(BuildContext context) {
147 0 : final textTheme = context.textTheme;
148 0 : final indent = ThemeHelper.getIndent(2);
149 0 : return SingleScrollWrapper(
150 0 : controller: focus,
151 0 : child: Container(
152 0 : margin: EdgeInsets.fromLTRB(indent, indent, indent, 240),
153 0 : child: LayoutBuilder(builder: (context, constraints) {
154 0 : double width = constraints.maxWidth;
155 0 : return Column(
156 0 : crossAxisAlignment: AppDesign.getAlignment(),
157 0 : children: [
158 0 : InputWrapper(
159 : type: NamedInputType.accountSelector,
160 : isRequired: true,
161 0 : value: accountFrom != null ? widget.state.getByUuid(accountFrom!) : null,
162 0 : title: AppLocale.labels.accountFrom,
163 0 : tooltip: '${AppLocale.labels.titleAccountTooltip} (${AppLocale.labels.accountFrom})',
164 0 : showError: hasErrors && accountFrom == null,
165 0 : state: widget.state,
166 0 : onChange: (value) => setState(() {
167 0 : accountFrom = value?.uuid;
168 : if (value != null) {
169 0 : accountFromCurrency = value.currency;
170 0 : currency = accountFromCurrency;
171 : }
172 : }),
173 : width: width,
174 : ),
175 0 : InputWrapper(
176 : type: NamedInputType.accountSelector,
177 : isRequired: true,
178 0 : value: accountTo != null ? widget.state.getByUuid(accountTo!) : null,
179 0 : title: AppLocale.labels.accountTo,
180 0 : tooltip: '${AppLocale.labels.titleAccountTooltip} (${AppLocale.labels.accountTo})',
181 0 : showError: hasErrors && accountTo == null,
182 0 : state: widget.state,
183 0 : onChange: (value) => setState(() {
184 0 : accountTo = value?.uuid;
185 : if (value != null) {
186 0 : accountToCurrency = value.currency;
187 0 : currency ??= accountToCurrency;
188 : }
189 : }),
190 : width: width,
191 : ),
192 0 : RowWidget(
193 : indent: indent,
194 : maxWidth: width,
195 : chunk: const [125, null],
196 0 : children: [
197 0 : [
198 0 : InputWrapper.currency(
199 : type: NamedInputType.currencyShort,
200 0 : value: currency,
201 0 : title: AppLocale.labels.currency,
202 0 : tooltip: AppLocale.labels.currencyTooltip,
203 0 : onChange: (value) => setState(() => currency = value),
204 : ),
205 : ],
206 0 : [
207 0 : InputWrapper.text(
208 0 : title: AppLocale.labels.expenseTransfer,
209 0 : tooltip: AppLocale.labels.billSetTooltip,
210 0 : controller: amount,
211 : inputType: const TextInputType.numberWithOptions(decimal: true),
212 0 : formatter: [
213 0 : SimpleInputFormatter.filterDouble,
214 : ],
215 : ),
216 : ],
217 : ],
218 : ),
219 0 : CurrencyExchangeInput(
220 0 : key: ValueKey('transfer${currency?.code}${accountFromCurrency?.code}${accountToCurrency?.code}'),
221 0 : width: width + indent,
222 : indent: indent,
223 0 : target: currency,
224 0 : controller: exchange,
225 0 : source: [accountFromCurrency, accountToCurrency, Exchange.defaultCurrency],
226 : ),
227 0 : InputWrapper.text(
228 0 : title: AppLocale.labels.description,
229 0 : tooltip: AppLocale.labels.transferTooltip,
230 0 : controller: description,
231 : ),
232 0 : TagsInput(
233 0 : title: AppLocale.labels.tags,
234 0 : tooltip: AppLocale.labels.tagsTooltip,
235 0 : controller: tags,
236 : ),
237 0 : Text(
238 0 : AppLocale.labels.balanceDate,
239 0 : style: textTheme.bodyLarge,
240 : ),
241 0 : DateTimeInput(
242 0 : width: width - indent,
243 0 : value: createdAt,
244 0 : setState: (value) => setState(() => createdAt = value),
245 : ),
246 : ThemeHelper.formEndBox,
247 : ],
248 : );
249 : }),
250 : ),
251 : );
252 : }
253 : }
|