-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #394 from thomassth/event-json
old args convertor
- Loading branch information
Showing
3 changed files
with
66 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,16 @@ class Event { | |
/// Indicates if this event counts as busy time, tentative, unavaiable or is still free time | ||
late Availability availability; | ||
|
||
///Note for development: | ||
/// | ||
///JSON field names are coded in dart, swift and kotlin to facilitate data exchange. | ||
///Make sure all locations are updated if changes needed to be made. | ||
///Swift: | ||
///`ios/Classes/SwiftDeviceCalendarPlugin.swift` | ||
///Kotlin: | ||
///`android/src/main/kotlin/com/builttoroam/devicecalendar/models/Event.kt` | ||
///`android/src/main/kotlin/com/builttoroam/devicecalendar/CalendarDelegate.kt` | ||
///`android/src/main/kotlin/com/builttoroam/devicecalendar/DeviceCalendarPlugin.kt` | ||
Event(this.calendarId, | ||
{this.eventId, | ||
this.title, | ||
|
@@ -60,33 +70,60 @@ class Event { | |
this.url, | ||
this.allDay = false}); | ||
|
||
///Get Event from JSON. | ||
/// | ||
///Sample JSON: | ||
///{calendarId: 00, eventId: 0000, eventTitle: Sample Event, eventDescription: This is a sample event, eventStartDate: 1563719400000, eventStartTimeZone: Asia/Hong_Kong, eventEndDate: 1640532600000, eventEndTimeZone: Asia/Hong_Kong, eventAllDay: false, eventLocation: Yuenlong Station, eventURL: null, availability: BUSY, attendees: [{name: commonfolk, emailAddress: [email protected], role: 1, isOrganizer: false, attendanceStatus: 3}], reminders: [{minutes: 39}]} | ||
Event.fromJson(Map<String, dynamic>? json) { | ||
if (json == null) { | ||
throw ArgumentError(ErrorMessages.fromJsonMapIsNull); | ||
} | ||
String? foundUrl; | ||
String? startLocationName; | ||
String? endLocationName; | ||
int? startTimestamp; | ||
int? endTimestamp; | ||
bool legacyJSON = false; | ||
var legacyName = { | ||
title: 'title', | ||
description: 'description', | ||
startTimestamp: 'start', | ||
endTimestamp: 'end', | ||
startLocationName: 'startTimeZone', | ||
endLocationName: 'endTimeZone', | ||
allDay: 'allDay', | ||
location: 'location', | ||
foundUrl: 'url', | ||
}; | ||
legacyName.forEach((key, value) { | ||
if (json[value] != null) { | ||
key = json[value]; | ||
legacyJSON = true; | ||
} | ||
}); | ||
|
||
eventId = json['eventId']; | ||
calendarId = json['calendarId']; | ||
title = json['eventTitle']; | ||
description = json['eventDescription']; | ||
|
||
final int? startTimestamp = json['eventStartDate']; | ||
final String? startLocationName = json['eventStartTimeZone']; | ||
startTimestamp = json['eventStartDate']; | ||
startLocationName = json['eventStartTimeZone']; | ||
var startTimeZone = timeZoneDatabase.locations[startLocationName]; | ||
startTimeZone ??= local; | ||
start = startTimestamp != null | ||
? TZDateTime.fromMillisecondsSinceEpoch(startTimeZone, startTimestamp) | ||
: TZDateTime.now(local); | ||
|
||
final int? endTimestamp = json['eventEndDate']; | ||
final String? endLocationName = json['eventEndTimeZone']; | ||
endTimestamp = json['eventEndDate']; | ||
endLocationName = json['eventEndTimeZone']; | ||
var endLocation = timeZoneDatabase.locations[endLocationName]; | ||
endLocation ??= startTimeZone; | ||
end = endTimestamp != null | ||
? TZDateTime.fromMillisecondsSinceEpoch(endLocation, endTimestamp) | ||
: TZDateTime.now(local); | ||
allDay = json['eventAllDay'] ?? false; | ||
if (Platform.isAndroid && (allDay ?? false)){ | ||
if (Platform.isAndroid && (allDay ?? false)) { | ||
// On Android, the datetime in an allDay event is adjusted to local | ||
// timezone, which can result in the wrong day, so we need to bring the | ||
// date back to midnight UTC to get the correct date | ||
|
@@ -102,7 +139,7 @@ class Event { | |
location = json['eventLocation']; | ||
availability = parseStringToAvailability(json['availability']); | ||
|
||
var foundUrl = json['eventURL']?.toString(); | ||
foundUrl = json['eventURL']?.toString(); | ||
if (foundUrl?.isEmpty ?? true) { | ||
url = null; | ||
} else { | ||
|
@@ -138,6 +175,10 @@ class Event { | |
return Reminder.fromJson(decodedReminder); | ||
}).toList(); | ||
} | ||
if (legacyJSON) { | ||
throw FormatException( | ||
'legacy JSON detected. Please update your current JSONs as they may not be supported later on.'); | ||
} | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
|