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