Skip to content

Commit

Permalink
Optimize backward compatibility for v4 JSON structures
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Dec 19, 2024
1 parent e356201 commit b29e6a1
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/model/alarm_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class AlarmSettings {
/// This factory adds backward compatibility for v4 JSON structures
/// by detecting the absence of certain fields and adjusting them.
factory AlarmSettings.fromJson(Map<String, dynamic> json) {
// Handle backward compatibility for v4 JSON structures
if (!json.containsKey('volumeSettings')) {
try {
// Try parsing with the default (v5) parser
return _$AlarmSettingsFromJson(json);
} catch (e) {
// Fallback to v4 parsing logic

// Process volume settings for v4
final volume = (json['volume'] as num?)?.toDouble();
final fadeDurationSeconds = (json['fadeDuration'] as num?)?.toDouble();
Expand All @@ -47,19 +51,16 @@ class AlarmSettings {
};

// Default `allowAlarmOverlap` to false for v4
if (!json.containsKey('allowAlarmOverlap')) {
json['allowAlarmOverlap'] = false;
}
json['allowAlarmOverlap'] = json['allowAlarmOverlap'] ?? false;

// Adjust `dateTime` field for v4
if (json['dateTime'] != null) {
if (json['dateTime'] is int) {
final dateTimeValue = json['dateTime'] as int;
// In v4, dateTime was stored in microseconds,
// so convert to milliseconds
// In v4, dateTime was stored in microseconds, convert to milliseconds
json['dateTime'] = dateTimeValue ~/ 1000;
} else if (json['dateTime'] is String) {
// If `dateTime` is a string (ISO 8601 format), parse it
// Parse ISO 8601 date string
json['dateTime'] =
DateTime.parse(json['dateTime'] as String).millisecondsSinceEpoch;
} else {
Expand All @@ -68,9 +69,10 @@ class AlarmSettings {
} else {
throw ArgumentError('dateTime is missing in the JSON data');
}
}

return _$AlarmSettingsFromJson(json);
// Try parsing again with the adjusted JSON
return _$AlarmSettingsFromJson(json);
}
}

/// Converts from wire datatype.
Expand Down

0 comments on commit b29e6a1

Please sign in to comment.