103 lines
3.0 KiB
Dart
103 lines
3.0 KiB
Dart
/// 闹钟实体(家属端对接 client 接口,与设备端 Room AlarmInfo 字段对齐)。
|
|
///
|
|
/// 重复类型与设备端一致:0-只响一次 1-每天 2-工作日 3-自定义。
|
|
class AlarmModel {
|
|
const AlarmModel({
|
|
this.id,
|
|
this.hour = 7,
|
|
this.minute = 0,
|
|
this.label = '',
|
|
this.repeatType = 1,
|
|
this.customDays = '',
|
|
this.isEnabled = true,
|
|
this.vibration = false,
|
|
this.volume = 80,
|
|
this.ringtoneUri,
|
|
this.snoozeInterval = 5,
|
|
});
|
|
|
|
/// 闹钟ID(新增时为 null)。
|
|
final int? id;
|
|
final int hour;
|
|
final int minute;
|
|
final String label;
|
|
|
|
/// 重复类型:0-只响一次 1-每天 2-工作日 3-自定义。
|
|
final int repeatType;
|
|
|
|
/// 自定义星期("2,3,6,7",与设备端 Calendar.MONDAY=2 对齐)。
|
|
final String customDays;
|
|
final bool isEnabled;
|
|
final bool vibration;
|
|
final int volume;
|
|
final String? ringtoneUri;
|
|
final int snoozeInterval;
|
|
|
|
/// 显示用时间字符串(HH:mm)。
|
|
String get timeText =>
|
|
'${hour.toString().padLeft(2, '0')}:${minute.toString().padLeft(2, '0')}';
|
|
|
|
factory AlarmModel.fromJson(Map<String, dynamic> json) => AlarmModel(
|
|
id: _toInt(json['id']),
|
|
hour: _toInt(json['hour']) ?? 0,
|
|
minute: _toInt(json['minute']) ?? 0,
|
|
label: (json['label'] as String?) ?? '',
|
|
repeatType: _toInt(json['repeatType']) ?? 1,
|
|
customDays: (json['customDays'] as String?) ?? '',
|
|
isEnabled: _toInt(json['isEnabled']) == 1,
|
|
vibration: _toInt(json['vibration']) == 1,
|
|
volume: _toInt(json['volume']) ?? 80,
|
|
ringtoneUri: json['ringtoneUri'] as String?,
|
|
snoozeInterval: _toInt(json['snoozeInterval']) ?? 5,
|
|
);
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'id': id,
|
|
'hour': hour,
|
|
'minute': minute,
|
|
'label': label,
|
|
'repeatType': repeatType,
|
|
'customDays': customDays,
|
|
'isEnabled': isEnabled ? 1 : 0,
|
|
'vibration': vibration ? 1 : 0,
|
|
'volume': volume,
|
|
'ringtoneUri': ringtoneUri,
|
|
'snoozeInterval': snoozeInterval,
|
|
};
|
|
|
|
AlarmModel copyWith({
|
|
int? id,
|
|
int? hour,
|
|
int? minute,
|
|
String? label,
|
|
int? repeatType,
|
|
String? customDays,
|
|
bool? isEnabled,
|
|
bool? vibration,
|
|
int? volume,
|
|
String? ringtoneUri,
|
|
int? snoozeInterval,
|
|
}) =>
|
|
AlarmModel(
|
|
id: id ?? this.id,
|
|
hour: hour ?? this.hour,
|
|
minute: minute ?? this.minute,
|
|
label: label ?? this.label,
|
|
repeatType: repeatType ?? this.repeatType,
|
|
customDays: customDays ?? this.customDays,
|
|
isEnabled: isEnabled ?? this.isEnabled,
|
|
vibration: vibration ?? this.vibration,
|
|
volume: volume ?? this.volume,
|
|
ringtoneUri: ringtoneUri ?? this.ringtoneUri,
|
|
snoozeInterval: snoozeInterval ?? this.snoozeInterval,
|
|
);
|
|
}
|
|
|
|
int? _toInt(dynamic value) {
|
|
if (value == null) return null;
|
|
if (value is int) return value;
|
|
if (value is num) return value.toInt();
|
|
if (value is String) return int.tryParse(value);
|
|
return null;
|
|
}
|