feat: Persist label state after get all labels

This commit is contained in:
dab246
2026-02-03 19:46:24 +07:00
committed by Dat H. Pham
parent 9c211c05ef
commit 4fea45d062
11 changed files with 187 additions and 152 deletions
@@ -6,7 +6,7 @@ import 'package:tmail_ui_user/features/labels/domain/model/edit_label_request.da
import 'package:tmail_ui_user/features/labels/data/model/label_change_response.dart';
abstract class LabelDatasource {
Future<List<Label>> getAllLabels(AccountId accountId);
Future<({List<Label> labels, State? newState})> getAllLabels(AccountId accountId);
Future<Label> createNewLabel(AccountId accountId, Label labelData);
@@ -15,7 +15,7 @@ class LabelDatasourceImpl extends LabelDatasource {
LabelDatasourceImpl(this._labelApi, this._exceptionThrower);
@override
Future<List<Label>> getAllLabels(AccountId accountId) {
Future<({List<Label> labels, State? newState})> getAllLabels(AccountId accountId) {
return Future.sync(() async {
return await _labelApi.getAllLabels(accountId);
}).catchError(_exceptionThrower.throwException);
@@ -24,7 +24,7 @@ class LabelApi
LabelApi(this._httpClient, this._uuid);
Future<List<Label>> getAllLabels(AccountId accountId) async {
Future<({List<Label> labels, State? newState})> getAllLabels(AccountId accountId) async {
final builder = JmapRequestBuilder(_httpClient, ProcessingInvocation());
final method = GetLabelMethod(accountId);
final invocation = builder.invocation(method);
@@ -37,7 +37,7 @@ class LabelApi
GetLabelResponse.deserialize,
);
return response?.list ?? [];
return (labels: response?.list ?? [], newState: response?.state);
}
Future<Label> createNewLabel(AccountId accountId, Label labelData) async {
@@ -14,7 +14,7 @@ class LabelRepositoryImpl extends LabelRepository {
LabelRepositoryImpl(this._labelDatasource);
@override
Future<List<Label>> getAllLabels(AccountId accountId) {
Future<({List<Label> labels, State? newState})> getAllLabels(AccountId accountId) {
return _labelDatasource.getAllLabels(accountId);
}
@@ -6,7 +6,7 @@ import 'package:tmail_ui_user/features/labels/domain/model/edit_label_request.da
import 'package:tmail_ui_user/features/labels/domain/model/label_changes_result.dart';
abstract class LabelRepository {
Future<List<Label>> getAllLabels(AccountId accountId);
Future<({List<Label> labels, State? newState})> getAllLabels(AccountId accountId);
Future<Label> createNewLabel(AccountId accountId, Label labelData);
@@ -1,16 +1,18 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:labels/model/label.dart';
class GettingAllLabel extends LoadingState {}
class GetAllLabelSuccess extends UIState {
final List<Label> labels;
final State? newState;
GetAllLabelSuccess(this.labels);
GetAllLabelSuccess(this.labels, this.newState);
@override
List<Object> get props => [labels];
List<Object?> get props => [labels, newState];
}
class GetAllLabelFailure extends FeatureFailure {
@@ -13,8 +13,8 @@ class GetAllLabelInteractor {
Stream<Either<Failure, Success>> execute(AccountId accountId) async* {
try {
yield Right(GettingAllLabel());
final labels = await _labelRepository.getAllLabels(accountId);
yield Right(GetAllLabelSuccess(labels));
final result = await _labelRepository.getAllLabels(accountId);
yield Right(GetAllLabelSuccess(result.labels, result.newState));
} catch (e) {
yield Left(GetAllLabelFailure(e));
}
@@ -179,6 +179,7 @@ class LabelController extends BaseController with LabelContextMenuMixin {
void handleSuccessViewState(Success success) {
if (success is GetAllLabelSuccess) {
labels.value = success.labels..sortByAlphabetically();
setCurrentLabelState(success.newState);
} else if (success is CreateNewLabelSuccess) {
_handleCreateNewLabelSuccess(success);
} else if (success is GetLabelSettingStateSuccess) {
@@ -10,6 +10,7 @@ import 'package:tmail_ui_user/features/labels/domain/usecases/create_new_label_i
import 'package:tmail_ui_user/features/labels/domain/usecases/edit_label_interactor.dart';
import 'package:tmail_ui_user/features/labels/domain/usecases/delete_a_label_interactor.dart';
import 'package:tmail_ui_user/features/labels/domain/usecases/get_all_label_interactor.dart';
import 'package:tmail_ui_user/features/labels/domain/usecases/get_label_changes_interactor.dart';
import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_name_interactor.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';
import 'package:uuid/uuid.dart';
@@ -37,6 +38,7 @@ class LabelInteractorBindings extends InteractorsBindings {
Get.lazyPut(() => CreateNewLabelInteractor(Get.find<LabelRepository>()));
Get.lazyPut(() => EditLabelInteractor(Get.find<LabelRepository>()));
Get.lazyPut(() => DeleteALabelInteractor(Get.find<LabelRepository>()));
Get.lazyPut(() => GetLabelChangesInteractor(Get.find<LabelRepository>()));
Get.lazyPut(() => VerifyNameInteractor());
}
@@ -252,6 +252,7 @@ class LabelUtils {
}) {
final idsToRemove = {
...destroyedIds,
...created.map((label) => label.id),
...updated.map((label) => label.id),
};