2024-05-19 10:01:00 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-05-23 12:00:26 +00:00
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:solian/exts.dart';
|
2024-05-21 16:05:03 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-05-29 14:42:11 +00:00
|
|
|
import 'package:solian/models/realm.dart';
|
2024-05-19 10:01:00 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/router.dart';
|
2024-06-22 15:59:11 +00:00
|
|
|
import 'package:solian/theme.dart';
|
2024-07-12 14:31:45 +00:00
|
|
|
import 'package:solian/widgets/app_bar_leading.dart';
|
2024-06-22 15:59:11 +00:00
|
|
|
import 'package:solian/widgets/app_bar_title.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:solian/widgets/attachments/attachment_publish.dart';
|
2024-05-21 16:05:03 +00:00
|
|
|
import 'package:solian/widgets/posts/post_item.dart';
|
2024-07-09 14:39:44 +00:00
|
|
|
import 'package:solian/widgets/feed/feed_tags_field.dart';
|
2024-07-07 03:46:48 +00:00
|
|
|
import 'package:textfield_tags/textfield_tags.dart';
|
2024-07-09 13:23:38 +00:00
|
|
|
import 'package:badges/badges.dart' as badges;
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
class PostPublishArguments {
|
2024-05-21 16:05:03 +00:00
|
|
|
final Post? edit;
|
|
|
|
final Post? reply;
|
|
|
|
final Post? repost;
|
2024-05-29 14:42:11 +00:00
|
|
|
final Realm? realm;
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
PostPublishArguments({this.edit, this.reply, this.repost, this.realm});
|
2024-05-21 16:05:03 +00:00
|
|
|
}
|
2024-05-19 10:01:00 +00:00
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
class PostPublishScreen extends StatefulWidget {
|
2024-05-21 16:05:03 +00:00
|
|
|
final Post? edit;
|
|
|
|
final Post? reply;
|
|
|
|
final Post? repost;
|
2024-05-29 14:42:11 +00:00
|
|
|
final Realm? realm;
|
2024-05-21 16:05:03 +00:00
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
const PostPublishScreen({
|
2024-05-21 16:05:03 +00:00
|
|
|
super.key,
|
|
|
|
this.edit,
|
|
|
|
this.reply,
|
|
|
|
this.repost,
|
|
|
|
this.realm,
|
|
|
|
});
|
2024-05-19 10:01:00 +00:00
|
|
|
|
|
|
|
@override
|
2024-07-09 13:23:38 +00:00
|
|
|
State<PostPublishScreen> createState() => _PostPublishScreenState();
|
2024-05-19 10:01:00 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
class _PostPublishScreenState extends State<PostPublishScreen> {
|
2024-05-19 10:01:00 +00:00
|
|
|
final _contentController = TextEditingController();
|
2024-07-07 03:46:48 +00:00
|
|
|
final _tagsController = StringTagController();
|
2024-05-19 10:01:00 +00:00
|
|
|
|
2024-05-25 16:11:00 +00:00
|
|
|
bool _isBusy = false;
|
2024-05-19 10:01:00 +00:00
|
|
|
|
2024-05-20 15:11:26 +00:00
|
|
|
List<int> _attachments = List.empty();
|
2024-05-19 16:08:20 +00:00
|
|
|
|
2024-07-09 13:23:38 +00:00
|
|
|
bool _isDraft = false;
|
|
|
|
|
2024-05-26 13:03:25 +00:00
|
|
|
void showAttachments() {
|
2024-05-19 16:08:20 +00:00
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
2024-07-07 05:38:43 +00:00
|
|
|
isScrollControlled: true,
|
2024-06-27 03:05:15 +00:00
|
|
|
builder: (context) => AttachmentPublishPopup(
|
2024-05-19 16:08:20 +00:00
|
|
|
usage: 'i.attachment',
|
|
|
|
current: _attachments,
|
2024-07-09 15:06:55 +00:00
|
|
|
onUpdate: (value) {
|
|
|
|
setState(() => _attachments = value);
|
|
|
|
},
|
2024-05-19 16:08:20 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
void applyPost() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (!await auth.isAuthorized) return;
|
|
|
|
if (_contentController.value.text.isEmpty) return;
|
|
|
|
|
2024-05-25 16:11:00 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-05-19 10:01:00 +00:00
|
|
|
|
2024-06-22 14:39:32 +00:00
|
|
|
final client = auth.configureClient('interactive');
|
2024-05-19 10:01:00 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
final payload = {
|
2024-05-19 10:01:00 +00:00
|
|
|
'content': _contentController.value.text,
|
2024-07-07 03:46:48 +00:00
|
|
|
'tags': _tagsController.getTags?.map((x) => {'alias': x}).toList() ??
|
|
|
|
List.empty(),
|
2024-05-19 16:08:20 +00:00
|
|
|
'attachments': _attachments,
|
2024-07-09 13:23:38 +00:00
|
|
|
'is_draft': _isDraft,
|
2024-05-21 16:05:03 +00:00
|
|
|
if (widget.edit != null) 'alias': widget.edit!.alias,
|
|
|
|
if (widget.reply != null) 'reply_to': widget.reply!.id,
|
|
|
|
if (widget.repost != null) 'repost_to': widget.repost!.id,
|
2024-05-29 14:42:11 +00:00
|
|
|
if (widget.realm != null) 'realm': widget.realm!.alias,
|
2024-05-21 16:05:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Response resp;
|
|
|
|
if (widget.edit != null) {
|
|
|
|
resp = await client.put('/api/posts/${widget.edit!.id}', payload);
|
|
|
|
} else {
|
|
|
|
resp = await client.post('/api/posts', payload);
|
|
|
|
}
|
2024-05-19 10:01:00 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-05-19 16:08:20 +00:00
|
|
|
context.showErrorDialog(resp.bodyString);
|
2024-05-19 10:01:00 +00:00
|
|
|
} else {
|
|
|
|
AppRouter.instance.pop(resp.body);
|
|
|
|
}
|
|
|
|
|
2024-05-25 16:11:00 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 10:01:00 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
void syncWidget() {
|
|
|
|
if (widget.edit != null) {
|
|
|
|
_contentController.text = widget.edit!.content;
|
|
|
|
_attachments = widget.edit!.attachments ?? List.empty();
|
2024-07-09 13:23:38 +00:00
|
|
|
_isDraft = widget.edit!.isDraft ?? false;
|
2024-05-21 16:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancelAction() {
|
|
|
|
AppRouter.instance.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
syncWidget();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-21 16:05:03 +00:00
|
|
|
final notifyBannerActions = [
|
|
|
|
TextButton(
|
|
|
|
onPressed: cancelAction,
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
2024-05-19 10:01:00 +00:00
|
|
|
return Material(
|
2024-05-21 16:05:03 +00:00
|
|
|
color: Theme.of(context).colorScheme.surface,
|
2024-05-19 10:01:00 +00:00
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-07-12 14:37:58 +00:00
|
|
|
leading: AppBarLeadingButton.adaptive(context),
|
2024-07-09 13:23:38 +00:00
|
|
|
title: AppBarTitle('postPublish'.tr),
|
2024-06-22 15:59:11 +00:00
|
|
|
centerTitle: false,
|
|
|
|
toolbarHeight: SolianTheme.toolbarHeight(context),
|
2024-05-19 10:01:00 +00:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2024-05-25 16:11:00 +00:00
|
|
|
onPressed: _isBusy ? null : () => applyPost(),
|
2024-07-09 13:23:38 +00:00
|
|
|
child: Text(
|
|
|
|
_isDraft
|
|
|
|
? 'draftSave'.tr.toUpperCase()
|
|
|
|
: 'postAction'.tr.toUpperCase(),
|
|
|
|
),
|
2024-05-19 10:01:00 +00:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
body: Stack(
|
|
|
|
children: [
|
|
|
|
ListView(
|
|
|
|
children: [
|
|
|
|
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
|
|
|
|
if (widget.edit != null && widget.edit!.isDraft != true)
|
|
|
|
MaterialBanner(
|
|
|
|
leading: const Icon(Icons.edit),
|
|
|
|
leadingPadding: const EdgeInsets.only(left: 10, right: 20),
|
|
|
|
dividerColor: Colors.transparent,
|
|
|
|
content: Text('postEditingNotify'.tr),
|
|
|
|
actions: notifyBannerActions,
|
|
|
|
),
|
|
|
|
if (widget.reply != null)
|
|
|
|
ExpansionTile(
|
|
|
|
leading: const FaIcon(
|
|
|
|
FontAwesomeIcons.reply,
|
|
|
|
size: 18,
|
|
|
|
).paddingOnly(left: 2),
|
|
|
|
title: Text('postReplyingNotify'.trParams(
|
|
|
|
{'username': '@${widget.reply!.author.name}'},
|
|
|
|
)),
|
|
|
|
collapsedBackgroundColor:
|
|
|
|
Theme.of(context).colorScheme.surfaceContainer,
|
|
|
|
children: [
|
|
|
|
PostItem(
|
|
|
|
item: widget.reply!,
|
|
|
|
isReactable: false,
|
|
|
|
).paddingOnly(bottom: 8),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (widget.repost != null)
|
|
|
|
ExpansionTile(
|
|
|
|
leading: const FaIcon(
|
|
|
|
FontAwesomeIcons.retweet,
|
|
|
|
size: 18,
|
|
|
|
).paddingOnly(left: 2),
|
|
|
|
title: Text('postRepostingNotify'.trParams(
|
|
|
|
{'username': '@${widget.repost!.author.name}'},
|
|
|
|
)),
|
|
|
|
collapsedBackgroundColor:
|
|
|
|
Theme.of(context).colorScheme.surfaceContainer,
|
|
|
|
children: [
|
|
|
|
PostItem(
|
|
|
|
item: widget.repost!,
|
|
|
|
isReactable: false,
|
|
|
|
).paddingOnly(bottom: 8),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (widget.realm != null)
|
|
|
|
MaterialBanner(
|
|
|
|
leading: const Icon(Icons.group),
|
|
|
|
leadingPadding: const EdgeInsets.only(left: 10, right: 20),
|
|
|
|
dividerColor: Colors.transparent,
|
|
|
|
content: Text(
|
|
|
|
'postInRealmNotify'
|
|
|
|
.trParams({'realm': '#${widget.realm!.alias}'}),
|
2024-07-06 19:02:10 +00:00
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
actions: notifyBannerActions,
|
|
|
|
),
|
|
|
|
const Divider(thickness: 0.3, height: 0.3)
|
|
|
|
.paddingOnly(bottom: 8),
|
|
|
|
Container(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
|
|
child: TextField(
|
|
|
|
maxLines: null,
|
|
|
|
autofocus: true,
|
|
|
|
autocorrect: true,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
controller: _contentController,
|
|
|
|
decoration: InputDecoration.collapsed(
|
|
|
|
hintText: 'postContentPlaceholder'.tr,
|
2024-05-19 10:01:00 +00:00
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
onTapOutside: (_) =>
|
|
|
|
FocusManager.instance.primaryFocus?.unfocus(),
|
2024-05-19 10:01:00 +00:00
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
),
|
|
|
|
const SizedBox(height: 120)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
child: Material(
|
|
|
|
elevation: 8,
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
2024-07-06 19:02:10 +00:00
|
|
|
child: Column(
|
2024-07-09 13:23:38 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-05-19 10:01:00 +00:00
|
|
|
children: [
|
2024-07-07 03:46:48 +00:00
|
|
|
TagsField(
|
2024-07-07 04:33:54 +00:00
|
|
|
initialTags:
|
|
|
|
widget.edit?.tags?.map((x) => x.alias).toList(),
|
2024-07-07 03:46:48 +00:00
|
|
|
tagsController: _tagsController,
|
|
|
|
hintText: 'postTagsPlaceholder'.tr,
|
|
|
|
),
|
2024-07-06 19:02:10 +00:00
|
|
|
const Divider(thickness: 0.3, height: 0.3),
|
|
|
|
SizedBox(
|
|
|
|
height: 56,
|
2024-07-09 13:23:38 +00:00
|
|
|
child: ListView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
2024-07-06 19:02:10 +00:00
|
|
|
children: [
|
2024-07-09 13:23:38 +00:00
|
|
|
IconButton(
|
|
|
|
icon: _isDraft
|
|
|
|
? const Icon(Icons.drive_file_rename_outline)
|
|
|
|
: const Icon(Icons.public),
|
|
|
|
color: _isDraft
|
|
|
|
? Colors.grey.shade600
|
|
|
|
: Colors.green.shade700,
|
|
|
|
onPressed: () {
|
|
|
|
setState(() => _isDraft = !_isDraft);
|
|
|
|
},
|
|
|
|
),
|
2024-07-09 15:06:55 +00:00
|
|
|
IconButton(
|
|
|
|
icon: badges.Badge(
|
|
|
|
badgeContent: Text(
|
|
|
|
_attachments.length.toString(),
|
|
|
|
style: const TextStyle(color: Colors.white),
|
|
|
|
),
|
|
|
|
showBadge: _attachments.isNotEmpty,
|
|
|
|
position: badges.BadgePosition.topEnd(
|
|
|
|
top: -12,
|
|
|
|
end: -8,
|
|
|
|
),
|
|
|
|
child: const Icon(Icons.camera_alt),
|
2024-07-06 19:02:10 +00:00
|
|
|
),
|
2024-07-09 15:06:55 +00:00
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
onPressed: () => showAttachments(),
|
|
|
|
),
|
2024-07-06 19:02:10 +00:00
|
|
|
],
|
2024-07-09 13:23:38 +00:00
|
|
|
).paddingSymmetric(horizontal: 6, vertical: 8),
|
2024-07-06 19:02:10 +00:00
|
|
|
),
|
2024-05-19 10:01:00 +00:00
|
|
|
],
|
2024-07-09 14:39:44 +00:00
|
|
|
).paddingOnly(bottom: MediaQuery.of(context).padding.bottom),
|
2024-05-19 10:01:00 +00:00
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
),
|
|
|
|
],
|
2024-05-19 10:01:00 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-07-07 03:46:48 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_contentController.dispose();
|
|
|
|
_tagsController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-05-19 10:01:00 +00:00
|
|
|
}
|