refactor: Update Matrix SDK

This commit is contained in:
Christian Pauly 2022-05-14 09:51:21 +02:00
parent a6ef4f670d
commit 2eac52a006
10 changed files with 48 additions and 53 deletions

View File

@ -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';

View File

@ -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),
),

View File

@ -1,7 +1,7 @@
import 'package:matrix/matrix.dart';
extension ClientPresenceExtension on Client {
List<Presence> get contactList {
List<CachedPresence> 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;
}
}

View File

@ -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:

View File

@ -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));
}

View File

@ -54,7 +54,7 @@ class _ContactsState extends State<ContactsList> {
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<ContactsList> {
}
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<Profile>(
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)!
]),
);
});

View File

@ -224,7 +224,7 @@ class MatrixState extends State<Matrix> with WidgetsBindingObserver {
final onUiaRequest = <String, StreamSubscription<UiaRequest>>{};
StreamSubscription<html.Event>? onFocusSub;
StreamSubscription<html.Event>? onBlurSub;
final onOwnPresence = <String, StreamSubscription<Presence>>{};
final onOwnPresence = <String, StreamSubscription<CachedPresence>>{};
String? _cachedPassword;
Timer? _cachedPasswordClearTimer;
@ -342,13 +342,12 @@ class MatrixState extends State<Matrix> 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);

View File

@ -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:

View File

@ -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:

View File

@ -6,9 +6,10 @@ import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_
Future<Client> 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(),