forked from SatoshiPortal/bullbitcoin-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_health_reminder_model.dart
More file actions
69 lines (59 loc) · 2.35 KB
/
Copy pathbackup_health_reminder_model.dart
File metadata and controls
69 lines (59 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'package:bb_mobile/features/backup_settings/domain/backup_health_reminder.dart';
class BackupHealthReminderModel {
final int version;
final int? lastAcknowledgedAtMillis;
final bool crossedTenMillionSats;
const BackupHealthReminderModel({
required this.version,
required this.lastAcknowledgedAtMillis,
required this.crossedTenMillionSats,
});
/// Reads only the keys this record still has. Keys written by an earlier
/// shape of the feature are ignored rather than rejected.
factory BackupHealthReminderModel.fromJson(Map<String, dynamic> json) =>
BackupHealthReminderModel(
version: _requiredInt(json, 'version'),
lastAcknowledgedAtMillis: _optionalInt(json, 'lastAcknowledgedAt'),
crossedTenMillionSats: _optionalBool(json, 'crossedTenMillionSats'),
);
factory BackupHealthReminderModel.fromEntity(
BackupHealthReminderRecord record, {
required int version,
}) => BackupHealthReminderModel(
version: version,
lastAcknowledgedAtMillis: record.lastAcknowledgedAt
?.toUtc()
.millisecondsSinceEpoch,
crossedTenMillionSats: record.crossedTenMillionSats,
);
Map<String, dynamic> toJson() => {
'version': version,
'lastAcknowledgedAt': lastAcknowledgedAtMillis,
'crossedTenMillionSats': crossedTenMillionSats,
};
BackupHealthReminderRecord toEntity({required String masterFingerprint}) =>
BackupHealthReminderRecord(
masterFingerprint: masterFingerprint,
lastAcknowledgedAt: _dateTimeFromMillis(lastAcknowledgedAtMillis),
crossedTenMillionSats: crossedTenMillionSats,
);
DateTime? _dateTimeFromMillis(int? milliseconds) => milliseconds == null
? null
: DateTime.fromMillisecondsSinceEpoch(milliseconds, isUtc: true);
static int _requiredInt(Map<String, dynamic> json, String key) {
final value = json[key];
if (value is int) return value;
throw FormatException('Invalid $key');
}
static int? _optionalInt(Map<String, dynamic> json, String key) {
final value = json[key];
if (value == null || value is int) return value as int?;
throw FormatException('Invalid $key');
}
static bool _optionalBool(Map<String, dynamic> json, String key) {
final value = json[key];
if (value == null) return false;
if (value is bool) return value;
throw FormatException('Invalid $key');
}
}