Skip to content

Commit

Permalink
🐛 Fix date getter may failed when nothing in storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Feb 18, 2020
1 parent 16d0075 commit 44a25c3
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/providers/date_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DateProvider extends ChangeNotifier {
Future<void> initCurrentWeek() async {
final _dateInCache = HiveBoxes.startWeekBox.get('startDate');
if (_dateInCache != null) _startDate = _dateInCache;
await getCurrentWeek(init: _dateInCache == null);
await getCurrentWeek();
initCurrentWeekTimer();
}

Expand All @@ -55,24 +55,23 @@ class DateProvider extends ChangeNotifier {
await HiveBoxes.startWeekBox.put('startDate', date);
}

Future<void> getCurrentWeek({bool init = false}) async {
Future<void> getCurrentWeek() async {
now = DateTime.now();
final box = HiveBoxes.startWeekBox;
try {
DateTime _day;
if (init) {
_day = box.get('startDate');
if (_day == null) {
final result = (await NetUtils.get(API.firstDayOfTerm)).data;
_day = DateTime.parse(jsonDecode(result)['start']);
} else {
_day = box.get('startDate');
}
if (_startDate == null) {
updateStartDate(_day);
} else {
if (_startDate != _day) updateStartDate(_day);
}

final _d = startDate.difference(now).inDays;
final _d = _startDate.difference(now).inDays;
if (_difference != _d) _difference = _d;

final _w = -((_difference - 1) / 7).floor();
Expand All @@ -98,7 +97,7 @@ class DateProvider extends ChangeNotifier {
void startFetchCurrentWeekTimer() {
_fetchCurrentWeekTimer?.cancel();
_fetchCurrentWeekTimer = Timer.periodic(30.seconds, (_) {
getCurrentWeek(init: true);
getCurrentWeek();
});
}
}
Expand Down

0 comments on commit 44a25c3

Please sign in to comment.