-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar_page.dart
153 lines (139 loc) · 5.03 KB
/
calendar_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:huntis/calendar.dart';
import 'package:huntis/components/login_button.dart';
import 'package:huntis/untis_api.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CalendarPage extends StatefulWidget {
const CalendarPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _CalendarPageState();
}
class _CalendarPageState extends State<CalendarPage> {
late Session untisSession;
late Schoolyear schoolYear;
late List<Period> timetable;
late TimeGrid timegrid;
late List<String> mySubjects;
late Map<String, Color> mySubjectColors;
late Map<String, String> mySubjectNames;
bool hasLoginData() {
SharedPreferences prefs = GetIt.instance<SharedPreferences>();
bool notNull = prefs.getString('serverURL') != null &&
prefs.getString('school') != null &&
prefs.getString('username') != null &&
prefs.getString('password') != null;
bool notEmpty = prefs.getString('serverURL') != '' &&
prefs.getString('school') != '' &&
prefs.getString('username') != '' &&
prefs.getString('password') != '';
return notNull && notEmpty;
}
Future<void> _doAPICalls() async {
GetIt getIt = GetIt.instance;
untisSession = getIt<Session>();
if (!untisSession.isLoggedIn) {
await untisSession.login();
}
// Get latest school year
var allSchoolYears = await untisSession.getSchoolyears();
allSchoolYears.sort((a, b) => a.endDate.compareTo(b.endDate));
schoolYear = allSchoolYears.last;
// Set startDate and endDate
late DateTime startDate, endDate;
if (DateTime.now()
.add(const Duration(days: 30))
.isAfter(schoolYear.endDate)) {
// If today is after or within the last 30 days of the school year
startDate = schoolYear.endDate.subtract(const Duration(days: 30));
endDate = schoolYear.endDate;
} else if (DateTime.now().subtract(const Duration(days: 30)).isBefore(schoolYear.startDate)) {
// If today is before or within the first 30 days of the school year
startDate = schoolYear.startDate;
endDate = schoolYear.startDate.add(const Duration(days: 30));
} else {
startDate = DateTime.now().subtract(const Duration(days: 15));
endDate = DateTime.now().add(const Duration(days: 15));
}
timetable = await untisSession.getPeriods(
startDate: startDate,
endDate: endDate,
);
mySubjects = _getMySubjects();
// Filter Timetable for mySubjects
timetable = timetable
.where((element) => mySubjects.contains(element.name))
.toList();
timegrid = await untisSession.getTimegrid();
mySubjectColors = _getSubjectColors();
mySubjectNames = _getSubjectNames();
}
Map<String, Color> _getSubjectColors() {
SharedPreferences prefs = GetIt.instance<SharedPreferences>();
List<String>? colors = prefs.getStringList('mySubjectColors');
Map<String, Color> mySubjectColors = {
for (var e in colors ?? [])
e.split(':')[0]: Color(int.parse(e.split(':')[1], radix: 16))
};
return mySubjectColors;
}
Map<String, String> _getSubjectNames() {
SharedPreferences prefs = GetIt.instance<SharedPreferences>();
List<String>? names = prefs.getStringList('mySubjectNames');
Map<String, String> mySubjectNames = {
for (var e in names ?? [])
e.split(':')[0]: e.split(':')[1]
};
return mySubjectNames;
}
List<String> _getMySubjects() {
SharedPreferences prefs = GetIt.instance<SharedPreferences>();
List<String>? mySubjects = prefs.getStringList('mySubjects');
return mySubjects ?? [];
}
@override
Widget build(BuildContext context) {
GetIt getIt = GetIt.instance;
if (!hasLoginData()) {
return Center(
child: Text('missing-login-data'.tr()),
);
} else if (!getIt.isRegistered<Session>() || !getIt<Session>().isLoggedIn) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('messages.not-logged-in'.tr()),
LoginButton(context: context),
ElevatedButton(
onPressed: () => setState(() {}),
child: Text('reload'.tr()),
),
],
),
);
} else {
return FutureBuilder(
future: _doAPICalls(),
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Calendar(
untisSession: untisSession,
schoolYear: schoolYear,
timetable: timetable,
timegrid: timegrid,
mySubjects: mySubjects,
mySubjectColors: mySubjectColors,
mySubjectNames: mySubjectNames,
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
);
}
}
}