dev
dart 82 lines 2.44 KB
Raw
1 import "package:cake_wallet/view_model/dashboard/action_list_item.dart";
2 import "package:cake_wallet/view_model/dashboard/date_section_item.dart";
3 import "package:flutter/foundation.dart";
4
5 enum _DateBucket { recent, last7Days, last30Days, byMonth }
6
7 List<ActionListItem> formattedItemsList(List<ActionListItem> items) {
8 final formattedList = <ActionListItem>[];
9 items.sort((a, b) => b.date.compareTo(a.date));
10
11 final now = DateTime.now();
12 final todayTreshold = DateTime(now.year, now.month, now.day);
13 final last7daysThreshold = now.subtract(const Duration(days: 7));
14 final last30daysThreshold = now.subtract(const Duration(days: 30));
15
16 _DateBucket? lastBucket;
17 DateTime? lastMonthDate;
18
19 for (final transaction in items) {
20 final date = transaction.date;
21
22 final bucket = date.isAfter(todayTreshold)
23 ? _DateBucket.recent
24 : date.isAfter(last7daysThreshold)
25 ? _DateBucket.last7Days
26 : date.isAfter(last30daysThreshold)
27 ? _DateBucket.last30Days
28 : _DateBucket.byMonth;
29
30 switch (bucket) {
31 case _DateBucket.recent:
32 if (lastBucket != _DateBucket.recent) {
33 formattedList.add(
34 TodayTransactionItem(
35 date,
36 key: const ValueKey("today_section_item_key"),
37 ),
38 );
39 }
40 break;
41 case _DateBucket.last7Days:
42 if (lastBucket != _DateBucket.last7Days) {
43 formattedList.add(
44 Last7daysTransactionItem(
45 date,
46 key: const ValueKey("last_7_days_section_item_key"),
47 ),
48 );
49 }
50 break;
51 case _DateBucket.last30Days:
52 if (lastBucket != _DateBucket.last30Days) {
53 formattedList.add(
54 Last30daysTransactionItem(
55 date,
56 key: const ValueKey("last_30_days_section_item_key"),
57 ),
58 );
59 }
60 break;
61 case _DateBucket.byMonth:
62 final isNewMonth = lastMonthDate == null ||
63 lastMonthDate.year != date.year ||
64 lastMonthDate.month != date.month;
65 if (isNewMonth) {
66 lastMonthDate = date;
67 formattedList.add(
68 DateSectionItem(
69 date,
70 key: ValueKey("date_section_item_${date.year}_${date.month}_key"),
71 ),
72 );
73 }
74 break;
75 }
76
77 lastBucket = bucket;
78 formattedList.add(transaction);
79 }
80
81 return formattedList;
82 }