Skip to content
Snippets Groups Projects
Commit 91ebf683 authored by doodlezucc's avatar doodlezucc
Browse files

generalize cacheable timetable

parent 60b958be
No related branches found
No related tags found
No related merge requests found
......@@ -9,21 +9,27 @@ import '../untis/teacher.dart';
import '../untis/timetable.dart';
import 'cacheable.dart';
typedef TimetableOptions = (int classId, DateTime date);
typedef TimetableOptions = (ElementType type, int elementId, DateTime date);
class CacheableTimetable extends Cacheable<TimetableData> {
final DateTime date;
final int classId;
final ElementType type;
final int elementId;
CacheableTimetable(super.untis, {required this.date, required this.classId});
CacheableTimetable(
super.untis, {
required this.date,
required this.type,
required this.elementId,
});
@override
RequestOptions get request => (
'timetable/weekly/data',
'GET',
{
'elementType': '${ElementType.classGroup.id}',
'elementId': '$classId',
'elementType': '${type.id}',
'elementId': '$elementId',
'date': date.toUntisDate(),
'formatId': '2',
'filter.departmentId': '-1',
......
......@@ -9,6 +9,7 @@ import 'dialogs/error_dialog.dart';
import 'drawer/drawer.dart';
import 'timetable/timetable.dart';
import 'toolbar/toolbar.dart';
import 'untis/element_type.dart';
import 'untis/fetch.dart';
import 'untis/timetable.dart';
import 'user/save_state.dart';
......@@ -108,8 +109,14 @@ class _MyHomePageState extends State<MyHomePage> {
final profile = await appContext.profile.firstNotNull;
final selectedClasses = profile.selectedClasses;
final allTimetables = await Future.wait(selectedClasses.map((classid) {
return Future(() => untis.timetableOf(classid, date).fetched);
final allTimetables = await Future.wait(selectedClasses.map((classId) {
final cacheable = untis.timetableOf(
type: ElementType.classGroup,
elementId: classId,
date: date,
);
return Future(() => cacheable.fetched);
}));
setState(() {
......
......@@ -40,10 +40,16 @@ class UntisFetch {
final Map<TimetableOptions, CacheableTimetable> _timetables = {};
CacheableTimetable timetableOf(int classId, DateTime date) {
final TimetableOptions options = (classId, date);
CacheableTimetable timetableOf({
required ElementType type,
required int elementId,
required DateTime date,
}) {
final TimetableOptions options = (type, elementId, date);
return _timetables.putIfAbsent(
options, () => CacheableTimetable(this, date: date, classId: classId));
options,
() => CacheableTimetable(this,
date: date, type: type, elementId: elementId));
}
Uri _makeUri(RequestOptions options) {
......
import 'package:better_untis/untis/element_type.dart';
import 'package:better_untis/untis/fetch.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('Basic Untis call', () async {
final untis = UntisFetch();
final timetable = await untis.timetableOf(20000, DateTime.now()).fetched;
final cacheable = untis.timetableOf(
type: ElementType.classGroup,
elementId: 20000,
date: DateTime.now(),
);
final timetable = await cacheable.fetched;
print(timetable.lessons);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment