2020-05-13 08:45:50 +00:00
|
|
|
import 'package:bot_toast/bot_toast.dart';
|
2020-10-03 11:11:07 +00:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-09-08 08:55:32 +00:00
|
|
|
import 'package:matrix_link_text/link_text.dart';
|
2020-02-16 08:16:47 +00:00
|
|
|
|
|
|
|
class SimpleDialogs {
|
|
|
|
final BuildContext context;
|
|
|
|
|
|
|
|
const SimpleDialogs(this.context);
|
|
|
|
|
2020-07-02 09:10:03 +00:00
|
|
|
Future<String> enterText(
|
|
|
|
{String titleText,
|
|
|
|
String confirmText,
|
|
|
|
String cancelText,
|
|
|
|
String hintText,
|
|
|
|
String labelText,
|
|
|
|
String prefixText,
|
|
|
|
String suffixText,
|
|
|
|
bool password = false,
|
|
|
|
bool multiLine = false,
|
|
|
|
TextInputType keyboardType}) async {
|
2020-05-13 13:58:59 +00:00
|
|
|
var textEditingController = TextEditingController();
|
|
|
|
final controller = textEditingController;
|
2020-02-16 08:56:17 +00:00
|
|
|
String input;
|
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => AlertDialog(
|
2020-04-27 11:36:39 +00:00
|
|
|
title: Text(titleText ?? 'Please enter a text'),
|
2020-02-16 08:56:17 +00:00
|
|
|
content: TextField(
|
|
|
|
controller: controller,
|
|
|
|
autofocus: true,
|
2020-04-08 15:43:07 +00:00
|
|
|
autocorrect: false,
|
2020-02-16 08:56:17 +00:00
|
|
|
onSubmitted: (s) {
|
|
|
|
input = s;
|
2020-07-18 20:19:28 +00:00
|
|
|
Navigator.of(c).pop();
|
2020-02-16 08:56:17 +00:00
|
|
|
},
|
2020-02-16 10:09:28 +00:00
|
|
|
minLines: multiLine ? 3 : 1,
|
|
|
|
maxLines: multiLine ? 3 : 1,
|
2020-02-19 15:23:13 +00:00
|
|
|
obscureText: password,
|
2020-02-16 10:09:28 +00:00
|
|
|
textInputAction: multiLine ? TextInputAction.newline : null,
|
2020-07-02 09:10:03 +00:00
|
|
|
keyboardType: keyboardType,
|
2020-02-16 08:56:17 +00:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: hintText,
|
|
|
|
labelText: labelText,
|
2020-02-16 10:09:28 +00:00
|
|
|
prefixText: prefixText,
|
|
|
|
suffixText: suffixText,
|
2020-02-16 10:36:18 +00:00
|
|
|
prefixStyle: TextStyle(color: Theme.of(context).primaryColor),
|
|
|
|
suffixStyle: TextStyle(color: Theme.of(context).primaryColor),
|
2020-02-16 08:56:17 +00:00
|
|
|
border: OutlineInputBorder(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2020-02-16 10:36:18 +00:00
|
|
|
child: Text(
|
|
|
|
cancelText?.toUpperCase() ??
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).close.toUpperCase(),
|
2020-02-16 08:56:17 +00:00
|
|
|
style: TextStyle(color: Colors.blueGrey)),
|
2020-07-18 20:19:28 +00:00
|
|
|
onPressed: () => Navigator.of(c).pop(),
|
2020-02-16 08:56:17 +00:00
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
child: Text(
|
2020-02-16 10:36:18 +00:00
|
|
|
confirmText?.toUpperCase() ??
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).confirm.toUpperCase(),
|
2020-02-16 08:56:17 +00:00
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
input = controller.text;
|
2020-07-18 20:19:28 +00:00
|
|
|
Navigator.of(c).pop();
|
2020-02-16 08:56:17 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2020-02-16 08:16:47 +00:00
|
|
|
Future<bool> askConfirmation({
|
|
|
|
String titleText,
|
2020-02-22 07:27:08 +00:00
|
|
|
String contentText,
|
2020-02-16 08:16:47 +00:00
|
|
|
String confirmText,
|
|
|
|
String cancelText,
|
2020-09-21 15:50:01 +00:00
|
|
|
bool dangerous = false,
|
2020-02-16 08:16:47 +00:00
|
|
|
}) async {
|
2020-05-13 13:58:59 +00:00
|
|
|
var confirmed = false;
|
2020-02-16 08:16:47 +00:00
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => AlertDialog(
|
2020-05-07 05:52:40 +00:00
|
|
|
title: Text(titleText ?? L10n.of(context).areYouSure),
|
2020-09-08 08:55:32 +00:00
|
|
|
content: contentText != null ? LinkText(text: contentText) : null,
|
2020-02-16 08:16:47 +00:00
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2020-02-16 10:36:18 +00:00
|
|
|
child: Text(
|
|
|
|
cancelText?.toUpperCase() ??
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).close.toUpperCase(),
|
2020-02-16 08:16:47 +00:00
|
|
|
style: TextStyle(color: Colors.blueGrey)),
|
2020-07-18 20:19:28 +00:00
|
|
|
onPressed: () => Navigator.of(c).pop(),
|
2020-02-16 08:16:47 +00:00
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
child: Text(
|
2020-02-16 10:36:18 +00:00
|
|
|
confirmText?.toUpperCase() ??
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).confirm.toUpperCase(),
|
2020-09-21 15:50:01 +00:00
|
|
|
style: TextStyle(color: dangerous ? Colors.red : null),
|
2020-02-16 08:16:47 +00:00
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
confirmed = true;
|
2020-07-18 20:19:28 +00:00
|
|
|
Navigator.of(c).pop();
|
2020-02-16 08:16:47 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return confirmed;
|
|
|
|
}
|
2020-04-27 11:36:39 +00:00
|
|
|
|
2020-05-12 07:02:33 +00:00
|
|
|
Future<void> inform({
|
|
|
|
String titleText,
|
|
|
|
String contentText,
|
|
|
|
String okText,
|
|
|
|
}) async {
|
|
|
|
await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (c) => AlertDialog(
|
|
|
|
title: titleText != null ? Text(titleText) : null,
|
|
|
|
content: contentText != null ? Text(contentText) : null,
|
2020-05-13 08:45:50 +00:00
|
|
|
actions: <Widget>[
|
2020-05-12 07:02:33 +00:00
|
|
|
FlatButton(
|
|
|
|
child: Text(
|
|
|
|
okText ?? L10n.of(context).ok.toUpperCase(),
|
|
|
|
),
|
|
|
|
onPressed: () {
|
2020-07-18 20:19:28 +00:00
|
|
|
Navigator.of(c).pop();
|
2020-05-12 07:02:33 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:36:39 +00:00
|
|
|
Future<dynamic> tryRequestWithLoadingDialog(Future<dynamic> request,
|
|
|
|
{Function(MatrixException) onAdditionalAuth}) async {
|
2020-10-02 12:44:05 +00:00
|
|
|
var completed = false;
|
|
|
|
final futureResult = tryRequestWithErrorToast(
|
|
|
|
request,
|
|
|
|
onAdditionalAuth: onAdditionalAuth,
|
|
|
|
).whenComplete(() => completed = true);
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
|
|
|
if (completed) return futureResult;
|
|
|
|
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 11:36:39 +00: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-05-13 08:45:50 +00:00
|
|
|
BotToast.showText(text: exception.errorMessage);
|
2020-04-27 11:36:39 +00:00
|
|
|
}
|
|
|
|
} catch (exception) {
|
2020-05-13 08:45:50 +00:00
|
|
|
BotToast.showText(text: exception.toString());
|
2020-04-27 11:36:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
void showLoadingDialog(BuildContext context) async {
|
|
|
|
await showDialog(
|
2020-04-27 11:36:39 +00:00
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (BuildContext context) => AlertDialog(
|
2020-10-02 12:44:05 +00:00
|
|
|
title: Text(L10n.of(context).loadingPleaseWait),
|
|
|
|
content: LinearProgressIndicator(),
|
2020-04-27 11:36:39 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-02-16 08:16:47 +00:00
|
|
|
}
|