1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/ui/tiles/comment_tile.dart

149 lines
4.0 KiB
Dart
Raw Normal View History

2022-03-18 18:05:21 +00:00
import 'package:comunic/helpers/server_config_helper.dart';
2019-05-16 12:52:22 +00:00
import 'package:comunic/models/comment.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
2020-04-15 16:58:45 +00:00
import 'package:comunic/ui/widgets/like_widget.dart';
2019-05-16 12:52:22 +00:00
import 'package:comunic/ui/widgets/network_image_widget.dart';
2020-04-16 08:14:45 +00:00
import 'package:comunic/ui/widgets/text_widget.dart';
2019-05-18 16:50:15 +00:00
import 'package:comunic/utils/date_utils.dart';
2019-05-18 13:54:10 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-11-01 13:17:46 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-05-16 12:52:22 +00:00
import 'package:flutter/material.dart';
/// Single comment tile
///
/// @author Pierre HUBERT
2022-03-18 18:05:21 +00:00
enum _CommentAction { DELETE, UPDATE, REPORT }
2019-05-18 14:48:19 +00:00
2019-05-16 12:52:22 +00:00
class CommentTile extends StatelessWidget {
final Comment comment;
final User user;
2019-05-18 16:48:12 +00:00
final void Function(Comment) onUpdateComment;
2019-05-18 14:48:19 +00:00
final void Function(Comment) onDeleteComment;
2022-03-18 18:05:21 +00:00
final void Function(Comment) onReportComment;
2019-05-16 12:52:22 +00:00
2019-05-18 13:54:10 +00:00
const CommentTile({
Key? key,
required this.comment,
required this.user,
required this.onUpdateComment,
required this.onDeleteComment,
2022-03-18 18:05:21 +00:00
required this.onReportComment,
2022-03-11 15:40:56 +00:00
}) : super(key: key);
2019-05-16 12:52:22 +00:00
@override
Widget build(BuildContext context) {
return ListTile(
2019-05-18 14:48:19 +00:00
leading: _buildAccountImageWidget(),
2019-05-18 13:54:10 +00:00
title: Text(
user.displayName,
),
2019-05-16 12:52:22 +00:00
subtitle: _buildCommentContent(),
2022-03-18 18:05:21 +00:00
trailing: _buildTrailing(),
2019-05-16 12:52:22 +00:00
);
}
2019-05-18 14:48:19 +00:00
Widget _buildAccountImageWidget() {
2022-03-18 18:05:21 +00:00
return AccountImageWidget(
user: user,
2019-05-18 14:48:19 +00:00
);
}
2019-05-16 12:52:22 +00:00
Widget _buildCommentContent() {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
// Comment image
Container(
child: comment.hasImage
? NetworkImageWidget(
url: comment.imageURL!,
2019-05-16 12:52:22 +00:00
allowFullScreen: true,
height: 100.0,
2019-05-18 13:54:10 +00:00
width: null,
2019-05-16 12:52:22 +00:00
)
: null,
),
// Comment text
Container(
2019-05-18 13:54:10 +00:00
child: comment.hasContent
2020-04-16 08:14:45 +00:00
? TextWidget(
content: comment.content,
2020-04-15 16:58:45 +00:00
style: TextStyle(
color: darkTheme() ? darkAccentColor : Colors.black),
2019-05-18 13:54:10 +00:00
)
: null,
2019-05-16 12:52:22 +00:00
),
2019-05-18 13:54:10 +00:00
// Comment likes
2020-04-15 17:17:29 +00:00
LikeWidget(likeElement: comment),
2019-05-16 12:52:22 +00:00
],
);
}
2019-05-18 13:54:10 +00:00
2022-03-18 18:05:21 +00:00
Widget _buildTrailing() {
return IntrinsicWidth(
child: Row(
children: [
Text(
diffTimeFromNowToStr(comment.timeSent)!,
style: TextStyle(fontSize: 10.0),
),
SizedBox(width: 5),
PopupMenuButton<_CommentAction>(
padding: EdgeInsets.all(0),
child: InkWell(
child: Icon(Icons.adaptive.more, size: 20),
),
onSelected: _selectedMenuOption,
itemBuilder: (c) => [
// Update comment content
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Update")!),
value: _CommentAction.UPDATE,
),
// Delete comment
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Delete")!),
value: _CommentAction.DELETE,
),
]..addAll(srvConfig!.isReportingEnabled && !comment.isOwner
? [
PopupMenuItem(
2022-03-18 18:21:08 +00:00
child: Text(tr("Report abuse")!),
2022-03-18 18:05:21 +00:00
value: _CommentAction.REPORT,
)
]
: []),
)
],
),
);
}
2019-05-18 14:48:19 +00:00
/// A menu option has been selected
void _selectedMenuOption(_CommentAction value) {
switch (value) {
2019-05-18 16:48:12 +00:00
// Update comment content
case _CommentAction.UPDATE:
onUpdateComment(comment);
break;
2019-05-18 14:48:19 +00:00
// Delete comment
case _CommentAction.DELETE:
onDeleteComment(comment);
break;
2022-03-18 18:05:21 +00:00
// Report comment
case _CommentAction.REPORT:
onReportComment(comment);
break;
2019-05-18 14:48:19 +00:00
}
}
2019-05-16 12:52:22 +00:00
}