fixed pending share race error on warm app start

This commit is contained in:
2026-07-02 15:28:05 +02:00
parent 4bc7ffd37a
commit 32f7c311bc
9 changed files with 125 additions and 9 deletions
+13
View File
@@ -12,4 +12,17 @@ class PendingShare {
bool get hasFiles => filePaths.isNotEmpty;
bool get hasText => text != null && text!.isNotEmpty;
bool get isEmpty => !hasFiles && !hasText;
/// True when [other] carries the same payload. The iOS Share Extension
/// fires two `open(url)` requests per share (see ShareViewController), so
/// the same share can arrive twice on the media stream — receivedAt is
/// deliberately ignored here so such duplicates compare equal.
bool contentEquals(PendingShare other) {
if (text != other.text) return false;
if (filePaths.length != other.filePaths.length) return false;
for (var i = 0; i < filePaths.length; i++) {
if (filePaths[i] != other.filePaths[i]) return false;
}
return true;
}
}
+21 -3
View File
@@ -25,7 +25,7 @@ class ShareIntentListener {
try {
final initial = await ReceiveSharingIntent.instance.getInitialMedia();
final share = _toPendingShare(initial);
if (share != null) pending.value = share;
if (share != null) _publish(share);
await ReceiveSharingIntent.instance.reset();
} catch (e) {
debugPrint('ShareIntentListener.initialize failed: $e');
@@ -37,13 +37,24 @@ class ShareIntentListener {
_streamSub ??= ReceiveSharingIntent.instance.getMediaStream().listen(
(items) {
final share = _toPendingShare(items);
if (share != null) pending.value = share;
if (share != null) _publish(share);
},
onError: (Object e) =>
debugPrint('ShareIntentListener stream error: $e'),
);
}
/// The iOS Share Extension fires two `open(url)` requests per share, so the
/// same payload can arrive twice in quick succession. Publishing the
/// duplicate would re-trigger the share-flow navigation, pop the already
/// open ShareTargetPage and thereby delete the temp files of the share that
/// is still in flight — swallow it instead.
void _publish(PendingShare share) {
final current = pending.value;
if (current != null && current.contentEquals(share)) return;
pending.value = share;
}
/// Cancels the warm-share subscription. The singleton survives, so a
/// subsequent [attach] re-subscribes.
void detach() {
@@ -53,8 +64,15 @@ class ShareIntentListener {
/// Discards the current share and removes any temp files the plugin copied
/// into the app cache. Idempotent.
void clear() {
///
/// Pass [ifCurrent] from UI that owns a specific share (e.g. the
/// ShareTargetPage pop handler): the call then only acts while that share
/// is still the pending one. Without the guard, popping a stale share page
/// after a new share arrived would delete the new share's temp files before
/// its upload ran.
void clear({PendingShare? ifCurrent}) {
final current = pending.value;
if (ifCurrent != null && !identical(current, ifCurrent)) return;
pending.value = null;
if (current != null) {
for (final path in current.filePaths) {