feat: Implement new status feature
This commit is contained in:
@ -11,7 +11,7 @@ class ConnectionStatusHeader extends StatefulWidget {
|
||||
class _ConnectionStatusHeaderState extends State<ConnectionStatusHeader> {
|
||||
StreamSubscription _onSyncSub;
|
||||
StreamSubscription _onSyncErrorSub;
|
||||
static bool _connected = false;
|
||||
static bool _connected = true;
|
||||
|
||||
set connected(bool connected) {
|
||||
if (mounted) {
|
||||
|
||||
@ -1,63 +0,0 @@
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/utils/app_route.dart';
|
||||
import 'package:fluffychat/utils/presence_extension.dart';
|
||||
import 'package:fluffychat/views/chat.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
||||
import '../avatar.dart';
|
||||
import '../matrix.dart';
|
||||
|
||||
class PresenceDialog extends StatelessWidget {
|
||||
final Uri avatarUrl;
|
||||
final String displayname;
|
||||
final Presence presence;
|
||||
|
||||
const PresenceDialog(
|
||||
this.presence, {
|
||||
this.avatarUrl,
|
||||
this.displayname,
|
||||
Key key,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Avatar(avatarUrl, displayname),
|
||||
title: Text(displayname),
|
||||
subtitle: Text(presence.senderId),
|
||||
),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
Text(presence.getLocalizedStatusMessage(context)),
|
||||
],
|
||||
),
|
||||
actions: <Widget>[
|
||||
if (presence.senderId != Matrix.of(context).client.userID)
|
||||
FlatButton(
|
||||
child: Text(L10n.of(context).sendAMessage),
|
||||
onPressed: () async {
|
||||
final roomId = await User(
|
||||
presence.senderId,
|
||||
room: Room(id: '', client: Matrix.of(context).client),
|
||||
).startDirectChat();
|
||||
await Navigator.of(context).pushAndRemoveUntil(
|
||||
AppRoute.defaultRoute(
|
||||
context,
|
||||
ChatView(roomId),
|
||||
),
|
||||
(Route r) => r.isFirst);
|
||||
},
|
||||
),
|
||||
FlatButton(
|
||||
child: Text(L10n.of(context).close),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/utils/app_route.dart';
|
||||
import 'package:fluffychat/views/chat.dart';
|
||||
import 'package:fluffychat/views/presence_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../avatar.dart';
|
||||
import '../matrix.dart';
|
||||
|
||||
class PresenceListItem extends StatelessWidget {
|
||||
final Room room;
|
||||
|
||||
const PresenceListItem(this.room);
|
||||
|
||||
void _startChatAction(BuildContext context, String userId) async {
|
||||
final roomId = await User(userId,
|
||||
room: Room(client: Matrix.of(context).client, id: ''))
|
||||
.startDirectChat();
|
||||
await Navigator.of(context).pushAndRemoveUntil(
|
||||
AppRoute.defaultRoute(
|
||||
context,
|
||||
ChatView(roomId),
|
||||
),
|
||||
(Route r) => r.isFirst);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final user = room.getUserByMXIDSync(room.directChatMatrixID);
|
||||
final presence =
|
||||
Matrix.of(context).client.presences[room.directChatMatrixID];
|
||||
final hasStatus = presence?.presence?.statusMsg != null;
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: () => presence?.presence?.statusMsg == null
|
||||
? _startChatAction(context, user.id)
|
||||
: /*showDialog(
|
||||
context: context,
|
||||
builder: (_) => PresenceDialog(
|
||||
presence,
|
||||
avatarUrl: user.avatarUrl,
|
||||
displayname: user.calcDisplayname(),
|
||||
),
|
||||
),*/
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => PresenceView(
|
||||
presence: presence,
|
||||
avatarUrl: user.avatarUrl,
|
||||
displayname: user.calcDisplayname(),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Container(
|
||||
width: 76,
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
SizedBox(height: 10),
|
||||
Container(
|
||||
child: Stack(
|
||||
children: [
|
||||
Avatar(user.avatarUrl, user.calcDisplayname()),
|
||||
if (presence?.presence?.currentlyActive == true)
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
right: 0,
|
||||
child: Container(
|
||||
width: 10,
|
||||
height: 10,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
color: Colors.green,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
width: 1,
|
||||
color: !hasStatus
|
||||
? Theme.of(context).secondaryHeaderColor
|
||||
: Theme.of(context).primaryColor,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(80),
|
||||
),
|
||||
padding: EdgeInsets.all(2),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 6.0, top: 0.0, right: 6.0),
|
||||
child: Text(
|
||||
user.calcDisplayname().trim().split(' ').first,
|
||||
overflow: TextOverflow.clip,
|
||||
maxLines: 1,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context)
|
||||
.textTheme
|
||||
.bodyText2
|
||||
.color
|
||||
.withOpacity(hasStatus ? 1 : 0.66),
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
83
lib/components/list_items/status_list_item.dart
Normal file
83
lib/components/list_items/status_list_item.dart
Normal file
@ -0,0 +1,83 @@
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/utils/user_status.dart';
|
||||
import 'package:fluffychat/views/status_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../avatar.dart';
|
||||
import '../matrix.dart';
|
||||
|
||||
class StatusListItem extends StatelessWidget {
|
||||
final UserStatus status;
|
||||
|
||||
const StatusListItem(this.status, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final client = Matrix.of(context).client;
|
||||
return FutureBuilder<Profile>(
|
||||
future: client.getProfileFromUserId(status.userId),
|
||||
builder: (context, snapshot) {
|
||||
final profile =
|
||||
snapshot.data ?? Profile(client.userID, Uri.parse(''));
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => StatusView(
|
||||
status: status,
|
||||
avatarUrl: profile.avatarUrl,
|
||||
displayname: profile.displayname,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Container(
|
||||
width: 76,
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
SizedBox(height: 10),
|
||||
Container(
|
||||
child: Stack(
|
||||
children: [
|
||||
Avatar(profile.avatarUrl, profile.displayname),
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
right: 0,
|
||||
child: Container(
|
||||
width: 10,
|
||||
height: 10,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
color: Colors.green,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
width: 1,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(80),
|
||||
),
|
||||
padding: EdgeInsets.all(2),
|
||||
),
|
||||
Padding(
|
||||
padding:
|
||||
const EdgeInsets.only(left: 6.0, top: 0.0, right: 6.0),
|
||||
child: Text(
|
||||
profile.displayname.trim().split(' ').first,
|
||||
overflow: TextOverflow.clip,
|
||||
maxLines: 1,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).textTheme.bodyText2.color,
|
||||
fontSize: 13,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/encryption.dart';
|
||||
@ -7,6 +8,7 @@ import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
|
||||
import 'package:fluffychat/utils/firebase_controller.dart';
|
||||
import 'package:fluffychat/utils/matrix_locals.dart';
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/utils/user_status.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
@ -17,6 +19,7 @@ import 'package:url_launcher/url_launcher.dart';
|
||||
import '../utils/app_route.dart';
|
||||
import '../utils/beautify_string_extension.dart';
|
||||
import '../utils/famedlysdk_store.dart';
|
||||
import '../utils/presence_extension.dart';
|
||||
import '../views/key_verification.dart';
|
||||
import 'avatar.dart';
|
||||
|
||||
@ -106,6 +109,7 @@ class MatrixState extends State<Matrix> {
|
||||
StreamSubscription onNotification;
|
||||
StreamSubscription<html.Event> onFocusSub;
|
||||
StreamSubscription<html.Event> onBlurSub;
|
||||
StreamSubscription onPresenceSub;
|
||||
|
||||
void onJitsiCall(EventUpdate eventUpdate) {
|
||||
final event = Event.fromJson(
|
||||
@ -191,6 +195,16 @@ class MatrixState extends State<Matrix> {
|
||||
@override
|
||||
void initState() {
|
||||
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) {
|
||||
debugPrint('[Matrix] Init matrix client');
|
||||
final Set verificationMethods = <KeyVerificationMethod>{
|
||||
@ -206,6 +220,9 @@ class MatrixState extends State<Matrix> {
|
||||
importantStateEvents: <String>{
|
||||
'im.ponies.room_emotes', // we want emotes to work properly
|
||||
});
|
||||
onPresenceSub ??= client.onPresence.stream
|
||||
.where((p) => p.isUserStatus)
|
||||
.listen(_storeUserStatus);
|
||||
onJitsiCallSub ??= client.onEvent.stream
|
||||
.where((e) =>
|
||||
e.type == 'timeline' &&
|
||||
@ -213,6 +230,7 @@ class MatrixState extends State<Matrix> {
|
||||
e.content['content']['msgtype'] == Matrix.callNamespace &&
|
||||
e.content['sender'] != client.userID)
|
||||
.listen(onJitsiCall);
|
||||
|
||||
onRoomKeyRequestSub ??=
|
||||
client.onRoomKeyRequest.stream.listen((RoomKeyRequest request) async {
|
||||
final room = request.room;
|
||||
@ -285,11 +303,54 @@ class MatrixState extends State<Matrix> {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
List<UserStatus> userStatuses = [];
|
||||
|
||||
void _storeUserStatus(Presence presence) {
|
||||
final currentStatusIndex =
|
||||
userStatuses.indexWhere((u) => u.userId == presence.senderId);
|
||||
final newUserStatus = UserStatus()
|
||||
..receivedAt = DateTime.now().millisecondsSinceEpoch
|
||||
..statusMsg = presence.presence.statusMsg
|
||||
..userId = presence.senderId;
|
||||
if (currentStatusIndex == -1) {
|
||||
userStatuses.add(newUserStatus);
|
||||
} else if (userStatuses[currentStatusIndex].statusMsg !=
|
||||
presence.presence.statusMsg) {
|
||||
if (presence.presence.statusMsg.trim().isEmpty) {
|
||||
userStatuses.removeAt(currentStatusIndex);
|
||||
} else {
|
||||
userStatuses[currentStatusIndex] = newUserStatus;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
_cleanUpUserStatus();
|
||||
}
|
||||
|
||||
void _cleanUpUserStatus() {
|
||||
final now = DateTime.now().millisecondsSinceEpoch;
|
||||
userStatuses
|
||||
.removeWhere((u) => (now - u.receivedAt) > (1000 * 60 * 60 * 24));
|
||||
userStatuses.sort((a, b) => b.receivedAt.compareTo(a.receivedAt));
|
||||
if (userStatuses.length > 40) {
|
||||
userStatuses.removeRange(40, userStatuses.length);
|
||||
}
|
||||
store.setItem(
|
||||
'fluffychat.user_statuses',
|
||||
jsonEncode(
|
||||
{
|
||||
'user_statuses': userStatuses.map((i) => i.toJson()).toList(),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
onRoomKeyRequestSub?.cancel();
|
||||
onKeyVerificationRequestSub?.cancel();
|
||||
onJitsiCallSub?.cancel();
|
||||
onPresenceSub?.cancel();
|
||||
onNotification?.cancel();
|
||||
onFocusSub?.cancel();
|
||||
onBlurSub?.cancel();
|
||||
|
||||
Reference in New Issue
Block a user