Solian/lib/screens/posts/post_editor.dart

644 lines
24 KiB
Dart
Raw Normal View History

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';
import 'package:gap/gap.dart';
2024-05-19 10:01:00 +00:00
import 'package:get/get.dart';
2024-07-30 08:29:30 +00:00
import 'package:intl/intl.dart';
2024-08-01 06:46:01 +00:00
import 'package:markdown_toolbar/markdown_toolbar.dart';
import 'package:solian/controllers/post_editor_controller.dart';
2024-05-19 16:08:20 +00:00
import 'package:solian/exts.dart';
import 'package:solian/models/post.dart';
2024-05-29 14:42:11 +00:00
import 'package:solian/models/realm.dart';
import 'package:solian/providers/attachment_uploader.dart';
2024-05-19 10:01:00 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/navigation.dart';
2024-05-19 10:01:00 +00:00
import 'package:solian/router.dart';
2024-06-22 15:59:11 +00:00
import 'package:solian/theme.dart';
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-10-14 13:13:57 +00:00
import 'package:solian/widgets/loading_indicator.dart';
2024-08-15 16:52:36 +00:00
import 'package:solian/widgets/markdown_text_content.dart';
import 'package:solian/widgets/posts/post_item.dart';
2024-07-09 13:23:38 +00:00
import 'package:badges/badges.dart' as badges;
2024-10-06 09:37:07 +00:00
import 'package:solian/widgets/root_container.dart';
2024-07-09 13:23:38 +00:00
class PostPublishArguments {
final Post? edit;
final Post? reply;
final Post? repost;
2024-05-29 14:42:11 +00:00
final Realm? realm;
PostPublishArguments({
this.edit,
this.reply,
this.repost,
this.realm,
});
}
2024-05-19 10:01:00 +00:00
2024-07-09 13:23:38 +00:00
class PostPublishScreen extends StatefulWidget {
final Post? edit;
final Post? reply;
final Post? repost;
2024-05-29 14:42:11 +00:00
final Realm? realm;
2024-07-30 08:53:13 +00:00
final int mode;
2024-07-09 13:23:38 +00:00
const PostPublishScreen({
super.key,
this.edit,
this.reply,
this.repost,
this.realm,
2024-07-30 08:53:13 +00:00
required this.mode,
});
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> {
final _editorController = PostEditorController();
2024-08-01 06:46:01 +00:00
final _contentFocusNode = FocusNode();
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
void _applyPost() async {
2024-05-19 10:01:00 +00:00
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
if (_editorController.isEmpty) return;
2024-05-19 10:01:00 +00:00
final AttachmentUploaderController uploader = Get.find();
if (uploader.queueOfUpload.any(
((x) => x.isUploading),
)) {
context.showErrorDialog('attachmentUploadInProgress'.tr);
return;
}
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = true);
2024-05-19 10:01:00 +00:00
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('interactive');
2024-05-19 10:01:00 +00:00
Response resp;
if (widget.edit != null) {
resp = await client.put(
'/${_editorController.typeEndpoint}/${widget.edit!.id}',
_editorController.payload,
);
} else {
resp = await client.post(
'/${_editorController.typeEndpoint}',
_editorController.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 {
2024-08-15 16:52:36 +00:00
_editorController.currentClear();
_editorController.localClear();
2024-05-19 10:01:00 +00:00
AppRouter.instance.pop(resp.body);
}
2024-05-25 16:11:00 +00:00
setState(() => _isBusy = false);
2024-05-19 10:01:00 +00:00
}
void _syncWidget() {
2024-07-30 08:53:13 +00:00
_editorController.mode.value = widget.mode;
if (widget.edit != null) {
_editorController.editTarget = widget.edit;
}
2024-08-01 07:21:43 +00:00
if (widget.realm != null) {
_editorController.realmZone.value = widget.realm;
}
}
void _cancelAction() {
2024-08-01 08:09:09 +00:00
_editorController.localClear();
AppRouter.instance.pop();
}
2024-08-01 03:49:28 +00:00
Post? get _editTo => _editorController.editTo.value;
2024-08-01 03:49:28 +00:00
Post? get _replyTo => _editorController.replyTo.value;
2024-08-01 03:49:28 +00:00
Post? get _repostTo => _editorController.repostTo.value;
@override
void initState() {
super.initState();
if (widget.edit == null && widget.reply == null && widget.repost == null) {
_editorController.localRead().then((res) {
if (!res) {
final navState = Get.find<NavigationStateProvider>();
_editorController.realmZone.value = navState.focusedRealm.value;
}
});
}
if (widget.reply != null) {
_editorController.replyTo.value = widget.reply;
}
if (widget.repost != null) {
_editorController.repostTo.value = widget.repost;
}
2024-08-01 06:46:01 +00:00
_editorController.contentController.addListener(() => setState(() {}));
_syncWidget();
}
2024-05-19 10:01:00 +00:00
@override
Widget build(BuildContext context) {
final notifyBannerActions = [
TextButton(
onPressed: _cancelAction,
child: Text('cancel'.tr),
)
];
2024-10-06 09:37:07 +00:00
return RootContainer(
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-30 08:53:13 +00:00
title: Obx(
() => AppBarTitle(
_editorController.mode.value == 0
? 'postEditorModeStory'.tr
: 'postEditorModeArticle'.tr,
),
),
2024-06-22 15:59:11 +00:00
centerTitle: false,
toolbarHeight: AppTheme.toolbarHeight(context),
2024-05-19 10:01:00 +00:00
actions: [
TextButton(
onPressed: _isBusy ? null : () => _applyPost(),
child: Obx(
() => Text(
_editorController.isDraft.isTrue
? 'draftSave'.tr.toUpperCase()
: 'postAction'.tr.toUpperCase(),
),
2024-07-09 13:23:38 +00:00
),
2024-05-19 10:01:00 +00:00
)
],
),
body: Column(
2024-08-01 06:46:01 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2024-07-09 14:39:44 +00:00
children: [
ListTile(
2024-10-13 11:56:37 +00:00
tileColor: Theme.of(context)
.colorScheme
.surfaceContainerLow
.withOpacity(0.5),
2024-09-17 15:08:00 +00:00
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_editorController.title ?? 'title'.tr,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
if (_editorController.aliasController.text.isNotEmpty)
Badge(
label: Text('#${_editorController.aliasController.text}'),
2024-09-17 15:08:00 +00:00
).paddingOnly(bottom: 2),
],
),
subtitle: Text(
_editorController.description ?? 'description'.tr,
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
contentPadding: const EdgeInsets.only(
2024-07-30 12:49:01 +00:00
left: 17,
right: 8,
top: 0,
bottom: 0,
),
trailing: IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
_editorController.editOverview(context).then((_) {
setState(() {});
});
},
),
),
2024-08-01 06:46:01 +00:00
if (_editTo != null && _editTo!.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 (_replyTo != 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: [
Container(
constraints: const BoxConstraints(maxHeight: 280),
child: SingleChildScrollView(
child: PostItem(
item: _replyTo!,
isReactable: false,
).paddingOnly(bottom: 8),
),
),
2024-08-01 06:46:01 +00:00
],
),
if (_repostTo != 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: [
Container(
constraints: const BoxConstraints(maxHeight: 280),
child: SingleChildScrollView(
child: PostItem(
item: _repostTo!,
isReactable: false,
).paddingOnly(bottom: 8),
),
),
2024-08-01 06:46:01 +00:00
],
),
2024-10-14 13:13:57 +00:00
LoadingIndicator(isActive: _isBusy),
Expanded(
2024-10-07 16:06:08 +00:00
child: DefaultTabController(
length: 2,
child: AppTheme.isLargeScreen(context)
? Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: _PostEditorTextField(
focusNode: _contentFocusNode,
controller: _editorController,
onUpdate: () => setState(() {}),
),
2024-08-15 16:52:36 +00:00
),
2024-10-07 16:06:08 +00:00
const VerticalDivider(width: 0.3, thickness: 0.3)
.paddingSymmetric(horizontal: 16),
Expanded(
child: SingleChildScrollView(
padding:
const EdgeInsets.only(top: 12, bottom: 64),
child: MarkdownTextContent(
isAutoWarp: _editorController.mode.value == 0,
content:
_editorController.contentController.text,
parentId: 'post-editor-preview',
).paddingOnly(right: 16),
2024-08-16 13:06:50 +00:00
),
2024-10-07 16:06:08 +00:00
),
],
)
: Column(
children: [
TabBar(
tabs: [
const Tab(icon: Icon(Icons.edit)),
const Tab(icon: Icon(Icons.preview)),
],
),
Expanded(
child: TabBarView(
2024-08-16 13:06:50 +00:00
children: [
2024-10-07 16:06:08 +00:00
_PostEditorTextField(
focusNode: _contentFocusNode,
controller: _editorController,
onUpdate: () => setState(() {}),
),
SingleChildScrollView(
padding: const EdgeInsets.only(
top: 12,
bottom: 64,
2024-08-16 13:06:50 +00:00
),
2024-10-07 16:06:08 +00:00
child: MarkdownTextContent(
isAutoWarp:
_editorController.mode.value == 0,
content: _editorController
.contentController.text,
parentId: 'post-editor-preview',
).paddingOnly(left: 16, right: 16),
)
2024-08-16 13:06:50 +00:00
],
),
2024-10-07 16:06:08 +00:00
),
],
),
),
2024-07-09 14:39:44 +00:00
),
Material(
color: Theme.of(context).colorScheme.surface,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-08-16 13:06:50 +00:00
const Divider(thickness: 0.3, height: 0.3),
2024-10-07 16:06:08 +00:00
SizedBox(
height: 40,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: MarkdownToolbar(
width: 38,
height: 38,
iconSize: 20,
spacing: 8,
hideImage: true,
useIncludedTextField: false,
backgroundColor: Theme.of(context).colorScheme.surface,
iconColor: Theme.of(context).colorScheme.onSurface,
controller: _editorController.contentController,
focusNode: _contentFocusNode,
borderRadius:
const BorderRadius.all(Radius.circular(20)),
).paddingSymmetric(horizontal: 12),
),
).paddingOnly(top: 12),
SizedBox(
height: 56,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
2024-08-16 13:06:50 +00:00
if (_editorController.mode.value == 0)
Obx(
() => TweenAnimationBuilder<double>(
tween: Tween(
begin: 0,
end: _editorController.contentLength.value /
4096,
),
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
builder: (context, value, _) => SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 3,
backgroundColor: Theme.of(context)
.colorScheme
.secondaryContainer,
color: _editorController.contentLength.value >
4096
? Colors.red[900]
: Theme.of(context).colorScheme.primary,
value: value,
),
).paddingAll(10),
),
).paddingSymmetric(horizontal: 4),
2024-07-30 12:49:01 +00:00
Obx(() {
final isDraft = _editorController.isDraft.value;
return IconButton(
icon: const Icon(
Icons.drive_file_rename_outline,
color: Colors.grey,
)
.animate(
target: isDraft ? 0 : 1,
)
.fadeOut(duration: 150.ms)
.swap(
duration: 150.ms,
builder: (_, __) => const Icon(
Icons.public,
color: Colors.green,
).animate().fadeIn(duration: 150.ms),
),
2024-07-09 13:23:38 +00:00
onPressed: () {
_editorController.toggleDraftMode();
2024-07-09 13:23:38 +00:00
},
2024-07-30 12:49:01 +00:00
);
}),
IconButton(
icon: const Icon(Icons.disabled_visible),
color: Theme.of(context).colorScheme.primary,
onPressed: () {
_editorController.editVisibility(context);
},
),
IconButton(
2024-07-30 12:49:01 +00:00
icon: Obx(() {
return badges.Badge(
2024-07-09 15:06:55 +00:00
badgeContent: Text(
_editorController.attachments.length.toString(),
2024-07-09 15:06:55 +00:00
style: const TextStyle(color: Colors.white),
),
showBadge:
_editorController.attachments.isNotEmpty,
2024-07-09 15:06:55 +00:00
position: badges.BadgePosition.topEnd(
top: -12,
end: -8,
),
2024-07-30 12:49:01 +00:00
child: const Icon(Icons.file_present_rounded),
);
}),
color: Theme.of(context).colorScheme.primary,
2024-07-30 12:49:01 +00:00
onPressed: () {
_editorController.editAttachment(context);
},
),
2024-07-30 18:00:03 +00:00
IconButton(
2024-08-01 03:49:28 +00:00
icon: Obx(() {
return badges.Badge(
badgeContent: Text(
_editorController.tags.length.toString(),
style: const TextStyle(color: Colors.white),
),
showBadge: _editorController.tags.isNotEmpty,
position: badges.BadgePosition.topEnd(
top: -12,
end: -8,
),
child: const Icon(Icons.label),
);
}),
2024-07-30 18:00:03 +00:00
color: Theme.of(context).colorScheme.primary,
onPressed: () {
_editorController.editCategoriesAndTags(context);
},
),
2024-08-01 07:21:43 +00:00
IconButton(
icon: Obx(() {
return badges.Badge(
showBadge:
_editorController.realmZone.value != null,
position: badges.BadgePosition.topEnd(
top: -4,
end: -6,
),
child: const Icon(Icons.workspaces),
);
}),
color: Theme.of(context).colorScheme.primary,
onPressed: () {
_editorController.editPublishZone(context);
},
),
2024-08-10 17:57:58 +00:00
IconButton(
icon: Obx(() {
return badges.Badge(
showBadge:
_editorController.thumbnail.value != null,
position: badges.BadgePosition.topEnd(
top: -4,
end: -6,
),
2024-10-07 16:06:08 +00:00
child: const Icon(Icons.wallpaper),
2024-08-10 17:57:58 +00:00
);
}),
color: Theme.of(context).colorScheme.primary,
onPressed: () {
_editorController.editThumbnail(context);
},
),
IconButton(
icon: Obx(() {
return badges.Badge(
showBadge:
_editorController.publishedAt.value != null ||
_editorController.publishedUntil.value !=
null,
position: badges.BadgePosition.topEnd(
top: -4,
end: -6,
),
child: const Icon(Icons.schedule),
);
}),
color: Theme.of(context).colorScheme.primary,
onPressed: () {
_editorController.editPublishDate(context);
},
),
],
).paddingSymmetric(horizontal: 6, vertical: 8),
),
],
).paddingOnly(bottom: MediaQuery.of(context).padding.bottom),
2024-07-09 14:39:44 +00:00
),
],
2024-05-19 10:01:00 +00:00
),
),
);
}
@override
void dispose() {
2024-08-01 06:46:01 +00:00
_contentFocusNode.dispose();
_editorController.dispose();
super.dispose();
}
2024-05-19 10:01:00 +00:00
}
2024-10-07 16:06:08 +00:00
class _PostEditorTextField extends StatelessWidget {
final FocusNode focusNode;
final PostEditorController controller;
final Function onUpdate;
const _PostEditorTextField({
required this.focusNode,
required this.controller,
required this.onUpdate,
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: ListView(
children: [
Container(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: TextField(
maxLines: null,
autofocus: true,
autocorrect: true,
keyboardType: TextInputType.multiline,
controller: controller.contentController,
focusNode: focusNode,
decoration: InputDecoration.collapsed(
hintText: 'postContentPlaceholder'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
),
),
const Gap(120)
],
),
),
Obx(() {
final textStyle = TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75),
);
final showFactors = [
controller.isRestoreFromLocal.value,
controller.lastSaveTime.value != null,
];
final doShow = showFactors.any((x) => x);
return Container(
padding: const EdgeInsets.symmetric(
vertical: 4,
horizontal: 16,
),
child: Row(
children: [
if (showFactors[0])
Text('postRestoreFromLocal'.tr, style: textStyle)
.paddingOnly(right: 4),
if (showFactors[0])
InkWell(
child: Text('clear'.tr, style: textStyle),
onTap: () {
controller.localClear();
controller.currentClear();
onUpdate();
},
),
if (showFactors.where((x) => x).length > 1)
Text(
'·',
style: textStyle,
).paddingSymmetric(horizontal: 8),
if (showFactors[1])
Text(
'postAutoSaveAt'.trParams({
'date': DateFormat('HH:mm:ss').format(
controller.lastSaveTime.value ?? DateTime.now(),
)
}),
style: textStyle,
),
],
),
)
.animate(
key: const Key('post-editor-hint-animation'),
target: doShow ? 1 : 0,
)
.fade(curve: Curves.easeInOut, duration: 300.ms);
}),
],
);
}
}