Line data Source code
1 : // Copyright 2025 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/iterator_controller.dart';
5 : import 'package:app_finance/_classes/controller/focus_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/storage/app_data.dart';
9 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
10 : import 'package:app_finance/_configs/account_type.dart';
11 : import 'package:app_finance/_configs/screen_helper.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/_ext/tags_ext.dart';
15 : import 'package:app_finance/design/form/date_range_input.dart';
16 : import 'package:app_finance/design/form/list_account_selector.dart';
17 : import 'package:app_finance/design/form/simple_input.dart';
18 : import 'package:app_finance/design/form/tags_input.dart';
19 : import 'package:app_finance/design/wrapper/input_wrapper.dart';
20 : import 'package:app_finance/design/wrapper/row_widget.dart';
21 : import 'package:app_finance/design/wrapper/single_scroll_wrapper.dart';
22 : import 'package:app_finance/design/wrapper/text_wrapper.dart';
23 : import 'package:app_finance/pages/invoice/invoice_page.dart';
24 : import 'package:material_ui/material_ui.dart';
25 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
26 :
27 : class InvoiceSearchPage extends StatefulWidget {
28 0 : const InvoiceSearchPage({super.key});
29 :
30 0 : @override
31 0 : InvoiceSearchPageState createState() => InvoiceSearchPageState();
32 : }
33 :
34 : class InvoiceSearchPageState extends InvoicePageState<InvoiceSearchPage> {
35 : String? account;
36 : String? type;
37 : Currency? currency;
38 : DateTime? startDate;
39 : DateTime? endDate;
40 : late TextEditingController description;
41 : late TextEditingController amountFrom;
42 : late TextEditingController amountTo;
43 : late ValueNotifier<List<String>> tags;
44 : late FocusController focus;
45 :
46 : late List<ListAccountSelectorItem> accountList =
47 : state.getList(AppDataType.accounts).map((item) => ListAccountSelectorItem(item: item)).toList();
48 :
49 0 : @override
50 : void initState() {
51 0 : description = TextEditingController();
52 0 : amountFrom = TextEditingController();
53 0 : amountTo = TextEditingController();
54 0 : tags = ValueNotifier([]);
55 0 : tags.addListener(() => setState(clearState));
56 0 : focus = FocusController();
57 0 : super.initState();
58 : }
59 :
60 0 : @override
61 : dispose() {
62 0 : description.dispose();
63 0 : amountFrom.dispose();
64 0 : amountTo.dispose();
65 0 : tags.dispose();
66 0 : focus.dispose();
67 0 : super.dispose();
68 : }
69 :
70 0 : @override
71 0 : String getTitle() => AppLocale.labels.searchTooltip;
72 :
73 0 : @override
74 0 : String getButtonName() => AppLocale.labels.searchTooltip;
75 :
76 0 : @override
77 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
78 0 : return FloatingActionButton(
79 : heroTag: 'invoice_search_page',
80 0 : onPressed: () => setState(clearState),
81 0 : tooltip: getButtonName(),
82 : child: const Icon(Icons.search),
83 : );
84 : }
85 :
86 0 : @override
87 : bool getContentFilter(InvoiceAppData o) {
88 0 : final descriptionMatch = o.title.toLowerCase().contains(description.text.toLowerCase());
89 0 : final tagsMatch = o.tags.hasTags(tags.value);
90 0 : final accountMatch = account == null || o.account == account;
91 0 : final typeMatch = type == null || state.getByUuid(o.account)?.type == type;
92 0 : final currencyMatch = currency == null || o.currency == currency;
93 0 : final startDateMatch = startDate == null || o.createdAt.isAfter(startDate!);
94 0 : final endDateMatch = endDate == null || o.createdAt.isBefore(endDate!);
95 0 : final amountFromMatch = amountFrom.text.isEmpty || o.details >= double.tryParse(amountFrom.text)!;
96 0 : final amountToMatch = amountTo.text.isEmpty || o.details <= double.tryParse(amountTo.text)!;
97 :
98 : return !(descriptionMatch &&
99 : tagsMatch &&
100 : accountMatch &&
101 : typeMatch &&
102 : currencyMatch &&
103 : startDateMatch &&
104 : endDateMatch &&
105 : amountFromMatch &&
106 : amountToMatch) ||
107 0 : o.accountFrom != null;
108 : }
109 :
110 0 : @override
111 : InterfaceIterator getContentStream() =>
112 0 : state.getStream<InvoiceAppData>(AppDataType.invoice, filter: getContentFilter);
113 :
114 0 : @override
115 : Widget addHeaderWidget() {
116 0 : final width = ScreenHelper.state().width - ThemeHelper.getIndent(4);
117 0 : final indent = ThemeHelper.getIndent(2);
118 0 : return SliverToBoxAdapter(
119 0 : child: SingleScrollWrapper(
120 0 : controller: focus,
121 0 : child: Column(
122 0 : crossAxisAlignment: AppDesign.getAlignment(),
123 0 : children: [
124 0 : InputWrapper.text(
125 0 : title: AppLocale.labels.description,
126 0 : controller: description,
127 0 : tooltip: AppLocale.labels.descriptionTooltip,
128 : ),
129 0 : TagsInput(
130 0 : title: AppLocale.labels.tags,
131 0 : tooltip: AppLocale.labels.tagsTooltip,
132 0 : controller: tags,
133 : ),
134 0 : RowWidget(
135 : indent: indent,
136 0 : maxWidth: width + indent,
137 : chunk: const [0.5, 0.5],
138 0 : children: [
139 0 : [
140 0 : InputWrapper.text(
141 0 : title: AppLocale.labels.amountFrom,
142 0 : tooltip: AppLocale.labels.billSetTooltip,
143 0 : controller: amountFrom,
144 : inputType: const TextInputType.numberWithOptions(decimal: true),
145 0 : formatter: [
146 0 : SimpleInputFormatter.filterDouble,
147 : ],
148 : ),
149 : ],
150 0 : [
151 0 : InputWrapper.text(
152 0 : title: AppLocale.labels.amountTo,
153 0 : tooltip: AppLocale.labels.billSetTooltip,
154 0 : controller: amountTo,
155 : inputType: const TextInputType.numberWithOptions(decimal: true),
156 0 : formatter: [
157 0 : SimpleInputFormatter.filterDouble,
158 : ],
159 : ),
160 : ],
161 : ],
162 : ),
163 0 : InputWrapper.select(
164 0 : value: type,
165 0 : title: AppLocale.labels.accountType,
166 0 : tooltip: AppLocale.labels.accountTypeTooltip,
167 0 : options: AccountType.getList(),
168 0 : onChange: (value) => setState(() {
169 0 : if (value != type) clearState();
170 0 : type = value;
171 : }),
172 : ),
173 0 : InputWrapper(
174 : type: NamedInputType.accountSelector,
175 0 : value: account != null ? state.getByUuid(account!) : null,
176 0 : title: AppLocale.labels.account,
177 0 : tooltip: AppLocale.labels.titleAccountTooltip,
178 0 : state: state,
179 0 : options: accountList,
180 0 : onChange: (value) => setState(() {
181 0 : if (value?.uuid != account) clearState();
182 0 : account = value?.uuid;
183 : }),
184 : width: width,
185 : ),
186 0 : InputWrapper.currency(
187 0 : value: currency,
188 0 : title: AppLocale.labels.currency,
189 0 : tooltip: AppLocale.labels.currencyTooltip,
190 0 : onChange: (value) => setState(() {
191 0 : if (value != currency) clearState();
192 0 : currency = value;
193 : }),
194 : ),
195 0 : TextWrapper(
196 0 : AppLocale.labels.dateRange,
197 0 : style: context.textTheme.bodyLarge,
198 : ),
199 0 : DateRangeInput(
200 0 : valueFrom: startDate,
201 0 : value: endDate,
202 0 : setState: (start, end) => setState(() {
203 0 : startDate = start;
204 0 : endDate = end;
205 0 : clearState();
206 : }),
207 : ),
208 : ],
209 : ),
210 : ),
211 : );
212 : }
213 : }
|