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_preferences.dart';
11 : import 'package:app_finance/_classes/storage/app_data.dart';
12 : import 'package:app_finance/_configs/theme_helper.dart';
13 : import 'package:app_finance/_ext/build_context_ext.dart';
14 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
15 : import 'package:app_finance/pages/_interfaces/interface_page_inject.dart';
16 : import 'package:app_finance/design/form/currency_exchange_input.dart';
17 : import 'package:app_finance/design/form/date_time_input.dart';
18 : import 'package:app_finance/design/button/full_sized_button_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/row_widget.dart';
22 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
23 : import 'package:material_ui/material_ui.dart';
24 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
25 :
26 : class IncomeTab<T> extends StatefulWidget {
27 : final String? account;
28 : final String? description;
29 : final Currency? currency;
30 : final double? amount;
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 IncomeTab({
38 : super.key,
39 : required this.state,
40 : required this.callback,
41 : this.account,
42 : this.description,
43 : this.currency,
44 : this.amount,
45 : this.createdAt,
46 : this.tags,
47 : this.isLeft = false,
48 : });
49 :
50 0 : @override
51 0 : IncomeTabState createState() => IncomeTabState();
52 : }
53 :
54 : class IncomeTabState<T extends IncomeTab> extends State<IncomeTab> {
55 : late FocusController focus;
56 : String? account;
57 : Currency? accountCurrency;
58 : Currency? currency;
59 : late TextEditingController amount;
60 : late TextEditingController description;
61 : late ValueNotifier<List<String>> tags;
62 : late ExchangeController exchange;
63 : late DateTime createdAt;
64 : bool hasErrors = false;
65 :
66 0 : @override
67 : void initState() {
68 0 : focus = FocusController();
69 0 : final value = AppPreferences.get(AppPreferences.prefAccount);
70 0 : final obj = widget.state.getByUuid(value ?? '');
71 0 : account = widget.account ?? obj?.uuid;
72 0 : currency = widget.currency ?? obj?.currency ?? Exchange.defaultCurrency;
73 0 : createdAt = widget.createdAt ?? DateTime.now();
74 0 : amount = TextEditingController(text: widget.amount != null ? widget.amount.toString() : '');
75 0 : description = TextEditingController(text: widget.description);
76 0 : tags = ValueNotifier([...?widget.tags]);
77 0 : accountCurrency = widget.state.getByUuid(account ?? '')?.currency;
78 0 : exchange = ExchangeController(
79 0 : {},
80 0 : store: widget.state,
81 0 : targetController: amount,
82 0 : target: currency,
83 0 : source: [accountCurrency, Exchange.defaultCurrency],
84 : );
85 :
86 0 : widget.callback((
87 0 : buildButton: buildButton,
88 0 : buttonName: getButtonName(),
89 0 : title: getTitle(),
90 : ));
91 0 : super.initState();
92 : }
93 :
94 0 : @override
95 : dispose() {
96 0 : description.dispose();
97 0 : tags.dispose();
98 0 : exchange.dispose();
99 0 : amount.dispose();
100 0 : focus.dispose();
101 0 : super.dispose();
102 : }
103 :
104 0 : String getTitle() => AppLocale.labels.createBillHeader;
105 :
106 0 : bool hasFormErrors() => account == null;
107 :
108 0 : void updateStorage() {
109 0 : AppPreferences.set(AppPreferences.prefAccount, account ?? '');
110 0 : exchange.save();
111 0 : widget.state.add(getValues());
112 : }
113 :
114 0 : InvoiceAppData getValues() => InvoiceAppData(
115 0 : title: description.text,
116 0 : color: widget.state.getByUuid(account ?? '')?.color,
117 0 : account: account ?? '',
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.createIncomeTooltip;
125 :
126 0 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
127 0 : NavigatorState nav = Navigator.of(context);
128 0 : return FullSizedButtonWidget(
129 : constraints: constraints,
130 0 : controller: focus,
131 0 : onPressed: () {
132 0 : if (!mounted) {
133 : return;
134 : }
135 0 : if (hasFormErrors()) {
136 0 : setState(() => hasErrors = true);
137 : return;
138 : }
139 0 : updateStorage();
140 0 : nav.pop();
141 : },
142 0 : title: getButtonName(),
143 : icon: Icons.save,
144 : );
145 : }
146 :
147 0 : @override
148 : Widget build(BuildContext context) {
149 0 : final textTheme = context.textTheme;
150 0 : final indent = ThemeHelper.getIndent(2);
151 0 : return SingleScrollWrapper(
152 0 : controller: focus,
153 0 : child: Container(
154 0 : margin: EdgeInsets.fromLTRB(indent, indent, indent, 240),
155 0 : child: LayoutBuilder(builder: (context, constraints) {
156 0 : double width = constraints.maxWidth;
157 0 : return Column(
158 0 : crossAxisAlignment: AppDesign.getAlignment(),
159 0 : children: [
160 0 : InputWrapper(
161 : type: NamedInputType.accountSelector,
162 : isRequired: true,
163 0 : value: account != null ? widget.state.getByUuid(account!) : null,
164 0 : title: AppLocale.labels.account,
165 0 : tooltip: AppLocale.labels.titleAccountTooltip,
166 0 : showError: hasErrors && account == null,
167 0 : state: widget.state,
168 0 : onChange: (value) => setState(() {
169 0 : account = value?.uuid;
170 : if (value != null) {
171 0 : currency = value.currency;
172 0 : accountCurrency = currency;
173 : }
174 : }),
175 : width: width,
176 : ),
177 0 : RowWidget(
178 : indent: indent,
179 : maxWidth: width,
180 : chunk: const [125, null],
181 0 : children: [
182 0 : [
183 0 : InputWrapper.currency(
184 : type: NamedInputType.currencyShort,
185 0 : value: currency,
186 0 : title: AppLocale.labels.currency,
187 0 : tooltip: AppLocale.labels.currencyTooltip,
188 0 : onChange: (value) => setState(() => currency = value),
189 : ),
190 : ],
191 0 : [
192 0 : InputWrapper.text(
193 0 : title: AppLocale.labels.income,
194 0 : tooltip: AppLocale.labels.billSetTooltip,
195 0 : controller: amount,
196 : inputType: const TextInputType.numberWithOptions(decimal: true),
197 0 : formatter: [
198 0 : SimpleInputFormatter.filterDouble,
199 : ],
200 : ),
201 : ],
202 : ],
203 : ),
204 0 : CurrencyExchangeInput(
205 0 : key: ValueKey('income${currency?.code}${accountCurrency?.code}'),
206 0 : width: width + indent,
207 : indent: indent,
208 0 : target: currency,
209 0 : controller: exchange,
210 0 : source: [accountCurrency, Exchange.defaultCurrency],
211 : ),
212 0 : InputWrapper.text(
213 0 : title: AppLocale.labels.description,
214 0 : tooltip: AppLocale.labels.incomeTooltip,
215 0 : controller: description,
216 : ),
217 0 : TagsInput(
218 0 : title: AppLocale.labels.tags,
219 0 : tooltip: AppLocale.labels.tagsTooltip,
220 0 : controller: tags,
221 : ),
222 0 : Text(
223 0 : AppLocale.labels.balanceDate,
224 0 : style: textTheme.bodyLarge,
225 : ),
226 0 : DateTimeInput(
227 0 : width: width - indent,
228 0 : value: createdAt,
229 0 : setState: (value) => setState(() => createdAt = value),
230 : ),
231 : ThemeHelper.formEndBox,
232 : ],
233 : );
234 : }),
235 : ),
236 : );
237 : }
238 : }
|