System Share on iOS

This commit is contained in:
2024-12-15 12:59:18 +08:00
parent 09ad917e5d
commit 89c912a35b
9 changed files with 166 additions and 46 deletions

View File

@ -23,11 +23,26 @@ import 'package:surface/widgets/post/post_meta_editor.dart';
import 'package:surface/widgets/dialog.dart';
import 'package:provider/provider.dart';
class PostEditorExtraProps {
final String? text;
final String? title;
final String? description;
final List<PostWriteMedia>? attachments;
const PostEditorExtraProps({
this.text,
this.title,
this.description,
this.attachments,
});
}
class PostEditorScreen extends StatefulWidget {
final String mode;
final int? postEditId;
final int? postReplyId;
final int? postRepostId;
final PostEditorExtraProps? extraProps;
const PostEditorScreen({
super.key,
@ -35,6 +50,7 @@ class PostEditorScreen extends StatefulWidget {
required this.postEditId,
required this.postReplyId,
required this.postRepostId,
this.extraProps,
});
@override
@ -130,6 +146,12 @@ class _PostEditorScreenState extends State<PostEditorScreen> {
replying: widget.postReplyId,
reposting: widget.postRepostId,
);
if (widget.extraProps != null) {
_writeController.contentController.text = widget.extraProps!.text ?? '';
_writeController.titleController.text = widget.extraProps!.title ?? '';
_writeController.descriptionController.text = widget.extraProps!.description ?? '';
_writeController.addAttachments(widget.extraProps!.attachments ?? []);
}
}
@override
@ -150,15 +172,15 @@ class _PostEditorScreenState extends State<PostEditorScreen> {
TextSpan(
text: _writeController.title.isNotEmpty ? _writeController.title : 'untitled'.tr(),
style: Theme.of(context).textTheme.titleLarge!.copyWith(
color: Theme.of(context).appBarTheme.foregroundColor!,
),
color: Theme.of(context).appBarTheme.foregroundColor!,
),
),
const TextSpan(text: '\n'),
TextSpan(
text: PostWriteController.kTitleMap[widget.mode]!.tr(),
style: Theme.of(context).textTheme.bodySmall!.copyWith(
color: Theme.of(context).appBarTheme.foregroundColor!,
),
color: Theme.of(context).appBarTheme.foregroundColor!,
),
),
]),
),

115
lib/screens/sharing.dart Normal file
View File

@ -0,0 +1,115 @@
import 'dart:async';
import 'dart:developer';
import 'package:cross_file/cross_file.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:surface/controllers/post_write_controller.dart';
import 'package:surface/screens/post/post_editor.dart';
import 'package:surface/widgets/dialog.dart';
class AppSharingListener extends StatefulWidget {
final Widget child;
const AppSharingListener({super.key, required this.child});
@override
State<AppSharingListener> createState() => _AppSharingListenerState();
}
class _AppSharingListenerState extends State<AppSharingListener> {
late StreamSubscription _shareIntentSubscription;
void _gotoPost(Iterable<SharedMediaFile> value) {
WidgetsBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('shareIntent').tr(),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('shareIntentDescription').tr(),
const Gap(8),
Card(
child: Column(
children: [
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
leading: Icon(Icons.post_add),
trailing: const Icon(Icons.chevron_right),
title: Text('shareIntentPostStory').tr(),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {
'mode': 'stories',
},
extra: PostEditorExtraProps(
attachments: value.map((e) => PostWriteMedia.fromFile(XFile(e.path))).toList(),
),
);
Navigator.pop(context);
},
),
],
),
)
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('dialogDismiss').tr(),
)
],
),
);
});
}
void _initialize() async {
_shareIntentSubscription = ReceiveSharingIntent.instance.getMediaStream().listen((value) {
if (value.isEmpty) return;
if (mounted) {
_gotoPost(value);
}
}, onError: (err) {
log("[ShareIntent] Unable to subscribe: $err");
});
}
void _initialHandle() {
ReceiveSharingIntent.instance.getInitialMedia().then((value) {
if (value.isEmpty) return;
if (mounted) {
_gotoPost(value);
}
ReceiveSharingIntent.instance.reset();
});
}
@override
void initState() {
super.initState();
_initialize();
_initialHandle();
}
@override
void dispose() {
_shareIntentSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}