LCOV - code coverage report
Current view: top level - lib/pages/bill/widgets - income_tab.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1 106 0.9 %
Date: 2024-10-04 11:12:13 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/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/structure/navigation/app_route.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/screen_helper.dart';
      14             : import 'package:app_finance/_configs/theme_helper.dart';
      15             : import 'package:app_finance/_ext/build_context_ext.dart';
      16             : import 'package:app_finance/_ext/double_ext.dart';
      17             : import 'package:app_finance/design/wrapper/input_wrapper.dart';
      18             : import 'package:app_finance/pages/bill/widgets/interface_bill_page_inject.dart';
      19             : import 'package:app_finance/design/form/currency_exchange_input.dart';
      20             : import 'package:app_finance/design/form/date_time_input.dart';
      21             : import 'package:app_finance/design/button/full_sized_button_widget.dart';
      22             : import 'package:app_finance/design/form/simple_input.dart';
      23             : import 'package:app_finance/design/wrapper/row_widget.dart';
      24             : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
      25             : import 'package:flutter/material.dart';
      26             : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
      27             : 
      28             : class IncomeTab<T> extends StatefulWidget {
      29             :   final String? account;
      30             :   final String? description;
      31             :   final Currency? currency;
      32             :   final double? amount;
      33             :   final DateTime? createdAt;
      34             :   final AppData state;
      35             :   final bool isLeft;
      36             :   final FnBillPageCallback callback;
      37             : 
      38           1 :   const IncomeTab({
      39             :     super.key,
      40             :     required this.state,
      41             :     required this.callback,
      42             :     this.account,
      43             :     this.description,
      44             :     this.currency,
      45             :     this.amount,
      46             :     this.createdAt,
      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 ExchangeController exchange;
      62             :   late DateTime createdAt;
      63             :   bool hasErrors = false;
      64             : 
      65           0 :   @override
      66             :   void initState() {
      67           0 :     focus = FocusController();
      68           0 :     final value = AppPreferences.get(AppPreferences.prefAccount);
      69           0 :     final obj = widget.state.getByUuid(value ?? '');
      70           0 :     account = widget.account ?? obj?.uuid;
      71           0 :     currency = widget.currency ?? obj?.currency ?? Exchange.defaultCurrency;
      72           0 :     createdAt = widget.createdAt ?? DateTime.now();
      73           0 :     amount = TextEditingController(text: widget.amount != null ? widget.amount.toString() : '');
      74           0 :     description = TextEditingController(text: widget.description);
      75           0 :     accountCurrency = widget.state.getByUuid(account ?? '')?.currency;
      76           0 :     exchange = ExchangeController({},
      77           0 :         store: widget.state, targetController: amount, target: currency, source: [accountCurrency]);
      78             : 
      79           0 :     widget.callback((
      80           0 :       buildButton: buildButton,
      81           0 :       buttonName: getButtonName(),
      82           0 :       title: getTitle(),
      83             :     ));
      84           0 :     super.initState();
      85             :   }
      86             : 
      87           0 :   @override
      88             :   dispose() {
      89           0 :     description.dispose();
      90           0 :     exchange.dispose();
      91           0 :     amount.dispose();
      92           0 :     focus.dispose();
      93           0 :     super.dispose();
      94             :   }
      95             : 
      96           0 :   String getTitle() => AppLocale.labels.createBillHeader;
      97             : 
      98           0 :   bool hasFormErrors() {
      99           0 :     setState(() => hasErrors = account == null);
     100           0 :     return hasErrors;
     101             :   }
     102             : 
     103           0 :   void updateStorage() {
     104           0 :     String uuid = account ?? '';
     105           0 :     AppPreferences.set(AppPreferences.prefAccount, uuid);
     106           0 :     exchange.save();
     107           0 :     widget.state.add(InvoiceAppData(
     108           0 :       title: description.text,
     109           0 :       color: widget.state.getByUuid(uuid)?.color,
     110             :       account: uuid,
     111           0 :       details: double.tryParse(amount.text)?.toFixed(currency?.decimalDigits) ?? 0.0,
     112           0 :       currency: currency,
     113           0 :       createdAt: createdAt,
     114             :     ));
     115             :   }
     116             : 
     117           0 :   String getButtonName() => AppLocale.labels.createIncomeTooltip;
     118             : 
     119           0 :   Widget buildButton(BuildContext context, BoxConstraints constraints) {
     120           0 :     NavigatorState nav = Navigator.of(context);
     121           0 :     return FullSizedButtonWidget(
     122             :       constraints: constraints,
     123           0 :       controller: focus,
     124           0 :       onPressed: () => {
     125           0 :         setState(() {
     126           0 :           if (hasFormErrors()) {
     127             :             return;
     128             :           }
     129           0 :           updateStorage();
     130           0 :           nav.popAndPushNamed(AppRoute.homeRoute);
     131             :         })
     132             :       },
     133           0 :       title: getButtonName(),
     134             :       icon: Icons.save,
     135             :     );
     136             :   }
     137             : 
     138           0 :   @override
     139             :   Widget build(BuildContext context) {
     140           0 :     final textTheme = context.textTheme;
     141           0 :     final indent = ThemeHelper.getIndent(2);
     142           0 :     double width = ScreenHelper.state().width - indent * 3;
     143           0 :     if (widget.isLeft) {
     144           0 :       width -= ThemeHelper.barHeight;
     145             :     }
     146             : 
     147           0 :     return SingleScrollWrapper(
     148           0 :       controller: focus,
     149           0 :       child: Container(
     150           0 :         margin: EdgeInsets.fromLTRB(indent, indent, indent, 240),
     151             :         width: width,
     152           0 :         child: Column(
     153           0 :           crossAxisAlignment: AppDesign.getAlignment(),
     154           0 :           children: [
     155           0 :             InputWrapper(
     156             :               type: NamedInputType.accountSelector,
     157             :               isRequired: true,
     158           0 :               value: account != null ? widget.state.getByUuid(account!) : null,
     159           0 :               title: AppLocale.labels.account,
     160           0 :               tooltip: AppLocale.labels.titleAccountTooltip,
     161           0 :               showError: hasErrors && account == null,
     162           0 :               state: widget.state,
     163           0 :               onChange: (value) => setState(() {
     164           0 :                 account = value?.uuid;
     165             :                 if (value != null) {
     166           0 :                   currency = value.currency;
     167           0 :                   accountCurrency = currency;
     168             :                 }
     169             :               }),
     170             :               width: width,
     171             :             ),
     172           0 :             RowWidget(
     173             :               indent: indent,
     174           0 :               maxWidth: width + indent,
     175             :               chunk: const [125, null],
     176           0 :               children: [
     177           0 :                 [
     178           0 :                   InputWrapper.currency(
     179             :                     type: NamedInputType.currencyShort,
     180           0 :                     value: currency,
     181           0 :                     title: AppLocale.labels.currency,
     182           0 :                     tooltip: AppLocale.labels.currencyTooltip,
     183           0 :                     onChange: (value) => setState(() => currency = value),
     184             :                   ),
     185             :                 ],
     186           0 :                 [
     187           0 :                   InputWrapper.text(
     188           0 :                     title: AppLocale.labels.income,
     189           0 :                     tooltip: AppLocale.labels.billSetTooltip,
     190           0 :                     controller: amount,
     191             :                     inputType: const TextInputType.numberWithOptions(decimal: true),
     192           0 :                     formatter: [
     193           0 :                       SimpleInputFormatter.filterDouble,
     194             :                     ],
     195             :                   ),
     196             :                 ],
     197             :               ],
     198             :             ),
     199           0 :             CurrencyExchangeInput(
     200           0 :               key: ValueKey('income${currency?.code}${accountCurrency?.code}'),
     201           0 :               width: width + indent,
     202             :               indent: indent,
     203           0 :               target: currency,
     204           0 :               controller: exchange,
     205           0 :               source: [accountCurrency],
     206             :             ),
     207           0 :             InputWrapper.text(
     208           0 :               title: AppLocale.labels.description,
     209           0 :               tooltip: AppLocale.labels.incomeTooltip,
     210           0 :               controller: description,
     211             :             ),
     212           0 :             Text(
     213           0 :               AppLocale.labels.balanceDate,
     214           0 :               style: textTheme.bodyLarge,
     215             :             ),
     216           0 :             DateTimeInput(
     217             :               width: width,
     218           0 :               value: createdAt,
     219           0 :               setState: (value) => setState(() => createdAt = value),
     220             :             ),
     221             :             ThemeHelper.formEndBox,
     222             :           ],
     223             :         ),
     224             :       ),
     225             :     );
     226             :   }
     227             : }

Generated by: LCOV version 1.14