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/delayed_call.dart';
5 : import 'package:app_finance/_classes/controller/exchange_controller.dart';
6 : import 'package:app_finance/_classes/herald/app_design.dart';
7 : import 'package:app_finance/_classes/herald/app_locale.dart';
8 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
9 : import 'package:app_finance/_classes/structure/bill_app_data.dart';
10 : import 'package:app_finance/_classes/controller/focus_controller.dart';
11 : import 'package:app_finance/_classes/storage/app_preferences.dart';
12 : import 'package:app_finance/_classes/storage/app_data.dart';
13 : import 'package:app_finance/_configs/account_type.dart';
14 : import 'package:app_finance/_configs/theme_helper.dart';
15 : import 'package:app_finance/_configs/screen_helper.dart';
16 : import 'package:app_finance/_ext/build_context_ext.dart';
17 : import 'package:app_finance/design/button/toolbar_button_widget.dart';
18 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
19 : import 'package:app_finance/pages/_interfaces/interface_page_inject.dart';
20 : import 'package:app_finance/design/form/currency_exchange_input.dart';
21 : import 'package:app_finance/design/form/date_time_input.dart';
22 : import 'package:app_finance/design/button/full_sized_button_widget.dart';
23 : import 'package:app_finance/design/form/list_account_selector.dart';
24 : import 'package:app_finance/design/form/simple_input.dart';
25 : import 'package:app_finance/design/wrapper/row_widget.dart';
26 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
27 : import 'package:material_ui/material_ui.dart';
28 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
29 : import 'package:flutter_solidart/flutter_solidart.dart';
30 :
31 : class ExpensesTab<T> extends StatefulWidget {
32 : final String? account;
33 : final String? budget;
34 : final Currency? currency;
35 : final double? bill;
36 : final String? description;
37 : final DateTime? createdAt;
38 : final AppData state;
39 : final bool isLeft;
40 : final FnPageCallback callback;
41 :
42 2 : const ExpensesTab({
43 : super.key,
44 : required this.state,
45 : required this.callback,
46 : this.account,
47 : this.budget,
48 : this.currency,
49 : this.bill,
50 : this.description,
51 : this.createdAt,
52 : this.isLeft = false,
53 : });
54 :
55 2 : @override
56 2 : ExpensesTabState createState() => ExpensesTabState();
57 : }
58 :
59 : class ExpensesTabState<T extends ExpensesTab> extends State<T> {
60 : late FocusController focus;
61 : String? account;
62 : Currency? accountCurrency;
63 : String? budget;
64 : Currency? budgetCurrency;
65 : Currency? currency;
66 : late TextEditingController bill;
67 : final billSignal = Signal<String>('');
68 : late TextEditingController description;
69 : final descriptionSignal = Signal<String>('');
70 : late ExchangeController exchange;
71 : DateTime? createdAt;
72 : bool hasErrors = false;
73 : bool isPushed = false;
74 6 : late List<ListAccountSelectorItem> accountList = widget.state
75 2 : .getList(AppDataType.accounts)
76 2 : .where((e) => ![AppAccountType.deposit.toString(), AppAccountType.credit.toString()].contains(e.type))
77 2 : .map((item) => ListAccountSelectorItem(item: item))
78 2 : .toList();
79 :
80 2 : @override
81 : void initState() {
82 4 : focus = FocusController();
83 2 : final accountId = AppPreferences.get(AppPreferences.prefAccount);
84 6 : final objAccount = widget.state.getByUuid(accountId ?? '');
85 2 : final budgetId = AppPreferences.get(AppPreferences.prefBudget);
86 6 : final objBudget = widget.state.getByUuid(budgetId ?? '');
87 6 : account = widget.account ?? objAccount?.uuid;
88 6 : budget = widget.budget ?? objBudget?.uuid;
89 6 : currency = widget.currency ?? objAccount?.currency ?? objBudget?.currency ?? Exchange.defaultCurrency;
90 8 : bill = TextEditingController(text: widget.bill != null ? widget.bill.toString() : '');
91 2 : final billTimer = DelayedCall(1000);
92 18 : bill.addListener(() => billTimer.run(() => billSignal.value = bill.text));
93 8 : description = TextEditingController(text: widget.description);
94 2 : final descriptionTimer = DelayedCall(1000);
95 4 : description.addListener(() => descriptionTimer.run(() => descriptionSignal.value = description.text));
96 6 : createdAt = widget.createdAt;
97 10 : accountCurrency = widget.state.getByUuid(account ?? '')?.currency;
98 10 : budgetCurrency = widget.state.getByUuid(budget ?? '')?.currency;
99 4 : exchange = ExchangeController(
100 2 : {},
101 4 : store: widget.state,
102 2 : targetController: bill,
103 2 : target: currency,
104 6 : source: [accountCurrency, budgetCurrency, Exchange.defaultCurrency],
105 : );
106 :
107 6 : widget.callback((
108 2 : buildButton: buildButton,
109 2 : buttonName: getButtonName(),
110 2 : title: getTitle(),
111 : ));
112 2 : super.initState();
113 : }
114 :
115 2 : @override
116 : dispose() {
117 2 : isPushed = false;
118 4 : description.dispose();
119 4 : exchange.dispose();
120 4 : bill.dispose();
121 4 : focus.dispose();
122 2 : super.dispose();
123 : }
124 :
125 6 : String getTitle() => AppLocale.labels.createBillHeader;
126 :
127 0 : bool hasFormErrors() => account == null || budget == null || bill.text.isEmpty;
128 :
129 0 : void updateStorage() {
130 0 : AppPreferences.set(AppPreferences.prefAccount, account ?? '');
131 0 : AppPreferences.set(AppPreferences.prefBudget, budget ?? '');
132 0 : if (currency != null) {
133 0 : CurrencyProvider.pin(currency!);
134 : }
135 0 : exchange.save();
136 0 : widget.state.add(getValues());
137 : }
138 :
139 0 : BillAppData getValues() => BillAppData(
140 0 : account: account ?? '',
141 0 : category: budget ?? '',
142 0 : currency: currency,
143 0 : title: description.text,
144 0 : details: double.tryParse(bill.text) ?? 0.0,
145 0 : createdAt: createdAt ?? DateTime.now(),
146 : );
147 :
148 6 : String getButtonName() => AppLocale.labels.createBillTooltip;
149 :
150 2 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
151 2 : NavigatorState nav = Navigator.of(context);
152 2 : return FullSizedButtonWidget(
153 : constraints: constraints,
154 2 : controller: focus,
155 0 : onPressed: () {
156 0 : if (!mounted) {
157 : return;
158 : }
159 0 : if (hasFormErrors()) {
160 0 : setState(() => hasErrors = true);
161 : return;
162 : }
163 0 : updateStorage();
164 0 : nav.pop();
165 : },
166 2 : title: getButtonName(),
167 : icon: Icons.save,
168 : );
169 : }
170 :
171 2 : @override
172 : Widget build(BuildContext context) {
173 2 : final textTheme = context.textTheme;
174 2 : final indent = ThemeHelper.getIndent(2);
175 :
176 2 : return SingleScrollWrapper(
177 2 : controller: focus,
178 2 : child: Container(
179 2 : margin: EdgeInsets.fromLTRB(indent, indent, indent, 240),
180 4 : child: LayoutBuilder(builder: (context, constraints) {
181 2 : double width = constraints.maxWidth;
182 2 : final display = ScreenHelper.getInstance(context, constraints);
183 2 : return Column(
184 2 : crossAxisAlignment: AppDesign.getAlignment(),
185 2 : children: [
186 2 : InputWrapper(
187 : type: NamedInputType.accountSelector,
188 : isRequired: true,
189 2 : value: account != null ? widget.state.getByUuid(account!) : null,
190 4 : title: AppLocale.labels.account,
191 4 : tooltip: AppLocale.labels.titleAccountTooltip,
192 2 : showError: hasErrors && account == null,
193 4 : state: widget.state,
194 2 : options: accountList,
195 0 : onChange: (value) => setState(() {
196 0 : account = value?.uuid;
197 : if (value != null) {
198 0 : accountCurrency = value.currency;
199 0 : currency = accountCurrency;
200 : }
201 : }),
202 : width: width,
203 : ),
204 2 : RowWidget(
205 : indent: indent,
206 : maxWidth: width,
207 : chunk: const [125, null],
208 2 : children: [
209 2 : [
210 2 : InputWrapper.currency(
211 : type: NamedInputType.currencyShort,
212 2 : value: currency,
213 4 : title: AppLocale.labels.currency,
214 4 : tooltip: AppLocale.labels.currencyTooltip,
215 0 : onChange: (value) => setState(() => currency = value),
216 : ),
217 : ],
218 2 : [
219 2 : InputWrapper.text(
220 4 : title: AppLocale.labels.expense,
221 : isRequired: true,
222 2 : controller: bill,
223 2 : showError: hasErrors && bill.text.isEmpty,
224 4 : tooltip: AppLocale.labels.billSetTooltip,
225 : inputType: const TextInputType.numberWithOptions(decimal: true),
226 4 : formatter: [SimpleInputFormatter.filterDouble],
227 : ),
228 : ],
229 : ],
230 : ),
231 2 : CurrencyExchangeInput(
232 10 : key: ValueKey('expense${currency?.code}${accountCurrency?.code}${budgetCurrency?.code}'),
233 2 : width: width + indent,
234 : indent: indent,
235 2 : target: currency,
236 2 : controller: exchange,
237 6 : source: [accountCurrency, budgetCurrency, Exchange.defaultCurrency],
238 : ),
239 2 : InputWrapper.text(
240 4 : title: AppLocale.labels.description,
241 2 : controller: description,
242 4 : tooltip: AppLocale.labels.descriptionTooltip,
243 : ),
244 2 : InputWrapper(
245 : type: NamedInputType.budgetSelector,
246 : isRequired: true,
247 2 : value: budget != null ? widget.state.getByUuid(budget!) : null,
248 4 : title: AppLocale.labels.budget,
249 2 : showError: hasErrors && budget == null,
250 4 : tooltip: AppLocale.labels.titleBudgetTooltip,
251 4 : state: widget.state,
252 0 : onChange: (value) => setState(() {
253 0 : budget = value?.uuid;
254 : if (value != null) {
255 0 : budgetCurrency = value.currency;
256 0 : currency ??= budgetCurrency;
257 : }
258 : }),
259 : width: width,
260 : ),
261 4 : SignalBuilder(builder: (_, __) {
262 2 : final bill = BillAppData(
263 2 : account: account ?? '',
264 2 : category: budget ?? '',
265 2 : currency: currency,
266 4 : title: descriptionSignal.value,
267 6 : details: double.tryParse(billSignal.value) ?? 0.0,
268 : );
269 8 : final categories = widget.state.prediction.predict(bill);
270 2 : if (categories.isEmpty) return ThemeHelper.emptyBox;
271 0 : final scope = categories.map((e) {
272 0 : final item = widget.state.getByUuid(e);
273 0 : return ToolbarButtonWidget(
274 : isWide: true,
275 : maxWidth:
276 0 : display.isBottom ? width - indent * 2 : width / categories.length - indent * categories.length,
277 0 : tooltip: item?.title ?? '',
278 0 : color: item?.color ?? context.colorScheme.primary,
279 0 : borderColor: item?.color?.withValues(alpha: 0.5),
280 0 : icon: item?.icon ?? Icons.category,
281 0 : onPressed: () => setState(() => budget = item?.uuid),
282 : );
283 : });
284 : return display.isBottom
285 0 : ? Column(children: scope.toList())
286 0 : : RowWidget(
287 0 : alignment: AppDesign.getAlignment<MainAxisAlignment>(),
288 : indent: indent,
289 0 : maxWidth: constraints.maxWidth,
290 0 : chunk: scope.map((_) => null).toList(),
291 0 : children: scope.map((e) => [e]).toList(),
292 : );
293 : }),
294 : ThemeHelper.hIndent2x,
295 2 : Text(
296 4 : AppLocale.labels.expenseDateTime,
297 2 : style: textTheme.bodyLarge,
298 : ),
299 2 : DateTimeInput(
300 2 : width: width - indent,
301 4 : value: createdAt ?? DateTime.now(),
302 0 : setState: (value) => setState(() => createdAt = value),
303 : ),
304 : ThemeHelper.formEndBox,
305 : ],
306 : );
307 : }),
308 : ),
309 : );
310 : }
311 : }
|