Skip to content

Commit

Permalink
🏗️ Consist datetime usage and date provider's now (Monday as first …
Browse files Browse the repository at this point in the history
…day).
  • Loading branch information
AlexV525 committed Feb 17, 2020
1 parent dcd32c4 commit c7ed37e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/pages/home/course_schedule_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class CourseSchedulePageState extends State<CourseSchedulePage> with AutomaticKe
now.subtract((7 * (currentWeek - dateProvider.currentWeek)).days +
Duration(days: now.weekday - 1 - i)),
) ==
DateFormat('MM/dd').format(DateTime.now())
DateFormat('MM/dd').format(now)
? currentThemeColor.withOpacity(0.35)
: null,
),
Expand Down
18 changes: 9 additions & 9 deletions lib/pages/home/my_info_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class MyInfoPageState extends State<MyInfoPage> {

int signedCount = 0;

DateTime now = DateTime.now();
String hello = '你好';

@override
Expand Down Expand Up @@ -247,9 +246,9 @@ class MyInfoPageState extends State<MyInfoPage> {
NetUtils.updateTicket();
}
},
child: Selector<DateProvider, int>(
selector: (_, provider) => provider.currentWeek,
builder: (_, currentWeek, __) => currentWeek != null && currentWeek <= 20
child: Consumer<DateProvider>(
builder: (_, provider, __) => provider.currentWeek != null &&
provider.currentWeek <= 20
? Container(
color: Theme.of(context).primaryColor,
padding: EdgeInsets.symmetric(
Expand All @@ -263,13 +262,14 @@ class MyInfoPageState extends State<MyInfoPage> {
children: <TextSpan>[
TextSpan(text: '${currentUser.name},$hello~\n'),
TextSpan(text: '今天是'),
TextSpan(text: '${DateFormat('MMMdd', 'zh_CN').format(now)}日,'),
TextSpan(text: '${DateFormat('EEE', 'zh_CN').format(now)},'),
if (currentWeek > 0)
TextSpan(
text: '${DateFormat('MMMdd', 'zh_CN').format(provider.now)}日,'),
TextSpan(text: '${DateFormat('EEE', 'zh_CN').format(provider.now)},'),
if (provider.currentWeek > 0)
TextSpan(children: <InlineSpan>[
TextSpan(text: '第'),
TextSpan(
text: '$currentWeek',
text: '${provider.currentWeek}',
style: TextStyle(
color: currentThemeColor,
fontWeight: FontWeight.bold,
Expand All @@ -281,7 +281,7 @@ class MyInfoPageState extends State<MyInfoPage> {
TextSpan(children: <InlineSpan>[
TextSpan(text: '距开学还有'),
TextSpan(
text: '${currentWeek.abs() + 1}',
text: '${provider.currentWeek.abs() + 1}',
style: TextStyle(
color: currentThemeColor,
fontWeight: FontWeight.bold,
Expand Down
15 changes: 12 additions & 3 deletions lib/providers/date_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class DateProvider extends ChangeNotifier {
notifyListeners();
}

DateTime _now;
DateTime get now => _now;
set now(DateTime value) {
assert(value != null);
if (value == _now) return;
_now = value;
notifyListeners();
}

Future<void> initCurrentWeek() async {
final _dateInCache = HiveBoxes.startWeekBox.get('startDate');
if (_dateInCache != null) _startDate = _dateInCache;
Expand All @@ -47,7 +56,7 @@ class DateProvider extends ChangeNotifier {
}

Future<void> getCurrentWeek({bool init = false}) async {
final now = DateTime.now();
now = DateTime.now();
final box = HiveBoxes.startWeekBox;
try {
DateTime _day;
Expand All @@ -63,10 +72,10 @@ class DateProvider extends ChangeNotifier {
if (_startDate != _day) updateStartDate(_day);
}

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

final _w = -(_difference / 7).floor();
final _w = -((_difference - 1) / 7).floor();
if (_currentWeek != _w && _w <= 20) {
_currentWeek = _w;
notifyListeners();
Expand Down

0 comments on commit c7ed37e

Please sign in to comment.