refactored alarm models
This commit is contained in:
+12
-7
@@ -1,18 +1,23 @@
|
||||
import 'fixed_time_alarm.dart';
|
||||
import 'location_alarm.dart';
|
||||
import 'time_alarm.dart';
|
||||
import 'relative_time_alarm.dart';
|
||||
|
||||
abstract class Alarm {
|
||||
String get id;
|
||||
String get taskId;
|
||||
AlarmType get alarmType;
|
||||
Map<String, dynamic> toJson();
|
||||
|
||||
factory Alarm.fromJson(Map<String, dynamic> json) {
|
||||
if (json.containsKey('triggerAt')) {
|
||||
return TimeAlarm.fromJson(json);
|
||||
}
|
||||
|
||||
if (json.containsKey('location')) {
|
||||
return LocationAlarm.fromJson(json);
|
||||
if (json.containsKey('alarmType')) {
|
||||
switch (json['alarmType'] as String) {
|
||||
case 'timeFixed':
|
||||
return FixedTimeAlarm.fromJson(json);
|
||||
case 'timeRelative':
|
||||
return RelativeTimeAlarm.fromJson(json);
|
||||
case 'location':
|
||||
return LocationAlarm.fromJson(json);
|
||||
}
|
||||
}
|
||||
|
||||
throw ArgumentError('Unknown alarm type');
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import '../alarm.dart';
|
||||
import 'create_alarm_request.dart';
|
||||
|
||||
class CreateFixedTimeAlarmRequest extends CreateAlarmRequest {
|
||||
final DateTime? triggerAt;
|
||||
|
||||
CreateFixedTimeAlarmRequest({this.triggerAt})
|
||||
: super(alarmType: AlarmType.timeFixed);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import '../alarm.dart' show AlarmType;
|
||||
import 'create_alarm_request.dart';
|
||||
|
||||
class CreateRelativeTimeAlarmRequest extends CreateAlarmRequest {
|
||||
final DateTime? triggerAt;
|
||||
|
||||
CreateRelativeTimeAlarmRequest({this.triggerAt})
|
||||
: super(alarmType: AlarmType.timeRelative);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import 'create_alarm_request.dart';
|
||||
|
||||
class CreateTimeAlarmRequest extends CreateAlarmRequest {
|
||||
final DateTime? triggerAt;
|
||||
|
||||
CreateTimeAlarmRequest({this.triggerAt, required super.alarmType});
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'alarm.dart';
|
||||
|
||||
class TimeAlarm implements Alarm {
|
||||
class FixedTimeAlarm implements Alarm {
|
||||
@override
|
||||
final String id;
|
||||
|
||||
@@ -9,14 +9,17 @@ class TimeAlarm implements Alarm {
|
||||
|
||||
final DateTime triggerAt;
|
||||
|
||||
const TimeAlarm({
|
||||
@override
|
||||
final AlarmType alarmType;
|
||||
|
||||
const FixedTimeAlarm({
|
||||
required this.id,
|
||||
required this.taskId,
|
||||
required this.triggerAt,
|
||||
});
|
||||
}) : alarmType = AlarmType.timeFixed;
|
||||
|
||||
factory TimeAlarm.fromJson(Map<String, dynamic> json) {
|
||||
return TimeAlarm(
|
||||
factory FixedTimeAlarm.fromJson(Map<String, dynamic> json) {
|
||||
return FixedTimeAlarm(
|
||||
id: json['id'] as String,
|
||||
taskId: json['taskId'] as String,
|
||||
triggerAt: DateTime.parse(json['triggerAt'] as String),
|
||||
@@ -29,6 +32,7 @@ class TimeAlarm implements Alarm {
|
||||
'id': id,
|
||||
'taskId': taskId,
|
||||
'triggerAt': triggerAt.toIso8601String(),
|
||||
'alarmType': alarmType,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,15 @@ class LocationAlarm implements Alarm {
|
||||
|
||||
final int radiusMeters;
|
||||
|
||||
@override
|
||||
final AlarmType alarmType;
|
||||
|
||||
const LocationAlarm({
|
||||
required this.id,
|
||||
required this.taskId,
|
||||
required this.location,
|
||||
required this.radiusMeters,
|
||||
});
|
||||
}) : alarmType = AlarmType.location;
|
||||
|
||||
factory LocationAlarm.fromJson(Map<String, dynamic> json) {
|
||||
return LocationAlarm(
|
||||
@@ -35,6 +38,7 @@ class LocationAlarm implements Alarm {
|
||||
'taskId': taskId,
|
||||
'location': location.toJson(),
|
||||
'radiusMeters': radiusMeters,
|
||||
'alarmType': alarmType,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'alarm.dart';
|
||||
|
||||
class RelativeTimeAlarm implements Alarm {
|
||||
@override
|
||||
final String id;
|
||||
|
||||
@override
|
||||
final String taskId;
|
||||
|
||||
final DateTime triggerAt;
|
||||
|
||||
@override
|
||||
final AlarmType alarmType;
|
||||
|
||||
const RelativeTimeAlarm({
|
||||
required this.id,
|
||||
required this.taskId,
|
||||
required this.triggerAt,
|
||||
}) : alarmType = AlarmType.timeRelative;
|
||||
|
||||
factory RelativeTimeAlarm.fromJson(Map<String, dynamic> json) {
|
||||
return RelativeTimeAlarm(
|
||||
id: json['id'] as String,
|
||||
taskId: json['taskId'] as String,
|
||||
triggerAt: DateTime.parse(json['triggerAt'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'taskId': taskId,
|
||||
'triggerAt': triggerAt.toIso8601String(),
|
||||
'alarmType': alarmType,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user