refactor: MVC chat encryption settings

This commit is contained in:
Christian Pauly 2021-04-14 14:02:10 +02:00
parent 28ed394e90
commit 576e840ba8
3 changed files with 76 additions and 60 deletions

View File

@ -8,7 +8,7 @@ import 'package:fluffychat/controllers/sign_up_password_controller.dart';
import 'package:fluffychat/views/widgets/matrix.dart'; import 'package:fluffychat/views/widgets/matrix.dart';
import 'package:fluffychat/views/chat.dart'; import 'package:fluffychat/views/chat.dart';
import 'package:fluffychat/controllers/chat_details_controller.dart'; import 'package:fluffychat/controllers/chat_details_controller.dart';
import 'package:fluffychat/views/chat_encryption_settings.dart'; import 'package:fluffychat/controllers/chat_encryption_settings_controller.dart';
import 'package:fluffychat/views/chat_list.dart'; import 'package:fluffychat/views/chat_list.dart';
import 'package:fluffychat/views/chat_permissions_settings.dart'; import 'package:fluffychat/views/chat_permissions_settings.dart';
import 'package:fluffychat/views/empty_page.dart'; import 'package:fluffychat/views/empty_page.dart';

View File

@ -0,0 +1,65 @@
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/views/chat_encryption_settings_view.dart';
import 'package:fluffychat/views/widgets/matrix.dart';
import 'package:flutter/material.dart';
import '../views/widgets/dialogs/key_verification_dialog.dart';
class ChatEncryptionSettings extends StatefulWidget {
final String id;
const ChatEncryptionSettings(this.id, {Key key}) : super(key: key);
@override
ChatEncryptionSettingsController createState() =>
ChatEncryptionSettingsController();
}
class ChatEncryptionSettingsController extends State<ChatEncryptionSettings> {
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);
}
};
await KeyVerificationDialog(request: req).show(context);
break;
case 'verify_user':
await unblock();
final req =
await room.client.userDeviceKeys[key.userId].startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
await KeyVerificationDialog(request: req).show(context);
break;
case 'block':
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
break;
case 'unblock':
await unblock();
setState(() => null);
break;
}
}
@override
Widget build(BuildContext context) => ChatEncryptionSettingsView(this);
}

View File

@ -1,70 +1,21 @@
import 'package:famedlysdk/encryption.dart';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/controllers/chat_encryption_settings_controller.dart';
import 'package:fluffychat/views/widgets/avatar.dart'; import 'package:fluffychat/views/widgets/avatar.dart';
import 'package:fluffychat/views/widgets/matrix.dart'; import 'package:fluffychat/views/widgets/matrix.dart';
import 'package:fluffychat/views/widgets/max_width_body.dart'; import 'package:fluffychat/views/widgets/max_width_body.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../views/widgets/dialogs/key_verification_dialog.dart';
import '../utils/device_extension.dart'; import '../utils/device_extension.dart';
class ChatEncryptionSettings extends StatefulWidget { class ChatEncryptionSettingsView extends StatelessWidget {
final String id; final ChatEncryptionSettingsController controller;
const ChatEncryptionSettings(this.id, {Key key}) : super(key: key); const ChatEncryptionSettingsView(this.controller, {Key key})
: super(key: key);
@override
_ChatEncryptionSettingsState createState() => _ChatEncryptionSettingsState();
}
class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
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);
}
};
await KeyVerificationDialog(request: req).show(context);
break;
case 'verify_user':
await unblock();
final req =
await room.client.userDeviceKeys[key.userId].startVerification();
req.onUpdate = () {
if (req.state == KeyVerificationState.done) {
setState(() => null);
}
};
await KeyVerificationDialog(request: req).show(context);
break;
case 'block':
if (key.directVerified) {
await key.setVerified(false);
}
await key.setBlocked(true);
setState(() => null);
break;
case 'unblock':
await unblock();
setState(() => null);
break;
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final room = Matrix.of(context).client.getRoomById(widget.id); final room = Matrix.of(context).client.getRoomById(controller.widget.id);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
@ -113,8 +64,8 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
deviceKeys[i - 1].userId) ...{ deviceKeys[i - 1].userId) ...{
Divider(height: 1, thickness: 1), Divider(height: 1, thickness: 1),
PopupMenuButton( PopupMenuButton(
onSelected: (action) => onSelected: (action) => controller.onSelected(
onSelected(context, action, deviceKeys[i]), context, action, deviceKeys[i]),
itemBuilder: (c) { itemBuilder: (c) {
final items = <PopupMenuEntry<String>>[]; final items = <PopupMenuEntry<String>>[];
if (room if (room
@ -163,8 +114,8 @@ class _ChatEncryptionSettingsState extends State<ChatEncryptionSettings> {
), ),
}, },
PopupMenuButton( PopupMenuButton(
onSelected: (action) => onSelected: (action) => controller.onSelected(
onSelected(context, action, deviceKeys[i]), context, action, deviceKeys[i]),
itemBuilder: (c) { itemBuilder: (c) {
final items = <PopupMenuEntry<String>>[]; final items = <PopupMenuEntry<String>>[];
if (deviceKeys[i].blocked || if (deviceKeys[i].blocked ||