From fda9b752df5f4900d588494d1ff0fcfd67672534 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 4 Jan 2020 12:53:49 +0000 Subject: [PATCH] Fix message types and update to newest sdk --- lib/components/list_items/message.dart | 4 +- lib/components/matrix.dart | 35 +++-- lib/components/message_content.dart | 209 +++++++++++++------------ lib/main.dart | 4 +- lib/utils/sqflite_store.dart | 1 + lib/views/chat_details.dart | 22 ++- pubspec.lock | 4 +- pubspec.yaml | 2 +- 8 files changed, 149 insertions(+), 132 deletions(-) diff --git a/lib/components/list_items/message.dart b/lib/components/list_items/message.dart index 47ceb0a..21b61e8 100644 --- a/lib/components/list_items/message.dart +++ b/lib/components/list_items/message.dart @@ -16,7 +16,9 @@ class Message extends StatelessWidget { @override Widget build(BuildContext context) { - if (event.typeKey != "m.room.message") return StateMessage(event); + if (![EventTypes.Message, EventTypes.Sticker].contains(event.type)) { + return StateMessage(event); + } Client client = Matrix.of(context).client; final bool ownMessage = event.senderId == client.userID; diff --git a/lib/components/matrix.dart b/lib/components/matrix.dart index 9d50381..9404c3d 100644 --- a/lib/components/matrix.dart +++ b/lib/components/matrix.dart @@ -131,24 +131,29 @@ class MatrixState extends State { StreamSubscription onSetupFirebase; - void setupFirebase(LoginState login) { + void setupFirebase(LoginState login) async { if (login != LoginState.logged) return; if (Platform.isIOS) iOS_Permission(); - _firebaseMessaging.getToken().then((token) { - print("Der token ist: $token"); - client.setPushers( - token, - "http", - "chat.fluffy.fluffychat", - "FluffyChat", - client.deviceName, - "en", - "https://janian.de:7023/", - append: false, - format: "event_id_only", + final String token = await _firebaseMessaging.getToken(); + if (token?.isEmpty ?? true) { + return Toast.show( + "Push notifications disabled.", + context, + duration: Toast.LENGTH_LONG, ); - }); + } + await client.setPushers( + token, + "http", + "chat.fluffy.fluffychat", + widget.clientName, + client.deviceName, + "en", + "https://janian.de:7023/", + append: false, + format: "event_id_only", + ); _firebaseMessaging.configure( onResume: (Map message) async { @@ -172,7 +177,7 @@ class MatrixState extends State { @override void initState() { if (widget.client == null) { - client = Client(widget.clientName, debug: true); + client = Client(widget.clientName, debug: false); if (!kIsWeb) { client.store = Store(client); } else { diff --git a/lib/components/message_content.dart b/lib/components/message_content.dart index 7781e37..6442ede 100644 --- a/lib/components/message_content.dart +++ b/lib/components/message_content.dart @@ -3,7 +3,6 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:link_text/link_text.dart'; import 'package:url_launcher/url_launcher.dart'; import 'matrix.dart'; @@ -19,102 +18,112 @@ class MessageContent extends StatelessWidget { Widget build(BuildContext context) { final int maxLines = textOnly ? 1 : null; + final Widget unknown = Text( + "${event.sender.calcDisplayname()} sent a ${event.typeKey} event", + maxLines: maxLines, + overflow: textOnly ? TextOverflow.ellipsis : null, + style: TextStyle( + color: textColor, + decoration: event.redacted ? TextDecoration.lineThrough : null, + ), + ); + switch (event.type) { - case EventTypes.Image: - if (textOnly) { - return Text( - "${event.sender.calcDisplayname()} has sent an image", - maxLines: maxLines, - style: TextStyle( - color: textColor, - decoration: event.redacted ? TextDecoration.lineThrough : null, - ), - ); + case EventTypes.Message: + case EventTypes.Sticker: + switch (event.messageType) { + case MessageTypes.Image: + case MessageTypes.Sticker: + if (textOnly) { + return Text( + "${event.sender.calcDisplayname()} has sent an image", + maxLines: maxLines, + style: TextStyle( + color: textColor, + decoration: + event.redacted ? TextDecoration.lineThrough : null, + ), + ); + } + final int size = 400; + final String src = MxContent(event.content["url"]).getThumbnail( + Matrix.of(context).client, + width: size * MediaQuery.of(context).devicePixelRatio, + height: size * MediaQuery.of(context).devicePixelRatio, + method: ThumbnailMethod.scale, + ); + return Bubble( + padding: BubbleEdges.all(0), + radius: Radius.circular(10), + elevation: 0, + child: InkWell( + onTap: () => launch( + MxContent(event.content["url"]) + .getDownloadLink(Matrix.of(context).client), + ), + child: kIsWeb + ? Image.network( + src, + width: size.toDouble(), + ) + : CachedNetworkImage( + imageUrl: src, + width: size.toDouble(), + ), + ), + ); + case MessageTypes.Audio: + case MessageTypes.File: + case MessageTypes.Video: + return Container( + width: 200, + child: RaisedButton( + color: Colors.blueGrey, + child: Text( + "Download ${event.getBody()}", + overflow: TextOverflow.fade, + softWrap: false, + maxLines: 1, + ), + onPressed: () => launch( + MxContent(event.content["url"]) + .getDownloadLink(Matrix.of(context).client), + ), + ), + ); + case MessageTypes.Text: + case MessageTypes.Reply: + case MessageTypes.Location: + case MessageTypes.None: + case MessageTypes.Notice: + final String senderPrefix = + textOnly && event.senderId != event.room.directChatMatrixID + ? event.senderId == Matrix.of(context).client.userID + ? "You: " + : "${event.sender.calcDisplayname()}: " + : ""; + return Text( + senderPrefix + event.getBody(), + maxLines: maxLines, + overflow: textOnly ? TextOverflow.ellipsis : null, + style: TextStyle( + color: textColor, + decoration: event.redacted ? TextDecoration.lineThrough : null, + ), + ); + case MessageTypes.Emote: + return Text( + "* " + event.getBody(), + maxLines: maxLines, + overflow: textOnly ? TextOverflow.ellipsis : null, + style: TextStyle( + color: textColor, + fontStyle: FontStyle.italic, + decoration: event.redacted ? TextDecoration.lineThrough : null, + ), + ); } - final int size = 400; - final String src = MxContent(event.content["url"]).getThumbnail( - Matrix.of(context).client, - width: size * MediaQuery.of(context).devicePixelRatio, - height: size * MediaQuery.of(context).devicePixelRatio, - method: ThumbnailMethod.scale, - ); - return Bubble( - padding: BubbleEdges.all(0), - radius: Radius.circular(10), - elevation: 0, - child: InkWell( - onTap: () => launch( - MxContent(event.content["url"]) - .getDownloadLink(Matrix.of(context).client), - ), - child: kIsWeb - ? Image.network( - src, - width: size.toDouble(), - ) - : CachedNetworkImage( - imageUrl: src, - width: size.toDouble(), - ), - ), - ); - case EventTypes.Audio: - case EventTypes.File: - case EventTypes.Video: - return Container( - width: 200, - child: RaisedButton( - color: Colors.blueGrey, - child: Text( - "Download ${event.getBody()}", - overflow: TextOverflow.fade, - softWrap: false, - maxLines: 1, - ), - onPressed: () => launch( - MxContent(event.content["url"]) - .getDownloadLink(Matrix.of(context).client), - ), - ), - ); - case EventTypes.Text: - case EventTypes.Reply: - case EventTypes.Notice: - final String senderPrefix = - textOnly && event.senderId != event.room.directChatMatrixID - ? event.senderId == Matrix.of(context).client.userID - ? "You: " - : "${event.sender.calcDisplayname()}: " - : ""; - if (textOnly) { - return Text( - senderPrefix + event.getBody(), - maxLines: maxLines, - overflow: TextOverflow.ellipsis, - style: TextStyle( - color: textColor, - decoration: event.redacted ? TextDecoration.lineThrough : null, - ), - ); - } - return LinkText( - text: senderPrefix + event.getBody(), - textStyle: TextStyle( - color: textColor, - decoration: event.redacted ? TextDecoration.lineThrough : null, - ), - ); - case EventTypes.Emote: - return Text( - "* " + event.getBody(), - maxLines: maxLines, - overflow: textOnly ? TextOverflow.ellipsis : null, - style: TextStyle( - color: textColor, - fontStyle: FontStyle.italic, - decoration: event.redacted ? TextDecoration.lineThrough : null, - ), - ); + return unknown; case EventTypes.RoomCreate: return Text( "${event.sender.calcDisplayname()} has created the chat", @@ -272,15 +281,7 @@ class MessageContent extends StatelessWidget { ), ); default: - return Text( - "${event.sender.calcDisplayname()} sent a ${event.typeKey} event", - maxLines: maxLines, - overflow: textOnly ? TextOverflow.ellipsis : null, - style: TextStyle( - color: textColor, - decoration: event.redacted ? TextDecoration.lineThrough : null, - ), - ); + return unknown; } } } diff --git a/lib/main.dart b/lib/main.dart index 3978406..5da3264 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,9 +17,9 @@ class App extends StatelessWidget { @override Widget build(BuildContext context) { return Matrix( - clientName: "FluffyWeb", + clientName: "FluffyChat", child: MaterialApp( - title: 'FluffyWeb', + title: 'FluffyChat', theme: ThemeData( brightness: Brightness.light, primaryColor: Color(0xFF5625BA), diff --git a/lib/utils/sqflite_store.dart b/lib/utils/sqflite_store.dart index b92ac53..0cf8a1e 100644 --- a/lib/utils/sqflite_store.dart +++ b/lib/utils/sqflite_store.dart @@ -411,6 +411,7 @@ class Store extends StoreAPI { res[i], client, states: getStatesFromRoomId(res[i]["room_id"]), + roomAccountData: getAccountDataFromRoomId(res[i]["room_id"]), ); roomList.add(room); } diff --git a/lib/views/chat_details.dart b/lib/views/chat_details.dart index 9fa1a17..8236843 100644 --- a/lib/views/chat_details.dart +++ b/lib/views/chat_details.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:famedlysdk/famedlysdk.dart'; @@ -27,11 +28,10 @@ class _ChatDetailsState extends State { List members; void setDisplaynameAction(BuildContext context, String displayname) async { final MatrixState matrix = Matrix.of(context); - final Map success = - await matrix.tryRequestWithLoadingDialog( + final success = await matrix.tryRequestWithLoadingDialog( widget.room.setName(displayname), ); - if (success != null && success.isEmpty) { + if (success != false) { Toast.show( "Displayname has been changed", context, @@ -48,8 +48,7 @@ class _ChatDetailsState extends State { maxHeight: 1600); if (tempFile == null) return; final MatrixState matrix = Matrix.of(context); - final Map success = - await matrix.tryRequestWithLoadingDialog( + final success = await matrix.tryRequestWithLoadingDialog( widget.room.setAvatar( MatrixFile( bytes: await tempFile.readAsBytes(), @@ -57,7 +56,7 @@ class _ChatDetailsState extends State { ), ), ); - if (success != null && success.isEmpty) { + if (success != false) { Toast.show( "Avatar has been changed", context, @@ -72,13 +71,22 @@ class _ChatDetailsState extends State { if (participants != null) setState(() => members = participants); } + StreamSubscription onUpdate; + + @override + void dispose() { + onUpdate?.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { members ??= widget.room.getParticipants(); final int actualMembersCount = widget.room.mInvitedMemberCount + widget.room.mJoinedMemberCount; final bool canRequestMoreMembers = members.length < actualMembersCount; - widget.room.onUpdate = () => setState(() => members = null); + this.onUpdate ??= widget.room.onUpdate.stream + .listen((id) => setState(() => members = null)); return AdaptivePageLayout( primaryPage: FocusPage.SECOND, firstScaffold: ChatList( diff --git a/pubspec.lock b/pubspec.lock index e19a503..eea0e62 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -82,8 +82,8 @@ packages: dependency: "direct main" description: path: "." - ref: c78330ea58c36eec197803eb461681d84fa50f42 - resolved-ref: c78330ea58c36eec197803eb461681d84fa50f42 + ref: "90a06ebce547ed853e501532a03491356a93e483" + resolved-ref: "90a06ebce547ed853e501532a03491356a93e483" url: "https://gitlab.com/famedly/famedlysdk.git" source: git version: "0.0.1" diff --git a/pubspec.yaml b/pubspec.yaml index 8e9ff4a..cec91ec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: famedlysdk: git: url: https://gitlab.com/famedly/famedlysdk.git - ref: c78330ea58c36eec197803eb461681d84fa50f42 + ref: ae1c757e4ec3e7a41a2471e471d7ae47d974e821 localstorage: ^3.0.1+4 bubble: ^1.1.9+1