2020-11-14 08:54:26 +01:00
|
|
|
import 'package:flushbar/flushbar_helper.dart';
|
2020-10-03 13:11:07 +02:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-02-16 09:16:47 +01:00
|
|
|
|
|
|
|
class SimpleDialogs {
|
|
|
|
final BuildContext context;
|
|
|
|
|
|
|
|
const SimpleDialogs(this.context);
|
|
|
|
|
2020-04-27 13:36:39 +02:00
|
|
|
Future<dynamic> tryRequestWithLoadingDialog(Future<dynamic> request,
|
|
|
|
{Function(MatrixException) onAdditionalAuth}) async {
|
2020-10-02 14:44:05 +02:00
|
|
|
final futureResult = tryRequestWithErrorToast(
|
|
|
|
request,
|
|
|
|
onAdditionalAuth: onAdditionalAuth,
|
2020-11-22 19:49:30 +01:00
|
|
|
);
|
2020-10-02 14:44:05 +02:00
|
|
|
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(),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2020-04-27 13:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-11-14 08:54:26 +01:00
|
|
|
await FlushbarHelper.createError(message: exception.errorMessage)
|
|
|
|
.show(context);
|
2020-04-27 13:36:39 +02:00
|
|
|
}
|
|
|
|
} catch (exception) {
|
2020-11-14 08:54:26 +01:00
|
|
|
await FlushbarHelper.createError(message: exception.toString())
|
|
|
|
.show(context);
|
2020-04-27 13:36:39 +02:00
|
|
|
}
|
2020-11-24 17:55:23 +01:00
|
|
|
return false;
|
2020-04-27 13:36:39 +02:00
|
|
|
}
|
2020-02-16 09:16:47 +01:00
|
|
|
}
|