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/storage/app_data.dart';
5 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
6 : import 'package:app_finance/_classes/structure/account_app_data.dart';
7 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
8 : import 'package:app_finance/_configs/account_type.dart';
9 : import 'package:app_finance/_configs/theme_helper.dart';
10 : import 'package:app_finance/_classes/storage/app_preferences.dart';
11 : import 'package:app_finance/design/generic/base_group_widget.dart';
12 : import 'package:app_finance/design/generic/base_line_widget.dart';
13 : import 'package:app_finance/design/generic/base_swipe_widget.dart';
14 : import 'package:app_finance/design/generic/base_widget.dart';
15 : import 'package:material_ui/material_ui.dart';
16 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
17 :
18 : class AccountWidget extends BaseWidget {
19 : late final Exchange exchange;
20 :
21 : // ignore: prefer_const_constructors_in_immutables
22 1 : AccountWidget({
23 : super.key,
24 : required super.title,
25 : required super.margin,
26 : required super.width,
27 : required super.state,
28 : super.limit,
29 : super.tooltip,
30 : super.route,
31 : super.routeList = AppRoute.accountViewRoute,
32 : super.hasExpand,
33 : super.toExpand,
34 : super.callback,
35 : });
36 :
37 1 : @override
38 : AppDataGetter get state {
39 1 : Map<String, List<dynamic>> pile = {};
40 3 : for (dynamic item in super.state.list) {
41 1 : String name = getName(item);
42 1 : if (limit == null) {
43 0 : name = item.uuid;
44 : }
45 1 : if (pile[name] == null) {
46 2 : pile[name] = [];
47 : }
48 2 : pile[name]!.add(item);
49 8 : pile[name]!.sort((a, b) => a.title.toString().compareTo(b.title.toString()));
50 : }
51 : return (
52 5 : list: pile.entries.map((e) => e.value).toList(),
53 1 : total: super.state.total,
54 1 : stream: super.state.stream,
55 : );
56 : }
57 :
58 1 : String getName(dynamic item) {
59 5 : return item.title.toString().split('/').first.trim();
60 : }
61 :
62 0 : dynamic wrapBySingleEntity(List<dynamic> items) {
63 0 : Currency? def = CurrencyProvider.find(AppPreferences.get(AppPreferences.prefCurrency));
64 0 : return AccountAppData(
65 0 : title: getName(items.first),
66 0 : type: AppAccountType.account.toString(),
67 : currency: def,
68 0 : details: items.fold(0.0, (value, e) => value + exchange.reform(e.details, e.currency, def)),
69 : );
70 : }
71 :
72 1 : @override
73 : Widget buildListWidget(item, BuildContext context) {
74 3 : return item is List && item.length > 1
75 1 : ? buildGroupedListWidget(item, context)
76 1 : : buildSingleListWidget(item, context);
77 : }
78 :
79 0 : List<dynamic> updateItems(items, summaryItem) {
80 0 : return items.map((o) {
81 0 : if (o is AccountAppData) {
82 0 : if (summaryItem.details == 0) {
83 0 : o.progress = 0;
84 : } else {
85 0 : o.progress = exchange.reform(o.details, o.currency, exchange.getDefaultCurrency()) / summaryItem.details;
86 : }
87 : }
88 : return o;
89 0 : }).toList();
90 : }
91 :
92 0 : Widget buildGroupedListWidget(List<dynamic> items, BuildContext context) {
93 0 : final item = wrapBySingleEntity(items);
94 0 : final scope = updateItems(items, item);
95 0 : return BaseGroupWidget(
96 0 : title: item.title,
97 0 : total: item.details,
98 0 : description: item.detailsFormatted,
99 0 : progress: scope.map((e) => e.progress).cast<double>().toList(),
100 0 : color: scope.map((e) => e.color ?? Colors.transparent).cast<Color>().toList(),
101 0 : width: width - ThemeHelper.getIndent() / 2,
102 : items: scope,
103 : route: AppRoute.accountRoute,
104 : );
105 : }
106 :
107 1 : Widget buildSingleListWidget(item, BuildContext context) {
108 2 : item = item is List ? item.first : item;
109 1 : return BaseSwipeWidget(
110 : routePath: AppRoute.accountEditRoute,
111 1 : uuid: item.uuid,
112 1 : child: BaseLineWidget(
113 1 : uuid: item.uuid,
114 1 : title: item.title,
115 1 : description: item.description ?? '',
116 1 : details: item.detailsFormatted,
117 1 : progress: item.progress,
118 1 : color: item.color ?? Colors.transparent,
119 1 : icon: item.icon ?? Icons.radio_button_unchecked_sharp,
120 1 : hidden: item.hidden,
121 1 : skip: item.skip,
122 1 : width: width,
123 1 : route: routeList,
124 1 : error: item.error,
125 : ),
126 : );
127 : }
128 : }
|