actions-test/lib/tools/fetch_wrapper.dart
Jakob Meier 8fffafde47
Added translations using l10n
Translations are provided in *.arb* format.
Some keys have descriptions
(indicated by leading @-symbol).
Descriptions should not be copied into the translation itself.

Currently only English is supported (app_en.arb),
but German is planned.

Apparently weblate merged .arb support at some time,
so it would be nice to enable community translations at some point.
2023-03-29 15:14:27 +02:00

74 lines
1.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:outbag_app/backend/errors.dart';
import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/tools/snackbar.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void doNetworkRequest(ScaffoldMessengerState? sm,
{required Future<Response> Function() req,
Function(Map<String, dynamic> body)? onOK,
bool Function()? onNetworkErr,
bool Function()? onUnknownErr,
Function()? onAnyErr,
Function()? after,
bool Function(Map<String, dynamic>)? onServerErr}) async {
AppLocalizations? trans = (sm!=null)?AppLocalizations.of(sm.context):null;
Response res;
try {
res = await req();
} catch (_) {
// network error
bool showBar = true;
if (onNetworkErr != null) {
showBar = onNetworkErr();
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: trans!.errorNetwork, action: trans.dismiss);
}
if (onAnyErr != null) {
onAnyErr();
}
if (after != null) {
after();
}
return;
}
try {
if (res.res == Result.ok) {
if (onOK != null) {
await onOK(res.body);
}
} else {
// server error
bool showBar = true;
if (onServerErr != null) {
showBar = onServerErr(res.body);
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: errorAsString(res.body, trans!), action: trans.ok);
}
if (onAnyErr != null) {
onAnyErr();
}
}
} catch (_) {
bool showBar = true;
if (onUnknownErr != null) {
showBar = onUnknownErr();
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: trans!.errorUnknown, action: trans.ok);
}
}
if (after != null) {
after();
}
}