Line data Source code
1 : // Copyright 2024 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/focus_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/payment_app_data.dart';
8 : import 'package:app_finance/_configs/automation_type.dart';
9 : import 'package:app_finance/_configs/payment_type.dart';
10 : import 'package:app_finance/_configs/screen_helper.dart';
11 : import 'package:app_finance/_configs/theme_helper.dart';
12 : import 'package:app_finance/design/button/full_sized_button_widget.dart';
13 : import 'package:app_finance/design/form/simple_input.dart';
14 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
15 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
16 : import 'package:app_finance/pages/_interfaces/abstract_add_page.dart';
17 : import 'package:app_finance/pages/bill/widgets/expenses_tab.dart';
18 : import 'package:app_finance/pages/bill/widgets/income_tab.dart';
19 : import 'package:app_finance/pages/bill/widgets/transfer_tab.dart';
20 : import 'package:material_ui/material_ui.dart';
21 :
22 : class PaymentAddPage extends AbstractAddPage {
23 2 : const PaymentAddPage({super.key});
24 :
25 0 : @override
26 0 : PaymentAddPageState createState() => PaymentAddPageState();
27 : }
28 :
29 : class PaymentAddPageState<T extends PaymentAddPage> extends AbstractAddPageState<T> {
30 : late FocusController focus;
31 : bool hasErrors = false;
32 : String? itemType;
33 : String? intervalType;
34 : late TextEditingController days;
35 : dynamic item;
36 : DateTime? updatedAt;
37 : final GlobalKey<ExpensesTabState> expensesTabKey = GlobalKey();
38 : final GlobalKey<IncomeTabState> incomeTabKey = GlobalKey();
39 : final GlobalKey<TransferTabState> transferTabKey = GlobalKey();
40 :
41 0 : @override
42 : void initState() {
43 0 : focus = FocusController();
44 0 : days = TextEditingController();
45 0 : super.initState();
46 : }
47 :
48 0 : @override
49 : void dispose() {
50 0 : focus.dispose();
51 0 : days.dispose();
52 0 : super.dispose();
53 : }
54 :
55 0 : @override
56 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
57 0 : NavigatorState nav = Navigator.of(context);
58 0 : return FullSizedButtonWidget(
59 : constraints: constraints,
60 0 : controller: focus,
61 0 : onPressed: () => triggerActionButton(nav),
62 0 : title: getButtonName(),
63 : icon: Icons.save,
64 : );
65 : }
66 :
67 0 : @override
68 0 : String getButtonName() => AppLocale.labels.createPaymentTooltip;
69 :
70 0 : @override
71 0 : String getTitle() => AppLocale.labels.paymentsHeadline;
72 :
73 0 : @override
74 : bool hasFormErrors() {
75 0 : setState(() => hasErrors = itemType == null ||
76 0 : intervalType == null ||
77 0 : (intervalType == AppAutomationType.days.name && (days.text.isEmpty || days.text == '0')));
78 0 : return hasErrors;
79 : }
80 :
81 0 : dynamic getValues() {
82 : dynamic values;
83 0 : if (itemType == AppPaymentType.bill.name) {
84 0 : values = expensesTabKey.currentState?.getValues();
85 : }
86 0 : if (itemType == AppPaymentType.invoice.name) {
87 0 : values = incomeTabKey.currentState?.getValues();
88 : }
89 0 : if (itemType == AppPaymentType.transfer.name) {
90 0 : values = transferTabKey.currentState?.getValues();
91 : }
92 0 : values?.setState(state);
93 : return values;
94 : }
95 :
96 0 : @override
97 : void updateStorage() {
98 0 : final values = getValues();
99 0 : state.add(PaymentAppData(
100 0 : title: intervalType ?? AppAutomationType.month.name,
101 0 : data: values.toFile(),
102 0 : days: int.tryParse(days.text) ?? 1,
103 0 : updatedAt: values.createdAt,
104 : ));
105 : }
106 :
107 0 : @override
108 : Widget buildContent(BuildContext context, BoxConstraints constraints) {
109 0 : double indent = ThemeHelper.getIndent(2);
110 0 : final isLeft = ScreenHelper.state().isLeftBar;
111 0 : return SingleScrollWrapper(
112 0 : controller: focus,
113 0 : child: Container(
114 0 : margin: EdgeInsets.fromLTRB(indent, indent, indent, indent),
115 0 : child: Column(
116 0 : crossAxisAlignment: AppDesign.getAlignment(),
117 0 : children: [
118 0 : InputWrapper.select(
119 : isRequired: true,
120 0 : value: intervalType,
121 0 : title: AppLocale.labels.paymentType,
122 0 : tooltip: AppLocale.labels.paymentType,
123 0 : showError: hasErrors && intervalType == null,
124 0 : options: AutomationType.getList(),
125 0 : onChange: (value) => setState(() => intervalType = value),
126 : ),
127 0 : if (intervalType == AppAutomationType.days.name)
128 0 : InputWrapper.text(
129 : isRequired: true,
130 0 : controller: days,
131 0 : title: AppLocale.labels.automationTypeDays,
132 0 : tooltip: AppLocale.labels.automationTypeDays,
133 0 : showError: hasErrors && (days.text.isEmpty || days.text == '0'),
134 : inputType: const TextInputType.numberWithOptions(),
135 0 : formatter: [SimpleInputFormatter.filterInt],
136 : ),
137 0 : InputWrapper.select(
138 : isRequired: true,
139 0 : value: itemType,
140 0 : title: AppLocale.labels.billTypeTooltip,
141 0 : tooltip: AppLocale.labels.billTypeTooltip,
142 0 : showError: hasErrors && itemType == null,
143 0 : options: PaymentType.getList(),
144 0 : onChange: (value) => setState(() => itemType = value),
145 : ),
146 0 : const Divider(),
147 0 : if (itemType == AppPaymentType.bill.name)
148 0 : ExpensesTab(
149 0 : key: expensesTabKey,
150 0 : state: state,
151 : isLeft: isLeft,
152 0 : callback: (_) => null,
153 0 : account: item?.account,
154 0 : budget: item?.category,
155 0 : currency: item?.currency,
156 0 : bill: item?.details,
157 0 : description: item?.title,
158 0 : createdAt: updatedAt,
159 : ),
160 0 : if (itemType == AppPaymentType.invoice.name)
161 0 : IncomeTab(
162 0 : key: incomeTabKey,
163 0 : state: state,
164 : isLeft: isLeft,
165 0 : callback: (_) => null,
166 0 : account: item?.account,
167 0 : amount: item?.details,
168 0 : currency: item?.currency,
169 0 : description: item?.description,
170 0 : createdAt: updatedAt,
171 : ),
172 0 : if (itemType == AppPaymentType.transfer.name)
173 0 : TransferTab(
174 0 : key: transferTabKey,
175 0 : state: state,
176 : isLeft: isLeft,
177 0 : callback: (_) => null,
178 0 : accountTo: item?.account,
179 0 : accountFrom: item?.accountFrom,
180 0 : amount: item?.details,
181 0 : currency: item?.currency,
182 0 : description: item?.description,
183 0 : createdAt: updatedAt,
184 : ),
185 : ],
186 : ),
187 : ),
188 : );
189 : }
190 : }
|