forked from student-hub/acs-upb-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses_page.dart
423 lines (380 loc) · 13.1 KB
/
classes_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
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
import 'package:acs_upb_mobile/authentication/service/auth_provider.dart';
import 'package:acs_upb_mobile/generated/l10n.dart';
import 'package:acs_upb_mobile/pages/class_feedback/view/class_feedback_checklist.dart';
import 'package:acs_upb_mobile/pages/classes/model/class.dart';
import 'package:acs_upb_mobile/pages/classes/service/class_provider.dart';
import 'package:acs_upb_mobile/pages/classes/view/class_view.dart';
import 'package:acs_upb_mobile/widgets/class_icon.dart';
import 'package:acs_upb_mobile/widgets/error_page.dart';
import 'package:acs_upb_mobile/widgets/icon_text.dart';
import 'package:acs_upb_mobile/widgets/scaffold.dart';
import 'package:acs_upb_mobile/widgets/spoiler.dart';
import 'package:flutter/material.dart';
import 'package:pedantic/pedantic.dart';
import 'package:provider/provider.dart';
import 'package:acs_upb_mobile/resources/remote_config.dart';
class ClassesPage extends StatefulWidget {
const ClassesPage({Key key}) : super(key: key);
@override
_ClassesPageState createState() => _ClassesPageState();
}
class _ClassesPageState extends State<ClassesPage> {
Set<ClassHeader> headers;
bool updating;
Future<void> updateClasses() async {
// If updating is null, classes haven't been initialized yet so they're not
// technically "updating"
if (updating != null) {
updating = true;
}
final ClassProvider classProvider =
Provider.of<ClassProvider>(context, listen: false);
final AuthProvider authProvider =
Provider.of<AuthProvider>(context, listen: false);
headers =
(await classProvider.fetchClassHeaders(uid: authProvider.uid)).toSet();
updating = false;
if (mounted) {
setState(() {});
}
}
@override
void initState() {
super.initState();
updateClasses();
}
@override
Widget build(BuildContext context) {
final classProvider = Provider.of<ClassProvider>(context);
final authProvider = Provider.of<AuthProvider>(context);
return AppScaffold(
title: Text(S.current.navigationClasses),
// TODO(IoanaAlexandru): Simply show all classes if user is not authenticated
needsToBeAuthenticated: true,
actions: [
if (RemoteConfigService.feedbackEnabled)
AppScaffoldAction(
icon: Icons.rate_review_outlined,
tooltip: S.current.navigationClassesFeedbackChecklist,
onPressed: () => Navigator.of(context).push(
MaterialPageRoute<ClassFeedbackChecklist>(
builder: (_) => ClassFeedbackChecklist(classes: headers),
),
),
),
AppScaffoldAction(
icon: Icons.edit_outlined,
tooltip: S.current.actionChooseClasses,
onPressed: () => Navigator.of(context).push(
MaterialPageRoute<ChangeNotifierProvider>(
builder: (_) => ChangeNotifierProvider.value(
value: classProvider,
child: FutureBuilder(
future: classProvider.fetchUserClassIds(authProvider.uid),
builder: (context, snap) {
if (snap.hasData) {
return AddClassesPage(
initialClassIds: snap.data,
onSave: (classIds) async {
await classProvider.setUserClassIds(
classIds, authProvider.uid);
unawaited(updateClasses());
Navigator.pop(context);
});
} else {
return const Center(child: CircularProgressIndicator());
}
},
)),
),
),
),
],
body: Stack(
children: [
if (updating != null)
headers != null && headers.isNotEmpty
? ClassList(
classes: headers,
sectioned: false,
onTap: (classHeader) => Navigator.of(context).push(
MaterialPageRoute<ChangeNotifierProvider>(
builder: (context) => ChangeNotifierProvider.value(
value: classProvider,
child: ClassView(
classHeader: classHeader,
),
),
),
),
)
: ErrorPage(
errorMessage: S.current.messageNoClassesYet,
imgPath: 'assets/illustrations/undraw_empty.png',
info: [
TextSpan(
text: '${S.current.messageGetStartedByPressing} '),
WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: Icon(
Icons.edit_outlined,
size:
Theme.of(context).textTheme.subtitle1.fontSize +
2,
),
),
TextSpan(text: ' ${S.current.messageButtonAbove}.'),
]),
if (updating == null)
const Center(child: CircularProgressIndicator()),
if (updating == true)
Container(
color: Theme.of(context).disabledColor,
child: const Center(child: CircularProgressIndicator())),
],
),
);
}
}
class AddClassesPage extends StatefulWidget {
const AddClassesPage({Key key, this.initialClassIds, this.onSave})
: super(key: key);
final List<String> initialClassIds;
final void Function(List<String>) onSave;
@override
_AddClassesPageState createState() =>
_AddClassesPageState(classIds: initialClassIds.toSet());
}
class _AddClassesPageState extends State<AddClassesPage> {
_AddClassesPageState({Set<String> classIds})
: classIds = Set<String>.from(classIds) ?? {};
Set<String> classIds;
Set<ClassHeader> headers;
Future<void> updateClasses() async {
final classProvider = Provider.of<ClassProvider>(context, listen: false);
headers = (await classProvider.fetchClassHeaders()).toSet();
if (mounted) {
setState(() {});
}
}
@override
void initState() {
super.initState();
updateClasses();
}
@override
Widget build(BuildContext context) {
return AppScaffold(
title: Text(S.current.actionChooseClasses),
actions: [
AppScaffoldAction(
text: S.current.buttonSave,
onPressed: () => widget.onSave(classIds.toList()),
)
],
body: ClassList(
classes: headers,
initiallySelected: classIds,
selectable: true,
onSelected: (selected, classId) {
if (selected) {
classIds.add(classId);
} else {
classIds.remove(classId);
}
},
),
);
}
}
class ClassList extends StatefulWidget {
ClassList(
{this.classes,
void Function(bool, String) onSelected,
Set<String> initiallySelected,
this.selectable = false,
this.sectioned = true,
void Function(ClassHeader) onTap})
: onSelected = onSelected ?? ((selected, classId) {}),
onTap = onTap ?? ((_) {}),
initiallySelected = initiallySelected ?? {};
final Set<ClassHeader> classes;
final void Function(bool, String) onSelected;
final Set<String> initiallySelected;
final bool selectable;
final void Function(ClassHeader) onTap;
final bool sectioned;
@override
_ClassListState createState() => _ClassListState();
}
class _ClassListState extends State<ClassList> {
Set<String> selectedClasses;
@override
void initState() {
super.initState();
selectedClasses = widget.initiallySelected;
}
String sectionName(BuildContext context, String year, String semester) =>
'${S.current.labelYear} $year, ${S.current.labelSemester} $semester';
Map<String, dynamic> classesBySection(
Set<ClassHeader> classes, BuildContext context) {
final map = <String, dynamic>{};
for (final c in classes) {
final List<String> category = c.category.split('/');
var currentPath = map;
for (int i = 0; i < category.length; i++) {
final String section = category[i].trim();
if (!currentPath.containsKey(section)) {
currentPath[section] = <String, dynamic>{};
}
currentPath = currentPath[section];
}
if (!currentPath.containsKey('/')) {
currentPath['/'] = <ClassHeader>{};
}
currentPath['/'].add(c);
}
return map;
}
_Section buildSections(BuildContext context, Map<String, dynamic> sections,
{int level = 0}) {
final List<Widget> children = [const SizedBox(height: 4)];
bool expanded = false;
sections.forEach((section, values) {
if (section == '/') {
children.addAll(values.map<Widget>(buildClassItem));
expanded = values.fold(
false,
(dynamic selected, ClassHeader header) =>
selected || widget.initiallySelected.contains(header.id));
} else {
final s = buildSections(context, sections[section], level: level + 1);
expanded = expanded || s.containsSelected;
children.add(AppSpoiler(
title: section,
level: level,
initiallyExpanded: s.containsSelected,
content: Padding(
padding: const EdgeInsets.only(left: 16),
child: Column(
children: s.widgets,
),
),
));
}
});
return _Section(widgets: children, containsSelected: expanded);
}
Widget buildClassItem(ClassHeader header) => Column(
children: [
ClassListItem(
selectable: widget.selectable,
initiallySelected: selectedClasses.contains(header.id),
classHeader: header,
onSelected: (selected) {
if (selected) {
selectedClasses.add(header.id);
} else {
selectedClasses.remove(header.id);
}
setState(() {});
widget.onSelected(selected, header.id);
},
onTap: () => widget.onTap(header),
),
const Divider(),
],
);
@override
Widget build(BuildContext context) {
if (widget.classes != null) {
return ListView(
children: [
if (widget.sectioned)
Padding(
padding: const EdgeInsets.only(left: 10, right: 10, top: 10),
child: IconText(
icon: Icons.info_outlined,
text: '${S.current.infoSelect} ${S.current.infoClasses}.',
style: Theme.of(context).textTheme.bodyText1,
),
),
Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: widget.sectioned
? (buildSections(
context, classesBySection(widget.classes, context)))
.widgets
: widget.classes.map(buildClassItem).toList()),
),
],
);
} else {
return const Center(child: CircularProgressIndicator());
}
}
}
// Utility class to allow `buildSections` to return two values
class _Section {
_Section({this.widgets, this.containsSelected});
List<Widget> widgets;
bool containsSelected;
}
class ClassListItem extends StatefulWidget {
ClassListItem(
{Key key,
this.classHeader,
this.initiallySelected = false,
void Function(bool) onSelected,
this.selectable = false,
void Function() onTap,
this.hint})
: onSelected = onSelected ?? ((_) {}),
onTap = onTap ?? (() {}),
super(key: key);
final ClassHeader classHeader;
final bool initiallySelected;
final void Function(bool) onSelected;
final bool selectable;
final void Function() onTap;
final String hint;
@override
_ClassListItemState createState() =>
_ClassListItemState(selected: initiallySelected);
}
class _ClassListItemState extends State<ClassListItem> {
_ClassListItemState({this.selected});
bool selected;
@override
Widget build(BuildContext context) {
return ListTile(
leading: ClassIcon(
classHeader: widget.classHeader,
selected: widget.selectable && selected,
),
title: Text(
widget.classHeader.name,
style: widget.selectable
? (selected
? Theme.of(context)
.textTheme
.subtitle1
.copyWith(fontWeight: FontWeight.bold)
: Theme.of(context)
.textTheme
.subtitle1
.copyWith(color: Theme.of(context).disabledColor))
: Theme.of(context).textTheme.subtitle1,
),
subtitle: widget.hint != null ? Text(widget.hint) : null,
onTap: () => setState(() {
if (widget.selectable) {
selected = !selected;
widget.onSelected(selected);
}
widget.onTap();
}),
);
}
}