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
+1
View File
@@ -23,6 +23,7 @@ AppException? _dioToAppException(DioException error) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.receiveTimeout:
case DioExceptionType.transformTimeout:
return NetworkException.timeout(technicalDetails: error.message);
case DioExceptionType.connectionError:
return NetworkException(technicalDetails: error.message);
@@ -14,6 +14,7 @@ AppException mapMarianumConnectError(DioException error) {
case DioExceptionType.connectionTimeout:
case DioExceptionType.sendTimeout:
case DioExceptionType.receiveTimeout:
case DioExceptionType.transformTimeout:
return NetworkException.timeout(technicalDetails: error.message);
case DioExceptionType.connectionError:
return NetworkException(technicalDetails: error.message);
+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) {
+13 -1
View File
@@ -187,11 +187,23 @@ class _FilesUploadDialogState extends State<FilesUploadDialog> {
'${_uploadableFiles.indexOf(file) + 1}/${_uploadableFiles.length}';
});
// A vanished source file would otherwise surface as a cryptic
// "Content-Length must contain only digits" HttpException, because
// statSync reports size -1 for missing files.
final fileStat = FileStat.statSync(filePath);
if (fileStat.type == FileSystemEntityType.notFound || fileStat.size < 0) {
_showUploadError(
'Die Datei "$fileName" ist nicht mehr verfügbar. '
'Bitte wähle sie erneut aus.',
);
return;
}
final HttpClientResponse uploadTask;
try {
uploadTask = await webdavClient.putFile(
File(filePath),
FileStat.statSync(filePath),
fileStat,
PathUri.parse(fullRemotePath),
onProgress: (progress) {
setState(() {
@@ -197,7 +197,7 @@ void _setExternalDraftAndOpenChat(
final settings = context.read<SettingsCubit>();
settings.val(write: true).talkSettings.drafts[room.token] = share.text!;
}
ShareIntentListener.instance.clear();
ShareIntentListener.instance.clear(ifCurrent: share);
_finishWithChat(context, room);
}
@@ -212,13 +212,18 @@ Future<void> _externalUploadFlow(
screen: FilesUploadDialog(
filePaths: share.filePaths,
remotePath: targetPath.join('/'),
onUploadFinished: (_) => _afterExternalUploaded(context, targetPath),
onUploadFinished: (_) =>
_afterExternalUploaded(context, targetPath, share),
),
);
}
void _afterExternalUploaded(BuildContext context, List<String> targetPath) {
ShareIntentListener.instance.clear();
void _afterExternalUploaded(
BuildContext context,
List<String> targetPath,
PendingShare share,
) {
ShareIntentListener.instance.clear(ifCurrent: share);
if (!context.mounted) return;
_finishWithFolder(context, targetPath);
}
@@ -40,7 +40,7 @@ class ShareTargetPage extends StatelessWidget {
@override
Widget build(BuildContext context) => PopScope(
onPopInvokedWithResult: (didPop, _) {
if (didPop) ShareIntentListener.instance.clear();
if (didPop) ShareIntentListener.instance.clear(ifCurrent: share);
},
child: Scaffold(
appBar: AppBar(title: Text(_appBarTitle())),
+66
View File
@@ -0,0 +1,66 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:marianum_mobile/share_intent/pending_share.dart';
PendingShare _share({
List<String> filePaths = const [],
String? text,
DateTime? receivedAt,
}) => PendingShare(
filePaths: filePaths,
text: text,
receivedAt: receivedAt ?? DateTime(2026, 1, 1),
);
void main() {
group('PendingShare.contentEquals', () {
test('equal payloads compare equal regardless of receivedAt', () {
final a = _share(
filePaths: ['/tmp/a.jpg', '/tmp/b.jpg'],
text: 'hi',
receivedAt: DateTime(2026, 1, 1),
);
final b = _share(
filePaths: ['/tmp/a.jpg', '/tmp/b.jpg'],
text: 'hi',
receivedAt: DateTime(2026, 1, 1, 0, 0, 1),
);
expect(a.contentEquals(b), isTrue);
expect(b.contentEquals(a), isTrue);
});
test('differing text is not equal', () {
final a = _share(filePaths: ['/tmp/a.jpg'], text: 'hi');
final b = _share(filePaths: ['/tmp/a.jpg'], text: 'ho');
expect(a.contentEquals(b), isFalse);
});
test('null text vs text is not equal', () {
final a = _share(filePaths: ['/tmp/a.jpg']);
final b = _share(filePaths: ['/tmp/a.jpg'], text: 'hi');
expect(a.contentEquals(b), isFalse);
});
test('differing file lists are not equal', () {
final a = _share(filePaths: ['/tmp/a.jpg']);
final b = _share(filePaths: ['/tmp/b.jpg']);
final c = _share(filePaths: ['/tmp/a.jpg', '/tmp/b.jpg']);
expect(a.contentEquals(b), isFalse);
expect(a.contentEquals(c), isFalse);
});
test('file order matters', () {
final a = _share(filePaths: ['/tmp/a.jpg', '/tmp/b.jpg']);
final b = _share(filePaths: ['/tmp/b.jpg', '/tmp/a.jpg']);
expect(a.contentEquals(b), isFalse);
});
test('empty shares compare equal', () {
expect(_share().contentEquals(_share()), isTrue);
});
});
}