🚸 Enhanced share feature

This commit is contained in:
LittleSheep 2024-08-04 18:32:16 +08:00
parent 62ff1c2f1c
commit d9804ba00b
3 changed files with 38 additions and 12 deletions

View File

@ -12,6 +12,7 @@ const i18nEnglish = {
'draftBox': 'Draft Box', 'draftBox': 'Draft Box',
'more': 'More', 'more': 'More',
'share': 'Share', 'share': 'Share',
'shareNoUri': 'Share text content',
'feed': 'Feed', 'feed': 'Feed',
'unlink': 'Unlink', 'unlink': 'Unlink',
'feedSearch': 'Search Feed', 'feedSearch': 'Search Feed',

View File

@ -20,6 +20,7 @@ const i18nSimplifiedChinese = {
'draftBox': '草稿箱', 'draftBox': '草稿箱',
'more': '更多', 'more': '更多',
'share': '分享', 'share': '分享',
'shareNoUri': '分享文字内容',
'feed': '资讯', 'feed': '资讯',
'unlink': '移除链接', 'unlink': '移除链接',
'feedSearch': '搜索资讯', 'feedSearch': '搜索资讯',

View File

@ -7,6 +7,7 @@ import 'package:get/get.dart';
import 'package:share_plus/share_plus.dart'; import 'package:share_plus/share_plus.dart';
import 'package:solian/exts.dart'; import 'package:solian/exts.dart';
import 'package:solian/models/post.dart'; import 'package:solian/models/post.dart';
import 'package:solian/platform.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/screens/posts/post_editor.dart'; import 'package:solian/screens/posts/post_editor.dart';
@ -38,12 +39,24 @@ class _PostActionState extends State<PostAction> {
}); });
} }
Future<void> _doShare() async { Future<void> _doShare({bool noUri = false}) async {
final box = context.findRenderObject() as RenderBox?; final box = context.findRenderObject() as RenderBox?;
if ((PlatformInfo.isAndroid || PlatformInfo.isIOS) && !noUri) {
await Share.shareUri(
Uri.parse('https://sn.solsynth.dev/posts/view/${widget.item.id}'),
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
} else {
final extraContent = [
widget.item.body['title'],
widget.item.body['description'],
];
final isExtraNotEmpty = extraContent.any((x) => x != null);
await Share.share( await Share.share(
'postShareContent'.trParams({ 'postShareContent'.trParams({
'username': widget.item.author.nick, 'username': widget.item.author.nick,
'content': widget.item.body['content'] ?? 'no content', 'content':
'${extraContent.join('\n')}${isExtraNotEmpty ? '\n\n' : ''}${widget.item.body['content'] ?? 'no content'}',
'link': 'https://sn.solsynth.dev/posts/view/${widget.item.id}', 'link': 'https://sn.solsynth.dev/posts/view/${widget.item.id}',
}), }),
subject: 'postShareSubject'.trParams({ subject: 'postShareSubject'.trParams({
@ -52,6 +65,7 @@ class _PostActionState extends State<PostAction> {
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
); );
} }
}
@override @override
void initState() { void initState() {
@ -86,6 +100,16 @@ class _PostActionState extends State<PostAction> {
contentPadding: const EdgeInsets.symmetric(horizontal: 24), contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Icons.share), leading: const Icon(Icons.share),
title: Text('share'.tr), title: Text('share'.tr),
trailing: PlatformInfo.isIOS || PlatformInfo.isAndroid
? IconButton(
icon: const Icon(Icons.link_off),
tooltip: 'shareNoUri'.tr,
onPressed: () async {
await _doShare(noUri: true);
Navigator.pop(context);
},
)
: null,
onTap: () async { onTap: () async {
await _doShare(); await _doShare();
Navigator.pop(context); Navigator.pop(context);