From 7fc18b40db91be7b78a00f3f79eceb9c83bc6be2 Mon Sep 17 00:00:00 2001
From: LittleSheep <littlesheep.code@hotmail.com>
Date: Sun, 22 Dec 2024 00:41:41 +0800
Subject: [PATCH] :sparkles: Able to edit post alias

---
 assets/translations/en-US.json                |  2 +
 assets/translations/zh-CN.json                |  2 +
 lib/controllers/post_write_controller.dart    |  5 +-
 lib/screens/post/post_editor.dart             |  2 +-
 lib/widgets/post/post_media_pending_list.dart | 48 +++++++++++--------
 lib/widgets/post/post_meta_editor.dart        | 12 +++++
 lib/widgets/post/post_mini_editor.dart        |  2 +-
 7 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/assets/translations/en-US.json b/assets/translations/en-US.json
index 98ce23e..79b1239 100644
--- a/assets/translations/en-US.json
+++ b/assets/translations/en-US.json
@@ -139,6 +139,8 @@
   "fieldPostTitle": "Title",
   "fieldPostDescription": "Description",
   "fieldPostTags": "Tags",
+  "fieldPostAlias": "Alias",
+  "fieldPostAliasHint": "Optional, used to represent the post in URL, should follow URL-Safe.",
   "postPublish": "Publish",
   "postPosted": "Post has been posted.",
   "postPublishedAt": "Published At",
diff --git a/assets/translations/zh-CN.json b/assets/translations/zh-CN.json
index 5379d86..a07bd85 100644
--- a/assets/translations/zh-CN.json
+++ b/assets/translations/zh-CN.json
@@ -123,6 +123,8 @@
   "fieldPostTitle": "标题",
   "fieldPostDescription": "描述",
   "fieldPostTags": "标签",
+  "fieldPostAlias": "别名",
+  "fieldPostAliasHint": "可选项,用于在 URL 中表示该帖子,应遵循 URL-Safe 的原则。",
   "postPublish": "发布",
   "postPublishedAt": "发布于",
   "postPublishedUntil": "取消发布于",
