TF-4236 Implement remove a label from an thread

This commit is contained in:
dab246
2026-01-09 19:49:21 +07:00
committed by Dat H. Pham
parent b37359af03
commit 1058b94363
23 changed files with 402 additions and 47 deletions
@@ -225,4 +225,11 @@ abstract class EmailDataSource {
EmailId emailId,
KeyWordIdentifier labelKeyword,
);
Future<void> removeLabelFromThread(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
);
}
@@ -604,4 +604,21 @@ class EmailDataSourceImpl extends EmailDataSource {
);
}).catchError(_exceptionThrower.throwException);
}
@override
Future<void> removeLabelFromThread(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
) {
return Future.sync(() async {
return await emailAPI.removeLabelFromThread(
session,
accountId,
emailIds,
labelKeyword,
);
}).catchError(_exceptionThrower.throwException);
}
}
@@ -590,4 +590,9 @@ class EmailHiveCacheDataSourceImpl extends EmailDataSource {
Future<void> removeLabelFromEmail(Session session, AccountId accountId, EmailId emailId, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
@override
Future<void> removeLabelFromThread(Session session, AccountId accountId, List<EmailId> emailIds, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
}
@@ -366,4 +366,9 @@ class EmailLocalStorageDataSourceImpl extends EmailDataSource {
Future<void> removeLabelFromEmail(Session session, AccountId accountId, EmailId emailId, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
@override
Future<void> removeLabelFromThread(Session session, AccountId accountId, List<EmailId> emailIds, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
}
@@ -280,4 +280,9 @@ class EmailSessionStorageDatasourceImpl extends EmailDataSource {
Future<void> removeLabelFromEmail(Session session, AccountId accountId, EmailId emailId, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
@override
Future<void> removeLabelFromThread(Session session, AccountId accountId, List<EmailId> emailIds, KeyWordIdentifier labelKeyword) {
throw UnimplementedError();
}
}
@@ -1026,4 +1026,39 @@ class EmailAPI with HandleSetErrorMixin, MailAPIMixin {
throw parseErrorForSetResponse(response, emailId.id);
}
}
Future<void> removeLabelFromThread(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
) async {
final method = SetEmailMethod(accountId)
..addUpdates(emailIds.generateMapUpdateObjectLabel(
labelKeyword,
remove: true,
));
final builder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
final invocation = builder.invocation(method);
final capabilities = method.requiredCapabilities
.toCapabilitiesSupportTeamMailboxes(session, accountId);
final result = await (builder..usings(capabilities)).build().execute();
final response = result.parse<SetEmailResponse>(
invocation.methodCallId,
SetEmailResponse.deserialize,
);
final emailIdsUpdated = response?.updated?.keys ?? <Id>[];
final ids = emailIds.map((emailId) => emailId.id);
final isUpdated = emailIdsUpdated.every(ids.contains);
if (emailIdsUpdated.isEmpty || !isUpdated) {
for (var id in emailIds) {
throw parseErrorForSetResponse(response, id.id);
}
}
}
}
@@ -525,4 +525,19 @@ class EmailRepositoryImpl extends EmailRepository {
labelKeyword,
);
}
@override
Future<void> removeLabelFromThread(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
) {
return emailDataSource[DataSourceType.network]!.removeLabelFromThread(
session,
accountId,
emailIds,
labelKeyword,
);
}
}
@@ -182,4 +182,11 @@ abstract class EmailRepository {
EmailId emailId,
KeyWordIdentifier labelKeyword,
);
Future<void> removeLabelFromThread(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
);
}
@@ -0,0 +1,33 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
class RemovingALabelFromAThread extends LoadingState {}
class RemoveALabelFromAThreadSuccess extends UIState {
final List<EmailId> emailIds;
final KeyWordIdentifier labelKeyword;
final String labelDisplay;
RemoveALabelFromAThreadSuccess(
this.emailIds,
this.labelKeyword,
this.labelDisplay,
);
@override
List<Object> get props => [emailIds, labelKeyword, labelDisplay];
}
class RemoveALabelFromAThreadFailure extends FeatureFailure {
final String labelDisplay;
RemoveALabelFromAThreadFailure({
dynamic exception,
required this.labelDisplay,
}) : super(exception: exception);
@override
List<Object?> get props => [...super.props, labelDisplay];
}
@@ -0,0 +1,43 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/mail/email/email.dart';
import 'package:jmap_dart_client/jmap/mail/email/keyword_identifier.dart';
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
import 'package:tmail_ui_user/features/email/domain/state/labels/remove_a_label_from_an_thread_state.dart';
class RemoveALabelFromAThreadInteractor {
final EmailRepository _emailRepository;
RemoveALabelFromAThreadInteractor(this._emailRepository);
Stream<Either<Failure, Success>> execute(
Session session,
AccountId accountId,
List<EmailId> emailIds,
KeyWordIdentifier labelKeyword,
String labelDisplay,
) async* {
try {
yield Right(RemovingALabelFromAThread());
await _emailRepository.removeLabelFromThread(
session,
accountId,
emailIds,
labelKeyword,
);
yield Right(RemoveALabelFromAThreadSuccess(
emailIds,
labelKeyword,
labelDisplay,
));
} catch (e) {
yield Left(RemoveALabelFromAThreadFailure(
exception: e,
labelDisplay: labelDisplay,
));
}
}
}
@@ -270,11 +270,8 @@ class EmailView extends GetWidget<SingleEmailController> {
if (isLabelAvailable) {
emailLabels = presentationEmail.getLabelList(listLabels);
}
return EmailSubjectWidget(
presentationEmail: presentationEmail.copyWith(
subject: threadSubject,
),
emailSubject: threadSubject ?? presentationEmail.getEmailTitle(),
imagePaths: controller.imagePaths,
isMobileResponsive: isMobileResponsive,
labels: emailLabels,
@@ -2,14 +2,13 @@ import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/views/button/tmail_button_widget.dart';
import 'package:flutter/material.dart';
import 'package:labels/model/label.dart';
import 'package:model/email/presentation_email.dart';
import 'package:tmail_ui_user/features/email/presentation/styles/email_subject_styles.dart';
import 'package:tmail_ui_user/features/labels/presentation/widgets/label_widget.dart';
typedef OnDeleteLabelAction = void Function(Label label);
class EmailSubjectWidget extends StatefulWidget {
final PresentationEmail presentationEmail;
final String emailSubject;
final ImagePaths imagePaths;
final bool isMobileResponsive;
final List<Label>? labels;
@@ -17,7 +16,7 @@ class EmailSubjectWidget extends StatefulWidget {
const EmailSubjectWidget({
super.key,
required this.presentationEmail,
required this.emailSubject,
required this.imagePaths,
this.isMobileResponsive = false,
this.labels,
@@ -29,7 +28,7 @@ class EmailSubjectWidget extends StatefulWidget {
}
class _EmailSubjectWidgetState extends State<EmailSubjectWidget> {
String get _title => widget.presentationEmail.getEmailTitle();
String get _title => widget.emailSubject;
bool get _hasLabels => _currentLabels?.isNotEmpty == true;
@@ -41,6 +40,12 @@ class _EmailSubjectWidgetState extends State<EmailSubjectWidget> {
_currentLabels = widget.labels;
}
@override
void didUpdateWidget(covariant EmailSubjectWidget oldWidget) {
super.didUpdateWidget(oldWidget);
_currentLabels = widget.labels;
}
@override
Widget build(BuildContext context) {
final padding = widget.isMobileResponsive