-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
RecurrenceRuleProxy.java
359 lines (330 loc) Β· 11.2 KB
/
RecurrenceRuleProxy.java
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Titanium SDK
* Copyright TiDev, Inc. 04/07/2022-Present. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.calendar;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Kroll.proxy
public class RecurrenceRuleProxy extends KrollProxy
{
private static final String TAG = "RecurrenceRule";
// Keys for daysOfTheWeek dictionary.
private final String dayOfWeekKey = "daysOfWeek";
private final String weekNumberKey = "week";
// Keys for end dictionary.
private final String until = "endDate";
private final String count = "occurrenceCount";
// Field with the Date of the event begin to provide parity with iOS.
// Some information exposed can't be get solely from the recurrence rule.
private Date eventBegin;
private String rRule;
//region values that can be set from a Creation Dictionary
private TiRecurrenceFrequencyType frequency;
private Integer interval = 1;
private int[] daysOfTheMonth = new int[] {};
private int[] daysOfTheYear = new int[] {};
private int[] monthsOfTheYear = new int[] {};
private int[] weeksOfTheYear = new int[] {};
private String calendarID;
private KrollDict endDictionary = new KrollDict();
private KrollDict[] daysOfTheWeek = new KrollDict[] {};
//endregion
// Map matching days of the week constants from Titanium docs with their String counterparts in RRULE column.
private static final Map<String, Integer> weekdaysMap;
static
{
weekdaysMap = new HashMap<>();
weekdaysMap.put("SU", 1);
weekdaysMap.put("MO", 2);
weekdaysMap.put("TU", 3);
weekdaysMap.put("WE", 4);
weekdaysMap.put("TH", 5);
weekdaysMap.put("FR", 6);
weekdaysMap.put("SA", 7);
}
// Constructor for creating a recurrence rule from a Kroll creation dictionary.
public RecurrenceRuleProxy(KrollDict creationDictionary)
{
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_ID)) {
this.calendarID = TiConvert.toString(creationDictionary.get(TiC.PROPERTY_CALENDAR_ID));
}
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_MONTH)) {
this.daysOfTheMonth = creationDictionary.getIntArray(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_MONTH);
}
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_WEEK)) {
this.daysOfTheWeek = creationDictionary.getKrollDictArray(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_WEEK);
}
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_YEAR)) {
this.daysOfTheYear = creationDictionary.getIntArray(TiC.PROPERTY_CALENDAR_DAYS_OF_THE_YEAR);
}
if (creationDictionary.containsKey(TiC.PROPERTY_END)) {
this.endDictionary = (KrollDict) creationDictionary.getKrollDict(TiC.PROPERTY_END);
}
if (creationDictionary.containsKey(TiC.PROPERTY_FREQUENCY)) {
this.frequency =
TiRecurrenceFrequencyType.fromTiIntId(TiConvert.toInt(creationDictionary.get(TiC.PROPERTY_FREQUENCY)));
}
if (this.frequency == null) {
this.frequency = TiRecurrenceFrequencyType.DAILY;
}
if (creationDictionary.containsKey(TiC.PROPERTY_INTERVAL)
&& TiConvert.toInt(creationDictionary.get(TiC.PROPERTY_INTERVAL)) > 0) {
this.interval = TiConvert.toInt(creationDictionary.get(TiC.PROPERTY_INTERVAL));
} else {
Log.e(TAG, "Interval must be greater than 0.\n");
TiApplication.terminateActivityStack();
}
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_MONTHS_OF_THE_YEAR)) {
this.monthsOfTheYear = creationDictionary.getIntArray(TiC.PROPERTY_CALENDAR_MONTHS_OF_THE_YEAR);
}
if (creationDictionary.containsKey(TiC.PROPERTY_CALENDAR_WEEKS_OF_THE_YEAR)) {
this.weeksOfTheYear = creationDictionary.getIntArray(TiC.PROPERTY_CALENDAR_WEEKS_OF_THE_YEAR);
}
}
public String generateRRULEString()
{
StringBuilder finalRRule = new StringBuilder();
// Handle frequency.
if (this.frequency != null) {
String frequencyPart = "FREQ=" + frequency.toRfcStringId();
finalRRule.append(frequencyPart);
finalRRule.append(";");
// Handle frequency specific rules in different context.
switch (this.frequency) {
case WEEKLY:
StringBuilder weeklyRecurrencesString = new StringBuilder("BYDAY=");
int commaIndex = 0;
for (KrollDict dict : this.daysOfTheWeek) {
weeklyRecurrencesString.append(dict.get(this.dayOfWeekKey).toString());
if (commaIndex < this.daysOfTheWeek.length) {
weeklyRecurrencesString.append(",");
}
}
finalRRule.append(weeklyRecurrencesString);
finalRRule.append(";");
break;
case MONTHLY:
StringBuilder monthlyReccurencesString = new StringBuilder();
// daysOfTheWeek dictionary is with highest priority.
if (this.daysOfTheWeek.length > 0) {
monthlyReccurencesString.append("BYDAY=");
int index = this.daysOfTheWeek[0].getInt(this.weekNumberKey);
monthlyReccurencesString.append(index);
// Potentially add week start (Sunday or Monday) different from the default one.
monthlyReccurencesString.append(RecurrenceRuleProxy.weekdaysMap.keySet().toArray()[index]);
} else {
monthlyReccurencesString.append("BYMONTHDAY=");
// Case in which we do not have items in daysOfTheWeek array.
monthlyReccurencesString.append(this.daysOfTheMonth[0]);
}
finalRRule.append(monthlyReccurencesString);
finalRRule.append(";");
break;
}
}
// Handle interval.
if (this.interval != null) {
finalRRule.append("INTERVAL=");
finalRRule.append(this.interval);
finalRRule.append(";");
}
// Handle end.
if (this.endDictionary.containsKey(this.until)) {
// Dictionary can contain only one of the keys,
// so what's left is a specific date end rule.
finalRRule.append("UNTIL=");
finalRRule.append(this.endDictionary.get(this.until));
finalRRule.append(";");
} else if (this.endDictionary.containsKey(this.count)) {
// End rule is with occurrence count.
finalRRule.append("COUNT=");
finalRRule.append(this.endDictionary.get(this.count));
finalRRule.append(";");
}
return finalRRule.toString();
}
// Constructor for getting a recurrence rule from the Calendar.
public RecurrenceRuleProxy(String nativeRRule, int calendarID, Date begin)
{
// Only create a proxy if we have a valid recurrence rule.
if (nativeRRule != null && !nativeRRule.equals("")) {
this.eventBegin = begin;
parseRecurrenceRule(nativeRRule, calendarID);
}
calculateEnd();
calculateInterval();
fillFrequencyFields();
}
//region Methods for converting native to Kroll values
private void fillFrequencyFields()
{
//reused variables in different cases
String days;
String byDay;
if (this.frequency != null) {
switch (this.frequency) {
case YEARLY:
Calendar cal = Calendar.getInstance();
cal.setTime(this.eventBegin);
//weeksOfTheYear
this.weeksOfTheYear = new int[] { cal.get(Calendar.WEEK_OF_YEAR) };
//monthsOfTheYear
this.monthsOfTheYear = new int[] { cal.get(Calendar.MONTH) };
days = matchExpression(".*(BYYEARDAY=[0-9]*).*", 10);
if (days != null) {
//daysOfTheYear
this.daysOfTheYear = new int[] { Integer.parseInt(days) };
}
break;
case MONTHLY:
//daysOfTheMonth
days = matchExpression(".*(BYMONTHDAY=(-)*[0-9]*).*", 11);
if (days != null) {
this.daysOfTheMonth = new int[] { Integer.parseInt(days) };
}
//daysOfTheWeek
byDay = matchExpression(".*(BYDAY=[,0-9A-Z]*).*", 6);
if (byDay != null) {
KrollDict daysOfTheWeekDictionary = new KrollDict();
daysOfTheWeekDictionary.put(
this.dayOfWeekKey,
RecurrenceRuleProxy.weekdaysMap.get(byDay.substring(byDay.length() - 2)));
daysOfTheWeekDictionary.put(this.weekNumberKey, byDay.substring(0, byDay.length() - 2));
this.daysOfTheWeek = new KrollDict[] { daysOfTheWeekDictionary };
}
break;
case WEEKLY:
//daysOfTheWeek
byDay = matchExpression(".*(BYDAY=[,0-9A-Z]*).*", 6);
if (byDay != null) {
// Split the days from result.
String[] daysArray = byDay.split(",");
this.daysOfTheWeek = new KrollDict[daysArray.length];
for (int i = 0; i < this.daysOfTheWeek.length; i++) {
KrollDict daysOfTheWeekDictionary = new KrollDict();
daysOfTheWeekDictionary.put(this.dayOfWeekKey, weekdaysMap.get(daysArray[i]));
// In the context of a weekly recurrence week number is irrelevant.
daysOfTheWeekDictionary.put(this.weekNumberKey, 0);
this.daysOfTheWeek[i] = daysOfTheWeekDictionary;
}
}
break;
}
}
}
private void calculateEnd()
{
// Check for until specific date condition.
String date = matchExpression(".*(UNTIL=[0-9A-Z]*).*", 6);
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMdd");
if (date != null) {
try {
this.endDictionary.put(this.until, sDateFormat.parse(date.substring(0, 8)));
} catch (ParseException e) {
e.printStackTrace();
}
}
// Check for repeat count.
String until = matchExpression(".*(COUNT=[0-9]*).*", 6);
if (until != null) {
this.endDictionary.put(this.count, until);
}
}
private void calculateInterval()
{
String interval = matchExpression(".*(INTERVAL=[0-9]*).*", 9);
if (interval != null) {
this.interval = Integer.valueOf(interval);
}
}
//endregion
//region kroll proxy methods
@Kroll.getProperty
public String getCalendarID()
{
return this.calendarID;
}
@Kroll.getProperty
public int[] getDaysOfTheMonth()
{
return this.daysOfTheMonth;
}
@Kroll.getProperty
public KrollDict[] getDaysOfTheWeek()
{
return this.daysOfTheWeek;
}
@Kroll.getProperty
public int[] getDaysOfTheYear()
{
return this.daysOfTheYear;
}
@Kroll.getProperty
public KrollDict getEnd()
{
return this.endDictionary;
}
@Kroll.getProperty
public int getFrequency()
{
return this.frequency.toTiIntId();
}
@Kroll.getProperty
public int getInterval()
{
return this.interval;
}
@Kroll.getProperty
public int[] monthsOfTheYear()
{
return this.monthsOfTheYear;
}
@Kroll.getProperty
public int[] getWeeksOfTheYear()
{
return this.weeksOfTheYear;
}
//endregion
private void parseRecurrenceRule(String rrule, int calendarID)
{
if (rrule == null) {
return;
}
// Set the recurrence rule for future use.
this.rRule = rrule;
// Set the ID of the calendar.
this.calendarID = String.valueOf(calendarID);
// Set the frequency in constructor, because it is required for other properties.
String frequency = matchExpression(".*(FREQ=[A-Z]*).*", 5);
if (frequency == null) {
this.frequency = TiRecurrenceFrequencyType.DAILY;
} else {
this.frequency = TiRecurrenceFrequencyType.fromRfcStringId(frequency);
}
}
private String matchExpression(String regEx, int length)
{
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(this.rRule);
if (matcher.matches()) {
return matcher.group(1).substring(length);
}
return null;
}
}