Skip to content

Commit

Permalink
[timetable] [import] failure description for network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
liplum committed Sep 3, 2024
1 parent 85621dc commit 89f5207
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## v2.5.2
- Remember selected semester info only in current session.
- Introduced Mimir User agent.
- a convenient theme mode switcher in the timetable palette editor was added.
- A convenient theme mode switcher in the timetable palette editor was added.
- Updated the built-in timetable palettes with inverse text color.
- A new deep link handler, Go route, to navigate to an in-app route.
Usage: `sit-life://go/any-route`. And in FeaturedMarkdownWidget, it has a shorthand, `/any-route`.
Expand Down
5 changes: 3 additions & 2 deletions assets/l10n/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ timetable:
fromFile: From File
fromFileBtn: From file
tryImportBtn: Try import
connectivityCheckerDesc: You need to check the access to Student registration system before importing a timetable.
connectivityCheckerDesc: You need to check the access to student registration system before importing a timetable.
selectSemesterTip: Please select a semester first
endTip: Please enter the info
failed: Import Failed
failedDesc: Can't import the timetable, please try again
networkFailedDesc: Can't access the student registration system, please check if you've connect to the campus network, then try again later
failedDesc: Can't import the timetable, please try again later
failedTip: Failed to import, please try again
importing: Trying to fetch the timetable...
timetableInfo: Timetable Info
Expand Down
1 change: 1 addition & 0 deletions assets/l10n/zh-Hans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ timetable:
selectSemesterTip: 请先选择学年与学期
endTip: 请完善课程表的信息。
failed: 导入失败
networkFailedDesc: 无法访问教务系统,请检查是否连接校园网,并稍后再试
failedDesc: 无法导入这个课程表, 请稍后再试
failedTip: 导入失败, 请稍后再试
importing: 正在尝试获取课程表的数据……
Expand Down
5 changes: 3 additions & 2 deletions assets/l10n/zh-Hant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ timetable:
selectSemesterTip: 請先選擇學年與學期
endTip: 請補充課程表的資訊。
failed: 匯入失敗
failedDesc: 無法匯入課程表,請之後再試。
failedTip: 匯入失敗,請之後再試。
networkFailedDesc: 無法訪問教務系統,請檢查是否連線校園網路,請之後再試
failedDesc: 無法匯入課程表,請之後再試
failedTip: 匯入失敗,請之後再試
importing: 正在擷取課程表數據……
timetableInfo: 課程表資訊
defaultName: "{semester} {yearStart} 課程表"
Expand Down
16 changes: 14 additions & 2 deletions lib/network/widgets/checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class ConnectivityChecker extends StatefulWidget {
final Duration? autoStartDelay;
final WhereToCheck where;

/// in parallel
final int checkTaskNumber;

/// {@template mimir.network.widgets.checker}
/// Whether it's connected will be turned.
/// Throw any error if connection fails.
Expand All @@ -45,7 +48,8 @@ class ConnectivityChecker extends StatefulWidget {
required this.check,
this.autoStartDelay,
required this.where,
});
this.checkTaskNumber = 1,
}) : assert(checkTaskNumber >= 1);

@override
State<ConnectivityChecker> createState() => _ConnectivityCheckerState();
Expand Down Expand Up @@ -118,7 +122,7 @@ class _ConnectivityCheckerState extends State<ConnectivityChecker> {
status = _Status.connecting;
});
try {
final connected = await widget.check();
final connected = await check();
if (!mounted) return;
setState(() {
if (connected) {
Expand All @@ -136,6 +140,13 @@ class _ConnectivityCheckerState extends State<ConnectivityChecker> {
}
}

Future<bool> check() async {
final task = await Future.any(List.generate(widget.checkTaskNumber, (i) => widget.check()));
return task == true;
// final task = await Future.wait(List.generate(widget.checkTaskNumber, (i) => widget.check()));
// return task.contains(true);
}

Widget buildStatus(BuildContext ctx) {
// TODO: it's student registration system
final tip = switch (status) {
Expand Down Expand Up @@ -281,6 +292,7 @@ class ConnectivityCheckerSheet extends StatelessWidget {
initialDesc: desc,
check: check,
where: where,
checkTaskNumber: 3,
onConnected: () {
context.pop(true);
},
Expand Down
2 changes: 2 additions & 0 deletions lib/timetable/i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ class _Import {

String get failedDesc => "$ns.failedDesc".tr();

String get networkFailedDesc => "$ns.networkFailedDesc".tr();

String get failedTip => "$ns.failedTip".tr();

String get tryImportBtn => "$ns.tryImportBtn".tr();
Expand Down
31 changes: 16 additions & 15 deletions lib/timetable/page/import.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -16,6 +17,7 @@ import 'package:mimir/school/widgets/semester.dart';
import 'package:mimir/settings/meta.dart';
import 'package:mimir/settings/settings.dart';
import 'package:mimir/timetable/utils.dart';
import 'package:mimir/utils/error.dart';
import 'package:rettulf/rettulf.dart';

import '../i18n.dart';
Expand Down Expand Up @@ -79,11 +81,13 @@ class _ImportTimetablePageState extends ConsumerState<ImportTimetablePage> {

Future<bool> checkConnectivity() async {
if (connected) return true;
final result = await context.showSheet<bool>((ctx) => ConnectivityCheckerSheet(
desc: i18n.import.connectivityCheckerDesc,
check: TimetableInit.service.checkConnectivity,
where: WhereToCheck.studentReg,
));
final result = await context.showSheet<bool>(
(ctx) => ConnectivityCheckerSheet(
desc: i18n.import.connectivityCheckerDesc,
check: TimetableInit.service.checkConnectivity,
where: WhereToCheck.studentReg,
),
);
connected = result == true;
return connected;
}
Expand Down Expand Up @@ -189,20 +193,17 @@ class _ImportTimetablePageState extends ConsumerState<ImportTimetablePage> {
);
if (!mounted) return;
context.pop(newTimetable);
} catch (e, stackTrace) {
if (e is ParallelWaitError) {
final inner = e.errors.$1 as AsyncError;
debugPrint(inner.toString());
debugPrintStack(stackTrace: inner.stackTrace);
} else {
debugPrint(e.toString());
debugPrintStack(stackTrace: stackTrace);
}
} catch (error, stackTrace) {
debugPrintError(error, stackTrace);
setState(() {
_status = ImportStatus.failed;
});
if (!mounted) return;
await context.showTip(title: i18n.import.failed, desc: i18n.import.failedDesc, primary: i18n.ok);
await context.showTip(
title: i18n.import.failed,
desc: error is DioException ? i18n.import.networkFailedDesc : i18n.import.failedDesc,
primary: i18n.ok,
);
} finally {
if (_status == ImportStatus.importing) {
setState(() {
Expand Down

0 comments on commit 89f5207

Please sign in to comment.