2021-06-10 19:32:16 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-08-12 11:30:31 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
|
2020-09-07 17:08:01 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2021-03-14 18:40:05 +01:00
|
|
|
import 'package:characters/characters.dart';
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
2021-11-13 13:06:36 +01:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
|
|
import 'package:fluffychat/widgets/avatar.dart';
|
2021-11-09 21:32:16 +01:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
2020-08-12 11:30:31 +02:00
|
|
|
|
|
|
|
class MessageReactions extends StatelessWidget {
|
|
|
|
final Event event;
|
|
|
|
final Timeline timeline;
|
|
|
|
|
2021-10-14 18:09:30 +02:00
|
|
|
const MessageReactions(this.event, this.timeline, {Key key})
|
|
|
|
: super(key: key);
|
2020-08-12 11:30:31 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final allReactionEvents =
|
2021-04-21 14:19:54 +02:00
|
|
|
event.aggregatedEvents(timeline, RelationshipTypes.reaction);
|
2020-08-12 11:30:31 +02:00
|
|
|
final reactionMap = <String, _ReactionEntry>{};
|
2021-06-10 19:32:16 +02:00
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
|
2020-08-12 11:30:31 +02:00
|
|
|
for (final e in allReactionEvents) {
|
|
|
|
if (e.content['m.relates_to'].containsKey('key')) {
|
|
|
|
final key = e.content['m.relates_to']['key'];
|
|
|
|
if (!reactionMap.containsKey(key)) {
|
|
|
|
reactionMap[key] = _ReactionEntry(
|
|
|
|
key: key,
|
|
|
|
count: 0,
|
|
|
|
reacted: false,
|
2021-06-10 19:32:16 +02:00
|
|
|
reactors: [],
|
2020-08-12 11:30:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
reactionMap[key].count++;
|
2021-06-10 19:32:16 +02:00
|
|
|
reactionMap[key].reactors.add(e.sender);
|
2020-08-12 11:30:31 +02:00
|
|
|
reactionMap[key].reacted |= e.senderId == e.room.client.userID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final reactionList = reactionMap.values.toList();
|
|
|
|
reactionList.sort((a, b) => b.count - a.count > 0 ? 1 : -1);
|
|
|
|
return Wrap(
|
2021-06-10 19:32:16 +02:00
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 4.0,
|
|
|
|
children: reactionList
|
|
|
|
.map(
|
|
|
|
(r) => _Reaction(
|
2020-08-12 11:30:31 +02:00
|
|
|
reactionKey: r.key,
|
|
|
|
count: r.count,
|
|
|
|
reacted: r.reacted,
|
|
|
|
onTap: () {
|
|
|
|
if (r.reacted) {
|
|
|
|
final evt = allReactionEvents.firstWhere(
|
|
|
|
(e) =>
|
|
|
|
e.senderId == e.room.client.userID &&
|
|
|
|
e.content['m.relates_to']['key'] == r.key,
|
|
|
|
orElse: () => null);
|
|
|
|
if (evt != null) {
|
2020-12-25 09:58:34 +01:00
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () => evt.redactEvent(),
|
2020-12-25 09:58:34 +01:00
|
|
|
);
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-25 09:58:34 +01:00
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () =>
|
|
|
|
event.room.sendReaction(event.eventId, r.key));
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|
|
|
|
},
|
2021-06-10 19:32:16 +02:00
|
|
|
onLongPress: () async => await _AdaptableReactorsDialog(
|
|
|
|
client: client,
|
|
|
|
reactionEntry: r,
|
|
|
|
).show(context),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList());
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _Reaction extends StatelessWidget {
|
|
|
|
final String reactionKey;
|
|
|
|
final int count;
|
|
|
|
final bool reacted;
|
|
|
|
final void Function() onTap;
|
2021-06-10 19:32:16 +02:00
|
|
|
final void Function() onLongPress;
|
2020-08-12 11:30:31 +02:00
|
|
|
|
2021-06-10 19:32:16 +02:00
|
|
|
const _Reaction({
|
|
|
|
this.reactionKey,
|
|
|
|
this.count,
|
|
|
|
this.reacted,
|
|
|
|
this.onTap,
|
|
|
|
this.onLongPress,
|
|
|
|
});
|
2020-08-12 11:30:31 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final textColor = Theme.of(context).brightness == Brightness.dark
|
|
|
|
? Colors.white
|
|
|
|
: Colors.black;
|
2021-08-29 12:15:51 +02:00
|
|
|
final color = Theme.of(context).scaffoldBackgroundColor;
|
2020-08-12 11:30:31 +02:00
|
|
|
final fontSize = DefaultTextStyle.of(context).style.fontSize;
|
|
|
|
Widget content;
|
|
|
|
if (reactionKey.startsWith('mxc://')) {
|
|
|
|
final src = Uri.parse(reactionKey)?.getThumbnail(
|
|
|
|
Matrix.of(context).client,
|
|
|
|
width: 9999,
|
|
|
|
height: fontSize * MediaQuery.of(context).devicePixelRatio,
|
|
|
|
method: ThumbnailMethod.scale,
|
|
|
|
);
|
|
|
|
content = Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
2020-10-17 09:29:27 +02:00
|
|
|
CachedNetworkImage(
|
2021-04-21 14:19:54 +02:00
|
|
|
imageUrl: src.toString(),
|
2020-10-17 09:29:27 +02:00
|
|
|
height: fontSize,
|
|
|
|
),
|
2021-11-13 13:06:36 +01:00
|
|
|
const SizedBox(width: 4),
|
2020-08-12 11:30:31 +02:00
|
|
|
Text(count.toString(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: textColor,
|
|
|
|
fontSize: DefaultTextStyle.of(context).style.fontSize,
|
|
|
|
)),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} else {
|
2021-03-14 18:40:05 +01:00
|
|
|
var renderKey = Characters(reactionKey);
|
2020-08-12 11:30:31 +02:00
|
|
|
if (renderKey.length > 10) {
|
2021-03-14 18:40:05 +01:00
|
|
|
renderKey = renderKey.getRange(0, 9) + Characters('…');
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|
|
|
|
content = Text('$renderKey $count',
|
|
|
|
style: TextStyle(
|
|
|
|
color: textColor,
|
|
|
|
fontSize: DefaultTextStyle.of(context).style.fontSize,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return InkWell(
|
2021-03-04 12:28:06 +01:00
|
|
|
onTap: () => onTap != null ? onTap() : null,
|
2021-06-10 19:32:16 +02:00
|
|
|
onLongPress: () => onLongPress != null ? onLongPress() : null,
|
2021-11-13 13:06:36 +01:00
|
|
|
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
|
2020-08-12 11:30:31 +02:00
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: color,
|
2021-11-13 19:22:11 +01:00
|
|
|
border: reacted
|
|
|
|
? Border.all(
|
|
|
|
width: 1,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
)
|
|
|
|
: null,
|
2021-11-13 13:06:36 +01:00
|
|
|
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
|
2020-08-12 11:30:31 +02:00
|
|
|
),
|
2021-11-13 13:06:36 +01:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 3),
|
2020-08-12 11:30:31 +02:00
|
|
|
child: content,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ReactionEntry {
|
|
|
|
String key;
|
|
|
|
int count;
|
|
|
|
bool reacted;
|
2021-06-10 19:32:16 +02:00
|
|
|
List<User> reactors;
|
|
|
|
|
|
|
|
_ReactionEntry({this.key, this.count, this.reacted, this.reactors});
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AdaptableReactorsDialog extends StatelessWidget {
|
2021-06-23 15:35:23 +02:00
|
|
|
final Client client;
|
2021-06-10 19:32:16 +02:00
|
|
|
final _ReactionEntry reactionEntry;
|
2020-08-12 11:30:31 +02:00
|
|
|
|
2021-06-10 19:32:16 +02:00
|
|
|
const _AdaptableReactorsDialog({
|
|
|
|
Key key,
|
|
|
|
this.client,
|
|
|
|
this.reactionEntry,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
Future<bool> show(BuildContext context) => PlatformInfos.isCupertinoStyle
|
|
|
|
? showCupertinoDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => this,
|
|
|
|
barrierDismissible: true,
|
|
|
|
useRootNavigator: false,
|
|
|
|
)
|
|
|
|
: showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => this,
|
|
|
|
barrierDismissible: true,
|
|
|
|
useRootNavigator: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final body = SingleChildScrollView(
|
|
|
|
child: Wrap(
|
|
|
|
spacing: 8.0,
|
|
|
|
runSpacing: 4.0,
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
for (var reactor in reactionEntry.reactors)
|
|
|
|
Chip(
|
|
|
|
avatar: Avatar(
|
|
|
|
reactor.avatarUrl,
|
|
|
|
reactor.displayName,
|
|
|
|
client: client,
|
|
|
|
),
|
|
|
|
label: Text(reactor.displayName),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
final title = Center(child: Text(reactionEntry.key));
|
|
|
|
|
|
|
|
return PlatformInfos.isCupertinoStyle
|
|
|
|
? CupertinoAlertDialog(
|
|
|
|
title: title,
|
|
|
|
content: body,
|
|
|
|
)
|
|
|
|
: AlertDialog(
|
|
|
|
title: title,
|
|
|
|
content: body,
|
|
|
|
);
|
|
|
|
}
|
2020-08-12 11:30:31 +02:00
|
|
|
}
|