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/payments_controller.dart';
5 : import 'package:app_finance/_classes/integration/notification/notification_controller.dart';
6 : import 'package:app_finance/_classes/herald/app_design.dart';
7 : import 'package:app_finance/_classes/herald/app_start_of_month.dart';
8 : import 'package:app_finance/_classes/storage/app_data.dart';
9 : import 'package:app_finance/_classes/herald/app_locale.dart';
10 : import 'package:app_finance/_classes/structure/currency/exchange.dart';
11 : import 'package:app_finance/_classes/storage/app_preferences.dart';
12 : import 'package:app_finance/_configs/screen_helper.dart';
13 : import 'package:app_finance/_configs/theme_helper.dart';
14 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
15 : import 'package:app_finance/_ext/date_time_ext.dart';
16 : import 'package:app_finance/components/_core/component_data.dart';
17 : import 'package:app_finance/components/_core/components_builder.dart';
18 : import 'package:app_finance/components/_core/list_component_registry.dart';
19 : import 'package:app_finance/components/component_chart.dart';
20 : import 'package:app_finance/components/widgets/account_flow_chart.dart';
21 : import 'package:app_finance/components/widgets/budget_forecast_chart.dart';
22 : import 'package:app_finance/components/component_recent.dart';
23 : import 'package:app_finance/components/widgets/bill_ytd_chart.dart';
24 : import 'package:app_finance/pages/_interfaces/abstract_page_state.dart';
25 : import 'package:app_finance/pages/home/home_edit_page.dart';
26 : import 'package:app_finance/pages/home/widgets/security_bio_tab.dart';
27 : import 'package:app_finance/pages/home/widgets/security_tab.dart';
28 : import 'package:app_finance/pages/start/start_page.dart';
29 : import 'package:app_finance/design/wrapper/grid_layer.dart';
30 : import 'package:app_finance/design/wrapper/markdown_builder_wrapper.dart';
31 : import 'package:app_finance/pages/home/widgets/init_tab.dart';
32 : import 'package:app_finance/design/button/toolbar_button_widget.dart';
33 : import 'package:app_finance/components/widgets/account_widget.dart';
34 : import 'package:app_finance/components/widgets/bill_widget.dart';
35 : import 'package:app_finance/components/widgets/budget_widget.dart';
36 : import 'package:app_finance/pages/home/widgets/goal_widget.dart';
37 : import 'package:flutter/foundation.dart';
38 : import 'package:material_ui/material_ui.dart';
39 : import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
40 : import 'package:flutter_svg/flutter_svg.dart';
41 : import 'package:package_info_plus/package_info_plus.dart';
42 : import 'package:provider/provider.dart';
43 : import 'package:simple_totp_auth/simple_totp_auth.dart';
44 :
45 : class HomePage extends StatefulWidget {
46 1 : const HomePage({super.key});
47 :
48 1 : @override
49 1 : HomePageState createState() => HomePageState();
50 : }
51 :
52 : class HomePageState extends AbstractPageState<HomePage> {
53 : String? toExpand;
54 : String secret = '';
55 : bool isEditMode = false;
56 : bool isOtpPassed = false;
57 : late String version;
58 :
59 1 : @override
60 : initState() {
61 1 : super.initState();
62 2 : toExpand = AppPreferences.get(AppPreferences.prefExpand);
63 2 : version = AppPreferences.get(AppPreferences.prefVersion) ?? '';
64 2 : secret = AppPreferences.get(AppPreferences.prefPrivacyKey) ?? '';
65 2 : if (secret.isEmpty) {
66 2 : secret = TOTP.generateSecret();
67 2 : AppPreferences.set(AppPreferences.prefPrivacyKey, secret);
68 : }
69 2 : PackageInfo.fromPlatform().then((PackageInfo value) {
70 0 : if (version != value.version) {
71 0 : WidgetsBinding.instance.addPostFrameCallback((_) => showModalBottomSheet(
72 0 : context: context,
73 0 : builder: (BuildContext context) => buildReleaseHelper(context, version),
74 : ));
75 0 : AppPreferences.set(AppPreferences.prefVersion, value.version);
76 : }
77 : });
78 : }
79 :
80 0 : Widget buildReleaseHelper(BuildContext context, String version) => buildHelper(
81 : context,
82 : type: 'upgrade',
83 0 : builder: (context, snapshot) {
84 0 : if (snapshot.hasData) {
85 0 : String data = snapshot.data!;
86 0 : if (version.isNotEmpty) {
87 0 : data = data.split('### $version')[0];
88 : }
89 0 : return Directionality(
90 0 : textDirection: AppDesign.getAlignment<TextDirection>(),
91 0 : child: Markdown(data: data, onTapLink: MarkdownBuilderWrapper.openLink),
92 : );
93 : }
94 0 : return Container();
95 : },
96 : );
97 :
98 0 : @override
99 0 : String getTitle() => AppLocale.labels.appTitle;
100 :
101 1 : @override
102 : Widget buildBottomBar(BuildContext context, BoxConstraints constraints) {
103 2 : return ThemeHelper.isWearable ? ThemeHelper.emptyBox : super.buildBottomBar(context, constraints);
104 : }
105 :
106 0 : @override
107 : Widget? buildRightBar(BuildContext context, BoxConstraints constraints) {
108 0 : return ThemeHelper.isWearable ? null : super.buildRightBar(context, constraints);
109 : }
110 :
111 1 : @override
112 : Widget? getBarLeading(NavigatorState nav) {
113 1 : if (ScreenHelper.state().isWide) {
114 : return ThemeHelper.emptyBox;
115 : }
116 1 : return Builder(
117 1 : builder: (BuildContext context) {
118 1 : final scaffold = Scaffold.of(context);
119 1 : return ToolbarButtonWidget(
120 : icon: Icons.menu,
121 2 : tooltip: AppLocale.labels.navigationTooltip,
122 3 : onPressed: () => AppDesign.isRightToLeft() ? scaffold.openEndDrawer() : scaffold.openDrawer(),
123 : );
124 : },
125 : );
126 : }
127 :
128 1 : @override
129 : List<Widget> getBarActions(NavigatorState nav) {
130 1 : final isWide = ScreenHelper.state().isWide;
131 1 : return [
132 1 : ToolbarButtonWidget(
133 : isWide: isWide,
134 : icon: Icons.app_registration_outlined,
135 2 : tooltip: AppLocale.labels.customizeTooltip,
136 0 : onPressed: () => setState(() => isEditMode = true),
137 : ),
138 3 : if (![TargetPlatform.iOS, TargetPlatform.macOS, TargetPlatform.android].contains(defaultTargetPlatform))
139 0 : ToolbarButtonWidget(
140 : isWide: isWide,
141 : icon: Icons.switch_access_shortcut_add_outlined,
142 0 : tooltip: AppLocale.labels.subscriptionTooltip,
143 0 : onPressed: () => nav.pushNamed(AppRoute.subscriptionRoute),
144 : ),
145 : ];
146 : }
147 :
148 1 : @override
149 : Widget getBarTitle(BuildContext context, [bool isBottom = false]) {
150 1 : return SvgPicture.asset(
151 : 'assets/images/fingrom.svg',
152 : height: isBottom ? 20 : 40,
153 : alignment: Alignment.centerLeft,
154 2 : semanticsLabel: AppLocale.labels.appTitle,
155 : );
156 : }
157 :
158 1 : @override
159 2 : String getButtonName() => AppLocale.labels.addMainTooltip;
160 :
161 1 : @override
162 : Widget buildButton(BuildContext context, BoxConstraints constraints) {
163 1 : NavigatorState nav = Navigator.of(context);
164 1 : return FloatingActionButton(
165 : heroTag: 'home_page',
166 1 : mini: ThemeHelper.isWearable,
167 2 : onPressed: () => nav.pushNamed(AppRoute.billAddRoute),
168 1 : tooltip: getButtonName(),
169 : child: const Icon(Icons.add),
170 : );
171 : }
172 :
173 1 : @override
174 : Widget build(BuildContext context) {
175 2 : Provider.of<AppLocale>(context, listen: false).updateState(context);
176 1 : if (isEditMode) {
177 0 : return HomeEditPage(callback: () => setState(() => isEditMode = false));
178 : }
179 1 : if (AppPreferences.get(AppPreferences.prefPrivacyPolicy) == null) {
180 : return const StartPage();
181 : }
182 2 : return Consumer<AppData>(builder: (context, appState, _) {
183 2 : if (AppPreferences.get(AppPreferences.prefIsBio) == AppPreferences.isActive && !isOtpPassed) {
184 0 : return SecurityBioTab(
185 0 : onSuccess: () => setState(() => isOtpPassed = true),
186 : );
187 2 : } else if (AppPreferences.get(AppPreferences.prefIsOTP) == AppPreferences.isActive && !isOtpPassed) {
188 0 : return SecurityTab(
189 0 : onSuccess: () => setState(() => isOtpPassed = true),
190 : );
191 : }
192 1 : if (appState.isLoading) {
193 : return const InitTab();
194 : }
195 3 : WidgetsBinding.instance.addPostFrameCallback((_) {
196 1 : PaymentsController(appState);
197 1 : final messenger = ScaffoldMessenger.of(context);
198 1 : final notifications = NotificationController.instance;
199 1 : notifications.onImported = (added) => messenger.showSnackBar(
200 0 : SnackBar(content: Text(AppLocale.labels.notificationAutoImported(added))),
201 : );
202 1 : notifications.attach(appState);
203 : });
204 1 : return super.build(context);
205 : });
206 : }
207 :
208 0 : Widget buildContentWideScreen() {
209 0 : return ComponentsBuilder([
210 0 : {
211 0 : componentData.key: ComponentRegistry.chart.toString(),
212 : componentData.startX: 0.0,
213 : componentData.startY: 4.0,
214 : componentData.endX: 4.0,
215 : componentData.endY: 7.0,
216 : componentData.order: 0,
217 0 : componentData.type: ComponentChartType.accountFlow.toString(),
218 : },
219 0 : {
220 0 : componentData.key: ComponentRegistry.chart.toString(),
221 : componentData.startX: 4.0,
222 : componentData.startY: 4.0,
223 : componentData.endX: 8.0,
224 : componentData.endY: 7.0,
225 : componentData.order: 1,
226 0 : componentData.type: ComponentChartType.billYtd.toString()
227 : },
228 0 : {
229 0 : componentData.key: ComponentRegistry.chart.toString(),
230 : componentData.startX: 8.0,
231 : componentData.startY: 4.0,
232 : componentData.endX: 12.0,
233 : componentData.endY: 7.0,
234 : componentData.order: 2,
235 0 : componentData.type: ComponentChartType.budgetForecast.toString()
236 : },
237 0 : {
238 0 : componentData.key: ComponentRegistry.summary.toString(),
239 : componentData.startX: 0,
240 : componentData.startY: 0,
241 : componentData.endX: 12.0,
242 : componentData.endY: 4.0,
243 : componentData.order: 3,
244 0 : componentData.type: ComponentRecentType.account.toString()
245 : }
246 : ]);
247 : }
248 :
249 0 : Widget buildContentSmallScreen() {
250 0 : return ComponentsBuilder([
251 0 : {
252 0 : componentData.key: ComponentRegistry.recent.toString(),
253 : componentData.startX: 0,
254 : componentData.startY: 0,
255 : componentData.endX: 3.0,
256 : componentData.endY: 2.0,
257 : componentData.order: 0,
258 0 : componentData.type: ComponentRecentType.account.toString(),
259 : ComponentRecent.count: 0
260 : },
261 0 : {
262 0 : componentData.key: ComponentRegistry.recent.toString(),
263 : componentData.startX: 3.0,
264 : componentData.startY: 0.0,
265 : componentData.endX: 6.0,
266 : componentData.endY: 2.0,
267 : componentData.order: 1,
268 0 : componentData.type: ComponentRecentType.budget.toString(),
269 : ComponentRecent.count: 0
270 : },
271 0 : {
272 0 : componentData.key: ComponentRegistry.recent.toString(),
273 : componentData.startX: 0.0,
274 : componentData.startY: 2.0,
275 : componentData.endX: 6.0,
276 : componentData.endY: 12.0,
277 : componentData.order: 2,
278 0 : componentData.type: ComponentRecentType.bill.toString(),
279 : ComponentRecent.count: 10
280 : }
281 : ]);
282 : }
283 :
284 1 : @override
285 : Widget buildContent(BuildContext context, BoxConstraints constraints) {
286 1 : final data = ComponentsBuilder.getData(context);
287 1 : bool isWide = ScreenHelper.state().isWide;
288 1 : final countHeight = ThemeHelper.getHeightCount(context, constraints);
289 1 : final countWidth = ThemeHelper.getWidthCount(constraints, context);
290 1 : if (data != null && data.isNotEmpty) {
291 0 : return ComponentsBuilder(data);
292 1 : } else if (countHeight > 3 && isWide) {
293 0 : return buildContentWideScreen();
294 1 : } else if (countHeight < 3 && countWidth == 1 && !ThemeHelper.isHorizontal(constraints)) {
295 0 : return buildContentSmallScreen();
296 : }
297 1 : double indent = ThemeHelper.getIndent();
298 1 : EdgeInsets margin = EdgeInsets.only(top: indent);
299 1 : bool isVertical = countWidth == 1 && !ThemeHelper.isWearable;
300 1 : double width = ThemeHelper.getWidth(context, 3, constraints);
301 4 : double partWidth = width / countWidth - indent * (countWidth - 1);
302 3 : DateTime curr = DateTime.now().getStartingDay(AppStartOfMonth.get());
303 :
304 1 : final billWidget = BillWidget(
305 : margin: margin,
306 4 : title: '${AppLocale.labels.billHeadline}, ${curr.fullMonth()}',
307 2 : state: state.get(AppDataType.bills),
308 : limit: 7,
309 : route: AppRoute.billRoute,
310 2 : tooltip: AppLocale.labels.billTooltip,
311 : width: isVertical ? width : partWidth,
312 : hasExpand: isVertical,
313 1 : toExpand: toExpand,
314 0 : callback: (v) => setState(() => toExpand = v),
315 : );
316 1 : final accountWidget = AccountWidget(
317 : margin: margin,
318 5 : title: '${AppLocale.labels.accountHeadline}, ${AppLocale.labels.total}',
319 2 : state: state.get(AppDataType.accounts),
320 : limit: 7,
321 : route: AppRoute.accountRoute,
322 2 : tooltip: AppLocale.labels.accountTooltip,
323 : width: isVertical ? width : partWidth,
324 : hasExpand: isVertical,
325 1 : toExpand: toExpand,
326 0 : callback: (v) => setState(() => toExpand = v),
327 3 : )..exchange = Exchange(store: super.state);
328 1 : final budgetWidget = BudgetWidget(
329 : margin: margin,
330 5 : title: '${AppLocale.labels.budgetHeadline}, ${AppLocale.labels.left}',
331 2 : state: state.get(AppDataType.budgets),
332 : limit: 7,
333 : route: AppRoute.budgetRoute,
334 2 : tooltip: AppLocale.labels.budgetTooltip,
335 : width: isVertical ? width : partWidth,
336 : hasExpand: isVertical,
337 1 : toExpand: toExpand,
338 0 : callback: (v) => setState(() => toExpand = v),
339 3 : )..exchange = Exchange(store: super.state);
340 :
341 1 : return GridLayer(
342 : padding: indent,
343 : width: width,
344 : crossAxisCount: countWidth,
345 : strategy: switch (countWidth) {
346 1 : 4 => [
347 0 : [0, 6],
348 0 : [2, 5],
349 0 : [3, 4],
350 0 : [1, 7]
351 : ],
352 1 : 3 => [
353 0 : [2],
354 0 : [3],
355 0 : [0, 1]
356 : ],
357 2 : 2 => [
358 1 : [2, 3],
359 1 : [0, 1]
360 : ],
361 0 : _ => [
362 0 : [0, 1, 2, 3]
363 : ]
364 : },
365 1 : children: [
366 1 : countHeight > 3
367 : ? isWide
368 0 : ? Expanded(
369 0 : child: Padding(
370 0 : padding: EdgeInsets.only(top: indent / 2),
371 : child: const ComponentRecent({'type': ComponentRecentType.goals, 'count': 7}),
372 : ),
373 : )
374 1 : : GoalWidget(
375 : width: isVertical ? width : partWidth,
376 2 : state: super.state.getList(AppDataType.goals),
377 : )
378 : : ThemeHelper.emptyBox,
379 : billWidget,
380 : accountWidget,
381 : budgetWidget,
382 0 : () => const Expanded(
383 : child: Column(
384 : children: [
385 : Expanded(child: ThemeHelper.emptyBox),
386 : AccountFlowChart(),
387 : ThemeHelper.hIndent6x,
388 : ],
389 : ),
390 : ),
391 0 : () => const Expanded(
392 : child: Column(
393 : children: [
394 : Expanded(child: ThemeHelper.emptyBox),
395 : BudgetForecastChart(),
396 : ThemeHelper.hIndent6x,
397 : ],
398 : ),
399 : ),
400 0 : () => const Expanded(
401 : child: Column(
402 : children: [
403 : Expanded(child: ThemeHelper.emptyBox),
404 : BillYtdChart(),
405 : ThemeHelper.hIndent6x,
406 : ],
407 : ),
408 : ),
409 0 : () => const Expanded(
410 : child: ComponentRecent({'type': ComponentRecentType.invoice, 'count': 7}),
411 : ),
412 : ],
413 : );
414 : }
415 : }
|