Skip to content

Commit

Permalink
Move int overflow check in validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Feb 2, 2024
1 parent b4c3e7c commit 94051ae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
17 changes: 6 additions & 11 deletions example/lib/screens/edit_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
setState(() {
final DateTime now = DateTime.now();
selectedDateTime = now.copyWith(
hour: res.hour,
minute: res.minute,
second: 0,
millisecond: 0,
microsecond: 0);
hour: res.hour,
minute: res.minute,
second: 0,
millisecond: 0,
microsecond: 0,
);
if (selectedDateTime.isBefore(now)) {
selectedDateTime = selectedDateTime.add(const Duration(days: 1));
}
Expand All @@ -82,16 +83,10 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
}

AlarmSettings buildAlarmSettings() {
//[DO] id must be less than Android's max int value
final id = creating
? DateTime.now().millisecondsSinceEpoch % 10000
: widget.alarmSettings!.id;

// [DON'T] id must be less than Android's max int value
// final id = creating
// ? DateTime.now().millisecondsSinceEpoch
// : widget.alarmSettings!.id;

final alarmSettings = AlarmSettings(
id: id,
dateTime: selectedDateTime,
Expand Down
10 changes: 10 additions & 0 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ class Alarm {
'Alarm id cannot be 0 or -1. Provided: ${alarmSettings.id}',
);
}
if (alarmSettings.id > 2147483647) {
throw AlarmException(
'Alarm id cannot be set larger than Int max value (2147483647). Provided: ${alarmSettings.id}',
);
}
if (alarmSettings.id < 2147483648) {
throw AlarmException(
'Alarm id cannot be set smaller than Int min value (-2147483648). Provided: ${alarmSettings.id}',
);
}
if (!alarmSettings.assetAudioPath.contains('.')) {
throw AlarmException(
'Provided audio path is not valid: ${alarmSettings.assetAudioPath}',
Expand Down
4 changes: 1 addition & 3 deletions lib/model/alarm_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ class AlarmSettings {
required this.notificationBody,
this.enableNotificationOnKill = true,
this.androidFullScreenIntent = true,
}) : assert(id != 0 && id != -1, "id cannot be set to 0 or -1"),
assert(id <= 2147483647,
"id cannot be set larger then Android Int.MAX_VALUE");
});

/// Constructs an `AlarmSettings` instance from the given JSON data.
factory AlarmSettings.fromJson(Map<String, dynamic> json) => AlarmSettings(
Expand Down

0 comments on commit 94051ae

Please sign in to comment.