simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -23,72 +23,31 @@ import '../../../../../api/mhsl/custom_timetable_event/get/get_custom_timetable_
|
||||
import '../../../../../api/request_cache.dart';
|
||||
|
||||
/// Pulls the timetable from the Marianum-Connect mobile API. Each endpoint is
|
||||
/// its own HTTP call; this provider exposes the lazy futures so the bloc can
|
||||
/// chain them without seeing the dio layer.
|
||||
/// its own HTTP call; this provider exposes the futures so the bloc can chain
|
||||
/// them without seeing the dio layer.
|
||||
class TimetableDataProvider {
|
||||
Future<TimetableGetWeekResponse> getWeek(
|
||||
DateTime startDate,
|
||||
DateTime endDate, {
|
||||
void Function(Object)? onError,
|
||||
bool renew = false,
|
||||
}) async {
|
||||
try {
|
||||
return await TimetableGetWeek().run(from: startDate, until: endDate);
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
DateTime endDate,
|
||||
) async => TimetableGetWeek().run(from: startDate, until: endDate);
|
||||
|
||||
Future<TimetableGetRoomsResponse> getRooms({
|
||||
void Function(Object)? onError,
|
||||
bool renew = false,
|
||||
}) async {
|
||||
try {
|
||||
return await TimetableGetRooms().run();
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}) => _reportingErrors(TimetableGetRooms().run, onError);
|
||||
|
||||
Future<TimetableGetSubjectsResponse> getSubjects({
|
||||
void Function(Object)? onError,
|
||||
bool renew = false,
|
||||
}) async {
|
||||
try {
|
||||
return await TimetableGetSubjects().run();
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}) => _reportingErrors(TimetableGetSubjects().run, onError);
|
||||
|
||||
Future<TimetableGetHolidaysResponse> getSchoolHolidays({
|
||||
void Function(Object)? onError,
|
||||
bool renew = false,
|
||||
}) async {
|
||||
try {
|
||||
return await TimetableGetHolidays().run();
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}) => _reportingErrors(TimetableGetHolidays().run, onError);
|
||||
|
||||
Future<TimetableGetSchoolyearResponse> getCurrentSchoolyear({
|
||||
void Function(Object)? onError,
|
||||
bool renew = false,
|
||||
}) async {
|
||||
try {
|
||||
return await TimetableGetSchoolyear().run();
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}) => _reportingErrors(TimetableGetSchoolyear().run, onError);
|
||||
|
||||
Future<TimetableGetTimegridResponse> getTimegrid({bool renew = false}) =>
|
||||
Future<TimetableGetTimegridResponse> getTimegrid() =>
|
||||
TimetableGetTimegrid().run();
|
||||
|
||||
Future<GetCustomTimetableEventResponse> getCustomEvents({
|
||||
@@ -131,4 +90,18 @@ class TimetableDataProvider {
|
||||
if (DemoMode.active) return Future.value();
|
||||
return TimetableSubjectColorRemove().run(subjectShort);
|
||||
}
|
||||
|
||||
// Reports the original error before rethrowing: callers await several of
|
||||
// these together, and a parallel wait only surfaces a combined error.
|
||||
static Future<T> _reportingErrors<T>(
|
||||
Future<T> Function() request,
|
||||
void Function(Object)? onError,
|
||||
) async {
|
||||
try {
|
||||
return await request();
|
||||
} catch (e) {
|
||||
onError?.call(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user