fluffychat/lib/pages/chat_encryption_settings/chat_encryption_settings_view.dart

203 lines
9.1 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:matrix/matrix.dart';
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/chat_encryption_settings/chat_encryption_settings.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/layouts/max_width_body.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
import '../../utils/matrix_sdk_extensions.dart/device_extension.dart';
2022-09-10 12:11:11 +02:00
import '../../widgets/m2_popup_menu_button.dart';
2020-02-04 14:42:35 +01:00
2021-05-22 09:13:47 +02:00
class ChatEncryptionSettingsView extends StatelessWidget {
2021-04-14 14:02:10 +02:00
final ChatEncryptionSettingsController controller;
2020-02-04 14:42:35 +01:00
2022-01-29 12:35:03 +01:00
const ChatEncryptionSettingsView(this.controller, {Key? key})
2021-05-22 09:13:47 +02:00
: super(key: key);
2020-06-25 16:29:06 +02:00
2020-02-04 14:42:35 +01:00
@override
Widget build(BuildContext context) {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(controller.roomId!)!;
2020-02-04 14:42:35 +01:00
return Scaffold(
appBar: AppBar(
2021-05-23 13:11:55 +02:00
leading: IconButton(
2021-10-14 18:09:30 +02:00
icon: const Icon(Icons.close_outlined),
2021-05-23 13:11:55 +02:00
onPressed: () =>
2022-01-29 12:35:03 +01:00
VRouter.of(context).toSegments(['rooms', controller.roomId!]),
2021-05-23 13:11:55 +02:00
),
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.tapOnDeviceToVerify),
2022-01-15 13:18:25 +01:00
elevation: 0,
2020-02-04 14:42:35 +01:00
),
2021-04-09 18:26:44 +02:00
body: MaxWidthBody(
withScrolling: true,
2022-01-15 13:18:25 +01:00
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.deviceVerifyDescription),
2022-01-15 13:18:25 +01:00
leading: CircleAvatar(
backgroundColor: Theme.of(context).secondaryHeaderColor,
foregroundColor: Theme.of(context).colorScheme.secondary,
child: const Icon(Icons.lock),
),
),
const Divider(height: 1),
StreamBuilder(
stream: room.onUpdate.stream,
builder: (context, snapshot) {
return FutureBuilder<List<DeviceKeys>>(
future: room.getUserDeviceKeys(),
builder: (BuildContext context, snapshot) {
if (snapshot.hasError) {
return Center(
2022-08-14 16:59:21 +02:00
child: Text(
'${L10n.of(context)!.oopsSomethingWentWrong}: ${snapshot.error}'),
2022-01-15 13:18:25 +01:00
);
}
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator.adaptive(
strokeWidth: 2));
}
2022-01-29 12:35:03 +01:00
final deviceKeys = snapshot.data!;
2022-01-15 13:18:25 +01:00
return ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: deviceKeys.length,
itemBuilder: (BuildContext context, int i) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (i == 0 ||
deviceKeys[i].userId !=
deviceKeys[i - 1].userId) ...{
const Divider(height: 1, thickness: 1),
2022-09-10 12:11:11 +02:00
M2PopupMenuButton(
2022-01-29 12:35:03 +01:00
onSelected: (dynamic action) => controller
.onSelected(context, action, deviceKeys[i]),
2022-01-15 13:18:25 +01:00
itemBuilder: (c) {
final items = <PopupMenuEntry<String>>[];
if (room
.client
2022-01-29 12:35:03 +01:00
.userDeviceKeys[deviceKeys[i].userId]!
2022-01-15 13:18:25 +01:00
.verified ==
UserVerifiedStatus.unknown) {
items.add(PopupMenuItem(
value: 'verify_user',
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.verifyUser),
2022-01-15 13:18:25 +01:00
));
}
return items;
},
child: ListTile(
leading: Avatar(
mxContent: room
.unsafeGetUserFromMemoryOrFallback(
deviceKeys[i].userId)
2022-01-15 13:18:25 +01:00
.avatarUrl,
name: room
.unsafeGetUserFromMemoryOrFallback(
deviceKeys[i].userId)
2022-01-15 13:18:25 +01:00
.calcDisplayname(),
),
title: Text(
room
.unsafeGetUserFromMemoryOrFallback(
deviceKeys[i].userId)
2022-01-15 13:18:25 +01:00
.calcDisplayname(),
),
subtitle: Text(
deviceKeys[i].userId,
style: const TextStyle(
fontWeight: FontWeight.w300),
),
),
2021-04-09 18:26:44 +02:00
),
2022-01-15 13:18:25 +01:00
},
2022-09-10 12:11:11 +02:00
M2PopupMenuButton(
2022-01-29 12:35:03 +01:00
onSelected: (dynamic action) => controller
.onSelected(context, action, deviceKeys[i]),
2022-01-15 13:18:25 +01:00
itemBuilder: (c) {
final items = <PopupMenuEntry<String>>[];
if (deviceKeys[i].blocked ||
!deviceKeys[i].verified) {
items.add(PopupMenuItem(
value: deviceKeys[i].userId ==
room.client.userID
2021-04-09 18:26:44 +02:00
? 'verify'
: 'verify_user',
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.verifyStart),
2022-01-15 13:18:25 +01:00
));
}
if (deviceKeys[i].blocked) {
items.add(PopupMenuItem(
value: 'unblock',
2022-01-29 12:35:03 +01:00
child:
Text(L10n.of(context)!.unblockDevice),
2022-01-15 13:18:25 +01:00
));
}
if (!deviceKeys[i].blocked) {
items.add(PopupMenuItem(
value: 'block',
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.blockDevice),
2022-01-15 13:18:25 +01:00
));
}
return items;
},
child: ListTile(
leading: CircleAvatar(
foregroundColor: Colors.white,
backgroundColor: deviceKeys[i].color,
child: Icon(deviceKeys[i].icon),
2021-02-27 10:41:58 +01:00
),
2022-01-15 13:18:25 +01:00
title: Text(
deviceKeys[i].displayname,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
subtitle: Row(
children: [
Text(
2022-01-29 12:35:03 +01:00
deviceKeys[i].deviceId!,
2022-01-15 13:18:25 +01:00
style: const TextStyle(
fontWeight: FontWeight.w300),
),
const Spacer(),
Text(
deviceKeys[i].blocked
2022-01-29 12:35:03 +01:00
? L10n.of(context)!.blocked
2022-01-15 13:18:25 +01:00
: deviceKeys[i].verified
2022-01-29 12:35:03 +01:00
? L10n.of(context)!.verified
: L10n.of(context)!.unverified,
2022-01-15 13:18:25 +01:00
style: TextStyle(
fontSize: 14,
color: deviceKeys[i].color,
),
),
],
2021-02-27 10:41:58 +01:00
),
2022-01-15 13:18:25 +01:00
),
2021-04-09 18:26:44 +02:00
),
2022-01-15 13:18:25 +01:00
],
2020-06-25 16:29:06 +02:00
),
2022-01-15 13:18:25 +01:00
);
},
2021-04-09 18:26:44 +02:00
);
2022-01-15 13:18:25 +01:00
}),
],
),
2021-04-09 18:26:44 +02:00
),
2020-02-04 14:42:35 +01:00
);
}
}
extension on DeviceKeys {
Color get color => blocked
? Colors.red
: verified
? Colors.green
: Colors.orange;
}