refactor: Switch to loading dialog
parent
9521a93622
commit
75797a62b9
@ -1,50 +0,0 @@
|
||||
import 'package:flushbar/flushbar_helper.dart';
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
||||
class SimpleDialogs {
|
||||
final BuildContext context;
|
||||
|
||||
const SimpleDialogs(this.context);
|
||||
|
||||
Future<dynamic> tryRequestWithLoadingDialog(Future<dynamic> request,
|
||||
{Function(MatrixException) onAdditionalAuth}) async {
|
||||
final futureResult = tryRequestWithErrorToast(
|
||||
request,
|
||||
onAdditionalAuth: onAdditionalAuth,
|
||||
);
|
||||
return showDialog<dynamic>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (BuildContext context) {
|
||||
futureResult.then(
|
||||
(result) => Navigator.of(context).pop<dynamic>(result),
|
||||
);
|
||||
return AlertDialog(
|
||||
title: Text(L10n.of(context).loadingPleaseWait),
|
||||
content: LinearProgressIndicator(),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<dynamic> tryRequestWithErrorToast(Future<dynamic> request,
|
||||
{Function(MatrixException) onAdditionalAuth}) async {
|
||||
try {
|
||||
return await request;
|
||||
} on MatrixException catch (exception) {
|
||||
if (exception.requireAdditionalAuthentication &&
|
||||
onAdditionalAuth != null) {
|
||||
return await tryRequestWithErrorToast(onAdditionalAuth(exception));
|
||||
} else {
|
||||
await FlushbarHelper.createError(message: exception.errorMessage)
|
||||
.show(context);
|
||||
}
|
||||
} catch (exception) {
|
||||
await FlushbarHelper.createError(message: exception.toString())
|
||||
.show(context);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
||||
extension LocalizedExceptionExtension on Object {
|
||||
String toLocalizedString(BuildContext context) {
|
||||
if (this is MatrixException) {
|
||||
switch ((this as MatrixException).error) {
|
||||
case MatrixError.M_FORBIDDEN:
|
||||
return L10n.of(context).noPermission;
|
||||
case MatrixError.M_LIMIT_EXCEEDED:
|
||||
return L10n.of(context).tooManyRequestsWarning;
|
||||
default:
|
||||
return (this as MatrixException).errorMessage;
|
||||
}
|
||||
}
|
||||
if (this is MatrixConnectionException) {
|
||||
L10n.of(context).noConnectionToTheServer;
|
||||
}
|
||||
Logs().d('Something went wrong: ', this);
|
||||
return L10n.of(context).oopsSomethingWentWrong;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue