loris/lib/partials/post.dart

313 lines
8.3 KiB
Dart
Raw Normal View History

2022-07-03 13:47:24 +00:00
import 'package:flutter/material.dart';
2022-07-04 17:37:53 +00:00
import 'package:localization/localization.dart';
import 'package:loris/business_logic/account/account.dart';
import 'package:loris/business_logic/timeline/media.dart';
import 'package:loris/dialogues/makepost.dart';
import 'package:loris/partials/interaction_button.dart';
import 'package:loris/partials/media_attachment.dart';
2022-08-06 18:15:16 +00:00
import 'package:loris/partials/post_options.dart';
2022-08-06 16:46:31 +00:00
import 'package:loris/partials/post_text_renderer.dart';
2022-08-01 08:29:43 +00:00
import '../business_logic/timeline/timeline.dart' as tl;
import '../business_logic/interactions/interactions.dart' as interactions;
2022-07-03 13:47:24 +00:00
class Post extends StatefulWidget {
2022-08-26 20:40:10 +00:00
const Post({
required this.model,
Key? key,
this.reblogVisible = true,
2022-08-28 18:55:23 +00:00
this.hideSensitive = true,
2022-08-26 20:40:10 +00:00
}) : super(key: key);
2022-08-01 08:29:43 +00:00
final tl.PostModel model;
2022-08-26 20:40:10 +00:00
final bool reblogVisible;
2022-08-28 18:55:23 +00:00
final bool hideSensitive;
2022-07-03 13:47:24 +00:00
@override
State<Post> createState() => _PostState();
}
class _PostState extends State<Post> {
@override
Widget build(BuildContext context) {
return Column(
2022-08-22 11:32:40 +00:00
mainAxisSize: MainAxisSize.min,
2022-08-06 16:46:31 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2022-07-03 13:47:24 +00:00
children: [
2022-08-06 11:47:18 +00:00
DisplayName(account: widget.model.account),
2022-08-01 08:29:43 +00:00
PostBody(
2022-08-01 08:59:30 +00:00
sensitive: widget.model.sensitive,
2022-08-01 08:29:43 +00:00
content: widget.model.content,
2022-08-01 08:59:30 +00:00
spoilerText: widget.model.spoilerText,
2022-08-06 14:05:14 +00:00
media: widget.model.attachments,
2022-08-28 18:55:23 +00:00
forceShow: !widget.hideSensitive,
2022-08-01 08:29:43 +00:00
),
PostActionBar(model: widget.model),
2022-08-26 20:40:10 +00:00
RebloggedBy(
account: widget.model.rebloggedBy,
reblogVisible: widget.reblogVisible,
),
2022-07-03 14:56:41 +00:00
],
);
}
}
class DisplayName extends StatelessWidget {
const DisplayName({
2022-08-06 11:47:18 +00:00
required this.account,
2022-08-12 22:28:00 +00:00
this.isReblog = false,
2022-07-03 14:56:41 +00:00
Key? key,
}) : super(key: key);
2022-08-06 11:47:18 +00:00
final AccountModel account;
final bool isReblog;
2022-07-03 14:56:41 +00:00
@override
Widget build(BuildContext context) {
2022-08-12 22:28:00 +00:00
TextStyle? usernameStyle;
if (isReblog) {
usernameStyle = Theme.of(context).textTheme.bodyMedium;
} else {
usernameStyle = Theme.of(context).textTheme.displaySmall;
}
2022-07-03 14:56:41 +00:00
return Row(
children: [
2022-08-22 11:32:40 +00:00
ProfilePic(url: account.avatar),
2022-08-13 16:14:12 +00:00
const SizedBox(
width: 8,
),
Flexible(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
account.displayName,
2022-08-12 22:28:00 +00:00
style: usernameStyle,
),
Text(
account.acct,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
2022-07-03 13:47:24 +00:00
),
],
);
}
}
2022-08-07 17:01:59 +00:00
class RebloggedBy extends StatelessWidget {
const RebloggedBy({
required this.account,
Key? key,
2022-08-26 20:40:10 +00:00
this.reblogVisible = true,
2022-08-07 17:01:59 +00:00
}) : super(key: key);
final AccountModel? account;
2022-08-26 20:40:10 +00:00
final bool reblogVisible;
2022-08-07 17:01:59 +00:00
@override
Widget build(BuildContext context) {
2022-08-26 20:40:10 +00:00
if (account != null && reblogVisible) {
2022-08-07 17:01:59 +00:00
return Column(
children: [
Row(
children: [
const Icon(Icons.repeat),
2022-08-12 19:30:58 +00:00
Flexible(
child: SelectableText("reblogged-by".i18n()),
),
2022-08-07 17:01:59 +00:00
],
),
2022-08-12 19:30:58 +00:00
const SizedBox(
height: 8,
),
2022-08-12 22:28:00 +00:00
DisplayName(
account: account!,
isReblog: true,
),
2022-08-07 17:01:59 +00:00
],
);
} else {
return const SizedBox.shrink();
}
}
}
2022-08-06 11:47:18 +00:00
class ProfilePic extends StatelessWidget {
const ProfilePic({required this.url, Key? key}) : super(key: key);
final String url;
@override
Widget build(BuildContext context) {
const double width = 64;
if (url.isNotEmpty) {
2022-08-22 11:32:40 +00:00
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
fit: BoxFit.cover,
url,
errorBuilder: (context, error, stackTrace) => const Icon(
Icons.cruelty_free,
size: width,
),
height: width,
width: width,
2022-08-10 18:49:51 +00:00
),
2022-08-06 11:47:18 +00:00
);
} else {
return const Icon(
Icons.cruelty_free,
size: width,
);
}
}
}
2022-07-03 20:02:57 +00:00
class PostBody extends StatefulWidget {
2022-08-01 08:59:30 +00:00
const PostBody({
required this.sensitive,
required this.spoilerText,
required this.content,
2022-08-06 14:05:14 +00:00
required this.media,
2022-08-28 18:55:23 +00:00
required this.forceShow,
2022-08-01 08:59:30 +00:00
Key? key,
}) : super(key: key);
2022-08-01 08:29:43 +00:00
final String content;
2022-08-01 08:59:30 +00:00
final String spoilerText;
final bool sensitive;
2022-08-28 18:55:23 +00:00
final bool forceShow;
2022-08-06 14:05:14 +00:00
final List<MediaAttachmentModel> media;
2022-07-03 20:02:57 +00:00
@override
State<PostBody> createState() => _PostBodyState();
}
class _PostBodyState extends State<PostBody> {
bool visible = false;
2022-07-04 17:37:53 +00:00
String cwButtonText = "show".i18n();
Icon cwButtonIcon = const Icon(Icons.visibility);
2022-08-28 18:55:23 +00:00
@override
void initState() {
visible = !widget.sensitive;
super.initState();
}
2022-07-03 20:02:57 +00:00
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
padding: const EdgeInsets.fromLTRB(0, 6, 0, 6),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-08-01 08:59:30 +00:00
Visibility(
2022-08-28 18:55:23 +00:00
visible: widget.forceShow ? false : widget.sensitive,
child: Column(
children: [
SelectableText.rich(
TextSpan(
children: [
WidgetSpan(
child: OutlinedButton.icon(
onPressed: () {
setState(() {
visible = !visible;
if (visible) {
cwButtonIcon = const Icon(Icons.visibility_off);
cwButtonText = "hide".i18n();
} else {
cwButtonText = "show".i18n();
cwButtonIcon = const Icon(Icons.visibility);
}
});
},
icon: cwButtonIcon,
label: Text(cwButtonText),
),
),
const WidgetSpan(
child: SizedBox(
width: 8,
)),
TextSpan(text: widget.spoilerText),
],
2022-08-12 19:30:58 +00:00
),
2022-08-28 18:55:23 +00:00
),
const SizedBox(
height: 8,
)
],
2022-08-12 19:30:58 +00:00
),
2022-08-01 08:59:30 +00:00
),
2022-07-03 20:02:57 +00:00
Visibility(
2022-08-28 18:55:23 +00:00
visible: visible || widget.forceShow,
2022-08-06 14:05:14 +00:00
child: Column(
2022-08-06 16:46:31 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2022-08-06 14:05:14 +00:00
children: [
2022-08-28 18:55:23 +00:00
PostTextRenderer(input: widget.content),
2022-08-06 14:05:14 +00:00
MediaAttachments(models: widget.media),
],
2022-07-03 20:02:57 +00:00
),
),
],
2022-07-03 13:47:24 +00:00
),
2022-07-03 20:02:57 +00:00
);
}
2022-07-03 13:47:24 +00:00
}
class PostActionBar extends StatefulWidget {
const PostActionBar({required this.model, Key? key}) : super(key: key);
final tl.PostModel model;
@override
State<PostActionBar> createState() => _PostActionBarState();
}
class _PostActionBarState extends State<PostActionBar> {
@override
Widget build(BuildContext context) {
return Row(
2022-08-27 22:29:17 +00:00
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
2022-08-27 22:29:17 +00:00
Expanded(
flex: 20,
child: IconButton(
onPressed: () {
showDialog(
context: context,
builder: ((context) => MakePost(
inReplyTo: widget.model,
)));
},
icon: const Icon(Icons.reply),
tooltip: "reply".i18n(),
),
),
2022-08-27 22:29:17 +00:00
Expanded(
flex: 20,
child: InteractionButton(
model: widget.model,
type: interactions.InteractionType.reblog,
),
),
2022-08-27 22:29:17 +00:00
Expanded(
flex: 20,
child: InteractionButton(
model: widget.model,
type: interactions.InteractionType.favorite,
),
),
2022-08-27 22:29:17 +00:00
Expanded(
flex: 20,
child: IconButton(
tooltip: "post-options".i18n(),
2022-08-27 22:29:17 +00:00
onPressed: () {
popupPostOptions(context, widget.model);
},
icon: const Icon(Icons.more_horiz),
),
)
],
);
}
2022-07-03 13:47:24 +00:00
}