You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.0 KiB
Dart
180 lines
6.0 KiB
Dart
4 years ago
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||
|
import 'package:famedlysdk/famedlysdk.dart';
|
||
4 years ago
|
import 'package:fluffychat/views/widgets/max_width_body.dart';
|
||
4 years ago
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||
4 years ago
|
import 'package:fluffychat/views/widgets/matrix.dart';
|
||
4 years ago
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||
|
|
||
|
class Settings3Pid extends StatefulWidget {
|
||
|
static int sendAttempt = 0;
|
||
|
|
||
|
@override
|
||
|
_Settings3PidState createState() => _Settings3PidState();
|
||
|
}
|
||
|
|
||
|
class _Settings3PidState extends State<Settings3Pid> {
|
||
|
void _add3PidAction(BuildContext context) async {
|
||
|
final input = await showTextInputDialog(
|
||
|
context: context,
|
||
|
title: L10n.of(context).enterAnEmailAddress,
|
||
4 years ago
|
okLabel: L10n.of(context).ok,
|
||
|
cancelLabel: L10n.of(context).cancel,
|
||
4 years ago
|
useRootNavigator: false,
|
||
4 years ago
|
textFields: [
|
||
|
DialogTextField(
|
||
|
hintText: L10n.of(context).enterAnEmailAddress,
|
||
|
keyboardType: TextInputType.emailAddress,
|
||
|
),
|
||
|
],
|
||
|
);
|
||
|
if (input == null) return;
|
||
|
final clientSecret = DateTime.now().millisecondsSinceEpoch.toString();
|
||
4 years ago
|
final response = await showFutureLoadingDialog(
|
||
|
context: context,
|
||
|
future: () => Matrix.of(context).client.requestEmailToken(
|
||
4 years ago
|
input.single,
|
||
|
clientSecret,
|
||
|
Settings3Pid.sendAttempt++,
|
||
|
),
|
||
|
);
|
||
4 years ago
|
if (response.error != null) return;
|
||
4 years ago
|
final ok = await showOkAlertDialog(
|
||
|
context: context,
|
||
|
title: L10n.of(context).weSentYouAnEmail,
|
||
|
message: L10n.of(context).pleaseClickOnLink,
|
||
|
okLabel: L10n.of(context).iHaveClickedOnLink,
|
||
4 years ago
|
useRootNavigator: false,
|
||
4 years ago
|
);
|
||
|
if (ok == null) return;
|
||
4 years ago
|
final success = await showFutureLoadingDialog(
|
||
|
context: context,
|
||
|
future: () => Matrix.of(context).client.uiaRequestBackground(
|
||
4 years ago
|
(auth) => Matrix.of(context).client.addThirdPartyIdentifier(
|
||
4 years ago
|
clientSecret,
|
||
4 years ago
|
response.result.sid,
|
||
4 years ago
|
auth: auth,
|
||
4 years ago
|
),
|
||
|
),
|
||
4 years ago
|
);
|
||
4 years ago
|
if (success.error != null) return;
|
||
4 years ago
|
setState(() => _request = null);
|
||
|
}
|
||
|
|
||
|
Future<List<ThirdPartyIdentifier>> _request;
|
||
|
|
||
|
void _delete3Pid(
|
||
|
BuildContext context, ThirdPartyIdentifier identifier) async {
|
||
|
if (await showOkCancelAlertDialog(
|
||
|
context: context,
|
||
|
title: L10n.of(context).areYouSure,
|
||
4 years ago
|
okLabel: L10n.of(context).yes,
|
||
|
cancelLabel: L10n.of(context).cancel,
|
||
4 years ago
|
useRootNavigator: false,
|
||
4 years ago
|
) !=
|
||
|
OkCancelResult.ok) {
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
final success = await showFutureLoadingDialog(
|
||
|
context: context,
|
||
|
future: () => Matrix.of(context).client.deleteThirdPartyIdentifier(
|
||
4 years ago
|
identifier.address,
|
||
|
identifier.medium,
|
||
|
));
|
||
4 years ago
|
if (success.error != null) return;
|
||
4 years ago
|
setState(() => _request = null);
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
_request ??= Matrix.of(context).client.requestThirdPartyIdentifiers();
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
4 years ago
|
leading: BackButton(),
|
||
4 years ago
|
title: Text(L10n.of(context).passwordRecovery),
|
||
|
actions: [
|
||
|
IconButton(
|
||
4 years ago
|
icon: Icon(Icons.add_outlined),
|
||
4 years ago
|
onPressed: () => _add3PidAction(context),
|
||
4 years ago
|
tooltip: L10n.of(context).addEmail,
|
||
4 years ago
|
)
|
||
|
],
|
||
|
),
|
||
4 years ago
|
body: MaxWidthBody(
|
||
|
child: FutureBuilder<List<ThirdPartyIdentifier>>(
|
||
|
future: _request,
|
||
|
builder: (BuildContext context,
|
||
|
AsyncSnapshot<List<ThirdPartyIdentifier>> snapshot) {
|
||
|
if (snapshot.hasError) {
|
||
|
return Center(
|
||
|
child: Text(
|
||
|
snapshot.error.toString(),
|
||
|
textAlign: TextAlign.center,
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
if (!snapshot.hasData) {
|
||
|
return Center(child: CircularProgressIndicator());
|
||
|
}
|
||
|
final identifier = snapshot.data;
|
||
|
return Column(
|
||
|
children: [
|
||
|
ListTile(
|
||
|
leading: CircleAvatar(
|
||
|
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
|
||
|
foregroundColor:
|
||
|
identifier.isEmpty ? Colors.orange : Colors.grey,
|
||
|
child: Icon(
|
||
|
identifier.isEmpty
|
||
|
? Icons.warning_outlined
|
||
|
: Icons.info_outlined,
|
||
|
),
|
||
|
),
|
||
|
title: Text(
|
||
4 years ago
|
identifier.isEmpty
|
||
4 years ago
|
? L10n.of(context).noPasswordRecoveryDescription
|
||
|
: L10n.of(context)
|
||
|
.withTheseAddressesRecoveryDescription,
|
||
4 years ago
|
),
|
||
|
),
|
||
4 years ago
|
Divider(height: 1),
|
||
|
Expanded(
|
||
|
child: ListView.builder(
|
||
|
itemCount: identifier.length,
|
||
|
itemBuilder: (BuildContext context, int i) => ListTile(
|
||
|
leading: CircleAvatar(
|
||
|
backgroundColor:
|
||
|
Theme.of(context).scaffoldBackgroundColor,
|
||
|
foregroundColor: Colors.grey,
|
||
|
child: Icon(identifier[i].iconData)),
|
||
|
title: Text(identifier[i].address),
|
||
|
trailing: IconButton(
|
||
|
tooltip: L10n.of(context).delete,
|
||
|
icon: Icon(Icons.delete_forever_outlined),
|
||
|
color: Colors.red,
|
||
|
onPressed: () => _delete3Pid(context, identifier[i]),
|
||
|
),
|
||
4 years ago
|
),
|
||
|
),
|
||
|
),
|
||
4 years ago
|
],
|
||
|
);
|
||
|
},
|
||
|
),
|
||
4 years ago
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension on ThirdPartyIdentifier {
|
||
|
IconData get iconData {
|
||
|
switch (medium) {
|
||
|
case ThirdPartyIdentifierMedium.email:
|
||
|
return Icons.mail_outline_rounded;
|
||
|
case ThirdPartyIdentifierMedium.msisdn:
|
||
|
return Icons.phone_android_outlined;
|
||
|
}
|
||
|
return Icons.device_unknown_outlined;
|
||
|
}
|
||
|
}
|