You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
timemanager/lib/model/timer_entry.dart

107 lines
2.6 KiB

const String tableTimerEntry = 'timer_entry'; // 表名
// 数据库表结构
class TimerEntryFields {
static const String id = '_id';
static const String name = 'name';
static const String createdAt = 'createdAt';
static const String endAt = 'endAt';
static const String isActive = 'isActive';
static const String stopWatch = 'stopWatch';
static final List<String> values = [
/// Add all fields
id, name, createdAt, endAt, isActive, stopWatch
];
}
class TimerEntry {
int? id; // 计时器ID
String name; // 计时器名称
Stopwatch? stopwatch; // 计时器
bool isActice; // 计时器是否在计时
DateTime createdAt; // 计时器创建时间
DateTime? endAt; // 计时器结束时间
// 构造函数
TimerEntry({required this.name})
: stopwatch = Stopwatch(),
isActice = false,
createdAt = DateTime.now(),
endAt = DateTime.now();
bool get isActive => isActice;
num get stopWatch => stopWatch;
// 开始计时
void start() {
stopwatch!.start();
isActice = true;
}
// 暂停计时
void pause() {
stopwatch!.stop();
isActice = false;
}
// 停止计时
void stop() {
stopwatch!.stop();
isActice = false;
endAt = DateTime.now();
}
// 重置计时
void reset() {
stopwatch!.reset();
isActice = false;
endAt = null;
}
// 复制计时器
TimerEntry copy({
int? id,
String? name,
DateTime? createdAt,
DateTime? endAt,
bool? isActive,
Stopwatch? stopwatch,
}) =>
TimerEntry(
name: name ?? this.name,
)
..id = id ?? this.id
..createdAt = createdAt ?? this.createdAt
..endAt = endAt ?? this.endAt
..isActice = isActive ?? this.isActice
..stopwatch = stopwatch ?? this.stopwatch;
// fromJson
static TimerEntry fromJson(Map<String, Object?> json) {
return TimerEntry(
name: json[TimerEntryFields.name] as String,
)
..id = json[TimerEntryFields.id] as int?
..createdAt = DateTime.parse(json[TimerEntryFields.createdAt] as String)
..endAt = json[TimerEntryFields.endAt] == null
? null
: DateTime.parse(json[TimerEntryFields.endAt] as String)
..isActice = json[TimerEntryFields.isActive] == 1 ? true : false
..stopwatch = Stopwatch();
}
// toJson
Map<String, Object?> toJson() {
return {
TimerEntryFields.id: id,
TimerEntryFields.name: name,
TimerEntryFields.createdAt: createdAt.toIso8601String(),
TimerEntryFields.endAt: endAt?.toIso8601String(),
TimerEntryFields.isActive: isActice ? 1 : 0,
TimerEntryFields.stopWatch: stopwatch!.elapsed.inMilliseconds,
};
}
}