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_locale.dart';
5 : import 'package:app_finance/_classes/structure/abstract_app_data.dart';
6 : import 'package:app_finance/_classes/storage/app_data.dart';
7 : import 'package:app_finance/_configs/account_type.dart';
8 : import 'package:app_finance/_ext/date_time_ext.dart';
9 : import 'package:app_finance/_ext/int_ext.dart';
10 : import 'package:material_ui/material_ui.dart';
11 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
12 :
13 : class AccountAppData extends AbstractAppData {
14 : DateTime _closedAt;
15 : String type;
16 :
17 2 : AccountAppData({
18 : required super.title,
19 : required this.type,
20 : super.uuid,
21 : super.details,
22 : super.progress = 0.0,
23 : super.description,
24 : super.color,
25 : super.icon,
26 : super.currency,
27 : super.updatedAt,
28 : super.createdAt,
29 : super.createdAtFormatted,
30 : DateTime? closedAt,
31 : String? closedAtFormatted,
32 : super.hidden,
33 : super.skip,
34 2 : }) : _closedAt = closedAt ?? (closedAtFormatted != null ? DateTime.parse(closedAtFormatted) : DateTime.now());
35 :
36 2 : @override
37 : String getClassName() => 'AccountAppData';
38 :
39 2 : @override
40 : AppDataType getType() => AppDataType.accounts;
41 :
42 2 : @override
43 : AccountAppData clone() {
44 2 : return AccountAppData(
45 2 : uuid: super.uuid,
46 2 : title: super.title,
47 2 : type: type,
48 2 : details: super.details,
49 2 : progress: super.progress,
50 2 : description: super.description,
51 2 : color: super.color,
52 2 : icon: super.icon,
53 2 : currency: super.currency,
54 2 : createdAt: super.createdAt,
55 2 : closedAt: closedAt,
56 2 : hidden: super.hidden,
57 2 : skip: super.skip,
58 : );
59 : }
60 :
61 0 : factory AccountAppData.fromJson(Map<String, dynamic> json) {
62 0 : return AccountAppData(
63 0 : uuid: json['uuid'],
64 0 : title: json['title'],
65 0 : type: json['type'],
66 0 : details: 0.0 + json['details'],
67 0 : progress: 0.0 + json['progress'],
68 0 : description: json['description'],
69 0 : color: json['color'] != null ? MaterialColor(json['color'], const <int, Color>{}) : null,
70 0 : icon: (json['icon'] as int?)?.toIcon(),
71 0 : currency: CurrencyProvider.find(json['currency']),
72 0 : createdAt: DateTime.parse(json['createdAt']),
73 0 : updatedAt: DateTime.parse(json['updatedAt']),
74 0 : closedAt: DateTime.parse(json['closedAt']),
75 0 : hidden: json['hidden'],
76 0 : skip: json['skip'] ?? false,
77 : );
78 : }
79 :
80 2 : @override
81 2 : Map<String, dynamic> toJson() => {
82 2 : ...super.toJson(),
83 4 : 'type': type,
84 6 : 'closedAt': closedAt.toIso8601String(),
85 : };
86 :
87 : // ignore: unnecessary_getters_setters
88 4 : DateTime get closedAt => _closedAt;
89 0 : set closedAt(DateTime value) => _closedAt = value;
90 0 : String get closedAtFormatted => _closedAt.yearMonth();
91 0 : set closedAtFormatted(String value) => _closedAt = DateTime.parse(value);
92 :
93 8 : String get detailsFormatted => (super.details as double).toCurrency(currency: currency, withPattern: false);
94 :
95 2 : @override
96 : Widget? get error {
97 : String? error;
98 10 : final isCredit = [AppAccountType.credit.toString(), AppAccountType.creditCard.toString()].contains(type);
99 10 : final isCard = [AppAccountType.debitCard.toString(), AppAccountType.creditCard.toString()].contains(type);
100 4 : if (details < 0 && !isCredit) {
101 0 : error = AppLocale.labels.errorNegative;
102 0 : } else if (isCard && closedAt.isBefore(DateTime.now())) {
103 0 : error = AppLocale.labels.errorExpired;
104 : }
105 :
106 : return error == null
107 : ? null
108 0 : : Tooltip(
109 : message: error,
110 0 : child: Icon(Icons.warning, semanticLabel: error),
111 : );
112 : }
113 : }
|