fluffychat/lib/pages/chat/events/message_reactions.dart

229 lines
6.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
2022-01-29 11:35:03 +00:00
import 'package:collection/collection.dart' show IterableExtension;
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
2021-11-13 12:06:36 +00:00
import 'package:fluffychat/config/app_config.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/widgets/avatar.dart';
2021-11-09 20:32:16 +00:00
import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/mxc_image.dart';
class MessageReactions extends StatelessWidget {
final Event event;
final Timeline timeline;
const MessageReactions(this.event, this.timeline, {super.key});
@override
Widget build(BuildContext context) {
final allReactionEvents =
2021-04-21 12:19:54 +00:00
event.aggregatedEvents(timeline, RelationshipTypes.reaction);
final reactionMap = <String, _ReactionEntry>{};
final client = Matrix.of(context).client;
for (final e in allReactionEvents) {
final key = e.content
.tryGetMap<String, dynamic>('m.relates_to')
?.tryGet<String>('key');
if (key != null) {
if (!reactionMap.containsKey(key)) {
reactionMap[key] = _ReactionEntry(
key: key,
count: 0,
reacted: false,
reactors: [],
);
}
2022-01-29 11:35:03 +00:00
reactionMap[key]!.count++;
reactionMap[key]!.reactors!.add(e.senderFromMemoryOrFallback);
2022-01-29 11:35:03 +00: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(
spacing: 4.0,
runSpacing: 4.0,
children: [
...reactionList.map(
(r) => _Reaction(
reactionKey: r.key,
count: r.count,
reacted: r.reacted,
onTap: () {
if (r.reacted) {
final evt = allReactionEvents.firstWhereOrNull(
(e) =>
e.senderId == e.room.client.userID &&
e.content.tryGetMap('m.relates_to')?['key'] == r.key,
);
if (evt != null) {
showFutureLoadingDialog(
context: context,
future: () => evt.redactEvent(),
);
}
} else {
event.room.sendReaction(event.eventId, r.key!);
}
},
onLongPress: () async => await _AdaptableReactorsDialog(
client: client,
reactionEntry: r,
).show(context),
),
),
if (allReactionEvents.any((e) => e.status.isSending))
const SizedBox(
width: 28,
height: 28,
child: Padding(
padding: EdgeInsets.all(4.0),
child: CircularProgressIndicator.adaptive(strokeWidth: 1),
),
2021-11-22 14:35:51 +00:00
),
],
);
}
}
class _Reaction extends StatelessWidget {
2022-01-29 11:35:03 +00:00
final String? reactionKey;
final int? count;
final bool? reacted;
final void Function()? onTap;
final void Function()? onLongPress;
const _Reaction({
this.reactionKey,
this.count,
this.reacted,
this.onTap,
this.onLongPress,
});
@override
Widget build(BuildContext context) {
final textColor = Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black;
2021-08-29 10:15:51 +00:00
final color = Theme.of(context).scaffoldBackgroundColor;
final fontSize = DefaultTextStyle.of(context).style.fontSize;
Widget content;
2022-01-29 11:35:03 +00:00
if (reactionKey!.startsWith('mxc://')) {
content = Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
MxcImage(
uri: Uri.parse(reactionKey!),
width: 9999,
height: fontSize,
),
2021-11-13 12:06:36 +00:00
const SizedBox(width: 4),
Text(
count.toString(),
style: TextStyle(
color: textColor,
fontSize: DefaultTextStyle.of(context).style.fontSize,
),
),
],
);
} else {
2022-01-29 11:35:03 +00:00
var renderKey = Characters(reactionKey!);
if (renderKey.length > 10) {
renderKey = renderKey.getRange(0, 9) + Characters('');
}
content = Text(
'$renderKey $count',
style: TextStyle(
color: textColor,
fontSize: DefaultTextStyle.of(context).style.fontSize,
),
);
}
return InkWell(
2022-01-29 11:35:03 +00:00
onTap: () => onTap != null ? onTap!() : null,
onLongPress: () => onLongPress != null ? onLongPress!() : null,
2021-11-13 12:06:36 +00:00
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
child: Container(
decoration: BoxDecoration(
color: color,
2022-01-29 11:35:03 +00:00
border: reacted!
2021-11-13 18:22:11 +00:00
? Border.all(
width: 1,
color: Theme.of(context).primaryColor,
)
: null,
2021-11-13 12:06:36 +00:00
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
),
2021-11-13 12:06:36 +00:00
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 3),
child: content,
),
);
}
}
class _ReactionEntry {
2022-01-29 11:35:03 +00:00
String? key;
int count;
bool reacted;
2022-01-29 11:35:03 +00:00
List<User>? reactors;
2022-01-29 11:35:03 +00:00
_ReactionEntry({
this.key,
required this.count,
required this.reacted,
this.reactors,
});
}
class _AdaptableReactorsDialog extends StatelessWidget {
2022-01-29 11:35:03 +00:00
final Client? client;
final _ReactionEntry? reactionEntry;
const _AdaptableReactorsDialog({
this.client,
this.reactionEntry,
});
2023-10-28 15:32:37 +00:00
Future<bool?> show(BuildContext context) => showAdaptiveDialog(
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>[
2023-08-18 05:24:31 +00:00
for (final reactor in reactionEntry!.reactors!)
Chip(
avatar: Avatar(
2021-11-20 09:42:23 +00:00
mxContent: reactor.avatarUrl,
name: reactor.displayName,
client: client,
2023-11-11 16:56:23 +00:00
presenceUserId: reactor.stateKey,
),
2022-01-29 11:35:03 +00:00
label: Text(reactor.displayName!),
),
],
),
);
2022-01-29 11:35:03 +00:00
final title = Center(child: Text(reactionEntry!.key!));
2023-10-28 15:32:37 +00:00
return AlertDialog.adaptive(
title: title,
content: body,
);
}
}