Skip to content

Commit

Permalink
Add offline dialog (#954)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
farook-edev and anhappdev authored Feb 5, 2025
1 parent 390f130 commit 399ee37
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
24 changes: 15 additions & 9 deletions flutter/lib/benchmark/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,21 @@ class BenchmarkState extends ChangeNotifier {
modes: [taskRunner.perfMode, taskRunner.accuracyMode],
benchmarks: benchmarks,
);
await resourceManager.handleResources(
resources,
needToPurgeCache,
downloadMissing,
);
print('Finished loading resources with downloadMissing=$downloadMissing');
error = null;
stackTrace = null;
taskConfigFailedToLoad = false;
try {
await resourceManager.handleResources(
resources,
needToPurgeCache,
downloadMissing,
);
print('Finished loading resources with downloadMissing=$downloadMissing');
error = null;
stackTrace = null;
taskConfigFailedToLoad = false;
} catch (e, s) {
print('Could not load resources due to error: $e');
error = e;
stackTrace = s;
}
await Wakelock.disable();
}

Expand Down
1 change: 0 additions & 1 deletion flutter/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"dialogContentChecksumError": "The following files failed checksum validation:",
"dialogContentNoSelectedBenchmarkError": "Please select at least one benchmark.",


"benchModePerformanceOnly": "Performance Only",
"benchModeAccuracyOnly": "Accuracy Only",
"benchModeSubmissionRun": "Submission Run",
Expand Down
69 changes: 37 additions & 32 deletions flutter/lib/resources/resource_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,47 @@ class ResourceManager {
_loadingProgress = 0.001;
_done = false;
_onUpdate();

var internetResources = <Resource>[];
for (final resource in resources) {
if (resource.path.startsWith(_dataPrefix)) continue;
if (isInternetResource(resource.path)) {
internetResources.add(resource);
continue;
try {
var internetResources = <Resource>[];
for (final resource in resources) {
if (resource.path.startsWith(_dataPrefix)) continue;
if (isInternetResource(resource.path)) {
internetResources.add(resource);
continue;
}
throw 'forbidden path: ${resource.path} (only http://, https:// and local:// resources are allowed)';
}
throw 'forbidden path: ${resource.path} (only http://, https:// and local:// resources are allowed)';
}

final internetPaths = internetResources.map((e) => e.path).toList();
await cacheManager.cache(
internetPaths,
(double currentProgress, String currentPath) {
_loadingProgress = currentProgress;
_loadingPath = currentPath;
_onUpdate();
},
purgeOldCache,
downloadMissing,
);

final checksumFailed = await validateResourcesChecksum(resources);
if (checksumFailed.isNotEmpty) {
final mismatchedPaths = checksumFailed.map((e) => '\n${e.path}').join();
throw 'Checksum validation failed for: $mismatchedPaths';
}
final internetPaths = internetResources.map((e) => e.path).toList();
try {
await cacheManager.cache(
internetPaths,
(double currentProgress, String currentPath) {
_loadingProgress = currentProgress;
_loadingPath = currentPath;
_onUpdate();
},
purgeOldCache,
downloadMissing,
);
} on SocketException {
throw 'A network error has occurred. Please make sure you are connected to the internet.';
}

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

_loadingPath = '';
_loadingProgress = 1.0;
_done = true;
_onUpdate();
// delete downloaded archives to free up disk space
await cacheManager.deleteArchives(internetPaths);
} finally {
_loadingPath = '';
_loadingProgress = 1.0;
_done = true;
_onUpdate();
}
}

static Future<String> getApplicationDirectory() async {
Expand Down
12 changes: 10 additions & 2 deletions flutter/lib/ui/settings/resources_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:mlperfbench/benchmark/state.dart';
import 'package:mlperfbench/localizations/app_localizations.dart';
import 'package:mlperfbench/store.dart';
import 'package:mlperfbench/ui/confirm_dialog.dart';
import 'package:mlperfbench/ui/error_dialog.dart';

class ResourcesScreen extends StatefulWidget {
const ResourcesScreen({super.key});
Expand Down Expand Up @@ -169,8 +170,15 @@ class _ResourcesScreen extends State<ResourcesScreen> {
return AbsorbPointer(
absorbing: downloading,
child: ElevatedButton(
onPressed: () {
state.loadResources(downloadMissing: true);
onPressed: () async {
await state.loadResources(downloadMissing: true);
if (state.error != null) {
if (!mounted) return;
await showErrorDialog(context, <String>[state.error.toString()]);
// Reset both the error and stacktrace for further operation
state.error = null;
state.stackTrace = null;
}
},
style: ElevatedButton.styleFrom(
backgroundColor: downloading ? Colors.grey : Colors.blue),
Expand Down

0 comments on commit 399ee37

Please sign in to comment.