fluffychat/lib/views/chat_encryption_settings.dart

240 lines
9.5 KiB
Dart
Raw Normal View History

2020-06-25 16:29:06 +02:00
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
2020-02-23 09:31:44 +01:00
import 'package:fluffychat/components/avatar.dart';
2020-02-04 14:42:35 +01:00
import 'package:fluffychat/components/matrix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-11-22 22:48:10 +01:00
import '../components/dialogs/key_verification_dialog.dart';
2021-02-20 10:28:04 +01:00
import '../utils/device_extension.dart';
2020-02-04 14:42:35 +01:00
class ChatEncryptionSettings extends StatefulWidget {
final String id;
const ChatEncryptionSettings(this.id, {Key key}) : super(key: key);
@override
_ChatEncryptionSettingsState createState() => _ChatEncryptionSettingsState();
}
class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
2020-06-25 16:29:06 +02:00
Future<void> onSelected(
BuildContext context, String action, DeviceKeys key) async {
final room = Matrix.of(context).client.getRoomById(widget.id);
final unblock = () async {
if (key.blocked) {
await key.setBlocked(false);
}
};
switch (action) {
case 'verify':
await unblock();
final req = key.startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
2021-02-24 12:17:23 +01:00
await KeyVerificationDialog(request: req).show(context);
2020-06-25 16:29:06 +02:00
break;
case 'verify_user':
await unblock();
final req =
await room.client.userDeviceKeys[key.userId].startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
2021-02-24 12:17:23 +01:00
await KeyVerificationDialog(request: req).show(context);
2020-06-25 16:29:06 +02:00
break;
case 'block':
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
break;
case 'unblock':
await unblock();
setState(() => null);
break;
}
}
2020-02-04 14:42:35 +01:00
@override
Widget build(BuildContext context) {
2020-05-09 07:17:55 +02:00
final room = Matrix.of(context).client.getRoomById(widget.id);
2020-02-04 14:42:35 +01:00
return Scaffold(
appBar: AppBar(
2021-01-16 14:24:52 +01:00
leading: BackButton(),
2021-02-27 10:41:58 +01:00
title: Text(L10n.of(context).tapOnDeviceToVerify),
bottom: PreferredSize(
preferredSize: Size.fromHeight(56),
child: ListTile(
title: Text(L10n.of(context).deviceVerifyDescription),
leading: CircleAvatar(
backgroundColor: Theme.of(context).secondaryHeaderColor,
foregroundColor: Theme.of(context).accentColor,
child: Icon(Icons.lock),
),
),
),
2020-02-04 14:42:35 +01:00
),
2020-05-09 07:17:55 +02:00
body: StreamBuilder(
stream: room.onUpdate.stream,
builder: (context, snapshot) {
return FutureBuilder<List<DeviceKeys>>(
future: room.getUserDeviceKeys(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasError) {
return Center(
child: Text(L10n.of(context).oopsSomethingWentWrong +
2020-05-13 15:58:59 +02:00
': ' +
2020-05-09 07:17:55 +02:00
snapshot.error.toString()),
);
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
2020-05-13 15:58:59 +02:00
final deviceKeys = snapshot.data;
2021-02-27 10:41:58 +01:00
return ListView.builder(
2020-05-09 11:54:10 +02:00
itemCount: deviceKeys.length,
itemBuilder: (BuildContext context, int i) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (i == 0 ||
2021-02-27 10:41:58 +01:00
deviceKeys[i].userId != deviceKeys[i - 1].userId) ...{
Divider(height: 1, thickness: 1),
PopupMenuButton(
onSelected: (action) =>
onSelected(context, action, deviceKeys[i]),
itemBuilder: (c) {
var items = <PopupMenuEntry<String>>[];
if (room.client.userDeviceKeys[deviceKeys[i].userId]
.verified ==
UserVerifiedStatus.unknown) {
items.add(PopupMenuItem(
value: 'verify_user',
2021-03-04 12:28:06 +01:00
child: Text(L10n.of(context).verifyUser),
2021-02-27 10:41:58 +01:00
));
}
return items;
},
child: ListTile(
leading: Avatar(
room
.getUserByMXIDSync(deviceKeys[i].userId)
.avatarUrl,
room
2020-05-09 11:54:10 +02:00
.getUserByMXIDSync(deviceKeys[i].userId)
2021-02-27 10:41:58 +01:00
.calcDisplayname(),
),
title: Row(
children: [
Text(
room
.getUserByMXIDSync(deviceKeys[i].userId)
.calcDisplayname(),
),
Spacer(),
Text(
deviceKeys[i].userId,
style: TextStyle(
fontSize: 14,
color: Theme.of(context)
.textTheme
.bodyText1
.color
.withAlpha(150),
),
),
],
2020-02-23 09:31:44 +01:00
),
2020-05-09 07:17:55 +02:00
),
2020-05-09 11:54:10 +02:00
),
2021-02-27 10:41:58 +01:00
},
2020-06-25 16:29:06 +02:00
PopupMenuButton(
onSelected: (action) =>
onSelected(context, action, deviceKeys[i]),
itemBuilder: (c) {
var items = <PopupMenuEntry<String>>[];
if (deviceKeys[i].blocked ||
!deviceKeys[i].verified) {
items.add(PopupMenuItem(
2021-02-20 10:28:04 +01:00
value: deviceKeys[i].userId == room.client.userID
? 'verify'
: 'verify_user',
2021-03-04 12:28:06 +01:00
child: Text(L10n.of(context).verifyStart),
));
2020-05-09 11:54:10 +02:00
}
2020-06-25 16:29:06 +02:00
if (deviceKeys[i].blocked) {
items.add(PopupMenuItem(
value: 'unblock',
2021-03-04 12:28:06 +01:00
child: Text(L10n.of(context).unblockDevice),
2020-06-25 16:29:06 +02:00
));
}
if (!deviceKeys[i].blocked) {
items.add(PopupMenuItem(
value: 'block',
2021-03-04 12:28:06 +01:00
child: Text(L10n.of(context).blockDevice),
2020-06-25 16:29:06 +02:00
));
}
return items;
2020-05-09 11:54:10 +02:00
},
2020-06-25 16:29:06 +02:00
child: ListTile(
2021-02-20 10:28:04 +01:00
leading: CircleAvatar(
foregroundColor:
Theme.of(context).textTheme.bodyText1.color,
backgroundColor:
Theme.of(context).secondaryHeaderColor,
child: Icon(deviceKeys[i].icon),
),
2020-06-25 16:29:06 +02:00
title: Text(
2021-02-20 10:28:04 +01:00
deviceKeys[i].displayname,
2021-02-27 10:41:58 +01:00
maxLines: 1,
overflow: TextOverflow.ellipsis,
2020-06-25 16:29:06 +02:00
),
2021-02-27 10:41:58 +01:00
subtitle: Row(
children: [
Text(
deviceKeys[i].deviceId,
style: TextStyle(
fontSize: 14,
color: Theme.of(context)
.textTheme
.bodyText1
.color
.withAlpha(150),
),
),
Spacer(),
Text(
deviceKeys[i].blocked
? L10n.of(context).blocked
: deviceKeys[i].verified
? L10n.of(context).verified
: L10n.of(context).unknownDevice,
style: TextStyle(
fontSize: 14,
color: deviceKeys[i].blocked
? Colors.red
: deviceKeys[i].verified
? Colors.green
: Colors.orange,
),
),
],
2020-06-25 16:29:06 +02:00
),
),
2020-05-09 11:54:10 +02:00
),
],
2020-02-23 09:31:44 +01:00
),
2020-05-09 07:17:55 +02:00
);
},
);
}),
2020-02-04 14:42:35 +01:00
);
}
}