diff --git a/lib/controllers/post_write_controller.dart b/lib/controllers/post_write_controller.dart
index ae8fa98..6f61470 100644
--- a/lib/controllers/post_write_controller.dart
+++ b/lib/controllers/post_write_controller.dart
@@ -152,6 +152,7 @@ class PostWriteController extends ChangeNotifier {
   final TextEditingController contentController = TextEditingController();
   final TextEditingController titleController = TextEditingController();
   final TextEditingController descriptionController = TextEditingController();
+  final TextEditingController aliasController = TextEditingController();
 
   PostWriteController() {
     titleController.addListener(() => notifyListeners());
@@ -198,6 +199,7 @@ class PostWriteController extends ChangeNotifier {
         titleController.text = post.body['title'] ?? '';
         descriptionController.text = post.body['description'] ?? '';
         contentController.text = post.body['content'] ?? '';
+        aliasController.text = post.alias ?? '';
         publishedAt = post.publishedAt;
         publishedUntil = post.publishedUntil;
         visibleUsers = List.from(post.visibleUsersList ?? []);
@@ -269,7 +271,7 @@ class PostWriteController extends ChangeNotifier {
     notifyListeners();
   }
 
-  Future<void> post(BuildContext context) async {
+  Future<void> sendPost(BuildContext context) async {
     if (isBusy || publisher == null) return;
 
     final sn = context.read<SnNetworkProvider>();
@@ -334,6 +336,7 @@ class PostWriteController extends ChangeNotifier {
         data: {
           'publisher': publisher!.id,
           'content': contentController.text,
+          if (aliasController.text.isNotEmpty) 'alias': aliasController.text,
           if (titleController.text.isNotEmpty) 'title': titleController.text,
           if (descriptionController.text.isNotEmpty) 'description': descriptionController.text,
           if (thumbnail != null && thumbnail!.attachment != null) 'thumbnail': thumbnail!.attachment!.rid,
diff --git a/lib/screens/post/post_editor.dart b/lib/screens/post/post_editor.dart
index c11c508..c1df8c8 100644
--- a/lib/screens/post/post_editor.dart
+++ b/lib/screens/post/post_editor.dart
@@ -496,7 +496,7 @@ class _PostEditorScreenState extends State<PostEditorScreen> {
                           onPressed: (_writeController.isBusy || _writeController.publisher == null)
                               ? null
                               : () {
-                                  _writeController.post(context).then((_) {
+                                  _writeController.sendPost(context).then((_) {
                                     if (!context.mounted) return;
                                     Navigator.pop(context, true);
                                   });
diff --git a/lib/widgets/post/post_media_pending_list.dart b/lib/widgets/post/post_media_pending_list.dart
index d7d717b..9186e23 100644
--- a/lib/widgets/post/post_media_pending_list.dart
+++ b/lib/widgets/post/post_media_pending_list.dart
@@ -189,16 +189,19 @@ class PostMediaPendingList extends StatelessWidget {
                   child: AspectRatio(
                     aspectRatio: 1,
                     child: switch (thumbnail!.type) {
-                      PostWriteMediaType.image => LayoutBuilder(builder: (context, constraints) {
-                          return Image(
-                            image: thumbnail!.getImageProvider(
-                              context,
-                              width: (constraints.maxWidth * devicePixelRatio).round(),
-                              height: (constraints.maxHeight * devicePixelRatio).round(),
-                            )!,
-                            fit: BoxFit.contain,
-                          );
-                        }),
+                      PostWriteMediaType.image => Container(
+                        color: Theme.of(context).colorScheme.surfaceContainer,
+                        child: LayoutBuilder(builder: (context, constraints) {
+                            return Image(
+                              image: thumbnail!.getImageProvider(
+                                context,
+                                width: (constraints.maxWidth * devicePixelRatio).round(),
+                                height: (constraints.maxHeight * devicePixelRatio).round(),
+                              )!,
+                              fit: BoxFit.contain,
+                            );
+                          }),
+                      ),
                       _ => Container(
                           color: Theme.of(context).colorScheme.surface,
                           child: const Icon(Symbols.docs).center(),
@@ -236,18 +239,21 @@ class PostMediaPendingList extends StatelessWidget {
                       child: AspectRatio(
                         aspectRatio: 1,
                         child: switch (media.type) {
-                          PostWriteMediaType.image => LayoutBuilder(builder: (context, constraints) {
-                              return Image(
-                                image: media.getImageProvider(
-                                  context,
-                                  width: (constraints.maxWidth * devicePixelRatio).round(),
-                                  height: (constraints.maxHeight * devicePixelRatio).round(),
-                                )!,
-                                fit: BoxFit.cover,
-                              );
-                            }),
+                          PostWriteMediaType.image => Container(
+                            color: Theme.of(context).colorScheme.surfaceContainer,
+                            child: LayoutBuilder(builder: (context, constraints) {
+                                return Image(
+                                  image: media.getImageProvider(
+                                    context,
+                                    width: (constraints.maxWidth * devicePixelRatio).round(),
+                                    height: (constraints.maxHeight * devicePixelRatio).round(),
+                                  )!,
+                                  fit: BoxFit.contain,
+                                );
+                              }),
+                          ),
                           _ => Container(
-                              color: Theme.of(context).colorScheme.surface,
+                              color: Theme.of(context).colorScheme.surfaceContainer,
                               child: const Icon(Symbols.docs).center(),
                             ),
                         },
diff --git a/lib/widgets/post/post_meta_editor.dart b/lib/widgets/post/post_meta_editor.dart
index afdfbec..1bbd710 100644
--- a/lib/widgets/post/post_meta_editor.dart
+++ b/lib/widgets/post/post_meta_editor.dart
@@ -114,6 +114,18 @@ class PostMetaEditor extends StatelessWidget {
                 controller.setTags(value);
               },
             ).padding(horizontal: 24),
+            const Gap(4),
+            TextField(
+              controller: controller.aliasController,
+              decoration: InputDecoration(
+                labelText: 'fieldPostAlias'.tr(),
+                helperText: 'fieldPostAliasHint'.tr(),
+                helperMaxLines: 2,
+                border: UnderlineInputBorder(),
+              ),
+              onTapOutside: (_) =>
+                  FocusManager.instance.primaryFocus?.unfocus(),
+            ).padding(horizontal: 24),
             const Gap(12),
             ListTile(
               contentPadding: const EdgeInsets.symmetric(horizontal: 24),
diff --git a/lib/widgets/post/post_mini_editor.dart b/lib/widgets/post/post_mini_editor.dart
index aac53fa..93951de 100644
--- a/lib/widgets/post/post_mini_editor.dart
+++ b/lib/widgets/post/post_mini_editor.dart
@@ -217,7 +217,7 @@ class _PostMiniEditorState extends State<PostMiniEditor> {
                             _writeController.publisher == null)
                         ? null
                         : () {
-                            _writeController.post(context).then((_) {
+                            _writeController.sendPost(context).then((_) {
                               if (!context.mounted) return;
                               if (widget.onPost != null) widget.onPost!();
                               context.showSnackbar('postPosted'.tr());