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 'dart:io';
5 :
6 : import 'package:app_finance/_classes/herald/app_locale.dart';
7 : import 'package:app_finance/_classes/storage/app_data.dart';
8 : import 'package:app_finance/_classes/structure/bill_app_data.dart';
9 : import 'package:app_finance/_classes/structure/invoice_app_data.dart';
10 : import 'package:app_finance/_classes/structure/payment_app_data.dart';
11 : import 'package:app_finance/_configs/automation_type.dart';
12 : import 'package:app_finance/_ext/data_ext.dart';
13 : import 'package:app_finance/_ext/date_time_ext.dart';
14 : import 'package:flutter/foundation.dart';
15 : import 'package:home_widget/home_widget.dart';
16 :
17 : class PaymentsController {
18 : final AppData state;
19 :
20 6 : PaymentsController(this.state) {
21 18 : final data = state.getList(AppDataType.payments).cast();
22 6 : DateTime now = DateTime.now();
23 6 : for (PaymentAppData item in data) {
24 0 : item = item.clone();
25 : bool needUpdate = false;
26 0 : while (item.updatedAt.isBefore(now)) {
27 : needUpdate = true;
28 0 : dynamic obj = item.data.toDataObject(state);
29 0 : obj.createdAt = item.updatedAt;
30 0 : obj.updatedAt = item.updatedAt;
31 0 : obj.payment = item.uuid;
32 0 : state.add(obj);
33 0 : if (item.title == AppAutomationType.days.name) {
34 0 : item.updatedAt = item.updatedAt.add(Duration(days: item.days > 0 ? item.days : 1));
35 0 : } else if (item.title == AppAutomationType.week.name) {
36 0 : item.updatedAt = item.updatedAt.add(const Duration(days: 7));
37 0 : } else if (item.title == AppAutomationType.year.name) {
38 0 : item.updatedAt = item.updatedAt.getNextYear();
39 : } else {
40 0 : item.updatedAt = item.updatedAt.getNextMonth();
41 : }
42 : }
43 : if (needUpdate) {
44 0 : state.update(item.uuid!, item);
45 : }
46 : }
47 : // TODO: create iOS Widget
48 : if (!kIsWeb) {
49 6 : if (Platform.isAndroid) {
50 0 : _updateWidget();
51 : }
52 : }
53 : }
54 :
55 0 : String _prepareData() {
56 0 : List<Map<String, String>> result = [];
57 0 : List<PaymentAppData> data = state.getList(AppDataType.payments).cast();
58 0 : data.sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
59 0 : for (PaymentAppData item in data) {
60 0 : dynamic obj = item.data.toDataObject(state);
61 0 : String type = AppLocale.labels.flowTypeInvoice;
62 : String symbol = '+';
63 0 : if (obj is BillAppData) {
64 0 : type = AppLocale.labels.bill;
65 : symbol = '-';
66 0 : } else if (obj is InvoiceAppData && obj.accountFrom != null) {
67 0 : type = AppLocale.labels.transferHeadline;
68 : symbol = '';
69 : }
70 0 : result.add({
71 0 : '"title"': '"$type: ${obj.title.replaceAll('"', "'")}"',
72 0 : '"description"': '"${item.description.replaceAll('"', "'")}, ${item.updatedAt.monthDay()}"',
73 0 : '"details"': '"$symbol${obj.detailsFormatted}"',
74 : });
75 : }
76 0 : return result.toString();
77 : }
78 :
79 0 : Future<void> _updateWidget() async {
80 0 : await HomeWidget.saveWidgetData<String>('payments_widget_data', _prepareData());
81 0 : await HomeWidget.updateWidget(
82 : qualifiedAndroidName: 'com.tercad.fingrom.PaymentsWidgetProvider',
83 : // iOSName: 'PaymentsWidget',
84 : );
85 : }
86 : }
|