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:flutter/material.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 0 : 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 0 : }) : _closedAt = closedAt ?? (closedAtFormatted != null ? DateTime.parse(closedAtFormatted) : DateTime.now());
34 :
35 0 : @override
36 : String getClassName() => 'AccountAppData';
37 :
38 0 : @override
39 : AppDataType getType() => AppDataType.accounts;
40 :
41 0 : @override
42 : AccountAppData clone() {
43 0 : return AccountAppData(
44 0 : uuid: super.uuid,
45 0 : title: super.title,
46 0 : type: type,
47 0 : details: super.details,
48 0 : progress: super.progress,
49 0 : description: super.description,
50 0 : color: super.color,
51 0 : icon: super.icon,
52 0 : currency: super.currency,
53 0 : createdAt: super.createdAt,
54 0 : closedAt: closedAt,
55 0 : hidden: super.hidden,
56 : );
57 : }
58 :
59 0 : factory AccountAppData.fromJson(Map<String, dynamic> json) {
60 0 : return AccountAppData(
61 0 : uuid: json['uuid'],
62 0 : title: json['title'],
63 0 : type: json['type'],
64 0 : details: 0.0 + json['details'],
65 0 : progress: 0.0 + json['progress'],
66 0 : description: json['description'],
67 0 : color: json['color'] != null ? MaterialColor(json['color'], const <int, Color>{}) : null,
68 0 : icon: (json['icon'] as int?)?.toIcon(),
69 0 : currency: CurrencyProvider.find(json['currency']),
70 0 : createdAt: DateTime.parse(json['createdAt']),
71 0 : updatedAt: DateTime.parse(json['updatedAt']),
72 0 : closedAt: DateTime.parse(json['closedAt']),
73 0 : hidden: json['hidden'],
74 : );
75 : }
76 :
77 0 : @override
78 0 : Map<String, dynamic> toJson() => {
79 0 : ...super.toJson(),
80 0 : 'type': type,
81 0 : 'closedAt': closedAt.toIso8601String(),
82 : };
83 :
84 : // ignore: unnecessary_getters_setters
85 0 : DateTime get closedAt => _closedAt;
86 0 : set closedAt(DateTime value) => _closedAt = value;
87 0 : String get closedAtFormatted => _closedAt.yearMonth();
88 0 : set closedAtFormatted(String value) => _closedAt = DateTime.parse(value);
89 :
90 0 : String get detailsFormatted => (super.details as double).toCurrency(currency: currency, withPattern: false);
91 :
92 0 : @override
93 : Widget? get error {
94 : String? error;
95 0 : final isCredit = [AppAccountType.credit.toString(), AppAccountType.creditCard.toString()].contains(type);
96 0 : final isCard = [AppAccountType.debitCard.toString(), AppAccountType.creditCard.toString()].contains(type);
97 0 : if (details < 0 && !isCredit) {
98 0 : error = AppLocale.labels.errorNegative;
99 0 : } else if (isCard && closedAt.isBefore(DateTime.now())) {
100 0 : error = AppLocale.labels.errorExpired;
101 : }
102 :
103 : return error == null
104 : ? null
105 0 : : Tooltip(
106 : message: error,
107 0 : child: Icon(Icons.warning, semanticLabel: error),
108 : );
109 : }
110 : }
|