-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.dart
553 lines (502 loc) · 19.4 KB
/
startup.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sph_plan/home_page.dart';
import 'package:sph_plan/shared/exceptions/client_status_exceptions.dart';
import 'package:sph_plan/shared/widgets/whats_new.dart';
import 'package:sph_plan/view/bug_report/send_bugreport.dart';
import 'package:sph_plan/view/login/screen.dart';
import 'client/client.dart';
import 'client/fetcher.dart';
/// Just a collection of possible messages for the bottom progress indicator, so we that we have not magic strings.
class Message {
static String initialise = "Initialisieren...";
static String login = "Einloggen...";
static String load = "Lade Apps...";
static String finalise = "Finalisieren...";
static String error = "Beim Laden ist ein Fehler passiert!";
}
/// Status of each step
enum Status {
waiting,
loading,
finished,
error;
}
/// Collection of steps so we don't have magic strings.
class Step {
static String login = "Login";
// The rest is now dynamic, gotten by [client.loadApps]
}
/// More advanced class, so we can have a tidy and clean progress indicator of the steps.
class ProgressNotifier with ChangeNotifier {
final Map<String, Status> _steps = {};
Map<String, Status> get steps => _steps;
void set(String step, Status status) {
_steps[step] = status;
notifyListeners();
}
void reset() {
_steps.updateAll((_, value) => value = Status.waiting);
notifyListeners();
}
ProgressNotifier(List<String> steps) {
for (String step in steps) {
_steps.addEntries([MapEntry(step, Status.waiting)]);
}
}
}
/// Possible applet which will be fetched by [fetchApplet].
class Applet {
final Fetcher? fetcher;
final String step;
final String finishMessage;
Applet(
{required this.fetcher, required this.step, required this.finishMessage});
}
class StartupScreen extends StatefulWidget {
const StartupScreen({super.key});
@override
State<StartupScreen> createState() => _StartupScreenState();
}
class _StartupScreenState extends State<StartupScreen> {
ValueNotifier<String> loadingMessage =
ValueNotifier<String>(Message.initialise);
final List<Applet> appletFetchers = [];
final List<String> steps = [Step.login];
late final ProgressNotifier progress;
ValueNotifier<bool> noConnection = ValueNotifier<bool>(false);
ValueNotifier<bool> isError = ValueNotifier<bool>(false);
final Map<String, LanisException?> errors = {Step.login: null};
// We need to load storage first, so we have to wait before everything.
ValueNotifier<bool> finishedLoadingStorage = ValueNotifier<bool>(false);
void openWelcomeScreen() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const WelcomeLoginScreen()),
).then((_) async {
client.initialiseLoadApps();
await client.prepareDio();
// Context should be mounted
// ignore: use_build_context_synchronously
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
);
});
}
Future<void> fetchApplet(Applet applet) async {
progress.set(applet.step, Status.loading);
await applet.fetcher!.fetchData(forceRefresh: true);
await for (dynamic data in applet.fetcher!.stream) {
if (data.status == FetcherStatus.error) {
progress.set(applet.step, Status.error);
loadingMessage.value = Message.error;
isError.value = true;
errors[applet.step] = data.content;
if (data.content is NoConnectionException) {
noConnection.value = true;
}
return;
} else if (data.status == FetcherStatus.done) {
progress.set(applet.step, Status.finished);
loadingMessage.value = applet.finishMessage;
break;
}
}
progress.set(applet.step, Status.finished);
}
Future<void> performLogin() async {
// Step 1
// It doesn't show it immediately but a lot faster than before.
// I think this check is enough.
await client.prepareDio();
if (client.username == "") {
openWelcomeScreen();
return;
}
// Step 2 (if this fails, show login screen or notify error)
loadingMessage.value = Message.login;
progress.set(Step.login, Status.loading);
try {
await client.login();
client.initialiseLoadApps();
progress.set(Step.login, Status.finished);
loadingMessage.value = Message.load;
// Step 4 (fetch everything async)
await Future.wait(List.generate(appletFetchers.length,
(index) => fetchApplet(appletFetchers[index])));
// Step 5 (finish)
if (!isError.value) {
loadingMessage.value = Message.finalise;
whatsNew().then((value) {
if (value != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ReleaseNotesScreen(value)),
).then((_) => Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
));
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => const HomePage()),
);
}
});
}
return;
} on WrongCredentialsException {
openWelcomeScreen();
} on CredentialsIncompleteException {
openWelcomeScreen();
} on LanisException catch (e, stack) {
debugPrint(stack.toString());
errors[Step.login] = e;
isError.value = true;
progress.set(Step.login, Status.error);
if (e is NoConnectionException) {
noConnection.value = true;
}
loadingMessage.value = Message.error;
}
}
// Handy functions
IconData getIcon(Status status) {
switch (status) {
case Status.finished:
return Icons.check;
case Status.waiting:
return Icons.pending_outlined;
case Status.loading:
return Icons.sync;
case Status.error:
return Icons.error_outline;
}
}
String getProgressMessage(Status status) {
switch (status) {
case Status.finished:
return "Fertig";
case Status.waiting:
return "Warten";
case Status.loading:
return "Laden";
case Status.error:
return "Fehler";
}
}
@override
void initState() {
// So that we begin loading instantly on startup.
WidgetsBinding.instance.addPostFrameCallback((_) {
// We need to load this first so that everything works.
client.loadFromStorage().then((_) {
// Show welcome screen nearly instantly.
if (client.username == "") {
performLogin();
return;
}
finishedLoadingStorage.value = true;
if (client.applets != null) {
for (final loadApp in client.applets!.keys) {
final currentLoadApp = client.applets![loadApp];
if (currentLoadApp!.shouldFetch == true) {
steps.add(currentLoadApp.applet.fullName);
for (final fetcher in currentLoadApp.fetchers) {
appletFetchers.add(Applet(
fetcher: fetcher,
step: currentLoadApp.applet.fullName,
finishMessage:
"${currentLoadApp.applet.fullName} wurde(n) fertig geladen!"));
}
}
errors.addEntries([MapEntry(currentLoadApp.applet.fullName, null)]);
}
}
progress = ProgressNotifier(steps);
performLogin();
});
});
super.initState();
}
/// Either school image or app version.
Widget schoolLogo() {
var darkMode = Theme.of(context).brightness == Brightness.dark;
Widget deviceInfo = FutureBuilder(
future: PackageInfo.fromPlatform(),
builder: (context, packageInfo) {
return Text(
"lanis-mobile ${packageInfo.data?.version}",
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.25)),
);
},
);
return CachedNetworkImage(
imageUrl:
"https://startcache.schulportal.hessen.de/exporteur.php?a=schoollogo&i=${client.schoolID}",
fadeInDuration: const Duration(milliseconds: 0),
placeholder: (context, url) => deviceInfo,
errorWidget: (context, url, error) => deviceInfo,
imageBuilder: (context, imageProvider) => ColorFiltered(
colorFilter: darkMode
? const ColorFilter.matrix([-1, 0 ,0 ,0 ,255 ,0 ,-1 ,0 ,0 ,255 ,0 ,0 ,-1 ,0 ,255 ,0 ,0 ,0 ,1 ,0])
: const ColorFilter.mode(Colors.transparent, BlendMode.multiply),
child: Image(
image: imageProvider,
fit: BoxFit.cover,
),
),
);
}
Widget currentSteps() {
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondary,
borderRadius: BorderRadius.circular(16.0)),
padding: const EdgeInsets.all(12.0),
margin: const EdgeInsets.symmetric(horizontal: 84),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ValueListenableBuilder(
valueListenable: finishedLoadingStorage,
builder: (context, finishedLoadingStorage, _) {
if (finishedLoadingStorage) {
// Show all steps like login, vp, ...
return ListenableBuilder(
listenable: progress,
builder: (context, _) {
return ListView.separated(
shrinkWrap: true,
itemCount: steps.length,
separatorBuilder: (context, index) {
return const SizedBox(
height: 8,
);
},
itemBuilder: (context, index) {
final String step = steps[index];
final Status status = progress.steps[step]!;
return Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.only(
right: 4.0),
child: Icon(
getIcon(status),
color: Theme.of(context)
.colorScheme
.onSecondary,
),
),
Text(step,
style: Theme.of(context)
.textTheme
.labelLarge
?.copyWith(
color: Theme.of(context)
.colorScheme
.onSecondary)),
],
),
Text(getProgressMessage(status),
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: Theme.of(context)
.colorScheme
.onSecondary))
],
);
});
});
}
// Show only that client.loadFromStorage() is being executed.
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Lade gespeicherte Daten...",
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color: Theme.of(context)
.colorScheme
.onSecondary))
],
);
})
],
)),
);
}
Widget errorButtons() {
return ValueListenableBuilder(
valueListenable: isError,
builder: (context, _isError, _) {
if (_isError) {
return Padding(
padding: const EdgeInsets.only(top: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ValueListenableBuilder(
valueListenable: noConnection,
builder: (context, noInternet, _) {
if (noInternet) {
return const SizedBox.shrink();
}
return FilledButton(
onPressed: () {
// Prepare error message
String errorInfo = "";
errors.forEach((key, value) {
if (value == null) {
return;
}
errorInfo += "$key - ${value.cause}\n";
});
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BugReportScreen(
generatedMessage:
"AUTOMATISCH GENERIERT (LOGIN PAGE):\n$errorInfo\nMehr Details von dir:\n")),
);
},
child: const Text("Fehlerbericht senden"));
}),
Padding(
padding: const EdgeInsets.only(left: 8),
child: OutlinedButton(
onPressed: () async {
// Reset
progress.reset();
errors.updateAll((key, value) => value = null);
isError.value = false;
noConnection.value = false;
loadingMessage.value = "Initialisieren...";
await performLogin();
},
child: const Text("Erneut versuchen")),
)
],
),
);
}
return const SizedBox.shrink();
});
}
/// The bottom thing that shows a message and a "big" progress indicator.
Widget progressIndicator() {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: [
Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(16.0)),
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12),
margin: const EdgeInsets.all(12),
child: Row(
children: [
ValueListenableBuilder(
valueListenable: loadingMessage,
builder: (context, _loadingMessage, _) {
return Padding(
padding: const EdgeInsets.only(right: 12.0),
child: Text(_loadingMessage,
style: Theme.of(context)
.textTheme
.labelLarge
?.copyWith(
color:
Theme.of(context).colorScheme.onPrimary)),
);
}),
ValueListenableBuilder(
valueListenable: isError,
builder: (context, _isError, _) {
if (_isError) {
return Icon(
Icons.error,
color: Theme.of(context).colorScheme.onPrimary,
);
}
return SizedBox(
width: 25,
height: 25,
child: CircularProgressIndicator(
color: Theme.of(context).colorScheme.onPrimary,
),
);
})
],
),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
schoolLogo(),
Column(
children: [
// Greeting message
Container(
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.primary,
borderRadius: BorderRadius.circular(16.0)),
padding: const EdgeInsets.all(12.0),
child: ValueListenableBuilder(
valueListenable: noConnection,
builder: (context, noConnection, _) {
if (noConnection) {
return Text("Keine Verbindung!",
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
color: Theme.of(context)
.colorScheme
.onPrimary));
}
return Text("Willkommen zurück!",
style: Theme.of(context)
.textTheme
.headlineMedium
?.copyWith(
color: Theme.of(context)
.colorScheme
.onPrimary));
},
)),
currentSteps(),
errorButtons(),
],
),
progressIndicator()
],
),
),
);
}
}