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/structure/abstract_app_data.dart';
5 : import 'package:app_finance/_classes/storage/app_data.dart';
6 : import 'package:app_finance/_ext/date_time_ext.dart';
7 : import 'package:app_finance/_ext/int_ext.dart';
8 : import 'package:flutter/material.dart';
9 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
10 :
11 : class GoalAppData extends AbstractAppData {
12 : DateTime _closedAt;
13 : double initial;
14 :
15 2 : GoalAppData({
16 : required super.title,
17 : required this.initial,
18 : super.uuid,
19 : super.details = 0.0,
20 : super.progress = 0.0,
21 : super.description,
22 : super.color,
23 : super.icon,
24 : super.currency,
25 : super.updatedAt,
26 : super.createdAt,
27 : super.createdAtFormatted,
28 : DateTime? closedAt,
29 : String? closedAtFormatted,
30 : super.hidden,
31 2 : }) : _closedAt = closedAt ?? (closedAtFormatted != null ? DateTime.parse(closedAtFormatted) : DateTime.now());
32 :
33 0 : @override
34 : String getClassName() => 'GoalAppData';
35 :
36 0 : @override
37 : AppDataType getType() => AppDataType.goals;
38 :
39 0 : @override
40 : GoalAppData clone() {
41 0 : return GoalAppData(
42 0 : title: super.title,
43 0 : initial: initial,
44 0 : uuid: super.uuid,
45 0 : details: super.details,
46 0 : progress: super.progress,
47 0 : description: super.description,
48 0 : color: super.color,
49 0 : icon: super.icon,
50 0 : currency: super.currency,
51 0 : createdAt: super.createdAt,
52 0 : closedAt: closedAt,
53 0 : hidden: super.hidden,
54 : );
55 : }
56 :
57 0 : factory GoalAppData.fromJson(Map<String, dynamic> json) {
58 0 : return GoalAppData(
59 0 : title: json['title'],
60 0 : initial: json['initial'] ?? 0.0,
61 0 : uuid: json['uuid'],
62 0 : details: 0.0 + json['details'],
63 0 : progress: 0.0 + json['progress'],
64 0 : description: json['description'],
65 0 : color: json['color'] != null ? MaterialColor(json['color'], const <int, Color>{}) : null,
66 0 : icon: (json['icon'] as int?)?.toIcon(),
67 0 : currency: CurrencyProvider.find(json['currency']),
68 0 : updatedAt: DateTime.parse(json['updatedAt']),
69 0 : createdAt: DateTime.parse(json['createdAt']),
70 0 : closedAt: DateTime.parse(json['closedAt']),
71 0 : hidden: json['hidden'],
72 : );
73 : }
74 :
75 0 : @override
76 0 : Map<String, dynamic> toJson() => {
77 0 : ...super.toJson(),
78 0 : 'initial': initial,
79 0 : 'closedAt': closedAt.toIso8601String(),
80 : };
81 :
82 : // ignore: unnecessary_getters_setters
83 0 : DateTime get closedAt => _closedAt;
84 0 : set closedAt(DateTime value) => _closedAt = value;
85 0 : String get closedAtFormatted => _closedAt.yMEd();
86 0 : set closedAtFormatted(String value) => _closedAt = DateTime.parse(value);
87 :
88 0 : double get state {
89 0 : DateTime currentDate = DateTime.now();
90 0 : if (closedAt.isBefore(currentDate) || closedAt.isAtSameMomentAs(currentDate)) {
91 : return 1.0;
92 0 : } else if (currentDate.isBefore(super.createdAt) || super.createdAt.isAtSameMomentAs(currentDate)) {
93 : return 0.0;
94 : } else {
95 0 : double totalDays = closedAt.difference(super.createdAt).inDays.toDouble();
96 0 : double currentDays = currentDate.difference(super.createdAt).inDays.toDouble();
97 0 : return currentDays / totalDays;
98 : }
99 : }
100 :
101 0 : String get detailsFormatted => (super.details as double).toCurrency(currency: currency, withPattern: false);
102 : }
|