139 lines
4.1 KiB
Dart
139 lines
4.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:pshared/utils/snackbar.dart';
|
|
|
|
import 'package:pweb/utils/error/handler.dart';
|
|
import 'package:pweb/widgets/error/content.dart';
|
|
|
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
|
|
|
|
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUserOfErrorX({
|
|
required ScaffoldMessengerState scaffoldMessenger,
|
|
required String errorSituation,
|
|
required Object exception,
|
|
required AppLocalizations appLocalizations,
|
|
int delaySeconds = 3,
|
|
}) {
|
|
// A. Localized user-friendly error message
|
|
final String localizedError = ErrorHandler.handleErrorLocs(appLocalizations, exception);
|
|
|
|
// B. Technical details for advanced reference
|
|
final String technicalDetails = exception.toString();
|
|
|
|
// C. Build the snack bar
|
|
final snackBar = _buildMainErrorSnackBar(
|
|
errorSituation: errorSituation,
|
|
localizedError: localizedError,
|
|
technicalDetails: technicalDetails,
|
|
loc: appLocalizations,
|
|
scaffoldMessenger: scaffoldMessenger,
|
|
delaySeconds: delaySeconds,
|
|
);
|
|
|
|
// D. Show it
|
|
return scaffoldMessenger.showSnackBar(snackBar);
|
|
}
|
|
|
|
ScaffoldFeatureController<SnackBar, SnackBarClosedReason> notifyUserOfError({
|
|
required BuildContext context,
|
|
required String errorSituation,
|
|
required Object exception,
|
|
int delaySeconds = 3,
|
|
}) => notifyUserOfErrorX(
|
|
scaffoldMessenger: ScaffoldMessenger.of(context),
|
|
errorSituation: errorSituation,
|
|
exception: exception,
|
|
appLocalizations: AppLocalizations.of(context)!,
|
|
delaySeconds: delaySeconds,
|
|
);
|
|
|
|
Future<T?> executeActionWithNotification<T>({
|
|
required BuildContext context,
|
|
required Future<T> Function() action,
|
|
required String errorMessage,
|
|
String? successMessage,
|
|
int delaySeconds = 3,
|
|
}) async {
|
|
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
|
final localizations = AppLocalizations.of(context)!;
|
|
|
|
try {
|
|
final result = await action();
|
|
if (successMessage != null) {
|
|
notifyUser(context, successMessage, delaySeconds: delaySeconds);
|
|
}
|
|
return result;
|
|
} catch (e) {
|
|
// Report the error using your existing notifier.
|
|
notifyUserOfErrorX(
|
|
scaffoldMessenger: scaffoldMessenger,
|
|
errorSituation: errorMessage,
|
|
exception: e,
|
|
appLocalizations: localizations,
|
|
delaySeconds: delaySeconds,
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> postNotifyUserOfError({
|
|
required ScaffoldMessengerState scaffoldMessenger,
|
|
required String errorSituation,
|
|
required Object exception,
|
|
required AppLocalizations appLocalizations,
|
|
int delaySeconds = 3,
|
|
}) {
|
|
|
|
final completer = Completer<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>>();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => completer.complete(notifyUserOfErrorX(
|
|
scaffoldMessenger: scaffoldMessenger,
|
|
errorSituation: errorSituation,
|
|
exception: exception,
|
|
appLocalizations: appLocalizations,
|
|
delaySeconds: delaySeconds,
|
|
)),
|
|
);
|
|
|
|
return completer.future;
|
|
}
|
|
|
|
Future<ScaffoldFeatureController<SnackBar, SnackBarClosedReason>> postNotifyUserOfErrorX({
|
|
required BuildContext context,
|
|
required String errorSituation,
|
|
required Object exception,
|
|
int delaySeconds = 3,
|
|
}) => postNotifyUserOfError(
|
|
scaffoldMessenger: ScaffoldMessenger.of(context),
|
|
errorSituation: errorSituation,
|
|
exception: exception,
|
|
appLocalizations: AppLocalizations.of(context)!,
|
|
delaySeconds: delaySeconds,
|
|
);
|
|
|
|
|
|
/// 2) A helper function that returns the main SnackBar widget
|
|
SnackBar _buildMainErrorSnackBar({
|
|
required String errorSituation,
|
|
required String localizedError,
|
|
required String technicalDetails,
|
|
required AppLocalizations loc,
|
|
required ScaffoldMessengerState scaffoldMessenger,
|
|
int delaySeconds = 3,
|
|
}) => SnackBar(
|
|
duration: Duration(seconds: delaySeconds),
|
|
content: ErrorSnackBarContent(
|
|
situation: errorSituation,
|
|
localizedError: localizedError,
|
|
),
|
|
action: SnackBarAction(
|
|
label: loc.showDetailsAction,
|
|
onPressed: () => scaffoldMessenger.showSnackBar(SnackBar(
|
|
content: Text(technicalDetails),
|
|
duration: const Duration(seconds: 6),
|
|
)),
|
|
),
|
|
);
|