fix: Minor user status bugs

This commit is contained in:
Christian Pauly 2020-10-04 09:16:46 +02:00 committed by Inex Code
parent 1af5ba3314
commit 8dc382aa3d
2 changed files with 32 additions and 28 deletions

View file

@ -1,5 +1,4 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:famedlysdk/encryption.dart'; import 'package:famedlysdk/encryption.dart';
@ -55,6 +54,8 @@ class MatrixState extends State<Matrix> {
@override @override
BuildContext context; BuildContext context;
static const String userStatusesType = 'chat.fluffy.user_statuses';
Map<String, dynamic> get shareContent => _shareContent; Map<String, dynamic> get shareContent => _shareContent;
set shareContent(Map<String, dynamic> content) { set shareContent(Map<String, dynamic> content) {
_shareContent = content; _shareContent = content;
@ -90,6 +91,7 @@ class MatrixState extends State<Matrix> {
widget.clientName, widget.clientName,
); );
} }
_cleanUpUserStatus(userStatuses);
} }
Map<String, dynamic> getAuthByPassword(String password, [String session]) => { Map<String, dynamic> getAuthByPassword(String password, [String session]) => {
@ -195,16 +197,6 @@ class MatrixState extends State<Matrix> {
@override @override
void initState() { void initState() {
store = widget.store ?? Store(); store = widget.store ?? Store();
store.getItem('fluffychat.user_statuses').then(
(json) {
userStatuses = json == null
? []
: (jsonDecode(json)['user_statuses'] as List)
.map((j) => UserStatus.fromJson(j))
.toList();
_cleanUpUserStatus();
},
);
if (widget.client == null) { if (widget.client == null) {
debugPrint('[Matrix] Init matrix client'); debugPrint('[Matrix] Init matrix client');
final Set verificationMethods = <KeyVerificationMethod>{ final Set verificationMethods = <KeyVerificationMethod>{
@ -303,9 +295,18 @@ class MatrixState extends State<Matrix> {
super.initState(); super.initState();
} }
List<UserStatus> userStatuses = []; List<UserStatus> get userStatuses {
try {
return (client.accountData[userStatusesType].content['user_statuses']
as List)
.map((json) => UserStatus.fromJson(json))
.toList();
} catch (_) {}
return [];
}
void _storeUserStatus(Presence presence) { void _storeUserStatus(Presence presence) {
final tmpUserStatuses = List<UserStatus>.from(userStatuses);
final currentStatusIndex = final currentStatusIndex =
userStatuses.indexWhere((u) => u.userId == presence.senderId); userStatuses.indexWhere((u) => u.userId == presence.senderId);
final newUserStatus = UserStatus() final newUserStatus = UserStatus()
@ -313,37 +314,38 @@ class MatrixState extends State<Matrix> {
..statusMsg = presence.presence.statusMsg ..statusMsg = presence.presence.statusMsg
..userId = presence.senderId; ..userId = presence.senderId;
if (currentStatusIndex == -1) { if (currentStatusIndex == -1) {
userStatuses.add(newUserStatus); tmpUserStatuses.add(newUserStatus);
} else if (userStatuses[currentStatusIndex].statusMsg != } else if (tmpUserStatuses[currentStatusIndex].statusMsg !=
presence.presence.statusMsg) { presence.presence.statusMsg) {
if (presence.presence.statusMsg.trim().isEmpty) { if (presence.presence.statusMsg.trim().isEmpty) {
userStatuses.removeAt(currentStatusIndex); tmpUserStatuses.removeAt(currentStatusIndex);
} else { } else {
userStatuses[currentStatusIndex] = newUserStatus; tmpUserStatuses[currentStatusIndex] = newUserStatus;
} }
} else { } else {
return; return;
} }
_cleanUpUserStatus(); _cleanUpUserStatus(tmpUserStatuses);
} }
void _cleanUpUserStatus() { void _cleanUpUserStatus(List<UserStatus> tmpUserStatuses) {
final now = DateTime.now().millisecondsSinceEpoch; final now = DateTime.now().millisecondsSinceEpoch;
userStatuses tmpUserStatuses
.removeWhere((u) => (now - u.receivedAt) > (1000 * 60 * 60 * 24)); .removeWhere((u) => (now - u.receivedAt) > (1000 * 60 * 60 * 24));
userStatuses.sort((a, b) => b.receivedAt.compareTo(a.receivedAt)); tmpUserStatuses.sort((a, b) => b.receivedAt.compareTo(a.receivedAt));
if (userStatuses.length > 40) { if (tmpUserStatuses.length > 40) {
userStatuses.removeRange(40, userStatuses.length); tmpUserStatuses.removeRange(40, tmpUserStatuses.length);
} }
store.setItem( if (tmpUserStatuses != userStatuses) {
'fluffychat.user_statuses', client.setAccountData(
jsonEncode( client.userID,
userStatusesType,
{ {
'user_statuses': userStatuses.map((i) => i.toJson()).toList(), 'user_statuses': tmpUserStatuses.map((i) => i.toJson()).toList(),
}, },
),
); );
} }
}
@override @override
void dispose() { void dispose() {

View file

@ -81,6 +81,7 @@ class StatusView extends StatelessWidget {
backgroundColor: displayname.color, backgroundColor: displayname.color,
extendBody: true, extendBody: true,
appBar: AppBar( appBar: AppBar(
titleSpacing: 0.0,
brightness: Brightness.dark, brightness: Brightness.dark,
leading: IconButton( leading: IconButton(
icon: Icon( icon: Icon(
@ -100,6 +101,7 @@ class StatusView extends StatelessWidget {
), ),
subtitle: Text( subtitle: Text(
status?.userId ?? Matrix.of(context).client.userID, status?.userId ?? Matrix.of(context).client.userID,
maxLines: 1,
style: TextStyle(color: Colors.white), style: TextStyle(color: Colors.white),
), ),
), ),