-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventide_extensions.dart
129 lines (115 loc) · 2.89 KB
/
eventide_extensions.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
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:eventide/src/calendar_api.g.dart';
import 'package:eventide/src/eventide_platform_interface.dart';
extension ColorToValue on Color {
int toValue() =>
_floatToInt8(a) << 24 |
_floatToInt8(r) << 16 |
_floatToInt8(g) << 8 |
_floatToInt8(b) << 0;
int _floatToInt8(double x) => (x * 255.0).round() & 0xff;
}
extension CalendarToETCalendar on Calendar {
ETCalendar toETCalendar() {
return ETCalendar(
id: id,
title: title,
color: Color(color),
isWritable: isWritable,
account: account.toETAccount(),
);
}
}
extension EventToETEvent on Event {
ETEvent toETEvent() {
return ETEvent(
id: id,
title: title,
isAllDay: isAllDay,
startDate: DateTime.fromMillisecondsSinceEpoch(startDate),
endDate: DateTime.fromMillisecondsSinceEpoch(endDate),
calendarId: calendarId,
description: description,
url: url,
reminders: [
if (reminders != null) ...reminders!.toDurationList(),
],
);
}
}
extension ETEventCopy on ETEvent {
ETEvent copyWithReminders(List<Duration>? reminders) {
return ETEvent(
id: id,
title: title,
isAllDay: isAllDay,
startDate: startDate,
endDate: endDate,
calendarId: calendarId,
description: description,
url: url,
reminders: reminders ?? this.reminders,
);
}
}
extension AccountToETAccount on Account {
ETAccount toETAccount() {
return ETAccount(
name: name,
type: name,
);
}
}
extension ETAccountToAccount on ETAccount {
Account toAccount() {
return Account(
name: name,
type: name,
);
}
}
extension CalendarListToETCalendar on List<Calendar> {
List<ETCalendar> toETCalendarList() {
return map((c) => c.toETCalendar()).toList();
}
}
extension EventListToETEvent on List<Event> {
List<ETEvent> toETEventList() {
return map((e) => e.toETEvent()).toList();
}
}
extension NativeToDuration on int {
Duration toDuration() {
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
return Duration(seconds: abs());
case TargetPlatform.android:
return Duration(minutes: abs());
default:
throw UnsupportedError('Unsupported platform');
}
}
}
extension DurationToNative on Duration {
int toNativeDuration() {
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
return inSeconds.abs();
case TargetPlatform.android:
return inMinutes.abs();
default:
throw UnsupportedError('Unsupported platform');
}
}
}
extension DurationListToNative on List<Duration> {
List<int> toNativeList() {
return map((d) => d.toNativeDuration()).toList();
}
}
extension NativeListToDuration on List<int> {
List<Duration> toDurationList() {
return map((i) => i.toDuration()).toList();
}
}