loris/lib/partials/post.dart

383 lines
11 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';
2022-09-06 21:42:09 +00:00
import 'package:loris/dialogues/full_post_view.dart';
import 'package:loris/dialogues/makepost.dart';
2022-09-06 21:42:09 +00:00
import 'package:loris/dialogues/profile_view.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-09-05 17:01:04 +00:00
import 'package:url_launcher/url_launcher_string.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-09-26 14:30:02 +00:00
import 'package:loris/themes/themes.dart' as themes;
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-29 21:16:13 +00:00
this.hideActionBar = false,
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-08-29 21:16:13 +00:00
final bool hideActionBar;
2022-07-03 13:47:24 +00:00
@override
State<Post> createState() => _PostState();
}
class _PostState extends State<Post> {
@override
Widget build(BuildContext context) {
2022-08-29 21:16:13 +00:00
List<Widget> c = [];
c.addAll([
DisplayName(account: widget.model.account),
2022-09-26 14:30:02 +00:00
const SizedBox(height: 2),
2022-09-06 21:42:09 +00:00
SizedBox(
width: double.maxFinite,
child: PostBody(
model: widget.model,
sensitive: widget.model.sensitive,
content: widget.model.content,
spoilerText: widget.model.spoilerText,
media: widget.model.attachments,
forceShow: !widget.hideSensitive,
),
2022-08-29 21:16:13 +00:00
),
]);
if (!widget.hideActionBar) {
c.add(
PostActionBar(model: widget.model),
);
}
c.add(
RebloggedBy(
account: widget.model.rebloggedBy,
reblogVisible: widget.reblogVisible,
),
);
2022-07-03 13:47:24 +00:00
return Column(
2022-08-22 11:32:40 +00:00
mainAxisSize: MainAxisSize.min,
2022-09-23 17:08:05 +00:00
crossAxisAlignment: CrossAxisAlignment.stretch,
2022-08-29 21:16:13 +00:00
children: c,
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-09-05 17:01:04 +00:00
this.openInBrowser = 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-09-05 17:01:04 +00:00
final bool openInBrowser;
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-09-04 18:03:24 +00:00
return InkWell(
2022-09-26 14:30:02 +00:00
borderRadius: const BorderRadius.all(themes.defaultRadius),
2022-09-05 17:01:04 +00:00
onTap: !openInBrowser
2022-09-04 18:03:24 +00:00
? () {
showDialog(
2022-09-26 17:16:11 +00:00
barrierColor: Colors.transparent,
2022-09-04 18:03:24 +00:00
context: context,
builder: (context) => ProfileView(model: account),
);
}
2022-09-05 17:01:04 +00:00
: (() => launchUrlString(account.url)),
2022-09-04 18:03:24 +00:00
child: Row(
children: [
ProfilePic(url: account.avatar),
const SizedBox(
width: 8,
),
2022-09-04 18:03:24 +00:00
Flexible(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SelectableText(
minLines: 1,
2022-09-27 19:49:11 +00:00
maxLines: 2,
2022-09-04 18:03:24 +00:00
account.displayName,
style: usernameStyle,
),
SelectableText(
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(
2022-09-26 14:30:02 +00:00
borderRadius: const BorderRadius.all(themes.defaultRadius),
2022-08-22 11:32:40 +00:00
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-09-06 21:42:09 +00:00
this.openInBrowser = false,
2022-08-01 08:59:30 +00:00
Key? key,
2022-09-06 21:42:09 +00:00
required this.model,
2022-08-01 08:59:30 +00:00
}) : 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-09-06 21:42:09 +00:00
final bool openInBrowser;
final tl.PostModel model;
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) {
2022-09-06 21:42:09 +00:00
return InkWell(
2022-09-26 14:30:02 +00:00
borderRadius: const BorderRadius.all(themes.defaultRadius),
2022-09-06 21:42:09 +00:00
onTap: () => showDialog(
2022-09-26 17:16:11 +00:00
barrierColor: Colors.transparent,
2022-09-06 21:42:09 +00:00
context: context,
builder: (context) => FullPostView(originPostModel: widget.model),
),
2022-09-26 14:30:02 +00:00
child: SizedBox(
2022-09-06 21:42:09 +00:00
width: double.infinity,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Visibility(
visible: widget.forceShow ? false : widget.sensitive,
child: Column(
children: [
SizedBox(
width: double.maxFinite,
child: SelectableText.rich(
TextSpan(
children: [
WidgetSpan(
child: ElevatedButton.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-28 18:55:23 +00:00
),
2022-09-06 21:42:09 +00:00
),
2022-08-12 19:30:58 +00:00
),
2022-09-06 21:42:09 +00:00
const SizedBox(
height: 8,
)
],
),
2022-08-12 19:30:58 +00:00
),
2022-09-06 21:42:09 +00:00
Visibility(
visible: visible || widget.forceShow,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
PostTextRenderer(
input: widget.content,
identityName: widget.model.identity,
),
2022-09-06 21:42:09 +00:00
MediaAttachments(models: widget.media),
],
),
2022-07-03 20:02:57 +00:00
),
2022-09-06 21:42:09 +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) {
2022-09-23 17:08:05 +00:00
return Wrap(
spacing: 24,
runSpacing: 24,
runAlignment: WrapAlignment.center,
2022-09-23 17:08:05 +00:00
alignment: WrapAlignment.spaceAround,
2022-09-23 21:47:30 +00:00
crossAxisAlignment: WrapCrossAlignment.center,
children: [
SizedBox(
width: 64,
child: IconButton(
2022-08-27 22:29:17 +00:00
onPressed: () {
2022-09-23 17:08:05 +00:00
showDialog(
2022-09-26 17:16:11 +00:00
barrierColor: Colors.transparent,
context: context,
builder: ((context) => MakePost(
inReplyTo: widget.model,
)));
2022-08-27 22:29:17 +00:00
},
icon: const Icon(Icons.reply),
tooltip: "reply".i18n(),
),
),
SizedBox(
width: 64,
child: InteractionButton(
model: widget.model,
type: interactions.InteractionType.reblog,
),
),
SizedBox(
width: 64,
child: InteractionButton(
model: widget.model,
type: interactions.InteractionType.favorite,
),
),
SizedBox(
width: 64,
child: IconButton(
tooltip: "show-in-full".i18n(),
onPressed: () {
showDialog(
2022-09-26 17:16:11 +00:00
barrierColor: Colors.transparent,
context: context,
builder: (context) => FullPostView(
originPostModel: widget.model,
),
);
},
icon: const Icon(Icons.open_in_full)),
),
2022-09-23 17:08:05 +00:00
IconButton(
tooltip: "post-options".i18n(),
onPressed: () {
popupPostOptions(context, widget.model);
},
icon: const Icon(Icons.more_horiz),
2022-09-06 21:42:09 +00:00
),
],
);
}
2022-07-03 13:47:24 +00:00
}