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