mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-05 16:08:46 +00:00
TF-4827 Add a recovery seam to the size-rejection path
Ref: #4848 TF-4827 Route inline files to the upload recovery Addresses: 4182803889 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4182803889 TF-4827 Skip the upload recovery when a rejection carries no files Addresses: 4191301057 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191301057 TF-4827 Document the upload recovery contract Addresses: 4191300814, 4191300935, 4191301151 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191300814 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191300935 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191301151 TF-4827 Align ADR-0109 with inline files reaching the recovery Addresses: 4191308467 https://github.com/linagora/tmail-flutter/pull/4910#discussion_r4191308467
This commit is contained in:
1 parent
c317bae063
commit
6db4615efa
8 files changed
+200
-8
No files matched your search
@@ -62,12 +62,12 @@ Accepted
|
||||
- The rule rejects on the running total, so a rejected batch can hold no oversize file at all:
|
||||
24 MB already attached plus a 2 MB file under a 25 MB cap.
|
||||
- A recovery therefore takes the whole rejected batch, not the files over the cap.
|
||||
- Inline files are excluded: a pasted screenshot stays in the body, never becomes a link.
|
||||
- A batch with no regular file carries nothing to act on, so it shows the dialog.
|
||||
- Inline files are included: an oversize pasted image reaches the recovery like any picked file.
|
||||
- A recovery that does not handle a batch returns false, so the dialog shows.
|
||||
|
||||
```
|
||||
on ValidationRejected(failure):
|
||||
if request.regularFiles.isNotEmpty:
|
||||
if request.files.isNotEmpty:
|
||||
try:
|
||||
if await recovery?.recover(failure, request) == true:
|
||||
return false # recovery owns the UX from here
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
import 'package:model/upload/file_info.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_limits.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_prompt.dart';
|
||||
@@ -11,7 +12,14 @@ final class AttachmentUploadRequest {
|
||||
final AttachmentUploadSizeSnapshot sizes;
|
||||
final AttachmentUploadLimits limits;
|
||||
|
||||
const AttachmentUploadRequest({required this.sizes, required this.limits});
|
||||
/// Empty when built from byte counts only (re-attaching an uploaded attachment).
|
||||
final List<FileInfo> files;
|
||||
|
||||
AttachmentUploadRequest({
|
||||
required this.sizes,
|
||||
required this.limits,
|
||||
Iterable<FileInfo> files = const [],
|
||||
}) : files = List.unmodifiable(files);
|
||||
}
|
||||
|
||||
typedef AttachmentUploadDecision = ValidationDecision<AttachmentUploadFailure, AttachmentUploadPrompt>;
|
||||
|
||||
@@ -25,6 +25,7 @@ final class AttachmentUploadRequestFactory {
|
||||
proposedRegularAttachmentBytes: proposedRegularBytes,
|
||||
),
|
||||
limits: _limitsOf(state),
|
||||
files: candidate,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
|
||||
import 'package:core/utils/app_logger.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/validation_decision.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback.dart';
|
||||
|
||||
final class AttachmentUploadGate {
|
||||
@@ -11,11 +14,15 @@ final class AttachmentUploadGate {
|
||||
Future<bool> permits({
|
||||
required AttachmentUploadRequest request,
|
||||
required AttachmentValidationFeedback? Function() feedbackFactory,
|
||||
AttachmentUploadRecovery? Function()? recoveryFactory,
|
||||
}) async {
|
||||
switch (_validator.validate(request)) {
|
||||
case ValidationAllowed():
|
||||
return true;
|
||||
case ValidationRejected(:final failure):
|
||||
// Byte-only re-attach has no file for a recovery to act on.
|
||||
final recovery = request.files.isNotEmpty ? recoveryFactory?.call() : null;
|
||||
if (recovery != null && await _recovered(recovery, failure, request)) return false;
|
||||
final feedback = feedbackFactory();
|
||||
if (feedback == null) return false;
|
||||
await feedback.showFailure(failure);
|
||||
@@ -26,4 +33,17 @@ final class AttachmentUploadGate {
|
||||
return feedback.confirmAll(prompts);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _recovered(
|
||||
AttachmentUploadRecovery recovery,
|
||||
AttachmentUploadFailure failure,
|
||||
AttachmentUploadRequest request,
|
||||
) async {
|
||||
try {
|
||||
return await recovery.recover(failure, request);
|
||||
} catch (e) {
|
||||
logError('AttachmentUploadGate::_recovered: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart';
|
||||
|
||||
/// An alternative to the failure dialog, e.g. upload oversize to Drive instead.
|
||||
///
|
||||
/// Called only for rejections with files. False (not handled) shows the dialog.
|
||||
/// True means it took over and owns all feedback; completes at takeover, not
|
||||
/// at upload end. A throw declines only before any UI, after that own errors.
|
||||
abstract interface class AttachmentUploadRecovery {
|
||||
Future<bool> recover(AttachmentUploadFailure failure, AttachmentUploadRequest request);
|
||||
}
|
||||
|
||||
/// Only assigns fields; lookups go in `recover`, inside the gate's try.
|
||||
typedef AttachmentUploadRecoveryBuilder =
|
||||
AttachmentUploadRecovery Function(BuildContext context);
|
||||
+11
-1
@@ -7,6 +7,7 @@ import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request_factory.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_state_source.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_gate.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback_impl.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
@@ -29,15 +30,18 @@ class AttachmentUploadValidationService {
|
||||
final AttachmentUploadStateSource _stateSource;
|
||||
final AttachmentUploadGate _gate;
|
||||
final AttachmentValidationFeedbackBuilder _feedbackBuilder;
|
||||
final AttachmentUploadRecoveryBuilder? _recoveryBuilder;
|
||||
|
||||
AttachmentUploadValidationService({
|
||||
required AttachmentUploadStateSource stateSource,
|
||||
AttachmentUploadValidator? validator,
|
||||
AttachmentValidationFeedbackBuilder? feedbackBuilder,
|
||||
AttachmentUploadRecoveryBuilder? recoveryBuilder,
|
||||
}) : _stateSource = stateSource,
|
||||
_gate = AttachmentUploadGate(
|
||||
validator ?? AttachmentUploadValidator([const AttachmentSizeLimitRule()])),
|
||||
_feedbackBuilder = feedbackBuilder ?? AttachmentValidationFeedbackImpl.new;
|
||||
_feedbackBuilder = feedbackBuilder ?? AttachmentValidationFeedbackImpl.new,
|
||||
_recoveryBuilder = recoveryBuilder;
|
||||
|
||||
/// Bytes still attachable under the server cap, `null` when it advertises none.
|
||||
int? get remainingCapacityBytes {
|
||||
@@ -104,6 +108,12 @@ class AttachmentUploadValidationService {
|
||||
final resolvedContext = context ?? currentContext;
|
||||
return resolvedContext == null ? null : _feedbackBuilder(resolvedContext);
|
||||
},
|
||||
recoveryFactory: () {
|
||||
final resolvedContext = context ?? currentContext;
|
||||
return resolvedContext == null || _recoveryBuilder == null
|
||||
? null
|
||||
: _recoveryBuilder(resolvedContext);
|
||||
},
|
||||
);
|
||||
if (allowed) {
|
||||
onAllowed();
|
||||
|
||||
@@ -6,14 +6,14 @@ import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_size_snapshot.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/validation_decision.dart';
|
||||
|
||||
const _request = AttachmentUploadRequest(
|
||||
sizes: AttachmentUploadSizeSnapshot(
|
||||
final _request = AttachmentUploadRequest(
|
||||
sizes: const AttachmentUploadSizeSnapshot(
|
||||
currentAllAttachmentBytes: 0,
|
||||
proposedAllAttachmentBytes: 0,
|
||||
currentRegularAttachmentBytes: 0,
|
||||
proposedRegularAttachmentBytes: 0,
|
||||
),
|
||||
limits: AttachmentUploadLimits(warningLimitBytes: 1000000000),
|
||||
limits: const AttachmentUploadLimits(warningLimitBytes: 1000000000),
|
||||
);
|
||||
|
||||
class _StubAttachmentUploadRule implements AttachmentUploadRule {
|
||||
|
||||
@@ -5,11 +5,14 @@ import 'package:filesize/filesize.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:model/upload/file_info.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_size_limit_rule.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_failure.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_limits.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_request.dart';
|
||||
import 'package:tmail_ui_user/features/upload/domain/validator/attachment_upload_size_snapshot.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_gate.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_upload_recovery.dart';
|
||||
import 'package:tmail_ui_user/features/upload/presentation/validator/attachment_validation_feedback_impl.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
|
||||
@@ -19,6 +22,7 @@ AttachmentUploadRequest _request({
|
||||
int proposedAllAttachmentBytes = 0,
|
||||
int proposedRegularAttachmentBytes = 0,
|
||||
int? hardLimitBytes,
|
||||
List<FileInfo> files = const [],
|
||||
}) {
|
||||
return AttachmentUploadRequest(
|
||||
sizes: AttachmentUploadSizeSnapshot(
|
||||
@@ -28,9 +32,26 @@ AttachmentUploadRequest _request({
|
||||
proposedRegularAttachmentBytes: proposedRegularAttachmentBytes,
|
||||
),
|
||||
limits: AttachmentUploadLimits(warningLimitBytes: 1000000, hardLimitBytes: hardLimitBytes),
|
||||
files: files,
|
||||
);
|
||||
}
|
||||
|
||||
const _oversizeFile = FilePlaceholderInfo(fileName: 'big.zip', fileSize: 200);
|
||||
const _oversizeInlineFile = FilePlaceholderInfo(fileName: 'shot.png', fileSize: 200, isInline: true);
|
||||
|
||||
class _StubRecovery implements AttachmentUploadRecovery {
|
||||
final Future<bool> Function(AttachmentUploadFailure failure, AttachmentUploadRequest request) onRecover;
|
||||
bool called = false;
|
||||
|
||||
_StubRecovery(this.onRecover);
|
||||
|
||||
@override
|
||||
Future<bool> recover(AttachmentUploadFailure failure, AttachmentUploadRequest request) {
|
||||
called = true;
|
||||
return onRecover(failure, request);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
setUp(() {
|
||||
Get.testMode = true;
|
||||
@@ -118,4 +139,120 @@ void main() {
|
||||
expect(await future, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('AttachmentUploadGate.permits with recovery', () {
|
||||
testWidgets('no builder shows the failure dialog', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
|
||||
final future = gate.permits(
|
||||
request: _request(
|
||||
proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget);
|
||||
await tester.tap(find.text(AppLocalizations.of(context).got_it));
|
||||
await tester.pumpAndSettle();
|
||||
expect(await future, isFalse);
|
||||
});
|
||||
|
||||
testWidgets('recovery returning true skips the dialog and returns false', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
final recovery = _StubRecovery((_, __) async => true);
|
||||
|
||||
final allowed = await gate.permits(
|
||||
request: _request(
|
||||
proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
recoveryFactory: () => recovery,
|
||||
);
|
||||
|
||||
expect(recovery.called, isTrue);
|
||||
expect(allowed, isFalse);
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('recovery returning false falls back to the dialog', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
final recovery = _StubRecovery((_, __) async => false);
|
||||
|
||||
final future = gate.permits(
|
||||
request: _request(
|
||||
proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
recoveryFactory: () => recovery,
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget);
|
||||
await tester.tap(find.text(AppLocalizations.of(context).got_it));
|
||||
await tester.pumpAndSettle();
|
||||
expect(await future, isFalse);
|
||||
});
|
||||
|
||||
testWidgets('recovery throwing falls back to the dialog', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
final recovery = _StubRecovery((_, __) async => throw Exception('declined'));
|
||||
|
||||
final future = gate.permits(
|
||||
request: _request(
|
||||
proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeFile]),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
recoveryFactory: () => recovery,
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget);
|
||||
await tester.tap(find.text(AppLocalizations.of(context).got_it));
|
||||
await tester.pumpAndSettle();
|
||||
expect(await future, isFalse);
|
||||
});
|
||||
|
||||
testWidgets('inline-only files reach the recovery', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
final recovery = _StubRecovery((_, __) async => true);
|
||||
|
||||
final allowed = await gate.permits(
|
||||
request: _request(
|
||||
proposedAllAttachmentBytes: 200, hardLimitBytes: 100, files: [_oversizeInlineFile]),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
recoveryFactory: () => recovery,
|
||||
);
|
||||
|
||||
expect(recovery.called, isTrue);
|
||||
expect(allowed, isFalse);
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('rejection without files skips the recovery', (tester) async {
|
||||
await tester.pumpWidget(WidgetFixtures.makeTestableWidget(child: const SizedBox.shrink()));
|
||||
await tester.pump();
|
||||
final context = tester.element(find.byType(SizedBox));
|
||||
final recovery = _StubRecovery((_, __) async => true);
|
||||
|
||||
final future = gate.permits(
|
||||
request: _request(proposedAllAttachmentBytes: 200, hardLimitBytes: 100),
|
||||
feedbackFactory: () => AttachmentValidationFeedbackImpl(context),
|
||||
recoveryFactory: () => recovery,
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(recovery.called, isFalse);
|
||||
expect(find.byKey(const Key('confirm_dialog_action')), findsOneWidget);
|
||||
await tester.tap(find.text(AppLocalizations.of(context).got_it));
|
||||
await tester.pumpAndSettle();
|
||||
expect(await future, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user