From 6f48a68798cad49bad6342682f2f75c9362ecb6c Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 5 Feb 2020 09:26:41 +0100 Subject: [PATCH] Enhance chatencryptionsettings page --- lib/components/matrix.dart | 5 + lib/views/chat.dart | 101 +++++++++-------- lib/views/chat_encryption_settings.dart | 142 +++++++++++++----------- pubspec.lock | 4 +- pubspec.yaml | 2 +- test/widget_test.dart | 2 - 6 files changed, 135 insertions(+), 121 deletions(-) diff --git a/lib/components/matrix.dart b/lib/components/matrix.dart index ad3ca9a5..d6d4b6ad 100644 --- a/lib/components/matrix.dart +++ b/lib/components/matrix.dart @@ -97,6 +97,11 @@ class MatrixState extends State { hideLoadingDialog() => Navigator.of(_loadingDialogContext)?.pop(); + bool get encryptionEnabled => + client.userDeviceKeys.containsKey(client.userID) && + client.userDeviceKeys[client.userID].deviceKeys + .containsKey(client.deviceID); + Future downloadAndSaveContent(MxContent content, {int width, int height, ThumbnailMethod method}) async { final bool thumbnail = width == null && height == null ? false : true; diff --git a/lib/views/chat.dart b/lib/views/chat.dart index 219c327f..159cb2d3 100644 --- a/lib/views/chat.dart +++ b/lib/views/chat.dart @@ -383,58 +383,61 @@ class _ChatState extends State<_Chat> { ), SizedBox(width: 8), Expanded( - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 4.0), - child: TextField( - minLines: 1, - maxLines: kIsWeb ? 1 : 8, - keyboardType: kIsWeb - ? TextInputType.text - : TextInputType.multiline, - onSubmitted: (String text) { - send(); - FocusScope.of(context).requestFocus(inputFocus); - }, - focusNode: inputFocus, - controller: sendController, - decoration: InputDecoration( - hintText: I18n.of(context).writeAMessage, - border: InputBorder.none, - ), - onChanged: (String text) { - this.typingCoolDown?.cancel(); - this.typingCoolDown = - Timer(Duration(seconds: 2), () { - this.typingCoolDown = null; - this.currentlyTyping = false; - room.sendTypingInfo(false); - }); - this.typingTimeout ??= - Timer(Duration(seconds: 30), () { - this.typingTimeout = null; - this.currentlyTyping = false; - }); - if (!this.currentlyTyping) { - this.currentlyTyping = true; - room.sendTypingInfo(true, - timeout: - Duration(seconds: 30).inMilliseconds); - } - }, - ), - )), - SizedBox(width: 8), - if (sendController.text.isEmpty) - IconButton( - icon: Icon( - room.encrypted ? Icons.lock : Icons.lock_open), - onPressed: () => Navigator.of(context).push( - AppRoute.defaultRoute( - context, - ChatEncryptionSettingsView(widget.id), + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 4.0), + child: TextField( + minLines: 1, + maxLines: kIsWeb ? 1 : 8, + keyboardType: kIsWeb + ? TextInputType.text + : TextInputType.multiline, + onSubmitted: (String text) { + send(); + FocusScope.of(context).requestFocus(inputFocus); + }, + focusNode: inputFocus, + controller: sendController, + decoration: InputDecoration( + hintText: I18n.of(context).writeAMessage, + border: InputBorder.none, + suffixIcon: sendController.text.isEmpty + ? InkWell( + child: Icon(room.encrypted + ? Icons.lock + : Icons.lock_open), + onTap: () => Navigator.of(context).push( + AppRoute.defaultRoute( + context, + ChatEncryptionSettingsView( + widget.id), + ), + ), + ) + : null, ), + onChanged: (String text) { + this.typingCoolDown?.cancel(); + this.typingCoolDown = + Timer(Duration(seconds: 2), () { + this.typingCoolDown = null; + this.currentlyTyping = false; + room.sendTypingInfo(false); + }); + this.typingTimeout ??= + Timer(Duration(seconds: 30), () { + this.typingTimeout = null; + this.currentlyTyping = false; + }); + if (!this.currentlyTyping) { + this.currentlyTyping = true; + room.sendTypingInfo(true, + timeout: + Duration(seconds: 30).inMilliseconds); + } + }, ), ), + ), IconButton( icon: Icon(Icons.send), onPressed: () => send(), diff --git a/lib/views/chat_encryption_settings.dart b/lib/views/chat_encryption_settings.dart index 141f26e7..843c7dae 100644 --- a/lib/views/chat_encryption_settings.dart +++ b/lib/views/chat_encryption_settings.dart @@ -8,6 +8,7 @@ import 'package:fluffychat/utils/beautify_string_extension.dart'; import 'package:fluffychat/i18n/i18n.dart'; import 'package:fluffychat/views/chat_list.dart'; import 'package:flutter/material.dart'; +import 'package:toast/toast.dart'; class ChatEncryptionSettingsView extends StatelessWidget { final String id; @@ -49,6 +50,7 @@ class _ChatEncryptionSettingsState extends State { Widget build(BuildContext context) { room ??= Matrix.of(context).client.getRoomById(widget.id); roomUpdate ??= room.onUpdate.stream.listen((s) => setState(() => null)); + print(Matrix.of(context).client.userDeviceKeys.length); return Scaffold( appBar: AppBar( @@ -59,15 +61,19 @@ class _ChatEncryptionSettingsState extends State { ListTile( title: Text(I18n.of(context).encryptionAlgorithm), subtitle: Text(room.encryptionAlgorithm ?? I18n.of(context).none), - trailing: Icon(room.encrypted ? Icons.lock : Icons.lock_open), + trailing: Icon(room.encrypted ? Icons.lock : Icons.lock_open, + color: room.encrypted ? Colors.green : Colors.red), onTap: () { if (room.encrypted) return; + if (!Matrix.of(context).encryptionEnabled) { + Toast.show(I18n.of(context).needPantalaimonWarning, context, + duration: 8); + return; + } showDialog( context: context, builder: (BuildContext context) => ConfirmDialog( - I18n.of(context).enableEncryptionWarning + - " " + - I18n.of(context).needPantalaimonWarning, + I18n.of(context).enableEncryptionWarning, I18n.of(context).yes, (context) => Matrix.of(context).tryRequestWithLoadingDialog( room.enableEncryption(), @@ -83,75 +89,77 @@ class _ChatEncryptionSettingsState extends State { ), ), Divider(height: 1), - ListTile( - title: Text( - "${I18n.of(context).participatingUserDevices}:", - style: TextStyle( - fontWeight: FontWeight.bold, + if (room.encrypted) + ListTile( + title: Text( + "${I18n.of(context).participatingUserDevices}:", + style: TextStyle( + fontWeight: FontWeight.bold, + ), ), ), - ), - Divider(height: 1), - FutureBuilder>( - future: room.getUserDeviceKeys(), - builder: (BuildContext context, snapshot) { - if (snapshot.hasError) { - return Center( - child: Text(I18n.of(context).oopsSomethingWentWrong + - ": " + - snapshot.error.toString()), - ); - } - if (!snapshot.hasData) { - return Center(child: CircularProgressIndicator()); - } - final List deviceKeys = snapshot.data; - return Expanded( - child: ListView.separated( - separatorBuilder: (BuildContext context, int i) => - Divider(height: 1), - itemCount: deviceKeys.length, - itemBuilder: (BuildContext context, int i) => - CheckboxListTile( - title: Text( - "${deviceKeys[i].userId} - ${deviceKeys[i].deviceId}", - style: TextStyle( - color: deviceKeys[i].blocked - ? Colors.red - : deviceKeys[i].verified - ? Colors.green - : Colors.orange), - ), - subtitle: Text( - deviceKeys[i] - .keys["ed25519:${deviceKeys[i].deviceId}"] - .beautified, - style: TextStyle(color: Colors.black), - ), - value: deviceKeys[i].verified, - onChanged: (bool newVal) { - if (newVal == true) { - if (deviceKeys[i].blocked) { - deviceKeys[i] - .setBlocked(false, Matrix.of(context).client); - } + if (room.encrypted) Divider(height: 1), + if (room.encrypted) + FutureBuilder>( + future: room.getUserDeviceKeys(), + builder: (BuildContext context, snapshot) { + if (snapshot.hasError) { + return Center( + child: Text(I18n.of(context).oopsSomethingWentWrong + + ": " + + snapshot.error.toString()), + ); + } + if (!snapshot.hasData) { + return Center(child: CircularProgressIndicator()); + } + final List deviceKeys = snapshot.data; + return Expanded( + child: ListView.separated( + separatorBuilder: (BuildContext context, int i) => + Divider(height: 1), + itemCount: deviceKeys.length, + itemBuilder: (BuildContext context, int i) => + CheckboxListTile( + title: Text( + "${deviceKeys[i].userId} - ${deviceKeys[i].deviceId}", + style: TextStyle( + color: deviceKeys[i].blocked + ? Colors.red + : deviceKeys[i].verified + ? Colors.green + : Colors.orange), + ), + subtitle: Text( deviceKeys[i] - .setVerified(true, Matrix.of(context).client); - } else { - if (deviceKeys[i].verified) { + .keys["ed25519:${deviceKeys[i].deviceId}"] + .beautified, + style: TextStyle(color: Colors.black), + ), + value: deviceKeys[i].verified, + onChanged: (bool newVal) { + if (newVal == true) { + if (deviceKeys[i].blocked) { + deviceKeys[i] + .setBlocked(false, Matrix.of(context).client); + } deviceKeys[i] - .setVerified(false, Matrix.of(context).client); + .setVerified(true, Matrix.of(context).client); + } else { + if (deviceKeys[i].verified) { + deviceKeys[i] + .setVerified(false, Matrix.of(context).client); + } + deviceKeys[i] + .setBlocked(true, Matrix.of(context).client); } - deviceKeys[i] - .setBlocked(true, Matrix.of(context).client); - } - setState(() => null); - }, + setState(() => null); + }, + ), ), - ), - ); - }, - ), + ); + }, + ), ], ), ); diff --git a/pubspec.lock b/pubspec.lock index a8fe2cf6..73f0fa38 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -110,8 +110,8 @@ packages: dependency: "direct main" description: path: "." - ref: "166b1a9464843cd17e86d9330ac8782d6081f46b" - resolved-ref: "166b1a9464843cd17e86d9330ac8782d6081f46b" + ref: "6a6ca40e96712d2ad3638fec7ec3ec5b17eaedca" + resolved-ref: "6a6ca40e96712d2ad3638fec7ec3ec5b17eaedca" url: "https://gitlab.com/famedly/famedlysdk.git" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index f344d881..f475051b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: famedlysdk: git: url: https://gitlab.com/famedly/famedlysdk.git - ref: 166b1a9464843cd17e86d9330ac8782d6081f46b + ref: 4bf6a4bcb60ca8877b5f2d435978e47358c20cad localstorage: ^3.0.1+4 bubble: ^1.1.9+1 diff --git a/test/widget_test.dart b/test/widget_test.dart index 31a883bd..3685e300 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -7,8 +7,6 @@ import 'package:flutter_test/flutter_test.dart'; -import 'package:fluffychat/main.dart'; - void main() { testWidgets('Test if the app starts', (WidgetTester tester) async { // Build our app and trigger a frame.