From 2eac52a006e93c99bc4d677546758ba1a0e46b9a Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Sat, 14 May 2022 09:51:21 +0200 Subject: [PATCH] refactor: Update Matrix SDK --- lib/pages/chat/add_widget_tile.dart | 1 - .../user_bottom_sheet_view.dart | 2 +- .../client_presence_extension.dart | 23 ++++++++++--------- .../presence_extension.dart | 21 ++++++++--------- lib/utils/room_status_extension.dart | 19 ++++++++------- lib/widgets/contacts_list.dart | 15 +++++------- lib/widgets/matrix.dart | 13 +++++------ pubspec.lock | 2 +- pubspec.yaml | 2 +- test/utils/test_client.dart | 3 ++- 10 files changed, 48 insertions(+), 53 deletions(-) diff --git a/lib/pages/chat/add_widget_tile.dart b/lib/pages/chat/add_widget_tile.dart index 917a13b3..1034083b 100644 --- a/lib/pages/chat/add_widget_tile.dart +++ b/lib/pages/chat/add_widget_tile.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:matrix/matrix.dart'; -import 'package:matrix/widget.dart'; import 'package:fluffychat/pages/chat/add_widget_tile_view.dart'; diff --git a/lib/pages/user_bottom_sheet/user_bottom_sheet_view.dart b/lib/pages/user_bottom_sheet/user_bottom_sheet_view.dart index 3af87d05..9dbb33c0 100644 --- a/lib/pages/user_bottom_sheet/user_bottom_sheet_view.dart +++ b/lib/pages/user_bottom_sheet/user_bottom_sheet_view.dart @@ -136,7 +136,7 @@ class UserBottomSheetView extends StatelessWidget { subtitle: Text(presence.getLocalizedLastActiveAgo(context)), trailing: Icon(Icons.circle, - color: presence.presence.currentlyActive ?? false + color: presence.presence == PresenceType.online ? Colors.green : Colors.grey), ), diff --git a/lib/utils/matrix_sdk_extensions.dart/client_presence_extension.dart b/lib/utils/matrix_sdk_extensions.dart/client_presence_extension.dart index fcabae5e..e9d06794 100644 --- a/lib/utils/matrix_sdk_extensions.dart/client_presence_extension.dart +++ b/lib/utils/matrix_sdk_extensions.dart/client_presence_extension.dart @@ -1,7 +1,7 @@ import 'package:matrix/matrix.dart'; extension ClientPresenceExtension on Client { - List get contactList { + List get contactList { final directChatsMxid = rooms .where((r) => r.isDirectChat) .map((r) => r.directChatMatrixID) @@ -10,20 +10,21 @@ extension ClientPresenceExtension on Client { .map( (mxid) => presences[mxid] ?? - Presence.fromJson( - { - 'sender': mxid, - 'type': 'm.presence', - 'content': {'presence': 'offline'}, - }, + CachedPresence( + PresenceType.offline, + 0, + null, + false, + mxid ?? '', ), ) .toList(); - contactList.sort((a, b) => a.senderId.compareTo(b.senderId)); - contactList.sort((a, b) => (a.presence.lastActiveAgo?.toDouble() ?? - double.infinity) - .compareTo((b.presence.lastActiveAgo?.toDouble() ?? double.infinity))); + contactList.sort((a, b) => a.userid.compareTo(b.userid)); + contactList.sort((a, b) => ((a.lastActiveTimestamp ?? + DateTime.fromMillisecondsSinceEpoch(0)) + .compareTo( + b.lastActiveTimestamp ?? DateTime.fromMillisecondsSinceEpoch(0)))); return contactList; } } diff --git a/lib/utils/matrix_sdk_extensions.dart/presence_extension.dart b/lib/utils/matrix_sdk_extensions.dart/presence_extension.dart index 1698e537..bb6e1069 100644 --- a/lib/utils/matrix_sdk_extensions.dart/presence_extension.dart +++ b/lib/utils/matrix_sdk_extensions.dart/presence_extension.dart @@ -5,30 +5,29 @@ import 'package:matrix/matrix.dart'; import '../date_time_extension.dart'; -extension PresenceExtension on Presence { +extension PresenceExtension on CachedPresence { String getLocalizedLastActiveAgo(BuildContext context) { - if (presence.lastActiveAgo != null && presence.lastActiveAgo != 0) { - return L10n.of(context)!.lastActiveAgo( - DateTime.fromMillisecondsSinceEpoch( - DateTime.now().millisecondsSinceEpoch - - presence.lastActiveAgo!) - .localizedTimeShort(context)); + final lastActiveTimestamp = this.lastActiveTimestamp; + if (lastActiveTimestamp != null) { + return L10n.of(context)! + .lastActiveAgo(lastActiveTimestamp.localizedTimeShort(context)); } return L10n.of(context)!.lastSeenLongTimeAgo; } String getLocalizedStatusMessage(BuildContext context) { - if (presence.statusMsg?.isNotEmpty ?? false) { - return presence.statusMsg!; + final statusMsg = this.statusMsg; + if (statusMsg != null && statusMsg.isNotEmpty) { + return statusMsg; } - if (presence.currentlyActive ?? false) { + if (currentlyActive ?? false) { return L10n.of(context)!.currentlyActive; } return getLocalizedLastActiveAgo(context); } Color get color { - switch (presence.presence) { + switch (presence) { case PresenceType.online: return Colors.green; case PresenceType.offline: diff --git a/lib/utils/room_status_extension.dart b/lib/utils/room_status_extension.dart index fcbf4bcf..5468010c 100644 --- a/lib/utils/room_status_extension.dart +++ b/lib/utils/room_status_extension.dart @@ -8,26 +8,25 @@ import 'date_time_extension.dart'; import 'matrix_sdk_extensions.dart/filtered_timeline_extension.dart'; extension RoomStatusExtension on Room { - Presence? get directChatPresence => client.presences[directChatMatrixID]; + CachedPresence? get directChatPresence => + client.presences[directChatMatrixID]; String getLocalizedStatus(BuildContext context) { if (isDirectChat) { final directChatPresence = this.directChatPresence; if (directChatPresence != null && - (directChatPresence.presence.lastActiveAgo != null || - directChatPresence.presence.currentlyActive != null)) { - if (directChatPresence.presence.statusMsg?.isNotEmpty ?? false) { - return directChatPresence.presence.statusMsg!; + (directChatPresence.lastActiveTimestamp != null || + directChatPresence.currentlyActive != null)) { + if (directChatPresence.statusMsg?.isNotEmpty ?? false) { + return directChatPresence.statusMsg!; } - if (directChatPresence.presence.currentlyActive == true) { + if (directChatPresence.currentlyActive == true) { return L10n.of(context)!.currentlyActive; } - if (directChatPresence.presence.lastActiveAgo == null) { + if (directChatPresence.lastActiveTimestamp == null) { return L10n.of(context)!.lastSeenLongTimeAgo; } - final time = DateTime.fromMillisecondsSinceEpoch( - DateTime.now().millisecondsSinceEpoch - - directChatPresence.presence.lastActiveAgo!); + final time = directChatPresence.lastActiveTimestamp!; return L10n.of(context)! .lastActiveAgo(time.localizedTimeShort(context)); } diff --git a/lib/widgets/contacts_list.dart b/lib/widgets/contacts_list.dart index d51cf4a5..a021d2ac 100644 --- a/lib/widgets/contacts_list.dart +++ b/lib/widgets/contacts_list.dart @@ -54,7 +54,7 @@ class _ContactsState extends State { final contactList = Matrix.of(context) .client .contactList - .where((p) => p.senderId + .where((p) => p.userid .toLowerCase() .contains(widget.searchController.text.toLowerCase())) .toList(); @@ -66,17 +66,16 @@ class _ContactsState extends State { } class _ContactListTile extends StatelessWidget { - final Presence contact; + final CachedPresence contact; const _ContactListTile({Key? key, required this.contact}) : super(key: key); @override Widget build(BuildContext context) { return FutureBuilder( - future: - Matrix.of(context).client.getProfileFromUserId(contact.senderId), + future: Matrix.of(context).client.getProfileFromUserId(contact.userid), builder: (context, snapshot) { final displayname = snapshot.data?.displayName ?? - contact.senderId.localpart ?? + contact.userid.localpart ?? 'No valid MXID'; final avatarUrl = snapshot.data?.avatarUrl; return ListTile( @@ -104,7 +103,7 @@ class _ContactListTile extends StatelessWidget { ), title: Text(displayname), subtitle: Text(contact.getLocalizedStatusMessage(context), - style: contact.presence.statusMsg?.isNotEmpty ?? false + style: contact.statusMsg?.isNotEmpty ?? false ? TextStyle( color: Theme.of(context).colorScheme.secondary, fontWeight: FontWeight.bold, @@ -112,9 +111,7 @@ class _ContactListTile extends StatelessWidget { : null), onTap: () => VRouter.of(context).toSegments([ 'rooms', - Matrix.of(context) - .client - .getDirectChatFromUserId(contact.senderId)! + Matrix.of(context).client.getDirectChatFromUserId(contact.userid)! ]), ); }); diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index 1bb296b7..0890db34 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -224,7 +224,7 @@ class MatrixState extends State with WidgetsBindingObserver { final onUiaRequest = >{}; StreamSubscription? onFocusSub; StreamSubscription? onBlurSub; - final onOwnPresence = >{}; + final onOwnPresence = >{}; String? _cachedPassword; Timer? _cachedPasswordClearTimer; @@ -342,13 +342,12 @@ class MatrixState extends State with WidgetsBindingObserver { } }); // Cache and resend status message - onOwnPresence[name] ??= c.onPresence.stream.listen((presence) { + onOwnPresence[name] ??= c.onPresenceChanged.stream.listen((presence) { if (c.isLogged() && - c.userID == presence.senderId && - presence.presence.statusMsg != null) { - Logs().v('Update status message: "${presence.presence.statusMsg}"'); - store.setItem( - SettingKeys.ownStatusMessage, presence.presence.statusMsg); + c.userID == presence.userid && + presence.statusMsg != null) { + Logs().v('Update status message: "${presence.statusMsg}"'); + store.setItem(SettingKeys.ownStatusMessage, presence.statusMsg); } }); onUiaRequest[name] ??= c.onUiaRequest.stream.listen(uiaRequestHandler); diff --git a/pubspec.lock b/pubspec.lock index 5c3d042d..587c421d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1005,7 +1005,7 @@ packages: name: matrix url: "https://pub.dartlang.org" source: hosted - version: "0.8.20" + version: "0.9.4" matrix_api_lite: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c317760d..a2b186b8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,7 +56,7 @@ dependencies: keyboard_shortcuts: ^0.1.4 localstorage: ^4.0.0+1 lottie: ^1.2.2 - matrix: ^0.8.20 + matrix: ^0.9.4 matrix_homeserver_recommendations: ^0.2.0 matrix_link_text: ^1.0.2 native_imaging: diff --git a/test/utils/test_client.dart b/test/utils/test_client.dart index dc84d4b5..7cbf2339 100644 --- a/test/utils/test_client.dart +++ b/test/utils/test_client.dart @@ -6,9 +6,10 @@ import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_ Future prepareTestClient({ bool loggedIn = false, - String homeserver = 'https://fakeserver.notexisting', + Uri? homeserver, String id = 'FluffyChat Widget Test', }) async { + homeserver ??= Uri.parse('https://fakeserver.notexisting'); final client = Client( 'FluffyChat Widget Tests', httpClient: FakeMatrixApi(),