-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathgrades_dialog.dart
154 lines (137 loc) · 4.93 KB
/
grades_dialog.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
// Copyright (c) 2024 Sharezone UG (haftungsbeschränkt)
// Licensed under the EUPL-1.2-or-later.
//
// You may obtain a copy of the Licence at:
// https://joinup.ec.europa.eu/software/page/eupl
//
// SPDX-License-Identifier: EUPL-1.2
import 'package:clock/clock.dart';
import 'package:date/date.dart';
import 'package:design/design.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:sharezone/filesharing/dialog/course_tile.dart';
import 'package:sharezone/grades/grades_service/grades_service.dart'
hide InvalidGradeValueException;
import 'package:sharezone/grades/pages/create_term_page/create_term_page.dart';
import 'package:sharezone/grades/pages/grades_dialog/grades_dialog_controller.dart';
import 'package:sharezone/grades/pages/grades_dialog/grades_dialog_controller_factory.dart';
import 'package:sharezone/grades/pages/grades_dialog/grades_dialog_view.dart';
import 'package:sharezone/grades/pages/shared/saved_grade_icons.dart';
import 'package:sharezone/grades/pages/shared/select_grade_type_dialog.dart';
import 'package:sharezone/sharezone_plus/page/sharezone_plus_page.dart';
import 'package:sharezone/sharezone_plus/subscription_service/subscription_service.dart';
import 'package:sharezone/support/support_page.dart';
import 'package:sharezone_widgets/sharezone_widgets.dart';
part 'fields.dart';
class GradesDialog extends StatelessWidget {
const GradesDialog({super.key, this.gradeId});
static const tag = 'grades-dialog';
/// The [GradeId] of the grade to edit, `null` if grade should be created.
final GradeId? gradeId;
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<GradesDialogController>(
create: (context) {
final factory = context.read<GradesDialogControllerFactory>();
return factory.create();
},
builder: (context, _) {
return Scaffold(
appBar: AppBar(actions: const [_SaveButton()]),
body: const SingleChildScrollView(
padding: EdgeInsets.all(8),
child: SafeArea(child: MaxWidthConstraintBox(child: _Fields())),
),
);
},
);
}
}
class _SaveButton extends StatelessWidget {
const _SaveButton();
void showConfirmationSnackBar(BuildContext context) {
showSnackSec(context: context, text: 'Note gespeichert');
}
void showErrorSnackBar(BuildContext context, Object e) {
final unknownErrorMessage = 'Unbekannter Fehler: $e';
String? message;
if (e is SaveGradeException) {
message = switch (e) {
InvalidFieldsSaveGradeException() => _getInvalidFieldsMessage(e),
UnknownSaveGradeException() => unknownErrorMessage,
};
} else {
message = unknownErrorMessage;
}
showSnackSec(context: context, text: message);
}
String _getInvalidFieldsMessage(InvalidFieldsSaveGradeException e) {
assert(e.invalidFields.isNotEmpty);
if (e.invalidFields.length == 1) {
return switch (e.invalidFields.first) {
GradingDialogFields.gradeValue => 'Die Note fehlt oder ist ungültig.',
GradingDialogFields.title => 'Der Titel fehlt oder ist ungültig.',
GradingDialogFields.subject => 'Bitte gib ein Fach für die Note an.',
GradingDialogFields.term => 'Bitte gib ein Halbjahr für die an.',
};
}
final fields = e.invalidFields;
final fieldMessages = fields.map((f) => f.toUiString()).join(', ');
return 'Folgende Felder fehlen oder sind ungültig: $fieldMessages.';
}
@override
Widget build(BuildContext context) {
final controller = Provider.of<GradesDialogController>(
context,
listen: false,
);
return Padding(
padding: const EdgeInsets.only(right: 8),
child: FilledButton(
onPressed: () async {
try {
sendDataToFrankfurtSnackBar(context);
await controller.save();
if (!context.mounted) return;
showConfirmationSnackBar(context);
Navigator.of(context).pop();
} catch (e) {
if (!context.mounted) return;
showErrorSnackBar(context, e);
}
},
child: const Text("Speichern"),
),
);
}
}
class _Fields extends StatelessWidget {
const _Fields();
@override
Widget build(BuildContext context) {
return ListTileTheme(
data: Theme.of(context).listTileTheme.copyWith(
subtitleTextStyle: TextStyle(
color: Theme.of(context).colorScheme.onSurface.withValues(alpha: 0.5),
),
),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_GradeValue(),
_GradingSystem(),
_Subject(),
_Date(),
_GradingType(),
_Term(),
_TakeIntoAccountSwitch(),
_Title(),
_Details(),
],
),
);
}
}