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/herald/app_design.dart';
5 : import 'package:app_finance/_classes/storage/app_data.dart';
6 : import 'package:app_finance/_classes/herald/app_locale.dart';
7 : import 'package:app_finance/_classes/structure/account_app_data.dart';
8 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
9 : import 'package:app_finance/_configs/design_type.dart';
10 : import 'package:app_finance/_configs/theme_helper.dart';
11 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
12 : import 'package:app_finance/design/wrapper/background_wrapper.dart';
13 : import 'package:app_finance/pages/_interfaces/abstract_page_state.dart';
14 : import 'package:app_finance/pages/account/widgets/account_line_widget.dart';
15 : import 'package:app_finance/design/generic/base_header_widget.dart';
16 : import 'package:app_finance/design/generic/base_swipe_widget.dart';
17 : import 'package:app_finance/design/wrapper/tap_widget.dart';
18 : import 'package:app_finance/pages/account/widgets/header_widget.dart';
19 : import 'package:flutter/material.dart';
20 : import 'package:provider/provider.dart';
21 :
22 : class AccountPage extends StatefulWidget {
23 : final String? search;
24 :
25 1 : const AccountPage({
26 : super.key,
27 : this.search,
28 : });
29 :
30 1 : @override
31 1 : AccountPageState createState() => AccountPageState();
32 : }
33 :
34 : class AccountPageState extends AbstractPageState<AccountPage> {
35 1 : AppDataGetter _getItems() {
36 2 : if (widget.search != null) {
37 0 : final scope = state.getList(AppDataType.accounts).where((e) => e.title.toString().startsWith(widget.search!));
38 0 : final ex = Exchange(store: super.state);
39 : return (
40 0 : total: scope.fold(0.0, (v, e) => v + ex.reform(e.details, e.currency, ex.getDefaultCurrency())),
41 0 : list: scope.toList(),
42 0 : stream: state.getStream(AppDataType.accounts, filter: (e) => !e.title.toString().startsWith(widget.search!))
43 : );
44 : }
45 2 : return state.get(AppDataType.accounts);
46 : }
47 :
48 1 : @override
49 : String getTitle() {
50 2 : if (widget.search != null) {
51 0 : return AppLocale.labels.search(widget.search!);
52 : }
53 2 : return AppLocale.labels.accountHeadline;
54 : }
55 :
56 1 : @override
57 2 : String getButtonName() => AppLocale.labels.addAccountTooltip;
58 :
59 1 : @override
60 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
61 1 : NavigatorState nav = Navigator.of(context);
62 1 : return FloatingActionButton(
63 : heroTag: 'account_page',
64 2 : onPressed: () => nav.pushNamed(AppRoute.accountAddRoute),
65 1 : tooltip: getButtonName(),
66 : child: const Icon(Icons.add),
67 : );
68 : }
69 :
70 1 : @override
71 : Widget buildContent(BuildContext context, BoxConstraints constraints) {
72 1 : final items = _getItems();
73 1 : final design = Provider.of<AppDesign>(context, listen: false);
74 2 : if (design.value == AppDesignType.germany) {
75 0 : items.list.sort((a, b) => a.title.compareTo(b.title));
76 : }
77 1 : final width = ThemeHelper.getWidth(context, 4, constraints);
78 1 : final indent = ThemeHelper.getIndent();
79 1 : final widthCount = ThemeHelper.getWidthCount(constraints, context);
80 1 : return Padding(
81 1 : padding: EdgeInsets.all(indent),
82 1 : child: Column(
83 1 : children: [
84 1 : BaseHeaderWidget(
85 : route: AppRoute.homeRoute,
86 2 : tooltip: AppLocale.labels.homeTooltip,
87 : width: width,
88 : total: items.total,
89 5 : title: '${AppLocale.labels.accountHeadline}, ${AppLocale.labels.total}',
90 : ),
91 : ThemeHelper.hIndent,
92 1 : if (widthCount > 2) HeaderWidget(count: widthCount, width: width),
93 1 : Expanded(
94 1 : child: ListView.separated(
95 : scrollDirection: Axis.vertical,
96 : shrinkWrap: true,
97 2 : itemCount: items.list.length + 1,
98 2 : separatorBuilder: (_, index) => Divider(height: indent),
99 1 : itemBuilder: (BuildContext context, index) {
100 2 : if (index >= items.list.length) {
101 : return ThemeHelper.formEndBox;
102 : }
103 1 : AccountAppData item = items.list[index];
104 1 : return BackgroundWrapper(
105 : index: index,
106 1 : child: BaseSwipeWidget(
107 : routePath: AppRoute.accountEditRoute,
108 1 : uuid: item.uuid!,
109 1 : child: TapWidget(
110 : tooltip: '',
111 : route:
112 3 : RouteSettings(name: AppRoute.accountViewRoute, arguments: {routeArguments.uuid: item.uuid}),
113 1 : child: AccountLineWidget(
114 : item: item,
115 : width: width,
116 : count: widthCount,
117 : ),
118 : ),
119 : ),
120 : );
121 : },
122 : ),
123 : ),
124 : ],
125 : ),
126 : );
127 : }
128 : }
|