154 lines
4.7 KiB
Dart
154 lines
4.7 KiB
Dart
import 'package:ttstd_family_care/core/widgets/app_dialogs.dart';
|
|
import 'package:cupertino_ui/cupertino_ui.dart';
|
|
import '../../../l10n/app_localizations.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'sharing_controller.dart';
|
|
|
|
class ShareReceiver extends StatefulWidget {
|
|
const ShareReceiver({super.key, required this.router, required this.child});
|
|
final GoRouter router;
|
|
final Widget child;
|
|
@override
|
|
State<ShareReceiver> createState() => _ShareReceiverState();
|
|
}
|
|
|
|
class _ShareReceiverState extends State<ShareReceiver>
|
|
with WidgetsBindingObserver {
|
|
String get _currentPath =>
|
|
widget.router.routerDelegate.currentConfiguration.isEmpty
|
|
? widget.router.routeInformationProvider.value.uri.path
|
|
: widget.router.state.uri.path;
|
|
bool _navigatePending = false;
|
|
bool _clipboardCheckPending = true;
|
|
bool _clipboardFrameScheduled = false;
|
|
bool _clipboardDialogOpen = false;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
sharingContainer.listen(
|
|
sharingControllerProvider.select((value) => value.delivery),
|
|
(previous, next) {
|
|
_navigatePending = true;
|
|
_navigate();
|
|
},
|
|
);
|
|
sharingContainer.listen(
|
|
sharingControllerProvider.select((value) => value.clipboardUrl),
|
|
(previous, next) {
|
|
if (next != null) _scheduleClipboard();
|
|
},
|
|
);
|
|
widget.router.routerDelegate.addListener(_routeChanged);
|
|
_scheduleClipboard();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
_clipboardCheckPending = true;
|
|
_scheduleClipboard();
|
|
}
|
|
}
|
|
|
|
void _routeChanged() {
|
|
_navigate();
|
|
_scheduleClipboard();
|
|
}
|
|
|
|
void _scheduleClipboard() {
|
|
if (_clipboardFrameScheduled || !mounted) return;
|
|
_clipboardFrameScheduled = true;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
_clipboardFrameScheduled = false;
|
|
if (!mounted ||
|
|
WidgetsBinding.instance.lifecycleState != AppLifecycleState.resumed ||
|
|
_currentPath == '/splash') {
|
|
return;
|
|
}
|
|
if (_clipboardCheckPending) {
|
|
_clipboardCheckPending = false;
|
|
await sharingContainer
|
|
.read(sharingControllerProvider.notifier)
|
|
.checkClipboard();
|
|
}
|
|
if (mounted) _showClipboardPrompt();
|
|
});
|
|
WidgetsBinding.instance.ensureVisualUpdate();
|
|
}
|
|
|
|
Future<void> _showClipboardPrompt() async {
|
|
if (_clipboardDialogOpen ||
|
|
!mounted ||
|
|
WidgetsBinding.instance.lifecycleState != AppLifecycleState.resumed) {
|
|
return;
|
|
}
|
|
final state = sharingContainer.read(sharingControllerProvider);
|
|
final url = state.clipboardUrl;
|
|
final text = state.clipboardText;
|
|
final context = widget.router.routerDelegate.navigatorKey.currentContext;
|
|
if (url == null || text == null || context == null) return;
|
|
_clipboardDialogOpen = true;
|
|
final l10n = AppLocalizations.of(context);
|
|
try {
|
|
final accepted = await AppDialogs.show<bool>(
|
|
context: context,
|
|
builder: (context) => CupertinoAlertDialog(
|
|
title: Text(l10n.shareClipboardTitle),
|
|
content: Column(
|
|
children: [
|
|
Text(l10n.shareClipboardQuestion),
|
|
const SizedBox(height: 12),
|
|
Text(text, maxLines: 5, overflow: TextOverflow.ellipsis),
|
|
],
|
|
),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: Text(l10n.commonCancel),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDefaultAction: true,
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text(l10n.shareClipboardConfirm),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (mounted &&
|
|
sharingContainer.read(sharingControllerProvider).clipboardUrl ==
|
|
url) {
|
|
sharingContainer
|
|
.read(sharingControllerProvider.notifier)
|
|
.resolveClipboard(share: accepted == true);
|
|
}
|
|
} finally {
|
|
_clipboardDialogOpen = false;
|
|
}
|
|
}
|
|
|
|
void _navigate() {
|
|
if (!_navigatePending || !mounted || _currentPath == '/splash') {
|
|
return;
|
|
}
|
|
_navigatePending = false;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted && _currentPath != '/received-share') {
|
|
widget.router.push('/received-share');
|
|
}
|
|
});
|
|
WidgetsBinding.instance.ensureVisualUpdate();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
disposeSharingContainer();
|
|
widget.router.routerDelegate.removeListener(_routeChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.child;
|
|
}
|