From 1a2c5c93806be8313bc58358bcb848e3bc70984e Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 22 Feb 2020 09:03:44 +0100 Subject: [PATCH] Clean up --- lib/components/encryption_button.dart | 62 ++++++++++ lib/views/chat.dart | 157 ++++++++++---------------- 2 files changed, 120 insertions(+), 99 deletions(-) create mode 100644 lib/components/encryption_button.dart diff --git a/lib/components/encryption_button.dart b/lib/components/encryption_button.dart new file mode 100644 index 0000000..1a23f61 --- /dev/null +++ b/lib/components/encryption_button.dart @@ -0,0 +1,62 @@ +import 'dart:async'; + +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:fluffychat/utils/app_route.dart'; +import 'package:fluffychat/views/chat_encryption_settings.dart'; +import 'package:flutter/material.dart'; + +import 'matrix.dart'; + +class EncryptionButton extends StatefulWidget { + final Room room; + const EncryptionButton(this.room, {Key key}) : super(key: key); + @override + _EncryptionButtonState createState() => _EncryptionButtonState(); +} + +class _EncryptionButtonState extends State { + StreamSubscription _onSyncSub; + + @override + void dispose() { + _onSyncSub?.cancel(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + _onSyncSub ??= Matrix.of(context) + .client + .onSync + .stream + .listen((s) => setState(() => null)); + return FutureBuilder>( + future: widget.room.getUserDeviceKeys(), + builder: (BuildContext context, snapshot) { + Color color; + if (widget.room.encrypted && snapshot.hasData) { + final List deviceKeysList = snapshot.data; + color = Colors.orange; + if (deviceKeysList.indexWhere((DeviceKeys deviceKeys) => + deviceKeys.verified == false && + deviceKeys.blocked == false) == + -1) { + color = Colors.black.withGreen(220).withOpacity(0.75); + } + } else if (!widget.room.encrypted && + widget.room.joinRules != JoinRules.public) { + color = null; + } + return IconButton( + icon: Icon(widget.room.encrypted ? Icons.lock : Icons.lock_open, + size: 20, color: color), + onPressed: () => Navigator.of(context).push( + AppRoute.defaultRoute( + context, + ChatEncryptionSettingsView(widget.room.id), + ), + ), + ); + }); + } +} diff --git a/lib/views/chat.dart b/lib/views/chat.dart index 90e5029..0e5263b 100644 --- a/lib/views/chat.dart +++ b/lib/views/chat.dart @@ -6,6 +6,7 @@ import 'package:file_picker/file_picker.dart'; import 'package:fluffychat/components/adaptive_page_layout.dart'; import 'package:fluffychat/components/chat_settings_popup_menu.dart'; import 'package:fluffychat/components/dialogs/simple_dialogs.dart'; +import 'package:fluffychat/components/encryption_button.dart'; import 'package:fluffychat/components/list_items/message.dart'; import 'package:fluffychat/components/matrix.dart'; import 'package:fluffychat/components/reply_content.dart'; @@ -80,6 +81,8 @@ class _ChatState extends State<_Chat> { final int _loadHistoryCount = 100; + String inputText = ""; + void requestHistory() async { if (timeline.events.last.type != EventTypes.RoomCreate) { setState(() => this._loadingHistory = true); @@ -550,108 +553,63 @@ class _ChatState extends State<_Chat> { : Container(), ] : [ - kIsWeb - ? Container() - : PopupMenuButton( - icon: Icon(Icons.add), - onSelected: (String choice) async { - if (choice == "file") { - sendFileAction(context); - } else if (choice == "image") { - sendImageAction(context); - } - if (choice == "camera") { - openCameraAction(context); - } - }, - itemBuilder: (BuildContext context) => - >[ - PopupMenuItem( - value: "file", - child: ListTile( - leading: CircleAvatar( - backgroundColor: Colors.green, - foregroundColor: Colors.white, - child: Icon(Icons.attachment), - ), - title: Text( - I18n.of(context).sendFile), - contentPadding: - EdgeInsets.all(0), - ), - ), - PopupMenuItem( - value: "image", - child: ListTile( - leading: CircleAvatar( - backgroundColor: Colors.blue, - foregroundColor: Colors.white, - child: Icon(Icons.image), - ), - title: Text( - I18n.of(context).sendImage), - contentPadding: - EdgeInsets.all(0), - ), - ), - PopupMenuItem( - value: "camera", - child: ListTile( - leading: CircleAvatar( - backgroundColor: - Colors.purple, - foregroundColor: Colors.white, - child: Icon(Icons.camera_alt), - ), - title: Text(I18n.of(context) - .openCamera), - contentPadding: - EdgeInsets.all(0), - ), - ), - ], - ), - FutureBuilder>( - future: room.getUserDeviceKeys(), - builder: - (BuildContext context, snapshot) { - Color color; - if (room.encrypted && - snapshot.hasData) { - final List - deviceKeysList = snapshot.data; - color = Colors.orange; - if (deviceKeysList.indexWhere( - (DeviceKeys deviceKeys) => - deviceKeys.verified == - false && - deviceKeys.blocked == - false) == - -1) { - color = Colors.green[700]; - } - } else if (!room.encrypted && - room.joinRules != - JoinRules.public) { - color = Colors.red; + if (!kIsWeb && inputText.isEmpty) + PopupMenuButton( + icon: Icon(Icons.add), + onSelected: (String choice) async { + if (choice == "file") { + sendFileAction(context); + } else if (choice == "image") { + sendImageAction(context); } - return IconButton( - icon: Icon( - room.encrypted - ? Icons.lock - : Icons.lock_open, - size: 20, - color: color), - onPressed: () => - Navigator.of(context).push( - AppRoute.defaultRoute( - context, - ChatEncryptionSettingsView( - widget.id), + if (choice == "camera") { + openCameraAction(context); + } + }, + itemBuilder: (BuildContext context) => + >[ + PopupMenuItem( + value: "file", + child: ListTile( + leading: CircleAvatar( + backgroundColor: Colors.green, + foregroundColor: Colors.white, + child: Icon(Icons.attachment), ), + title: + Text(I18n.of(context).sendFile), + contentPadding: EdgeInsets.all(0), ), - ); - }), + ), + PopupMenuItem( + value: "image", + child: ListTile( + leading: CircleAvatar( + backgroundColor: Colors.blue, + foregroundColor: Colors.white, + child: Icon(Icons.image), + ), + title: Text( + I18n.of(context).sendImage), + contentPadding: EdgeInsets.all(0), + ), + ), + PopupMenuItem( + value: "camera", + child: ListTile( + leading: CircleAvatar( + backgroundColor: Colors.purple, + foregroundColor: Colors.white, + child: Icon(Icons.camera_alt), + ), + title: Text( + I18n.of(context).openCamera), + contentPadding: EdgeInsets.all(0), + ), + ), + ], + ), + EncryptionButton(room), Expanded( child: Padding( padding: const EdgeInsets.symmetric( @@ -693,6 +651,7 @@ class _ChatState extends State<_Chat> { timeout: Duration(seconds: 30) .inMilliseconds); } + setState(() => inputText = text); }, ), ),