Solian/lib/screens/posts/post_editor.dart

451 lines
17 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';
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';
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';
import 'package:solian/widgets/app_bar_leading.dart';
2024-06-22 15:59:11 +00:00
import 'package:solian/widgets/app_bar_title.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-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;
2024-07-09 13:23:38 +00:00
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
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
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-07-30 08:29:30 +00:00
_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() {
AppRouter.instance.pop();
}
2024-08-01 03:49:28 +00:00
Post? get _editTo => _editorController.editTo.value;
Post? get _replyTo => _editorController.replyTo.value;
Post? get _repostTo => _editorController.repostTo.value;
Realm? get _realm => _editorController.realmZone.value;
@override
void initState() {
super.initState();
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-05-19 10:01:00 +00:00
return Material(
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-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: SolianTheme.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(
tileColor: Theme.of(context).colorScheme.surfaceContainerLow,
title: Text(
_editorController.title ?? 'title'.tr,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
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: [
PostItem(
item: _replyTo!,
isReactable: false,
).paddingOnly(bottom: 8),
],
),
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: [
PostItem(
item: _repostTo!,
isReactable: false,
).paddingOnly(bottom: 8),
],
),
Expanded(
child: ListView(
children: [
if (_isBusy)
const LinearProgressIndicator().animate().scaleX(),
Container(
2024-07-30 12:49:01 +00:00
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: TextField(
maxLines: null,
autofocus: true,
autocorrect: true,
keyboardType: TextInputType.multiline,
controller: _editorController.contentController,
2024-08-01 06:46:01 +00:00
focusNode: _contentFocusNode,
decoration: InputDecoration.collapsed(
hintText: 'postContentPlaceholder'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-05-19 10:01:00 +00:00
),
),
const SizedBox(height: 120)
],
),
2024-07-09 14:39:44 +00:00
),
Material(
color: Theme.of(context).colorScheme.surface,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-07-30 08:29:30 +00:00
Obx(() {
final textStyle = TextStyle(
fontSize: 12,
color: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.75),
);
final showFactors = [
_editorController.isRestoreFromLocal.value,
_editorController.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: () {
_editorController.localClear();
_editorController.currentClear();
setState(() {});
},
),
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(
_editorController.lastSaveTime.value ??
DateTime.now(),
)
}),
style: textStyle,
),
],
),
)
2024-07-30 12:49:01 +00:00
.animate(
key: const Key('post-editor-hint-animation'),
target: doShow ? 1 : 0,
)
2024-07-30 08:29:30 +00:00
.fade(curve: Curves.easeInOut, duration: 300.ms);
}),
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, _) => LinearProgressIndicator(
minHeight: 2,
color: _editorController.contentLength.value > 4096
? Colors.red[900]
: Theme.of(context).colorScheme.primary,
value: value,
),
),
),
SizedBox(
height: 56,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
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-01 06:46:01 +00:00
MarkdownToolbar(
hideImage: true,
useIncludedTextField: false,
backgroundColor: Colors.transparent,
iconColor: Theme.of(context).colorScheme.onSurface,
controller: _editorController.contentController,
focusNode: _contentFocusNode,
borderRadius:
const BorderRadius.all(Radius.circular(20)),
width: 40,
).paddingSymmetric(horizontal: 4),
],
).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
}