fix(cnb): use Email/query + Email/get when switch mailbox and reload the email list
(cherry picked from commit 9444d8d28acb3ad4997091162f7addb763d83234)
This commit is contained in:
@@ -138,6 +138,83 @@ class ThreadRepositoryImpl extends ThreadRepository {
|
|||||||
yield newEmailResponse;
|
yield newEmailResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<EmailsResponse> forceQueryAllEmailsForWeb({
|
||||||
|
required Session session,
|
||||||
|
required AccountId accountId,
|
||||||
|
UnsignedInt? limit,
|
||||||
|
int? position,
|
||||||
|
Set<Comparator>? sort,
|
||||||
|
EmailFilter? emailFilter,
|
||||||
|
Properties? propertiesCreated,
|
||||||
|
}) async* {
|
||||||
|
final localDataSource = mapDataSource[DataSourceType.local];
|
||||||
|
final networkDataSource = mapDataSource[DataSourceType.network];
|
||||||
|
|
||||||
|
if (localDataSource == null || networkDataSource == null) {
|
||||||
|
logError(
|
||||||
|
'ThreadRepositoryImpl::forceQueryAllEmailsForWeb(): '
|
||||||
|
'Missing required data sources (local or network).',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load cached emails + cached state
|
||||||
|
final cachedList = await localDataSource.getAllEmailCache(
|
||||||
|
accountId,
|
||||||
|
session.username,
|
||||||
|
inMailboxId: emailFilter?.mailboxId,
|
||||||
|
sort: sort,
|
||||||
|
limit: limit,
|
||||||
|
filterOption: emailFilter?.filterOption,
|
||||||
|
);
|
||||||
|
|
||||||
|
final cachedState = await stateDataSource.getState(
|
||||||
|
accountId,
|
||||||
|
session.username,
|
||||||
|
StateType.email,
|
||||||
|
);
|
||||||
|
|
||||||
|
final localResponse = EmailsResponse(
|
||||||
|
emailList: cachedList,
|
||||||
|
state: cachedState,
|
||||||
|
);
|
||||||
|
|
||||||
|
log(
|
||||||
|
'ThreadRepositoryImpl::forceQueryAllEmailsForWeb(): '
|
||||||
|
'Local cache count = ${cachedList.length}; '
|
||||||
|
'State = ${cachedState?.value}',
|
||||||
|
);
|
||||||
|
|
||||||
|
if (localResponse.hasEmails()) {
|
||||||
|
yield localResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query fresh emails from server
|
||||||
|
final serverResponse = await networkDataSource.getAllEmail(
|
||||||
|
session,
|
||||||
|
accountId,
|
||||||
|
limit: limit,
|
||||||
|
position: position,
|
||||||
|
sort: sort,
|
||||||
|
filter: emailFilter?.filter,
|
||||||
|
properties: propertiesCreated,
|
||||||
|
);
|
||||||
|
|
||||||
|
final serverCount = serverResponse.emailList?.length ?? 0;
|
||||||
|
|
||||||
|
log(
|
||||||
|
'ThreadRepositoryImpl::forceQueryAllEmailsForWeb(): '
|
||||||
|
'Server email count = $serverCount',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Combine server list + keep existing state
|
||||||
|
yield EmailsResponse(
|
||||||
|
emailList: serverResponse.emailList,
|
||||||
|
state: cachedState,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
bool _isApproveFilterOption(FilterMessageOption? filterOption, List<Email>? listEmailResponse) {
|
bool _isApproveFilterOption(FilterMessageOption? filterOption, List<Email>? listEmailResponse) {
|
||||||
return filterOption != FilterMessageOption.all && listEmailResponse!.isNotEmpty;
|
return filterOption != FilterMessageOption.all && listEmailResponse!.isNotEmpty;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,16 @@ abstract class ThreadRepository {
|
|||||||
Properties? propertiesCreated,
|
Properties? propertiesCreated,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Stream<EmailsResponse> forceQueryAllEmailsForWeb({
|
||||||
|
required Session session,
|
||||||
|
required AccountId accountId,
|
||||||
|
UnsignedInt? limit,
|
||||||
|
int? position,
|
||||||
|
Set<Comparator>? sort,
|
||||||
|
EmailFilter? emailFilter,
|
||||||
|
Properties? propertiesCreated,
|
||||||
|
});
|
||||||
|
|
||||||
Stream<EmailsResponse> refreshChanges(
|
Stream<EmailsResponse> refreshChanges(
|
||||||
Session session,
|
Session session,
|
||||||
AccountId accountId,
|
AccountId accountId,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:core/core.dart';
|
import 'package:core/presentation/state/failure.dart';
|
||||||
|
import 'package:core/presentation/state/success.dart';
|
||||||
import 'package:dartz/dartz.dart';
|
import 'package:dartz/dartz.dart';
|
||||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
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/properties/properties.dart';
|
||||||
@@ -6,11 +7,11 @@ import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
|||||||
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
import 'package:jmap_dart_client/jmap/core/sort/comparator.dart';
|
||||||
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
import 'package:jmap_dart_client/jmap/core/unsigned_int.dart';
|
||||||
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
|
||||||
|
import 'package:model/extensions/email_extension.dart';
|
||||||
import 'package:tmail_ui_user/features/thread/domain/model/email_filter.dart';
|
import 'package:tmail_ui_user/features/thread/domain/model/email_filter.dart';
|
||||||
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
import 'package:tmail_ui_user/features/thread/domain/model/email_response.dart';
|
||||||
import 'package:tmail_ui_user/features/thread/domain/repository/thread_repository.dart';
|
import 'package:tmail_ui_user/features/thread/domain/repository/thread_repository.dart';
|
||||||
import 'package:tmail_ui_user/features/thread/domain/state/get_all_email_state.dart';
|
import 'package:tmail_ui_user/features/thread/domain/state/get_all_email_state.dart';
|
||||||
import 'package:model/model.dart';
|
|
||||||
|
|
||||||
class GetEmailsInMailboxInteractor {
|
class GetEmailsInMailboxInteractor {
|
||||||
final ThreadRepository threadRepository;
|
final ThreadRepository threadRepository;
|
||||||
@@ -28,13 +29,25 @@ class GetEmailsInMailboxInteractor {
|
|||||||
Properties? propertiesUpdated,
|
Properties? propertiesUpdated,
|
||||||
bool getLatestChanges = true,
|
bool getLatestChanges = true,
|
||||||
bool useCache = true,
|
bool useCache = true,
|
||||||
|
bool forceEmailQuery = false,
|
||||||
}
|
}
|
||||||
) async* {
|
) async* {
|
||||||
try {
|
try {
|
||||||
yield Right<Failure, Success>(GetAllEmailLoading());
|
yield Right<Failure, Success>(GetAllEmailLoading());
|
||||||
|
|
||||||
if (useCache) {
|
late Stream<EmailsResponse> sourceStream;
|
||||||
yield* threadRepository.getAllEmail(
|
|
||||||
|
if (forceEmailQuery) {
|
||||||
|
sourceStream = threadRepository.forceQueryAllEmailsForWeb(
|
||||||
|
session: session,
|
||||||
|
accountId: accountId,
|
||||||
|
limit: limit,
|
||||||
|
sort: sort,
|
||||||
|
emailFilter: emailFilter,
|
||||||
|
propertiesCreated: propertiesCreated,
|
||||||
|
);
|
||||||
|
} else if (useCache) {
|
||||||
|
sourceStream = threadRepository.getAllEmail(
|
||||||
session,
|
session,
|
||||||
accountId,
|
accountId,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
@@ -43,23 +56,24 @@ class GetEmailsInMailboxInteractor {
|
|||||||
propertiesCreated: propertiesCreated,
|
propertiesCreated: propertiesCreated,
|
||||||
propertiesUpdated: propertiesUpdated,
|
propertiesUpdated: propertiesUpdated,
|
||||||
getLatestChanges: getLatestChanges,
|
getLatestChanges: getLatestChanges,
|
||||||
).map((emailResponse) => _toGetEmailState(
|
);
|
||||||
emailResponse: emailResponse,
|
|
||||||
currentMailboxId: emailFilter?.mailboxId,
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
yield* threadRepository.loadAllEmailInFolderWithoutCache(
|
sourceStream = threadRepository.loadAllEmailInFolderWithoutCache(
|
||||||
session: session,
|
session: session,
|
||||||
accountId: accountId,
|
accountId: accountId,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
sort: sort,
|
sort: sort,
|
||||||
emailFilter: emailFilter,
|
emailFilter: emailFilter,
|
||||||
propertiesCreated: propertiesCreated,
|
propertiesCreated: propertiesCreated,
|
||||||
).map((emailResponse) => _toGetEmailState(
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
yield* sourceStream.map(
|
||||||
|
(emailResponse) => _toGetEmailState(
|
||||||
emailResponse: emailResponse,
|
emailResponse: emailResponse,
|
||||||
currentMailboxId: emailFilter?.mailboxId,
|
currentMailboxId: emailFilter?.mailboxId,
|
||||||
));
|
),
|
||||||
}
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
yield Left(GetAllEmailFailure(e));
|
yield Left(GetAllEmailFailure(e));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ import 'package:tmail_ui_user/main/routes/app_routes.dart';
|
|||||||
import 'package:tmail_ui_user/main/routes/navigation_router.dart';
|
import 'package:tmail_ui_user/main/routes/navigation_router.dart';
|
||||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||||
import 'package:tmail_ui_user/main/routes/route_utils.dart';
|
import 'package:tmail_ui_user/main/routes/route_utils.dart';
|
||||||
|
import 'package:tmail_ui_user/main/utils/app_config.dart';
|
||||||
import 'package:universal_html/html.dart' as html;
|
import 'package:universal_html/html.dart' as html;
|
||||||
|
|
||||||
typedef StartRangeSelection = int;
|
typedef StartRangeSelection = int;
|
||||||
@@ -273,6 +274,7 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
resetToOriginalValue();
|
resetToOriginalValue();
|
||||||
getAllEmailAction(
|
getAllEmailAction(
|
||||||
getLatestChanges: mailboxDashBoardController.isFirstSessionLoad,
|
getLatestChanges: mailboxDashBoardController.isFirstSessionLoad,
|
||||||
|
forceEmailQuery: forceEmailQuery,
|
||||||
);
|
);
|
||||||
mailboxDashBoardController.setIsFirstSessionLoad(false);
|
mailboxDashBoardController.setIsFirstSessionLoad(false);
|
||||||
} else if (mailbox == null) { // disable current mailbox when search active
|
} else if (mailbox == null) { // disable current mailbox when search active
|
||||||
@@ -353,7 +355,7 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
if (action is RefreshChangeEmailAction) {
|
if (action is RefreshChangeEmailAction) {
|
||||||
_refreshEmailChanges(newState: action.newState);
|
_refreshEmailChanges(newState: action.newState);
|
||||||
} else if (action is RefreshAllEmailAction) {
|
} else if (action is RefreshAllEmailAction) {
|
||||||
refreshAllEmail();
|
refreshAllEmail(forceEmailQuery: forceEmailQuery);
|
||||||
mailboxDashBoardController.clearEmailUIAction();
|
mailboxDashBoardController.clearEmailUIAction();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -472,6 +474,9 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
handleLoadMoreEmailsRequest();
|
handleLoadMoreEmailsRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get forceEmailQuery =>
|
||||||
|
PlatformInfo.isWeb && AppConfig.isForceEmailQueryEnabled;
|
||||||
|
|
||||||
void _handleErrorGetAllOrRefreshChangesEmail(Object error, StackTrace stackTrace) async {
|
void _handleErrorGetAllOrRefreshChangesEmail(Object error, StackTrace stackTrace) async {
|
||||||
logWarning('ThreadController::_handleErrorGetAllOrRefreshChangesEmail():Error: $error');
|
logWarning('ThreadController::_handleErrorGetAllOrRefreshChangesEmail():Error: $error');
|
||||||
if (error is CannotCalculateChangesMethodResponseException) {
|
if (error is CannotCalculateChangesMethodResponseException) {
|
||||||
@@ -578,6 +583,7 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
|
|
||||||
void getAllEmailAction({
|
void getAllEmailAction({
|
||||||
bool getLatestChanges = true,
|
bool getLatestChanges = true,
|
||||||
|
bool forceEmailQuery = false,
|
||||||
}) {
|
}) {
|
||||||
log('ThreadController::_getAllEmailAction:getLatestChanges = $getLatestChanges');
|
log('ThreadController::_getAllEmailAction:getLatestChanges = $getLatestChanges');
|
||||||
if (_session != null &&_accountId != null) {
|
if (_session != null &&_accountId != null) {
|
||||||
@@ -594,13 +600,14 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
),
|
),
|
||||||
getLatestChanges: getLatestChanges,
|
getLatestChanges: getLatestChanges,
|
||||||
useCache: selectedMailbox?.isCacheable ?? false,
|
useCache: selectedMailbox?.isCacheable ?? false,
|
||||||
|
forceEmailQuery: forceEmailQuery,
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
consumeState(Stream.value(Left(GetAllEmailFailure(NotFoundSessionException()))));
|
consumeState(Stream.value(Left(GetAllEmailFailure(NotFoundSessionException()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void refreshAllEmail() {
|
void refreshAllEmail({bool forceEmailQuery = false}) {
|
||||||
if (searchController.isSearchEmailRunning) {
|
if (searchController.isSearchEmailRunning) {
|
||||||
consumeState(Stream.value(Right(SearchingState())));
|
consumeState(Stream.value(Right(SearchingState())));
|
||||||
} else {
|
} else {
|
||||||
@@ -614,7 +621,7 @@ class ThreadController extends BaseController with EmailActionController {
|
|||||||
if (searchController.isSearchEmailRunning) {
|
if (searchController.isSearchEmailRunning) {
|
||||||
_searchEmail(limit: limitEmailFetched);
|
_searchEmail(limit: limitEmailFetched);
|
||||||
} else {
|
} else {
|
||||||
getAllEmailAction();
|
getAllEmailAction(forceEmailQuery: forceEmailQuery);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user