-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_page.dart
360 lines (337 loc) · 12.6 KB
/
home_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:sph_plan/shared/apps.dart';
import 'package:sph_plan/shared/exceptions/client_status_exceptions.dart';
import 'package:flutter/material.dart';
import 'package:sph_plan/client/client.dart';
import 'package:sph_plan/view/calendar/calendar.dart';
import 'package:sph_plan/view/conversations/conversations.dart';
import 'package:sph_plan/view/mein_unterricht/mein_unterricht.dart';
import 'package:sph_plan/view/settings/settings.dart';
import 'package:sph_plan/view/bug_report/send_bugreport.dart';
import 'package:sph_plan/view/vertretungsplan/vertretungsplan.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:sph_plan/client/connection_checker.dart';
import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart';
class Destination {
final String label;
final Icon icon;
final Icon selectedIcon;
final bool isSupported;
final bool enableBottomNavigation;
final bool enableDrawer;
final bool addDivider;
final Function? action;
final Widget? body;
//Either a body or an action has to be provided!
Destination(
{this.body,
this.action,
this.addDivider = false,
required this.enableBottomNavigation,
required this.enableDrawer,
required this.label,
required this.isSupported,
required this.icon,
required this.selectedIcon});
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late int selectedDestinationDrawer;
late bool doesSupportAnyApplet = false;
///The UI is build dynamically based on this list.
///Applets with destination.enableBottomNavigation enabled have to be placed at the beginning of the list or the bottom navigation bar will break.
List<Destination> destinations = [
Destination(
label: "Vertretungsplan",
icon: const Icon(Icons.group),
selectedIcon: const Icon(Icons.group_outlined),
isSupported: client.doesSupportFeature(SPHAppEnum.vertretungsplan),
enableBottomNavigation: true,
enableDrawer: true,
body: const VertretungsplanAnsicht()),
Destination(
label: "Kalender",
icon: const Icon(Icons.calendar_today),
selectedIcon: const Icon(Icons.calendar_today_outlined),
isSupported: client.doesSupportFeature(SPHAppEnum.kalender),
enableBottomNavigation: true,
enableDrawer: true,
body: const CalendarAnsicht()),
Destination(
label: "Nachrichten",
icon: const Icon(Icons.forum),
selectedIcon: const Icon(Icons.forum_outlined),
isSupported: client.doesSupportFeature(SPHAppEnum.nachrichten),
enableBottomNavigation: true,
enableDrawer: true,
body: const ConversationsAnsicht()),
Destination(
label: "Mein Unterricht",
icon: const Icon(Icons.school),
selectedIcon: const Icon(Icons.school_outlined),
isSupported: client.doesSupportFeature(SPHAppEnum.meinUnterricht),
enableBottomNavigation: true,
enableDrawer: true,
body: const MeinUnterrichtAnsicht()),
Destination(
label: "Lanis im Browser öffnen",
icon: const Icon(Icons.open_in_new),
selectedIcon: const Icon(Icons.open_in_new),
isSupported: true,
enableBottomNavigation: false,
enableDrawer: true,
action: (context) {
client.getLoginURL().then((response) {
launchUrl(Uri.parse(response));
});
}),
Destination(
label: "Moodle Login öffnen",
icon: const Icon(Icons.open_in_new),
selectedIcon: const Icon(Icons.open_in_new),
isSupported: true,
enableBottomNavigation: false,
enableDrawer: true,
action: (context) => launchUrl(
Uri.parse("https://mo${client.schoolID}.schulportal.hessen.de"))),
Destination(
label: "Einstellungen",
icon: const Icon(Icons.settings),
selectedIcon: const Icon(Icons.settings),
isSupported: true,
enableBottomNavigation: false,
enableDrawer: true,
addDivider: true,
action: (context) => Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SettingsScreen()),
)),
Destination(
label: "Fehlerbericht senden",
icon: const Icon(Icons.bug_report),
selectedIcon: const Icon(Icons.bug_report_outlined),
isSupported: true,
enableBottomNavigation: false,
enableDrawer: true,
action: (context) => Navigator.push(context,
MaterialPageRoute(builder: (context) => const BugReportScreen())),
),
];
void setDefaultDestination() {
for (var destination in destinations) {
if (destination.isSupported && destination.enableBottomNavigation) {
selectedDestinationDrawer = destinations.indexOf(destination);
doesSupportAnyApplet = true;
return;
}
}
selectedDestinationDrawer = -1;
}
@override
void initState() {
super.initState();
setDefaultDestination();
}
void openLanisInBrowser(BuildContext? context) {
client.getLoginURL().then((response) {
launchUrl(Uri.parse(response));
}).catchError((ex) {
if (context == null) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(ex.cause),
duration: const Duration(seconds: 1),
action: SnackBarAction(
label: 'ACTION',
onPressed: () {},
),
));
}, test: (e) => e is LanisException);
}
Widget noAppsSupported() {
return Center(
// In case no feature is supported at all just show an open in browser button.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.disabled_by_default_outlined,
size: 150,
),
const Padding(
padding: EdgeInsets.all(32),
child: Text(
"Es scheint so, als ob dein Account oder deine Schule keine Features dieser App direkt unterstützt! Stattdessen kannst du Lanis noch im Browser öffnen."),
),
ElevatedButton(
onPressed: () => openLanisInBrowser(context),
child: const Text("Im Browser öffnen"))
],
),
);
}
void openDestination(int index, bool fromDrawer) {
if (destinations[index].action != null) {
destinations[index].action!(context);
} else {
setState(() {
selectedDestinationDrawer = index;
if (fromDrawer) Navigator.pop(context);
});
}
}
NavigationDrawer navDrawer(context) {
List<Widget> drawerDestinations = [];
for (var destination in destinations) {
if (destination.enableDrawer) {
if (destination.addDivider) {
drawerDestinations.add(const Divider());
}
drawerDestinations.add(NavigationDrawerDestination(
label: Text(destination.label),
icon: destination.icon,
selectedIcon: destination.selectedIcon,
enabled: destination.isSupported,
));
}
}
final Color imageColor =
Theme.of(context).colorScheme.inversePrimary.withOpacity(0.5);
final Color textColor =
imageColor.computeLuminance() < 0.5 ? Colors.white : Colors.black;
return NavigationDrawer(
selectedIndex: selectedDestinationDrawer,
onDestinationSelected: (int index) => openDestination(index, true),
children: [
Padding(
padding: const EdgeInsets.only(bottom: 12.0),
child: Stack(
alignment: Alignment.centerLeft,
children: [
ClipRRect(
child: ImageFiltered(
imageFilter: ImageFilter.blur(sigmaX: 4, sigmaY: 4),
child: ColorFiltered(
colorFilter:
ColorFilter.mode(imageColor, BlendMode.srcOver),
child: AspectRatio(
aspectRatio: 16 / 9,
child: CachedNetworkImage(
imageUrl:
"https://startcache.schulportal.hessen.de/exporteur.php?a=schoolbg&i=${client.schoolID}&s=xs",
fadeInDuration: const Duration(milliseconds: 0),
fit: BoxFit.cover,
placeholder: (context, url) => const Image(
image: AssetImage("assets/icon.png"),
fit: BoxFit.cover,
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(left: 24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
client.schoolName,
style: Theme.of(context)
.textTheme
.titleMedium
?.copyWith(color: textColor),
),
Text(
"${client.userData["nachname"]}, ${client.userData["vorname"]}",
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
fontWeight: FontWeight.bold, color: textColor),
)
],
),
)
],
),
),
...drawerDestinations,
]);
}
NavigationBar navBar(context) {
List<NavigationDestination> barDestinations = [];
for (var destination in destinations) {
if (destination.enableBottomNavigation && destination.isSupported) {
barDestinations.add(NavigationDestination(
label: destination.label,
icon: destination.icon,
selectedIcon: destination.selectedIcon,
enabled: destination.isSupported,
tooltip: destination.label,
));
}
}
List<int?> indexNavbarTranslationLayer = [];
int helpIndex = 0;
for (var destination in destinations) {
if (destination.enableBottomNavigation && destination.isSupported) {
indexNavbarTranslationLayer.add(helpIndex);
helpIndex += 1;
} else {
indexNavbarTranslationLayer.add(null);
}
}
return NavigationBar(
destinations: barDestinations,
selectedIndex: indexNavbarTranslationLayer[selectedDestinationDrawer]!,
onDestinationSelected: (int index) =>
openDestination(indexNavbarTranslationLayer.indexOf(index), false),
labelBehavior: NavigationDestinationLabelBehavior.onlyShowSelected,
);
}
@override
Widget build(BuildContext context) {
return StreamBuilder<InternetStatus>(
stream: connectionChecker.onStatusChange,
builder: (context, network) {
return Scaffold(
appBar: AppBar(
title: Text(doesSupportAnyApplet
? destinations[selectedDestinationDrawer].label
: "Im Browser öffnen"),
bottom: network.data == InternetStatus.disconnected
? PreferredSize(
preferredSize: const Size.fromHeight(40),
child: Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.only(right: 8),
child: Icon(Icons.signal_wifi_off),
),
Text(
"Keine Verbindung! Geladene Daten sind noch aufrufbar!",
style: Theme.of(context).textTheme.labelLarge,
),
],
),
),
)
: null,
),
body: doesSupportAnyApplet
? destinations[selectedDestinationDrawer].body
: noAppsSupported(),
bottomNavigationBar:
doesSupportAnyApplet ? navBar(context) : null,
drawer: navDrawer(context));
});
}
}