2021-09-08 14:38:26 +02:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:lemmy_api_client/v3.dart';
|
2021-11-03 06:21:00 +01:00
|
|
|
import 'package:nested/nested.dart';
|
2021-09-08 14:38:26 +02:00
|
|
|
|
|
|
|
import '../../comment_tree.dart';
|
|
|
|
import '../../l10n/l10n.dart';
|
|
|
|
import '../../stores/config_store.dart';
|
|
|
|
import '../../util/async_store_listener.dart';
|
|
|
|
import '../../util/extensions/api.dart';
|
|
|
|
import '../../util/goto.dart';
|
2021-12-04 18:03:54 +01:00
|
|
|
import '../../util/mobx_provider.dart';
|
2021-09-08 14:38:26 +02:00
|
|
|
import '../../util/observer_consumers.dart';
|
|
|
|
import '../../util/text_color.dart';
|
|
|
|
import '../avatar.dart';
|
|
|
|
import '../info_table_popup.dart';
|
|
|
|
import '../markdown_text.dart';
|
|
|
|
import 'comment_actions.dart';
|
|
|
|
import 'comment_store.dart';
|
|
|
|
|
|
|
|
/// A single comment that renders its replies
|
|
|
|
class CommentWidget extends StatelessWidget {
|
|
|
|
final CommentTree commentTree;
|
|
|
|
final int? userMentionId;
|
|
|
|
final int depth;
|
|
|
|
final bool canBeMarkedAsRead;
|
|
|
|
final bool detached;
|
|
|
|
final bool hideOnRead;
|
|
|
|
|
|
|
|
const CommentWidget(
|
|
|
|
this.commentTree, {
|
|
|
|
this.depth = 0,
|
|
|
|
this.detached = false,
|
|
|
|
this.canBeMarkedAsRead = false,
|
|
|
|
this.hideOnRead = false,
|
|
|
|
this.userMentionId,
|
|
|
|
Key? key,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
CommentWidget.fromCommentView(
|
|
|
|
CommentView cv, {
|
|
|
|
bool canBeMarkedAsRead = false,
|
|
|
|
bool hideOnRead = false,
|
|
|
|
bool detached = true,
|
|
|
|
Key? key,
|
|
|
|
}) : this(
|
|
|
|
CommentTree(cv),
|
|
|
|
detached: detached,
|
|
|
|
canBeMarkedAsRead: canBeMarkedAsRead,
|
|
|
|
hideOnRead: hideOnRead,
|
|
|
|
key: key,
|
|
|
|
);
|
|
|
|
|
|
|
|
CommentWidget.fromPersonMentionView(
|
|
|
|
PersonMentionView userMentionView, {
|
|
|
|
bool hideOnRead = false,
|
|
|
|
Key? key,
|
|
|
|
}) : this(
|
|
|
|
CommentTree(CommentView.fromJson(userMentionView.toJson())),
|
|
|
|
hideOnRead: hideOnRead,
|
|
|
|
canBeMarkedAsRead: true,
|
|
|
|
detached: true,
|
|
|
|
userMentionId: userMentionView.personMention.id,
|
|
|
|
key: key,
|
|
|
|
);
|
|
|
|
|
|
|
|
static void showCommentInfo(BuildContext context, CommentView commentView) {
|
|
|
|
final percentOfUpvotes = 100 *
|
|
|
|
(commentView.counts.upvotes /
|
|
|
|
(commentView.counts.upvotes + commentView.counts.downvotes));
|
|
|
|
|
|
|
|
showInfoTablePopup(context: context, table: {
|
|
|
|
...commentView.toJson(),
|
|
|
|
'% of upvotes': '$percentOfUpvotes%',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-12-04 18:03:54 +01:00
|
|
|
return MobxProvider(
|
2021-09-08 14:38:26 +02:00
|
|
|
create: (context) => CommentStore(
|
|
|
|
context.read(),
|
|
|
|
commentTree: commentTree,
|
|
|
|
userMentionId: userMentionId,
|
|
|
|
depth: depth,
|
|
|
|
canBeMarkedAsRead: canBeMarkedAsRead,
|
|
|
|
detached: detached,
|
|
|
|
hideOnRead: hideOnRead,
|
|
|
|
),
|
2021-11-03 06:21:00 +01:00
|
|
|
builder: (context, child) => Nested(
|
|
|
|
children: [
|
|
|
|
AsyncStoreListener<BlockedPerson>(
|
|
|
|
asyncStore: context.read<CommentStore>().blockingState,
|
|
|
|
successMessageBuilder: (context, state) {
|
|
|
|
final name = state.personView.person.preferredName;
|
|
|
|
return state.blocked ? '$name blocked' : '$name unblocked';
|
|
|
|
},
|
|
|
|
),
|
|
|
|
AsyncStoreListener(
|
|
|
|
asyncStore: context.read<CommentStore>().votingState,
|
|
|
|
),
|
|
|
|
AsyncStoreListener(
|
2021-09-09 14:46:01 +02:00
|
|
|
asyncStore: context.read<CommentStore>().deletingState,
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
2021-11-03 06:21:00 +01:00
|
|
|
AsyncStoreListener(
|
|
|
|
asyncStore: context.read<CommentStore>().savingState,
|
|
|
|
),
|
|
|
|
AsyncStoreListener<CommentReportView>(
|
|
|
|
asyncStore: context.read<CommentStore>().reportingState,
|
|
|
|
successMessageBuilder: (context, data) => 'Comment reported',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
child: const _CommentWidget(),
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CommentWidget extends StatelessWidget {
|
|
|
|
static const colors = [
|
|
|
|
Colors.pink,
|
|
|
|
Colors.green,
|
|
|
|
Colors.amber,
|
|
|
|
Colors.cyan,
|
|
|
|
Colors.indigo,
|
|
|
|
];
|
|
|
|
|
|
|
|
static const indentWidth = 5.0;
|
|
|
|
|
|
|
|
const _CommentWidget();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
|
|
|
final body = ObserverBuilder<CommentStore>(
|
|
|
|
builder: (context, store) {
|
|
|
|
final comment = store.comment.comment;
|
|
|
|
|
|
|
|
if (comment.deleted) {
|
|
|
|
return Text(
|
2021-11-05 21:37:27 +01:00
|
|
|
L10n.of(context).deleted_by_creator,
|
2021-09-08 14:38:26 +02:00
|
|
|
style: const TextStyle(fontStyle: FontStyle.italic),
|
|
|
|
);
|
|
|
|
} else if (comment.removed) {
|
|
|
|
return const Text(
|
|
|
|
'comment deleted by moderator',
|
|
|
|
style: TextStyle(fontStyle: FontStyle.italic),
|
|
|
|
);
|
|
|
|
} else if (store.collapsed) {
|
|
|
|
return Opacity(
|
|
|
|
opacity: 0.3,
|
|
|
|
child: Text(
|
|
|
|
comment.content,
|
|
|
|
maxLines: 1,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.showRaw
|
|
|
|
? store.selectable
|
|
|
|
? SelectableText(comment.content)
|
|
|
|
: Text(comment.content)
|
|
|
|
: MarkdownText(
|
|
|
|
comment.content,
|
|
|
|
instanceHost: comment.instanceHost,
|
|
|
|
selectable: store.selectable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return ObserverBuilder<CommentStore>(
|
|
|
|
builder: (context, store) {
|
|
|
|
if (store.hideOnRead && store.comment.comment.read) {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
final comment = store.comment.comment;
|
|
|
|
final creator = store.comment.creator;
|
|
|
|
|
|
|
|
return InkWell(
|
|
|
|
onLongPress: store.selectable ? null : store.toggleCollapsed,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
left: max((store.depth - 1) * indentWidth, 0),
|
|
|
|
),
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(
|
|
|
|
left: store.depth > 0
|
|
|
|
? BorderSide(
|
|
|
|
color: colors[store.depth % colors.length],
|
|
|
|
width: indentWidth,
|
|
|
|
)
|
|
|
|
: BorderSide.none,
|
|
|
|
top: const BorderSide(width: 0.2),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Row(children: [
|
|
|
|
if (creator.avatar != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 5),
|
|
|
|
child: InkWell(
|
|
|
|
onTap: () =>
|
|
|
|
goToUser.fromPersonSafe(context, creator),
|
|
|
|
child: Avatar(
|
|
|
|
url: creator.avatar,
|
|
|
|
radius: 10,
|
|
|
|
noBlank: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
InkWell(
|
|
|
|
onTap: () => goToUser.fromPersonSafe(context, creator),
|
|
|
|
child: Text(
|
|
|
|
creator.originPreferredName,
|
|
|
|
style: TextStyle(
|
2021-09-11 16:26:10 +02:00
|
|
|
color: theme.colorScheme.secondary,
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (creator.isCakeDay) const Text(' 🍰'),
|
2021-09-11 16:26:10 +02:00
|
|
|
if (store.isOP)
|
|
|
|
_CommentTag('OP', theme.colorScheme.secondary),
|
2021-09-08 14:38:26 +02:00
|
|
|
if (creator.admin)
|
|
|
|
_CommentTag(
|
2021-11-05 21:37:27 +01:00
|
|
|
L10n.of(context).admin.toUpperCase(),
|
2021-09-11 16:26:10 +02:00
|
|
|
theme.colorScheme.secondary,
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
|
|
|
if (creator.banned)
|
|
|
|
const _CommentTag('BANNED', Colors.red),
|
|
|
|
if (store.comment.creatorBannedFromCommunity)
|
|
|
|
const _CommentTag('BANNED FROM COMMUNITY', Colors.red),
|
|
|
|
const Spacer(),
|
|
|
|
if (store.collapsed && store.children.isNotEmpty) ...[
|
|
|
|
_CommentTag(
|
|
|
|
'+${store.children.length}',
|
2021-09-12 22:37:07 +02:00
|
|
|
Theme.of(context).colorScheme.secondary,
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
|
|
|
const SizedBox(width: 7),
|
|
|
|
],
|
|
|
|
InkWell(
|
|
|
|
onTap: () => CommentWidget.showCommentInfo(
|
|
|
|
context,
|
|
|
|
store.comment,
|
|
|
|
),
|
|
|
|
child: Consumer<ConfigStore>(
|
|
|
|
builder: (context, configStore, child) {
|
|
|
|
return ObserverBuilder<CommentStore>(
|
|
|
|
builder: (context, store) => Row(
|
|
|
|
children: [
|
|
|
|
if (store.votingState.isLoading)
|
|
|
|
SizedBox.fromSize(
|
|
|
|
size: const Size.square(16),
|
2021-10-24 16:28:25 +02:00
|
|
|
child: const CircularProgressIndicator
|
|
|
|
.adaptive(),
|
2021-09-08 14:38:26 +02:00
|
|
|
)
|
|
|
|
else if (configStore.showScores)
|
|
|
|
Text(
|
2021-11-25 18:49:43 +01:00
|
|
|
store.comment.counts.score
|
|
|
|
.compact(context),
|
2021-09-08 14:38:26 +02:00
|
|
|
),
|
|
|
|
if (configStore.showScores)
|
|
|
|
const Text(' · ')
|
|
|
|
else
|
|
|
|
const SizedBox(width: 4),
|
2021-11-16 17:06:07 +01:00
|
|
|
Text(comment.published.timeago(context)),
|
2021-09-08 14:38:26 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
body,
|
|
|
|
const SizedBox(height: 5),
|
|
|
|
const CommentActions(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (!store.collapsed)
|
|
|
|
for (final c in store.children)
|
|
|
|
CommentWidget(c, depth: store.depth + 1),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CommentTag extends StatelessWidget {
|
|
|
|
final String text;
|
|
|
|
final Color backgroundColor;
|
|
|
|
|
|
|
|
const _CommentTag(this.text, this.backgroundColor);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 5),
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(5)),
|
|
|
|
color: backgroundColor,
|
|
|
|
),
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 3, vertical: 2),
|
|
|
|
child: Text(
|
|
|
|
text,
|
|
|
|
style: TextStyle(
|
|
|
|
color: textColorBasedOnBackground(backgroundColor),
|
|
|
|
fontSize: Theme.of(context).textTheme.bodyText1!.fontSize! - 5,
|
|
|
|
fontWeight: FontWeight.w800,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|