Skip to content

Commit

Permalink
fix(managers): properly assign default values whether empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Dec 6, 2021
1 parent cb12f9d commit a0ddc56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
6 changes: 4 additions & 2 deletions lib/src/model/cabin/cabin_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ class CabinManager extends WritableManager<Set<Cabin>> with ChangeNotifier {
if (notify) notifyListeners();
}

Set<Cabin> get _defaultCabins => SplayTreeSet();

@override
Future<Set<Cabin>> readFromFile() async {
try {
Expand All @@ -365,9 +367,9 @@ class CabinManager extends WritableManager<Set<Cabin>> with ChangeNotifier {

final cabins = _parseCabins(content);

return SplayTreeSet.from(cabins);
return cabins.isEmpty ? _defaultCabins : SplayTreeSet.from(cabins);
} on Exception {
return SplayTreeSet();
return _defaultCabins;
}
}

Expand Down
25 changes: 1 addition & 24 deletions lib/src/model/date/day_handler.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
import 'package:flutter/material.dart';

import '../school_year/school_year.dart';
import '../school_year/school_year_manager.dart';

final _defaultSchoolYears = {
SchoolYear(
startDate: DateTime(2018, DateTime.september, 3),
endDate: DateTime(2019, DateTime.july, 26),
),
SchoolYear(
startDate: DateTime(2019, DateTime.september, 2),
endDate: DateTime(2020, DateTime.july, 25),
),
SchoolYear(
startDate: DateTime(2020, DateTime.september),
endDate: DateTime(2021, DateTime.july, 24),
),
SchoolYear(
startDate: DateTime(2021, DateTime.august, 31),
endDate: DateTime(2022, DateTime.july, 23),
),
};

class DayHandler with ChangeNotifier {
late DateTime _dateTime;
late SchoolYearManager schoolYearManager;

DayHandler([SchoolYearManager? schoolYearManager]) {
_dateTime = DateTime.now();
this.schoolYearManager = schoolYearManager ??
SchoolYearManager(
schoolYears: _defaultSchoolYears,
notifyExternalListeners: notifyListeners,
);
SchoolYearManager(notifyExternalListeners: notifyListeners);
}

DateTime get dateTime => _dateTime;
Expand Down
25 changes: 23 additions & 2 deletions lib/src/model/school_year/school_year_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ class SchoolYearManager extends WritableManager<Set<SchoolYear>>
}
}

Set<SchoolYear> get _defaultSchoolYears => SplayTreeSet.from({
SchoolYear(
startDate: DateTime(2018, DateTime.september, 3),
endDate: DateTime(2019, DateTime.july, 26),
),
SchoolYear(
startDate: DateTime(2019, DateTime.september, 2),
endDate: DateTime(2020, DateTime.july, 25),
),
SchoolYear(
startDate: DateTime(2020, DateTime.september),
endDate: DateTime(2021, DateTime.july, 24),
),
SchoolYear(
startDate: DateTime(2021, DateTime.august, 31),
endDate: DateTime(2022, DateTime.july, 23),
),
});

@override
Future<Set<SchoolYear>> readFromFile() async {
try {
Expand All @@ -132,9 +151,11 @@ class SchoolYearManager extends WritableManager<Set<SchoolYear>>

final schoolYears = _parseSchoolYears(content);

return SplayTreeSet.from(schoolYears);
return schoolYears.isEmpty
? _defaultSchoolYears
: SplayTreeSet.from(schoolYears);
} on Exception {
return SplayTreeSet();
return _defaultSchoolYears;
}
}

Expand Down

0 comments on commit a0ddc56

Please sign in to comment.