Basic Webuntis error handling
This commit is contained in:
		@@ -1,6 +1,8 @@
 | 
			
		||||
import 'dart:convert';
 | 
			
		||||
import 'dart:developer';
 | 
			
		||||
 | 
			
		||||
import 'package:localstore/localstore.dart';
 | 
			
		||||
import 'package:marianum_mobile/api/webuntis/webuntisError.dart';
 | 
			
		||||
 | 
			
		||||
abstract class RequestCache<T> {
 | 
			
		||||
  static const int cacheNothing = 0;
 | 
			
		||||
@@ -10,9 +12,12 @@ abstract class RequestCache<T> {
 | 
			
		||||
 | 
			
		||||
  int maxCacheTime;
 | 
			
		||||
  Function(T) onUpdate;
 | 
			
		||||
  Function(Exception) onError;
 | 
			
		||||
  bool? renew;
 | 
			
		||||
 | 
			
		||||
  RequestCache(this.maxCacheTime, this.onUpdate, {this.renew = false});
 | 
			
		||||
  RequestCache(this.maxCacheTime, this.onUpdate, {this.onError = ignore, this.renew = false});
 | 
			
		||||
 | 
			
		||||
  static void ignore(Exception e) {}
 | 
			
		||||
 | 
			
		||||
  void start(String file, String document) async {
 | 
			
		||||
    Map<String, dynamic>? tableData = await Localstore.instance.collection(file).doc(document).get();
 | 
			
		||||
@@ -24,13 +29,18 @@ abstract class RequestCache<T> {
 | 
			
		||||
      if(renew == null || !renew!) return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    T newValue = await onLoad();
 | 
			
		||||
    onUpdate(newValue);
 | 
			
		||||
    try {
 | 
			
		||||
      T newValue = await onLoad();
 | 
			
		||||
      onUpdate(newValue);
 | 
			
		||||
 | 
			
		||||
    Localstore.instance.collection(file).doc(document).set({
 | 
			
		||||
      "json": jsonEncode(newValue),
 | 
			
		||||
      "lastupdate": DateTime.now().millisecondsSinceEpoch
 | 
			
		||||
    });
 | 
			
		||||
      Localstore.instance.collection(file).doc(document).set({
 | 
			
		||||
        "json": jsonEncode(newValue),
 | 
			
		||||
        "lastupdate": DateTime.now().millisecondsSinceEpoch
 | 
			
		||||
      });
 | 
			
		||||
    } on WebuntisError catch(e) {
 | 
			
		||||
      log("ERROR CATCHED");
 | 
			
		||||
      onError(e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  T onLocalData(String json);
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ class GetTimetableCache extends RequestCache<GetTimetableResponse> {
 | 
			
		||||
  int startdate;
 | 
			
		||||
  int enddate;
 | 
			
		||||
 | 
			
		||||
  GetTimetableCache({required onUpdate, required this.startdate, required this.enddate}) : super(RequestCache.cacheMinute, onUpdate) {
 | 
			
		||||
  GetTimetableCache({required onUpdate, onError, required this.startdate, required this.enddate}) : super(RequestCache.cacheMinute, onUpdate, onError: onError) {
 | 
			
		||||
    start("MarianumMobile", "wu-timetable-$startdate-$enddate");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
class WebuntisError {
 | 
			
		||||
class WebuntisError implements Exception {
 | 
			
		||||
  String message;
 | 
			
		||||
  int code;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,5 @@
 | 
			
		||||
import 'dart:developer';
 | 
			
		||||
 | 
			
		||||
import 'package:flutter/cupertino.dart';
 | 
			
		||||
import 'package:localstore/localstore.dart';
 | 
			
		||||
 | 
			
		||||
@@ -13,6 +15,12 @@ abstract class DataHolder extends ChangeNotifier {
 | 
			
		||||
  List<ApiResponse?> properties();
 | 
			
		||||
 | 
			
		||||
  bool primaryLoading() {
 | 
			
		||||
    return properties().where((element) => element != null).isEmpty;
 | 
			
		||||
    log(properties().toString());
 | 
			
		||||
 | 
			
		||||
    for(ApiResponse? element in properties()) {
 | 
			
		||||
      if(element == null) return true;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
    //return properties().where((element) => element != null).isEmpty;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,6 @@
 | 
			
		||||
 | 
			
		||||
import 'dart:developer';
 | 
			
		||||
 | 
			
		||||
import 'package:intl/intl.dart';
 | 
			
		||||
import 'package:marianum_mobile/api/apiResponse.dart';
 | 
			
		||||
import 'package:marianum_mobile/api/webuntis/queries/getHolidays/getHolidaysCache.dart';
 | 
			
		||||
@@ -11,6 +13,7 @@ import 'package:marianum_mobile/data/dataHolder.dart';
 | 
			
		||||
 | 
			
		||||
import '../../api/webuntis/queries/getTimetable/getTimetableCache.dart';
 | 
			
		||||
import '../../api/webuntis/queries/getTimetable/getTimetableResponse.dart';
 | 
			
		||||
import '../../api/webuntis/webuntisError.dart';
 | 
			
		||||
 | 
			
		||||
extension DateTimeExtension on DateTime {
 | 
			
		||||
  DateTime next(int day) {
 | 
			
		||||
@@ -40,11 +43,9 @@ class TimetableProps extends DataHolder {
 | 
			
		||||
  GetHolidaysResponse? _getHolidaysResponse;
 | 
			
		||||
  GetHolidaysResponse get getHolidaysResponse => _getHolidaysResponse!;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  TimetableProps() {
 | 
			
		||||
    //nearest();
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
  WebuntisError? error;
 | 
			
		||||
  WebuntisError? get getError => error;
 | 
			
		||||
  bool get hasError => error != null;
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  List<ApiResponse?> properties() {
 | 
			
		||||
@@ -54,12 +55,17 @@ class TimetableProps extends DataHolder {
 | 
			
		||||
  @override
 | 
			
		||||
  void run() {
 | 
			
		||||
    GetTimetableCache(
 | 
			
		||||
        startdate: int.parse(DateFormat("yyyyMMdd").format(startDate)),
 | 
			
		||||
        enddate: int.parse(DateFormat("yyyyMMdd").format(endDate)),
 | 
			
		||||
        onUpdate: (GetTimetableResponse data) => {
 | 
			
		||||
          _getTimetableResponse = data,
 | 
			
		||||
          notifyListeners(),
 | 
			
		||||
        }
 | 
			
		||||
      startdate: int.parse(DateFormat("yyyyMMdd").format(startDate)),
 | 
			
		||||
      enddate: int.parse(DateFormat("yyyyMMdd").format(endDate)),
 | 
			
		||||
      onUpdate: (GetTimetableResponse data) => {
 | 
			
		||||
        _getTimetableResponse = data,
 | 
			
		||||
        notifyListeners(),
 | 
			
		||||
      },
 | 
			
		||||
      onError: (Exception e) => {
 | 
			
		||||
        error = e as WebuntisError?,
 | 
			
		||||
        log("hello there"),
 | 
			
		||||
        notifyListeners(),
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    GetRoomsCache(
 | 
			
		||||
@@ -106,8 +112,17 @@ class TimetableProps extends DataHolder {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void updateWeek() {
 | 
			
		||||
    properties().forEach((element) => element = null);
 | 
			
		||||
    error = null;
 | 
			
		||||
    notifyListeners();
 | 
			
		||||
    startDate = getDate(_queryWeek.subtract(Duration(days: _queryWeek.weekday - 1)));
 | 
			
		||||
    endDate = getDate(_queryWeek.add(Duration(days: DateTime.daysPerWeek - _queryWeek.weekday)));
 | 
			
		||||
    run();
 | 
			
		||||
    try {
 | 
			
		||||
      run();
 | 
			
		||||
    } on WebuntisError catch(e) {
 | 
			
		||||
      error = e;
 | 
			
		||||
      notifyListeners();
 | 
			
		||||
      log(e.toString());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:marianum_mobile/screen/pages/more/roomplan/roomplan.dart';
 | 
			
		||||
import 'package:marianum_mobile/screen/settings/settings.dart';
 | 
			
		||||
import 'package:persistent_bottom_nav_bar/persistent_tab_view.dart';
 | 
			
		||||
 | 
			
		||||
import '../../../widget/ListItem.dart';
 | 
			
		||||
import 'message/message.dart';
 | 
			
		||||
@@ -10,11 +12,19 @@ class Overhang extends StatelessWidget {
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    return ListView(
 | 
			
		||||
      children: const [
 | 
			
		||||
        ListItemNavigator(icon: Icons.newspaper, text: "Marianum Message", target: Message()),
 | 
			
		||||
        ListItemNavigator(icon: Icons.room, text: "Raumplan", target: Roomplan()),
 | 
			
		||||
      ],
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
      appBar: AppBar(
 | 
			
		||||
        title: const Text("Mehr"),
 | 
			
		||||
        actions: [
 | 
			
		||||
          IconButton(onPressed: () => PersistentNavBarNavigator.pushNewScreen(context, screen: const Settings(), withNavBar: false), icon: const Icon(Icons.settings))
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
      body: ListView(
 | 
			
		||||
        children: const [
 | 
			
		||||
          ListItemNavigator(icon: Icons.newspaper, text: "Marianum Message", target: Message()),
 | 
			
		||||
          ListItemNavigator(icon: Icons.room, text: "Raumplan", target: Roomplan()),
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,10 @@
 | 
			
		||||
 | 
			
		||||
import 'dart:developer';
 | 
			
		||||
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:marianum_mobile/data/timetable/timetableProps.dart';
 | 
			
		||||
import 'package:marianum_mobile/screen/pages/timetable/weekView.dart';
 | 
			
		||||
import 'package:marianum_mobile/widget/errorView.dart';
 | 
			
		||||
import 'package:provider/provider.dart';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -26,83 +29,30 @@ class _TimetableState extends State<Timetable> {
 | 
			
		||||
  
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    TimetableProps timetable = Provider.of<TimetableProps>(context, listen: false);
 | 
			
		||||
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
      appBar: AppBar(
 | 
			
		||||
        title: const Text("Vertretungsplan"),
 | 
			
		||||
        actions: [
 | 
			
		||||
          IconButton(onPressed: () => timetable.switchWeek(previous: true), icon: const Icon(Icons.chevron_left)),
 | 
			
		||||
          IconButton(onPressed: () => timetable.nearest(), icon: const Icon(Icons.home)),
 | 
			
		||||
          IconButton(onPressed: () => timetable.switchWeek(), icon: const Icon(Icons.chevron_right))
 | 
			
		||||
        ],
 | 
			
		||||
      ),
 | 
			
		||||
      body: Consumer<TimetableProps>(
 | 
			
		||||
        builder: (context, value, child) {
 | 
			
		||||
 | 
			
		||||
          if(value.hasError) {
 | 
			
		||||
            return ErrorView(icon: Icons.error, text: value.error?.message ?? "Unbekannt?");
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          if(value.primaryLoading()) {
 | 
			
		||||
            log("LOADING");
 | 
			
		||||
            return const Center(child: CircularProgressIndicator());
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          TimetableProps timetable = Provider.of<TimetableProps>(context, listen: false);
 | 
			
		||||
          return Column(
 | 
			
		||||
            children: [
 | 
			
		||||
              Expanded(
 | 
			
		||||
                child: GestureDetector(
 | 
			
		||||
                  child: WeekView(value),
 | 
			
		||||
                  onHorizontalDragUpdate: (details) {
 | 
			
		||||
                    if(!draggable) return;
 | 
			
		||||
                    if(details.delta.dx > 5) {
 | 
			
		||||
                      draggable = false;
 | 
			
		||||
                      timetable.switchWeek(previous: true);
 | 
			
		||||
                    } else if(details.delta.dx < 5) {
 | 
			
		||||
                      draggable = false;
 | 
			
		||||
                      timetable.switchWeek();
 | 
			
		||||
                    }
 | 
			
		||||
                  },
 | 
			
		||||
                  onHorizontalDragEnd: (details) {
 | 
			
		||||
                    draggable = true;
 | 
			
		||||
                  },
 | 
			
		||||
                ),
 | 
			
		||||
              ),
 | 
			
		||||
 | 
			
		||||
              // Flexible(
 | 
			
		||||
              //   child:
 | 
			
		||||
              // ),
 | 
			
		||||
 | 
			
		||||
              Visibility(
 | 
			
		||||
                visible: false,
 | 
			
		||||
                child: Container(
 | 
			
		||||
                  padding: const EdgeInsets.only(top: 5, bottom: 5),
 | 
			
		||||
                  decoration: BoxDecoration(
 | 
			
		||||
                      border: Border(
 | 
			
		||||
                          top: BorderSide(width: 2, color: Theme.of(context).disabledColor)
 | 
			
		||||
                      )
 | 
			
		||||
                  ),
 | 
			
		||||
                  child: Row(
 | 
			
		||||
                    mainAxisAlignment: MainAxisAlignment.center,
 | 
			
		||||
 | 
			
		||||
                    children: [
 | 
			
		||||
                      IconButton(
 | 
			
		||||
                        onPressed: () => timetable.switchWeek(previous: true),
 | 
			
		||||
                        icon: const Icon(Icons.navigate_before_sharp),
 | 
			
		||||
                        color: Theme.of(context).primaryColor,
 | 
			
		||||
                        iconSize: 30,
 | 
			
		||||
                      ),
 | 
			
		||||
                      Row(
 | 
			
		||||
                        children: [
 | 
			
		||||
                          IconButton(
 | 
			
		||||
                            onPressed: () => timetable.nearest(),
 | 
			
		||||
                            icon: const Icon(Icons.home),
 | 
			
		||||
                            color: Theme.of(context).primaryColor,
 | 
			
		||||
                            iconSize: 30,
 | 
			
		||||
                          ),
 | 
			
		||||
                        ],
 | 
			
		||||
                      ),
 | 
			
		||||
                      IconButton(
 | 
			
		||||
                        onPressed: () => timetable.switchWeek(),
 | 
			
		||||
                        icon: const Icon(Icons.navigate_next_sharp),
 | 
			
		||||
                        color: Theme.of(context).primaryColor,
 | 
			
		||||
                        iconSize: 30,
 | 
			
		||||
                      )
 | 
			
		||||
                    ],
 | 
			
		||||
                  ),
 | 
			
		||||
                ),
 | 
			
		||||
              )
 | 
			
		||||
            ],
 | 
			
		||||
          );
 | 
			
		||||
          return WeekView(value);
 | 
			
		||||
        },
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
 
 | 
			
		||||
@@ -109,6 +109,8 @@ class _WeekViewState extends State<WeekView> {
 | 
			
		||||
  List<LaneEvents> _buildLaneEvents(TimetableProps data) {
 | 
			
		||||
    List<LaneEvents> laneEvents = List<LaneEvents>.empty(growable: true);
 | 
			
		||||
 | 
			
		||||
    if(data.primaryLoading()) throw UnimplementedError();
 | 
			
		||||
 | 
			
		||||
    GetTimetableResponse timetable = data.getTimetableResponse;
 | 
			
		||||
    GetRoomsResponse rooms = data.getRoomsResponse;
 | 
			
		||||
    GetSubjectsResponse subjects = data.getSubjectsResponse;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user