actions-test/lib/tools/fetch_wrapper.dart
Jakob Meier 6b98c696ea
Finishied room member screen
The screen allows every room member,
to view a list of other room members,
and their role (Owner, Admin or Member).

If the user is allowed
(Owner, Admin, changeAdmin/manageMembers),
to either kick or promote members to the admin role,
the list tile will have a tap event.
NOTE: This is why some members do not have a hover animation.
For example the owner cannot be kicked.
2023-03-25 10:31:14 +01:00

106 lines
2.3 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/backend/user.dart';
import 'package:outbag_app/tools/snackbar.dart';
void doNetworkRequest(ScaffoldMessengerState? sm,
{required Future<Response> Function(User?) req,
Function(Map<String, dynamic>)? onOK,
bool Function()? onNetworkErr,
bool Function()? onUnknownErr,
bool Function()? onUserErr,
Function()? onAnyErr,
Function()? after,
bool Function(Map<String, dynamic>)? onServerErr,
bool needUser = true}) async {
User? user;
try {
user = await User.fromDisk();
} catch (_) {
// no user data available
if (needUser) {
// show error & quit
bool showBar = true;
if (onUserErr != null) {
showBar = onUserErr();
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: 'No user found', action: 'Authorize',
onTap: () {
try {
// try to remove broken user
User.removeDisk();
} catch (_) {}
});
}
if (onAnyErr != null) {
onAnyErr();
}
if (after != null) {
after();
}
return;
}
}
Response res;
try {
res = await req(user);
} catch (_) {
// network error
bool showBar = true;
if (onNetworkErr != null) {
showBar = onNetworkErr();
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: 'Network Error', action: '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), action: 'OK');
}
if (onAnyErr != null) {
onAnyErr();
}
}
} catch (_) {
bool showBar = true;
if (onUnknownErr != null) {
showBar = onUnknownErr();
}
if (showBar && sm != null) {
showSimpleSnackbar(sm, text: 'Unknown Error', action: 'OK');
}
}
if (after != null) {
after();
}
}