Skip to content

Commit

Permalink
fix bug when new schedule does not fully override old
Browse files Browse the repository at this point in the history
  • Loading branch information
smart7even committed Jul 20, 2023
1 parent e6333f1 commit d0f226f
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/feature/schedule/data/schedule_local_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,20 @@ class ScheduleLocalDataProvider implements IScheduleLocalDataProvider {
DateTime currentDateTime = DateTime.now();

return _database.transaction(() async {
for (final daySchedule in schedule.daySchedules) {
await _deleteDaySchedule(daySchedule);
final startOfWeekDateTime = getStartOfStudyWeek(
schedule.week,
currentDateTime,
);

final days = [
for (int i = 0; i < 7; i++) startOfWeekDateTime.add(Duration(days: i)),
];

for (final day in days) {
await _deleteDay(day);
}

for (final daySchedule in schedule.daySchedules) {
for (final lesson in daySchedule.lessons) {
// save to db
await _database.into(_database.lessons).insert(
Expand Down Expand Up @@ -125,4 +136,18 @@ class ScheduleLocalDataProvider implements IScheduleLocalDataProvider {

return;
}

Future<void> _deleteDay(DateTime day) async {
final deleteStamement = _database.delete(_database.lessons)
..where(
(tbl) =>
tbl.start.year.equals(day.year) &
tbl.start.month.equals(day.month) &
tbl.start.day.equals(day.day),
);

await deleteStamement.go();

return;
}
}

0 comments on commit d0f226f

Please sign in to comment.