This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/timetable.dart
Denys Konovalov e86c1bad1d = 0.8.0-dev =
- code cleanup
- caching
- black-/whitelist
- sz/news fixes
- added settings options
...
2021-12-13 13:39:06 +01:00

231 lines
7.4 KiB
Dart

import 'package:meincantor/presets/teachers.dart';
import 'package:meincantor/presets/subjects.dart';
import 'package:meincantor/presets/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'Settings/Pages/plan_settings.dart';
class ClassTimetableBuilder {
final RefreshIndicator view;
ClassTimetableBuilder({required this.view});
factory ClassTimetableBuilder.buildView(
Map<String, dynamic> json, BuildContext context) {
List<Widget> list = LessonsListBuilder.buildList(json).lessons;
String info = TimetableInfo.fromJson(json).info;
if (info.isNotEmpty) {
list.insert(
0,
ListTile(
title: const Text("Informationen"),
leading: const Icon(MdiIcons.information),
onTap: () {
showModalBottomSheet<void>(
isScrollControlled: true,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
),
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 400,
child: ListView(
children: <Widget>[
ListTile(
title: const Text("Informationen",
style: TextStyle(fontWeight: FontWeight.bold)),
leading: const Icon(Icons.arrow_back),
onTap: () {
Navigator.of(context).pop();
},
),
Padding(
padding: const EdgeInsets.all(20),
child: Text(info),
),
],
),
);
},
);
},
));
}
return ClassTimetableBuilder(
view: RefreshIndicator(
onRefresh: () {
return Future.delayed(const Duration(seconds: 1));
},
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: list,
),
));
}
}
Future<Color> buildLessonColor(String lesson) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey("color$lesson")) {
Color lessonPresetColor = colors[lesson] ?? Colors.grey[700];
prefs.setInt("color$lesson", lessonPresetColor.value);
}
Color lessonColor = Color(prefs.getInt("color$lesson")!);
return lessonColor;
}
class LessonsListBuilder {
final List<Widget> lessons;
LessonsListBuilder({required this.lessons});
factory LessonsListBuilder.buildList(Map<String, dynamic> json,
{int count = 0}) {
List<Widget> children = [];
for (var element in ClassTimetable.fromJson(json).timetable) {
List<Widget> cardChildren = [];
if (element.count.toString() == count.toString() || count == 0) {
cardChildren.add(ListTile(
title: Text(element.count.toString() + '.' + ' ' + element.name,
style: TextStyle(color: element.fontColor)),
subtitle: Row(children: [
Icon(CupertinoIcons.person, color: element.fontColor),
const SizedBox(width: 5),
Text(element.teacher, style: TextStyle(color: element.fontColor)),
const Spacer(),
Icon(CupertinoIcons.home, color: element.fontColor),
const SizedBox(width: 5),
Text(element.room, style: TextStyle(color: element.fontColor))
]),
leading: Icon(CupertinoIcons.time, color: element.fontColor)));
if (element.info != '') {
cardChildren.add(ListTile(
title: Text(element.info,
style: TextStyle(color: element.fontColor))));
}
Widget card = FutureBuilder(
future: buildBlacklist(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (!((snapshot.data as List<dynamic>).contains(element.id))) {
return FutureBuilder(
future: element.color,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
color: snapshot.data as Color,
child: Column(
children: cardChildren,
));
} else {
return (const Center(
child: CircularProgressIndicator()));
}
},
);
} else {
return const SizedBox.shrink();
}
} else {
return const LinearProgressIndicator();
}
});
children.add(card);
}
}
return LessonsListBuilder(lessons: children);
}
}
class TimetableInfo {
final String info;
TimetableInfo({required this.info});
factory TimetableInfo.fromJson(Map<String, dynamic> json) {
return TimetableInfo(info: json['info']);
}
}
class ClassTimetable {
final List timetable;
ClassTimetable({required this.timetable});
factory ClassTimetable.fromJson(Map<String, dynamic> json) {
List<TimetableLesson> lessons = [];
json['courses'].forEach((value) {
String subject;
String teacher;
String room;
String info;
Color fontColor;
if (value['Fa'].runtimeType != String) {
subject = value['Fa']['#text'];
} else {
subject = value['Fa'];
}
if (value['Le'].runtimeType != String) {
teacher = value['Le']['#text'];
} else {
teacher = value['Le'];
}
if (value['Ra'].runtimeType != String && value['Ra'].runtimeType != int) {
if (value['Ra']['#text'] == '&nbsp;' || value['Ra']['#text'] == null) {
room = '';
} else {
room = value['Ra']['#text'].toString();
}
} else if (value['Ra'] == '&nbsp;') {
room = '';
} else {
room = value['Ra'].toString();
}
if (subject == '---') {
fontColor = Colors.red;
} else {
fontColor = Colors.white;
}
if (value['If'].runtimeType != String) {
info = '';
} else {
info = value['If'];
}
Future<Color> lessonColor = buildLessonColor(subject);
lessons.add(TimetableLesson(
value['St'],
value["Nr"],
subjects[subject] ?? subject.toString(),
teachers[teacher] ?? teacher.toString(),
room.toString(),
value['If'].toString(),
lessonColor,
fontColor,
info));
});
return ClassTimetable(timetable: lessons);
}
}
class TimetableLesson {
final int count;
final int id;
final String name;
final String teacher;
final String room;
final String comment;
final Future<Color> color;
final Color fontColor;
final String info;
const TimetableLesson(this.count, this.id, this.name, this.teacher, this.room,
this.comment, this.color, this.fontColor, this.info);
}