Skip to content

Commit 399ee37

Browse files
Add offline dialog (#954)
* feat: show error dialog when attempting to download resources while offline * formatting * use state.mounted instead of context.mounted to check if the screen is in the widget tree * Reset the resources UI without clearing cache * chore: removed unused import * Catch any exception instead of SocketException only * Format Dart files --------- Co-authored-by: Anh <[email protected]>
1 parent 390f130 commit 399ee37

File tree

4 files changed

+62
-44
lines changed

4 files changed

+62
-44
lines changed

flutter/lib/benchmark/state.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,21 @@ class BenchmarkState extends ChangeNotifier {
143143
modes: [taskRunner.perfMode, taskRunner.accuracyMode],
144144
benchmarks: benchmarks,
145145
);
146-
await resourceManager.handleResources(
147-
resources,
148-
needToPurgeCache,
149-
downloadMissing,
150-
);
151-
print('Finished loading resources with downloadMissing=$downloadMissing');
152-
error = null;
153-
stackTrace = null;
154-
taskConfigFailedToLoad = false;
146+
try {
147+
await resourceManager.handleResources(
148+
resources,
149+
needToPurgeCache,
150+
downloadMissing,
151+
);
152+
print('Finished loading resources with downloadMissing=$downloadMissing');
153+
error = null;
154+
stackTrace = null;
155+
taskConfigFailedToLoad = false;
156+
} catch (e, s) {
157+
print('Could not load resources due to error: $e');
158+
error = e;
159+
stackTrace = s;
160+
}
155161
await Wakelock.disable();
156162
}
157163

flutter/lib/l10n/app_en.arb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
"dialogContentChecksumError": "The following files failed checksum validation:",
9696
"dialogContentNoSelectedBenchmarkError": "Please select at least one benchmark.",
9797

98-
9998
"benchModePerformanceOnly": "Performance Only",
10099
"benchModeAccuracyOnly": "Accuracy Only",
101100
"benchModeSubmissionRun": "Submission Run",

flutter/lib/resources/resource_manager.dart

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -100,42 +100,47 @@ class ResourceManager {
100100
_loadingProgress = 0.001;
101101
_done = false;
102102
_onUpdate();
103-
104-
var internetResources = <Resource>[];
105-
for (final resource in resources) {
106-
if (resource.path.startsWith(_dataPrefix)) continue;
107-
if (isInternetResource(resource.path)) {
108-
internetResources.add(resource);
109-
continue;
103+
try {
104+
var internetResources = <Resource>[];
105+
for (final resource in resources) {
106+
if (resource.path.startsWith(_dataPrefix)) continue;
107+
if (isInternetResource(resource.path)) {
108+
internetResources.add(resource);
109+
continue;
110+
}
111+
throw 'forbidden path: ${resource.path} (only http://, https:// and local:// resources are allowed)';
110112
}
111-
throw 'forbidden path: ${resource.path} (only http://, https:// and local:// resources are allowed)';
112-
}
113113

114-
final internetPaths = internetResources.map((e) => e.path).toList();
115-
await cacheManager.cache(
116-
internetPaths,
117-
(double currentProgress, String currentPath) {
118-
_loadingProgress = currentProgress;
119-
_loadingPath = currentPath;
120-
_onUpdate();
121-
},
122-
purgeOldCache,
123-
downloadMissing,
124-
);
125-
126-
final checksumFailed = await validateResourcesChecksum(resources);
127-
if (checksumFailed.isNotEmpty) {
128-
final mismatchedPaths = checksumFailed.map((e) => '\n${e.path}').join();
129-
throw 'Checksum validation failed for: $mismatchedPaths';
130-
}
114+
final internetPaths = internetResources.map((e) => e.path).toList();
115+
try {
116+
await cacheManager.cache(
117+
internetPaths,
118+
(double currentProgress, String currentPath) {
119+
_loadingProgress = currentProgress;
120+
_loadingPath = currentPath;
121+
_onUpdate();
122+
},
123+
purgeOldCache,
124+
downloadMissing,
125+
);
126+
} on SocketException {
127+
throw 'A network error has occurred. Please make sure you are connected to the internet.';
128+
}
131129

132-
// delete downloaded archives to free up disk space
133-
await cacheManager.deleteArchives(internetPaths);
130+
final checksumFailed = await validateResourcesChecksum(resources);
131+
if (checksumFailed.isNotEmpty) {
132+
final mismatchedPaths = checksumFailed.map((e) => '\n${e.path}').join();
133+
throw 'Checksum validation failed for: $mismatchedPaths';
134+
}
134135

135-
_loadingPath = '';
136-
_loadingProgress = 1.0;
137-
_done = true;
138-
_onUpdate();
136+
// delete downloaded archives to free up disk space
137+
await cacheManager.deleteArchives(internetPaths);
138+
} finally {
139+
_loadingPath = '';
140+
_loadingProgress = 1.0;
141+
_done = true;
142+
_onUpdate();
143+
}
139144
}
140145

141146
static Future<String> getApplicationDirectory() async {

flutter/lib/ui/settings/resources_screen.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:mlperfbench/benchmark/state.dart';
99
import 'package:mlperfbench/localizations/app_localizations.dart';
1010
import 'package:mlperfbench/store.dart';
1111
import 'package:mlperfbench/ui/confirm_dialog.dart';
12+
import 'package:mlperfbench/ui/error_dialog.dart';
1213

1314
class ResourcesScreen extends StatefulWidget {
1415
const ResourcesScreen({super.key});
@@ -169,8 +170,15 @@ class _ResourcesScreen extends State<ResourcesScreen> {
169170
return AbsorbPointer(
170171
absorbing: downloading,
171172
child: ElevatedButton(
172-
onPressed: () {
173-
state.loadResources(downloadMissing: true);
173+
onPressed: () async {
174+
await state.loadResources(downloadMissing: true);
175+
if (state.error != null) {
176+
if (!mounted) return;
177+
await showErrorDialog(context, <String>[state.error.toString()]);
178+
// Reset both the error and stacktrace for further operation
179+
state.error = null;
180+
state.stackTrace = null;
181+
}
174182
},
175183
style: ElevatedButton.styleFrom(
176184
backgroundColor: downloading ? Colors.grey : Colors.blue),

0 commit comments

Comments
 (0)