Skip to content

Commit

Permalink
Merge pull request #59 from vicolo-dev/fix-weekly-alarm-schedule
Browse files Browse the repository at this point in the history
Fix weekly alarm schedule having inconsistent id
  • Loading branch information
AhsanSarwar45 authored Dec 18, 2023
2 parents 32b3af6 + d4b9499 commit ba5585c
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 161 deletions.
20 changes: 20 additions & 0 deletions lib/alarm/logic/alarm_controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ void triggerScheduledNotification(int scheduleId, Json params) async {
print("Alarm triggered: $scheduleId");
}
// print("Alarm Trigger Isolate: ${Service.getIsolateID(Isolate.current)}");
if (params == null) {
if (kDebugMode) {
print("Params was null when triggering alarm");
}
return;
}

if (params['type'] == null) {
if (kDebugMode) {
print("Params Type was null when triggering alarm");
}
return;
}

ScheduledNotificationType notificationType =
ScheduledNotificationType.values.byName(params['type']);
Expand Down Expand Up @@ -72,6 +85,13 @@ void stopScheduledNotification(List<dynamic> message) {
}

void triggerAlarm(int scheduleId, Json params) async {
if (params == null) {
if (kDebugMode) {
print("Params was null when triggering alarm");
}
return;
}

await updateAlarms();

GetStorage().write("fullScreenNotificationRecentlyShown", true);
Expand Down
8 changes: 7 additions & 1 deletion lib/alarm/screens/alarm_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:clock_app/alarm/widgets/alarm_time_picker.dart';
import 'package:clock_app/common/logic/customize_screen.dart';
import 'package:clock_app/common/types/picker_result.dart';
import 'package:clock_app/common/types/time.dart';
import 'package:clock_app/common/utils/json_serialize.dart';
import 'package:clock_app/common/widgets/fab.dart';
import 'package:clock_app/common/widgets/list/customize_list_item_screen.dart';
import 'package:clock_app/common/widgets/list/persistent_list_view.dart';
Expand Down Expand Up @@ -113,9 +114,14 @@ class _AlarmScreenState extends State<AlarmScreen> {

_handleCustomizeAlarm(Alarm alarm) async {
int index = _listController.getItemIndex(alarm);
// if (index < 0) return;
await _openCustomizeAlarmScreen(alarm, onSave: (newAlarm) {
newAlarm.update();
_listController.changeItems((alarms) => alarms[index] = newAlarm);
_listController.changeItems((alarms) {
print(
"alasasasrm ${alarms.map((alarms) => alarm.id).toList()}, ${alarm.id}");
alarms[index] = newAlarm;
});
_showNextScheduleSnackBar(newAlarm);
});
}
Expand Down
39 changes: 22 additions & 17 deletions lib/alarm/types/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ List<AlarmSchedule> createSchedules(SettingGroup settings) {
}

class Alarm extends CustomizableListItem {
Time _time;
late Time _time;
bool _isEnabled = true;
bool _isFinished = false;
DateTime? _snoozeTime;
Expand Down Expand Up @@ -271,22 +271,27 @@ class Alarm extends CustomizableListItem {
return (getSetting("Interval") as SelectSetting<RangeInterval>).value;
}

Alarm.fromJson(Json json)
: _time = Time.fromJson(json['timeOfDay']),
_isEnabled = json['enabled'],
_isFinished = json['finished'],
_snoozeTime = json['snoozeTime'] != 0
? DateTime.fromMillisecondsSinceEpoch(json['snoozeTime'])
: null,
_snoozeCount = json['snoozeCount'],
_settings = SettingGroup(
"Alarm Settings",
appSettings
.getGroup("Alarm")
.getGroup("Default Settings")
.copy()
.settingItems,
) {
Alarm.fromJson(Json json) {
if (json == null) {
_time = Time.now();
_schedules = createSchedules(_settings);
return;
}
_time = Time.fromJson(json['timeOfDay']);
_isEnabled = json['enabled'];
_isFinished = json['finished'];
_snoozeTime = json['snoozeTime'] != 0
? DateTime.fromMillisecondsSinceEpoch(json['snoozeTime'])
: null;
_snoozeCount = json['snoozeCount'];
_settings = SettingGroup(
"Alarm Settings",
appSettings
.getGroup("Alarm")
.getGroup("Default Settings")
.copy()
.settingItems,
);
_settings.loadValueFromJson(json['settings']);
_schedules = [
OnceAlarmSchedule.fromJson(json['schedules'][0]),
Expand Down
13 changes: 9 additions & 4 deletions lib/alarm/types/alarm_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ class AlarmRunner extends JsonSerializable {
cancelAlarm(_id);
}

AlarmRunner.fromJson(Json json) : _id = json['id'] {
int millisecondsSinceEpoch = json['currentScheduleDateTime'];

AlarmRunner.fromJson(Json? json) {
if (json == null) {
_id = UniqueKey().hashCode;
return;
}
_id = json['id'] ?? UniqueKey().hashCode;
int millisecondsSinceEpoch = json['currentScheduleDateTime'] ?? 0;
_currentScheduleDateTime = millisecondsSinceEpoch == 0
? null
: DateTime.fromMillisecondsSinceEpoch(json['currentScheduleDateTime']);
: DateTime.fromMillisecondsSinceEpoch(
json['currentScheduleDateTime'] ?? 0);
}

@override
Expand Down
14 changes: 10 additions & 4 deletions lib/alarm/types/alarm_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class AlarmTaskSchema extends JsonSerializable {
return _builder(onSolve, settings);
}

void loadFromJson(Json json) {
void loadFromJson(Json? json) {
if (json == null) return;
settings.loadValueFromJson(json['settings']);
}

Expand All @@ -48,7 +49,7 @@ class AlarmTaskSchema extends JsonSerializable {
}

class AlarmTask extends CustomizableListItem {
final AlarmTaskType type;
late final AlarmTaskType type;
late final AlarmTaskSchema _schema;

AlarmTask(this.type) : _schema = alarmTaskSchemasMap[type]!.copy();
Expand All @@ -57,8 +58,13 @@ class AlarmTask extends CustomizableListItem {
: type = task.type,
_schema = task._schema.copy();

AlarmTask.fromJson(Json json)
: type = AlarmTaskType.values.byName(json['type']) {
AlarmTask.fromJson(Json json) {
if (json == null) {
type = AlarmTaskType.math;
_schema = alarmTaskSchemasMap[type]!.copy();
return;
}
type = AlarmTaskType.values.byName(json['type']);
_schema = alarmTaskSchemasMap[type]!.copy();
_schema.loadFromJson(json['schema']);
}
Expand Down
12 changes: 8 additions & 4 deletions lib/alarm/types/schedules/daily_alarm_schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/time.dart';

class DailyAlarmSchedule extends AlarmSchedule {
final AlarmRunner _alarmRunner;
late final AlarmRunner _alarmRunner;

@override
DateTime? get currentScheduleDateTime => _alarmRunner.currentScheduleDateTime;
Expand Down Expand Up @@ -39,9 +39,13 @@ class DailyAlarmSchedule extends AlarmSchedule {
'alarmRunner': _alarmRunner.toJson(),
};

DailyAlarmSchedule.fromJson(Json json)
: _alarmRunner = AlarmRunner.fromJson(json['alarmRunner']),
super();
DailyAlarmSchedule.fromJson(Json json) {
if (json == null) {
_alarmRunner = AlarmRunner();
return;
}
_alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
}

@override
bool hasId(int id) {
Expand Down
36 changes: 23 additions & 13 deletions lib/alarm/types/schedules/dates_alarm_schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import 'package:clock_app/common/types/time.dart';
import 'package:clock_app/settings/types/setting.dart';

class DateSchedule extends JsonSerializable {
DateTime date;
AlarmRunner alarmRunner;
late DateTime date;
late AlarmRunner alarmRunner;

DateSchedule(this.date) : alarmRunner = AlarmRunner();

DateSchedule.fromJson(Json json)
: date = DateTime.parse(json['date']),
alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
DateSchedule.fromJson(Json json) {
if (json == null) {
date = DateTime.now();
return;
}
date = json['date'] != null ? DateTime.parse(json['date']) : DateTime.now();
alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
}

@override
Json toJson() => {
Expand All @@ -23,9 +28,9 @@ class DateSchedule extends JsonSerializable {

class DatesAlarmSchedule extends AlarmSchedule {
// List<WeekdaySchedule> _weekdaySchedules = [];
final DateTimeSetting _datesSetting;
final AlarmRunner _alarmRunner;
bool _isFinished;
late final DateTimeSetting _datesSetting;
late final AlarmRunner _alarmRunner;
late bool _isFinished;

@override
bool get isDisabled => false;
Expand Down Expand Up @@ -86,11 +91,16 @@ class DatesAlarmSchedule extends AlarmSchedule {
'isFinished': _isFinished,
};

DatesAlarmSchedule.fromJson(Json json, Setting datesSetting)
: _alarmRunner = AlarmRunner.fromJson(json['alarmRunner']),
_datesSetting = datesSetting as DateTimeSetting,
_isFinished = json['isFinished'],
super();
DatesAlarmSchedule.fromJson(Json json, Setting datesSetting) : super() {
_datesSetting = datesSetting as DateTimeSetting;
if (json == null) {
_alarmRunner = AlarmRunner();
_isFinished = false;
return;
}
_alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
_isFinished = json['isFinished'] ?? false;
}

@override
bool hasId(int id) {
Expand Down
12 changes: 8 additions & 4 deletions lib/alarm/types/schedules/once_alarm_schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ class OnceAlarmSchedule extends AlarmSchedule {
'isDisabled': _isDisabled,
};

OnceAlarmSchedule.fromJson(Json json)
: _alarmRunner = AlarmRunner.fromJson(json['alarmRunner']),
_isDisabled = json['isDisabled'],
super();
OnceAlarmSchedule.fromJson(Json json) {
if (json == null) {
_alarmRunner = AlarmRunner();
return;
}
_alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
_isDisabled = json['isDisabled'] ?? false;
}

@override
bool hasId(int id) {
Expand Down
18 changes: 11 additions & 7 deletions lib/alarm/types/schedules/range_alarm_schedule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:clock_app/settings/types/setting.dart';

class RangeAlarmSchedule extends AlarmSchedule {
late final AlarmRunner _alarmRunner;
final DateTimeSetting _datesRangeSetting;
final SelectSetting<RangeInterval> _intervalSetting;
late final DateTimeSetting _datesRangeSetting;
late final SelectSetting<RangeInterval> _intervalSetting;
bool _isFinished = false;

RangeInterval get interval => _intervalSetting.value;
Expand Down Expand Up @@ -66,11 +66,15 @@ class RangeAlarmSchedule extends AlarmSchedule {
};

RangeAlarmSchedule.fromJson(
Json json, Setting datesRangeSetting, Setting intervalSetting)
: _alarmRunner = AlarmRunner.fromJson(json['alarmRunner']),
_datesRangeSetting = datesRangeSetting as DateTimeSetting,
_intervalSetting = intervalSetting as SelectSetting<RangeInterval>,
super();
Json json, Setting datesRangeSetting, Setting intervalSetting) {
_datesRangeSetting = datesRangeSetting as DateTimeSetting;
_intervalSetting = intervalSetting as SelectSetting<RangeInterval>;
if (json == null) {
_alarmRunner = AlarmRunner();
return;
}
_alarmRunner = AlarmRunner.fromJson(json['alarmRunner']);
}

@override
bool hasId(int id) {
Expand Down
Loading

0 comments on commit ba5585c

Please sign in to comment.