TF-123 Implement refresh all mailbox

This commit is contained in:
dab246
2021-09-28 15:51:10 +07:00
committed by Dat H. Pham
parent 8f5171d581
commit eb29f514dc
52 changed files with 745 additions and 368 deletions
@@ -1,10 +1,10 @@
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_response.dart';
abstract class MailboxRepository {
Stream<List<Mailbox>> getAllMailbox(AccountId accountId, {Properties? properties});
Stream<MailboxResponse> getAllMailbox(AccountId accountId, {Properties? properties});
Stream<List<Mailbox>> refresh(AccountId accountId, State currentState);
Stream<MailboxResponse> refresh(AccountId accountId, State currentState);
}
@@ -1,14 +1,20 @@
import 'package:core/core.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:model/mailbox/presentation_mailbox.dart';
class GetAllMailboxSuccess extends UIState {
final List<PresentationMailbox> defaultMailboxList;
final List<PresentationMailbox> folderMailboxList;
final State? currentMailboxState;
GetAllMailboxSuccess({required this.defaultMailboxList, required this.folderMailboxList});
GetAllMailboxSuccess({
required this.defaultMailboxList,
required this.folderMailboxList,
required this.currentMailboxState
});
@override
List<Object> get props => [folderMailboxList];
List<Object?> get props => [defaultMailboxList, folderMailboxList, currentMailboxState];
}
class GetAllMailboxFailure extends FeatureFailure {
@@ -2,6 +2,7 @@ import 'package:core/core.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:jmap_dart_client/jmap/core/properties/properties.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_response.dart';
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
import 'package:tmail_ui_user/features/mailbox/domain/state/get_all_mailboxes_state.dart';
import 'package:tmail_ui_user/features/mailbox/domain/extensions/list_mailbox_extension.dart';
@@ -16,18 +17,21 @@ class GetAllMailboxInteractor {
try {
yield Right<Failure, Success>(LoadingState());
final streamListMailbox = _mailboxRepository.getAllMailbox(accountId, properties: properties);
final streamResult = streamListMailbox.asyncExpand((mailboxList) async* {
final tupleList = mailboxList.splitMailboxList((mailbox) => mailbox.hasRole());
yield Right<Failure, Success>(GetAllMailboxSuccess(
defaultMailboxList: tupleList.value1,
folderMailboxList: tupleList.value2));
});
yield* streamResult;
yield* _mailboxRepository
.getAllMailbox(accountId, properties: properties)
.map(_toGetMailboxState);
} catch (e) {
yield Left<Failure, Success>(GetAllMailboxFailure(e));
}
}
Either<Failure, Success> _toGetMailboxState(MailboxResponse mailboxResponse) {
final tupleList = mailboxResponse.mailboxes
?.splitMailboxList((mailbox) => mailbox.hasRole()) ?? Tuple2([], []);
return Right<Failure, Success>(GetAllMailboxSuccess(
defaultMailboxList: tupleList.value1,
folderMailboxList: tupleList.value2,
currentMailboxState: mailboxResponse.state));
}
}
@@ -0,0 +1,37 @@
import 'package:core/core.dart';
import 'package:dartz/dartz.dart';
import 'package:jmap_dart_client/jmap/account_id.dart';
import 'package:tmail_ui_user/features/mailbox/data/model/mailbox_response.dart';
import 'package:tmail_ui_user/features/mailbox/domain/repository/mailbox_repository.dart';
import 'package:tmail_ui_user/features/mailbox/domain/state/get_all_mailboxes_state.dart';
import 'package:tmail_ui_user/features/mailbox/domain/extensions/list_mailbox_extension.dart';
import 'package:model/model.dart';
import 'package:jmap_dart_client/jmap/core/state.dart' as jmapState;
class RefreshAllMailboxInteractor {
final MailboxRepository _mailboxRepository;
RefreshAllMailboxInteractor(this._mailboxRepository);
Stream<Either<Failure, Success>> execute(AccountId accountId, jmapState.State currentState) async* {
try {
yield Right<Failure, Success>(LoadingState());
yield* _mailboxRepository
.refresh(accountId, currentState)
.map(_toGetMailboxState);
} catch (e) {
yield Left<Failure, Success>(GetAllMailboxFailure(e));
}
}
Either<Failure, Success> _toGetMailboxState(MailboxResponse mailboxResponse) {
final tupleList = mailboxResponse.mailboxes
?.splitMailboxList((mailbox) => mailbox.hasRole()) ?? Tuple2([], []);
return Right<Failure, Success>(GetAllMailboxSuccess(
defaultMailboxList: tupleList.value1,
folderMailboxList: tupleList.value2,
currentMailboxState: mailboxResponse.state));
}
}