-
Notifications
You must be signed in to change notification settings - Fork 0
/
streakservice.dart
249 lines (216 loc) · 8.12 KB
/
streakservice.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//USER FUNCTIONs are: CalculateStreaks() and CalculateStreakData()
//|___EXPLANATION OF VARIABLES____________________________________________________
//|___longestStreak, lastStreak, currentStreakIsLastStreak trivial to understand
//|___streakListFilter is the streakList for the current month
//|___streakListFilterGroup is the streakListFilter with the streaks grouped, see RULE1
//______________________________________________________________________________
//Only use the value of variables(except supply variables,
// to be supplied before calling the USER FUNCTIONs) after calling
// the USER FUNCTION CalculateStreaks() or CalculateStreakData()
//______________________________________________________________________________
//only USER FUNCTIONs are to be called from outside user
//REDUNDANT for redundant lines
//SUPPLY are to be supplied from outside user, otherwise has default values
//RULE: ... defines rule for the function below it
//HELPER FUNCTION are just abstractions for little tasks
//ACTIONS listed at the bottom if one has to use the class explicitly
//______________________________________________________________________________
class StreakService {
int longestStreak = 0; //USER VARIABLE
int lastStreak = 0; //USER VARIABLE
bool currentStreakIsLastStreak = false; //USER VARIABLE
List<DateTime> streakListFilter = []; //USER VARIABLE, probably not needed
List<Map<DateTime, int>> streakListFilterGroup = []; //USER VARIABLE
//______________________________________________________________________________
//SUPPLY
List<DateTime> streakList = []; //Whole streakList
//supplyDate
DateTime supplyDate = DateTime.now(); //will be supplied from DuoCalendar
//sort the streakList
void SortStreakList() {
streakList.sort((a, b) => a.compareTo(b));
}
//Now we filtre the streakList for the current month
void StreakListFilterFunction() {
streakListFilter = streakList.where((element) {
return element.month == supplyDate.month &&
element.year == supplyDate.year;
}).toList();
//Now we sort the streakListFilter
streakListFilter.sort((a, b) => a.compareTo(b));
}
//RULE1:
//Now we group the streakListFilter so that we can get the streaks
//we create a map for each element of the streakListFilter, then make a list of the maps
//{DateTime: int}
//0 for stand alone day
//1 for streak start day
//2 for streak end day
//3 for streak middle day
void StreakListFilterGroupFunction() {
int len = streakListFilter.length;
bool previousDayIsStreak = false;
bool nextDayIsStreak = false;
//for 1st element of the streakListFilter
if (len == 1) {
streakListFilterGroup.add({streakListFilter[0]: 0});
} else {
if (streakListFilter[0].day + 1 == streakListFilter[1].day) {
streakListFilterGroup.add({streakListFilter[0]: 1}); //streak start day
previousDayIsStreak = true;
} else {
streakListFilterGroup.add({streakListFilter[0]: 0}); //stand alone day
}
}
//for the middle elements of the streakListFilter
for (int i = 1; i < len - 1; i++) {
if (streakListFilter[i].day - 1 == streakListFilter[i - 1].day) {
previousDayIsStreak = true;
} else {
previousDayIsStreak = false;
}
if (streakListFilter[i].day + 1 == streakListFilter[i + 1].day) {
nextDayIsStreak = true;
} else {
nextDayIsStreak = false;
}
if (previousDayIsStreak && nextDayIsStreak) {
streakListFilterGroup.add({streakListFilter[i]: 3});
} else if (previousDayIsStreak && !nextDayIsStreak) {
streakListFilterGroup.add({streakListFilter[i]: 2});
} else if (!previousDayIsStreak && nextDayIsStreak) {
streakListFilterGroup.add({streakListFilter[i]: 1});
} else {
streakListFilterGroup.add({streakListFilter[i]: 0});
}
}
//for the last element of the streakListFilter
if (len > 1) {
if (streakListFilter[len - 1].day - 1 == streakListFilter[len - 2].day) {
streakListFilterGroup.add({streakListFilter[len - 1]: 2});
} else {
streakListFilterGroup.add({streakListFilter[len - 1]: 0});
}
}
//1st element
}
//Calculate longest streak
void CalculateLongestStreak() {
SortStreakList(); //REDUNDANT, since in USER FUNCTION CalculateStreaks() we already call SortStreakList()
//we calculate for whole streakList
// print('inside CalculateLongestStreak()');
// print('streakList: $streakList');
int len = streakList.length;
int _currentStreak = 1;
int _longestStreak = 0;
if (streakList.length > 1) {
for (int i = 0; i < len - 1; i++) {
if (streakList[i].day + 1 == streakList[i + 1].day) {
_currentStreak++;
// print('counting currentStreak: $_currentStreak');
if (_currentStreak > _longestStreak) {
_longestStreak = _currentStreak;
// print('New longestStreak: $_longestStreak');
}
} else {
_currentStreak = 0;
}
}
} else {
_longestStreak = 1;
}
longestStreak = _longestStreak;
}
//Calculate current streak
void CalculateLastStreak() {
SortStreakList(); //REDUNDANT, since in USER FUNCTION CalculateStreaks() we already call SortStreakList()
//we calculate for whole streakList
int len = streakList.length;
int _currentStreak = 1;
for (int i = len - 1; i > 0; i--) {
if (streakList[i].day - 1 == streakList[i - 1].day) {
_currentStreak++;
} else {
break;
}
}
lastStreak = _currentStreak;
}
//Calculate lastDayIsStreak
void CalculateCurrentStreakIsLastStreak() {
//we calculate for whole streakList
currentStreakIsLastStreak = false;
int len = streakList.length;
List<int> cmplist1 = [
DateTime.now().day,
DateTime.now().month,
DateTime.now().year
];
List<int> cmplist2 = [
streakList[len - 1].day,
streakList[len - 1].month,
streakList[len - 1].year
];
List<int> cmplist3 = [
DateTime.now().day - 1,
DateTime.now().month,
DateTime.now().year
];
if (listCompare(cmplist2, cmplist1) || listCompare(cmplist2, cmplist3)) {
currentStreakIsLastStreak = true;
// print('currentStreakIsLastStreak: $currentStreakIsLastStreak');
}
}
//HELPER FUNCTION
bool listCompare(List<int> l1, List<int> l2) {
int len1 = l1.length;
int len2 = l2.length;
if (len1 != len2) {
return false;
}
for (int i = 0; i < len1; i++) {
if (l1[i] != l2[i]) {
return false;
}
}
return true;
}
void clearDuplicates() {
streakListFilter = streakListFilter.toSet().toList();
}
//USER FUNCTION_______________________________________________________________
void CalculateStreaks() {
//See if streakList is empty
if (streakList.isEmpty) {
return;
}
clearDuplicates();
SortStreakList(); //MANDATORY STEP 1
StreakListFilterFunction();
//Now check if the streakListFilter is empty
if (streakListFilter.isEmpty) {
return;
}
StreakListFilterGroupFunction();
}
void CalculateStreakData() {
//See if streakList is empty
if (streakList.isEmpty) {
return;
}
SortStreakList(); //MANDATORY STEP 1
CalculateLongestStreak(); //DEPENDS ON MANDATORY STEP 1
CalculateLastStreak(); //DEPENDS ON MANDATORY STEP 1
CalculateCurrentStreakIsLastStreak();
//if longest streak < current streak, then longest streak = current streak
if (longestStreak < lastStreak) {
longestStreak = lastStreak;
}
}
}
//ACTIONS:
//take streakList SUPPLY
//take supplyDate SUPPLY
//run StreakListFilterFunction, takes streakList and supplyDate, stores the result in streakListFilter[]
//run StreakListFilterGroupFunction, takes streakListFilter[], stores the result in streakListFilterGroup[]
//use streakListFilterGroup[] to display the streaks in the calendar