From 94051ae461d2a8be788144a27f5156c7f0ccabb2 Mon Sep 17 00:00:00 2001 From: Gautier de Lataillade <32983806+gdelataillade@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:34:29 +0100 Subject: [PATCH] Move int overflow check in validation method --- example/lib/screens/edit_alarm.dart | 17 ++++++----------- lib/alarm.dart | 10 ++++++++++ lib/model/alarm_settings.dart | 4 +--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/example/lib/screens/edit_alarm.dart b/example/lib/screens/edit_alarm.dart index 9b81bb6c..47daf8e0 100644 --- a/example/lib/screens/edit_alarm.dart +++ b/example/lib/screens/edit_alarm.dart @@ -69,11 +69,12 @@ class _ExampleAlarmEditScreenState extends State { 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)); } @@ -82,16 +83,10 @@ class _ExampleAlarmEditScreenState extends State { } 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, diff --git a/lib/alarm.dart b/lib/alarm.dart index 44a65342..657e1f47 100644 --- a/lib/alarm.dart +++ b/lib/alarm.dart @@ -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}', diff --git a/lib/model/alarm_settings.dart b/lib/model/alarm_settings.dart index 4558d3d3..5673ee3d 100644 --- a/lib/model/alarm_settings.dart +++ b/lib/model/alarm_settings.dart @@ -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 json) => AlarmSettings(