LCOV - code coverage report
Current view: top level - lib/pages/bill/widgets - expenses_tab.dart Coverage Total Hit
Test: lcov.info Lines: 72.2 % 169 122
Test Date: 2026-12-05 10:50:38 Functions: - 0 0

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

Generated by: LCOV version 2.0-1