fluffychat/lib/pages/settings_3pid/settings_3pid.dart

94 lines
2.9 KiB
Dart
Raw Normal View History

2021-04-17 11:27:58 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-04-17 11:27:58 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
2021-04-17 11:27:58 +02:00
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import 'settings_3pid_view.dart';
2021-04-17 11:27:58 +02:00
class Settings3Pid extends StatefulWidget {
static int sendAttempt = 0;
2022-01-29 12:35:03 +01:00
const Settings3Pid({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-04-17 11:27:58 +02:00
@override
Settings3PidController createState() => Settings3PidController();
}
class Settings3PidController extends State<Settings3Pid> {
void add3PidAction() async {
final input = await showTextInputDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-17 11:27:58 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.enterAnEmailAddress,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-04-17 11:27:58 +02:00
textFields: [
DialogTextField(
2022-01-29 12:35:03 +01:00
hintText: L10n.of(context)!.enterAnEmailAddress,
2021-04-17 11:27:58 +02:00
keyboardType: TextInputType.emailAddress,
),
],
);
if (input == null) return;
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
final response = await showFutureLoadingDialog(
context: context,
2021-09-13 15:26:51 +02:00
future: () => Matrix.of(context).client.requestTokenToRegisterEmail(
2021-09-10 11:23:40 +02:00
clientSecret,
2021-09-13 15:26:51 +02:00
input.single,
2021-04-17 11:27:58 +02:00
Settings3Pid.sendAttempt++,
),
);
if (response.error != null) return;
final ok = await showOkAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-17 11:27:58 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.weSentYouAnEmail,
message: L10n.of(context)!.pleaseClickOnLink,
okLabel: L10n.of(context)!.iHaveClickedOnLink,
2021-04-17 11:27:58 +02:00
);
2022-01-29 12:35:03 +01:00
if (ok != OkCancelResult.ok) return;
2021-04-17 11:27:58 +02:00
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.uiaRequestBackground(
2021-05-20 13:59:55 +02:00
(auth) => Matrix.of(context).client.add3PID(
2021-04-17 11:27:58 +02:00
clientSecret,
2022-01-29 12:35:03 +01:00
response.result!.sid,
2021-04-17 11:27:58 +02:00
auth: auth,
),
),
);
if (success.error != null) return;
setState(() => request = null);
}
2022-01-29 12:35:03 +01:00
Future<List<ThirdPartyIdentifier>?>? request;
2021-04-17 11:27:58 +02:00
void delete3Pid(ThirdPartyIdentifier identifier) async {
if (await showOkCancelAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-17 11:27:58 +02:00
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.areYouSure,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
2021-04-17 11:27:58 +02:00
) !=
OkCancelResult.ok) {
return;
}
final success = await showFutureLoadingDialog(
context: context,
2021-05-20 13:59:55 +02:00
future: () => Matrix.of(context).client.delete3pidFromAccount(
2021-04-17 11:27:58 +02:00
identifier.address,
identifier.medium,
));
if (success.error != null) return;
setState(() => request = null);
}
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => Settings3PidView(this);
2021-04-17 11:27:58 +02:00
}