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 'dart:collection';
5 : import 'dart:math';
6 :
7 : import 'package:app_finance/_ext/date_time_ext.dart';
8 :
9 : class SummaryAppData {
10 : final _scope = SplayTreeMap<double, String>();
11 : final _actual = SplayTreeMap<double, String>();
12 : final _hash = HashMap<String, double>();
13 : double total;
14 : int startingDay;
15 :
16 6 : SummaryAppData({
17 : this.total = 0,
18 : this.startingDay = 1,
19 : List<String> list = const [],
20 : }) {
21 6 : this.list = list;
22 : }
23 :
24 12 : SplayTreeMap<double, String> get origin => _scope;
25 :
26 36 : List<String> get list => _scope.values.toList().reversed.toList();
27 :
28 18 : List<String> get listActual => _actual.values.toList().reversed.toList();
29 :
30 6 : set list(List<String> list) {
31 12 : for (int i = 0; i < list.length; i++) {
32 0 : add(list[i], id: i + 1);
33 : }
34 : }
35 :
36 2 : add(String value, {DateTime? updatedAt, int? id}) {
37 4 : if (_hash.containsKey(value)) {
38 8 : _scope.remove(_hash[value]);
39 8 : _actual.remove(_hash[value]);
40 4 : _hash.remove(value);
41 : }
42 2 : final current = DateTime.now();
43 : updatedAt ??= current;
44 10 : double key = (id ?? updatedAt.millisecondsSinceEpoch).toDouble() + Random().nextDouble();
45 4 : _scope[key] = value;
46 4 : _hash[value] = key;
47 8 : if (key >= current.getStartingDay(startingDay).millisecondsSinceEpoch) {
48 4 : _actual[key] = value;
49 : }
50 : }
51 : }
|