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:
dab246
2026-01-28 11:35:22 +07:00
committed by Dat H. Pham
parent 616a9247e8
commit 5cbf189ffe
4 changed files with 123 additions and 15 deletions
@@ -138,6 +138,83 @@ class ThreadRepositoryImpl extends ThreadRepository {
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) {
return filterOption != FilterMessageOption.all && listEmailResponse!.isNotEmpty;
}