-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventide.dart
249 lines (234 loc) · 8.43 KB
/
eventide.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
import 'package:eventide/src/eventide_extensions.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:eventide/src/eventide_exception.dart';
import 'package:eventide/src/eventide_platform_interface.dart';
import 'package:eventide/src/calendar_api.g.dart';
class Eventide extends EventidePlatform {
final CalendarApi _calendarApi;
Eventide({
@visibleForTesting CalendarApi? calendarApi,
}) : _calendarApi = calendarApi ?? CalendarApi();
/// Requests permission to access the calendar.
///
/// This method call is only necessary if you want to ask for permission early at runtime,
/// as the plugin will automatically request permission when needed.
///
/// Returns `true` if permission is granted, `false` otherwise.
@override
Future<bool> requestCalendarPermission() async {
try {
return await _calendarApi.requestCalendarPermission();
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Creates a new calendar with the given [title], [color] and [accountName].
///
/// Note that [accountName] is an Android feature. It will be ignored on iOS.
///
/// Returns the created [ETCalendar].
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] on iOS if not one callendar source has been found or if the created calendar id is not found.
///
/// Throws a [ETGenericException] on iOS if the color hex cannot be converted to a UIColor.
///
/// Throws a [ETGenericException] if any other error occurs during calendar creation.
@override
Future<ETCalendar> createCalendar({
required String title,
required Color color,
ETAccount? account,
}) async {
try {
final calendar = await _calendarApi.createCalendar(
title: title,
color: color.toValue(),
account: account?.toAccount(),
);
return calendar.toETCalendar();
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Retrieves a list of calendars.
/// If [onlyWritableCalendars] is `true`, only writable calendars are returned.
///
/// Returns a list of [ETCalendar]s.
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETGenericException] if any other error occurs during calendars retrieval.
@override
Future<List<ETCalendar>> retrieveCalendars({
bool onlyWritableCalendars = true,
ETAccount? fromAccount,
}) async {
try {
final calendars = await _calendarApi.retrieveCalendars(
onlyWritableCalendars: onlyWritableCalendars,
from: fromAccount?.toAccount(),
);
return calendars.toETCalendarList();
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Deletes the calendar with the given [calendarId].
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the calendar with the given [calendarId] is not found.
///
/// Throws a [ETNotEditableException] if the calendar is not editable.
///
/// Throws a [ETGenericException] if any other error occurs during calendar deletion.
@override
Future<void> deleteCalendar({
required String calendarId,
}) async {
try {
await _calendarApi.deleteCalendar(calendarId: calendarId);
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Creates a new event with the given [title], [startDate], [endDate], and [calendarId].
/// Optionally, you can provide a [description], [url], and a list of [reminders] duration.
///
/// /!\ Note that a [Duration] in seconds will not be supported by Android for API limitations.
///
/// Returns the created [Event].
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the calendar with the given [calendarId] is not found or if the created event id is not found.
///
/// Throws a [ETGenericException] if any other error occurs during event creation.
@override
Future<ETEvent> createEvent({
required String calendarId,
required String title,
required DateTime startDate,
required DateTime endDate,
bool isAllDay = false,
String? description,
String? url,
List<Duration>? reminders,
}) async {
try {
final event = await _calendarApi.createEvent(
title: title,
startDate: startDate.millisecondsSinceEpoch,
endDate: endDate.millisecondsSinceEpoch,
isAllDay: isAllDay,
calendarId: calendarId,
description: description,
url: url,
);
if (reminders != null) {
for (final reminder in reminders) {
await _calendarApi.createReminder(
reminder: reminder.toNativeDuration(),
eventId: event.id,
);
}
}
return event.toETEvent().copyWithReminders(reminders);
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Retrieves a list of events from the calendar with the given [calendarId].
/// Optionally, you can provide a [startDate] and [endDate] to filter the events.
///
/// Returns a list of [Event]s.
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the calendar with the given [calendarId] is not found.
///
/// Throws a [ETGenericException] if any other error occurs during events retrieval.
@override
Future<List<ETEvent>> retrieveEvents({
required String calendarId,
DateTime? startDate,
DateTime? endDate,
}) async {
try {
final start = startDate ?? DateTime.now();
final end = endDate ?? DateTime.now().add(const Duration(days: 7));
final events = await _calendarApi.retrieveEvents(
calendarId: calendarId,
startDate: start.millisecondsSinceEpoch,
endDate: end.microsecondsSinceEpoch,
);
return events.toETEventList();
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Deletes the event with the given [eventId] from the calendar with the given [calendarId].
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the event with the given [eventId] is not found.
///
/// Throws a [ETNotEditableException] if the calendar is not editable.
///
/// Throws a [ETGenericException] if any other error occurs during event deletion.
@override
Future<void> deleteEvent({
required String eventId,
}) async {
try {
await _calendarApi.deleteEvent(eventId: eventId);
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Creates a new reminder with the given [durationBeforeEvent] for the event with the given [eventId].
///
/// /!\ Note that a [Duration] in seconds will not be supported by Android for API limitations.
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the event with the given [eventId] is not found.
///
/// Throws a [ETGenericException] if any other error occurs during reminder creation.
@override
Future<ETEvent> createReminder({
required Duration durationBeforeEvent,
required String eventId,
}) async {
try {
final updatedEvent = await _calendarApi.createReminder(
reminder: durationBeforeEvent.toNativeDuration(), eventId: eventId);
return updatedEvent.toETEvent();
} on PlatformException catch (e) {
throw e.toETException();
}
}
/// Deletes the reminder with the given [durationBeforeEvent] for the event with the given [eventId].
///
/// Throws a [ETPermissionException] if the user refuses to grant calendar permissions.
///
/// Throws a [ETNotFoundException] if the event with the given [eventId] is not found.
///
/// Throws a [ETGenericException] if any other error occurs during reminder deletion.
@override
Future<ETEvent> deleteReminder({
required Duration durationBeforeEvent,
required String eventId,
}) async {
try {
final updatedEvent = await _calendarApi.deleteReminder(
reminder: durationBeforeEvent.toNativeDuration(), eventId: eventId);
return updatedEvent.toETEvent();
} on PlatformException catch (e) {
throw e.toETException();
}
}
}