重构ClipboardRecordingModel以支持可选的时间戳,并更新相关测试以改进JSON解析和验证。

This commit is contained in:
2026-06-04 17:32:54 +08:00
parent dfbdbbdb66
commit 7c342c4477
4 changed files with 55 additions and 9 deletions

View File

@@ -19,8 +19,8 @@ class ClipboardRecordingModel {
factory ClipboardRecordingModel.fromJson(Map<String, dynamic> json) {
return ClipboardRecordingModel(
title: _readString(json, 'title'),
startTimestamp: _readInt(json, 'startTimestamp'),
endTimestamp: _readInt(json, 'endTimestamp'),
startTimestamp: _readOptionalInt(json, 'startTimestamp'),
endTimestamp: _readOptionalInt(json, 'endTimestamp'),
address: _readString(json, 'address'),
filename: _readOptionalString(json, 'filename'),
);
@@ -52,8 +52,9 @@ class ClipboardRecordingModel {
throw FormatException('Clipboard field "$key" must be a String.');
}
static int _readInt(Map<String, dynamic> json, String key) {
static int? _readOptionalInt(Map<String, dynamic> json, String key) {
final value = json[key];
if (value == null) return null;
if (value is int) return value;
throw FormatException('Clipboard field "$key" must be an int.');
}

View File

@@ -30,8 +30,6 @@ class RecordingViewModel extends StateNotifier<RecordingModel> {
RecordingModel(
clipboardRecordingModel: ClipboardRecordingModel(
title: '',
startTimestamp: 0,
endTimestamp: 0,
address: '',
),
),
@@ -40,8 +38,6 @@ class RecordingViewModel extends StateNotifier<RecordingModel> {
static final _defaultClipboard = ClipboardRecordingModel(
title: '',
startTimestamp: 0,
endTimestamp: 0,
address: '',
);