TF-2877 Create notification interactors and controller

This commit is contained in:
DatDang
2024-06-04 10:04:14 +07:00
committed by Dat H. Pham
parent b89820618c
commit ef0e4ad2a8
10 changed files with 454 additions and 0 deletions
@@ -0,0 +1,14 @@
import 'package:tmail_ui_user/features/manage_account/domain/state/notification_setting_state.dart';
class GettingNotificationSetting extends NotificationSettingHandling {}
class GetNotificationSettingSuccess extends NotificationSettingSuccess {
GetNotificationSettingSuccess({required this.isEnabled});
final bool isEnabled;
@override
List<Object?> get props => [isEnabled];
}
class GetNotificationSettingFailure extends NotificationSettingFailure {}
@@ -0,0 +1,8 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
class NotificationSettingHandling extends LoadingState {}
class NotificationSettingSuccess extends UIState {}
class NotificationSettingFailure extends FeatureFailure {}
@@ -0,0 +1,7 @@
import 'package:tmail_ui_user/features/manage_account/domain/state/notification_setting_state.dart';
class TogglingNotificationSetting extends NotificationSettingHandling {}
class ToggleNotificationSettingSuccess extends NotificationSettingSuccess {}
class ToggleNotificationSettingFailure extends NotificationSettingFailure {}
@@ -0,0 +1,22 @@
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/core/user_name.dart';
import 'package:tmail_ui_user/features/manage_account/domain/repository/notification_repository.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/get_notification_setting_state.dart';
class GetNotificationSettingInteractor {
final NotificationRepository _notificationRepository;
GetNotificationSettingInteractor(this._notificationRepository);
Stream<Either<Failure, Success>> execute(UserName userName) async* {
try {
yield Right(GettingNotificationSetting());
final isEnabled = await _notificationRepository.getNotificationSetting(userName);
yield Right(GetNotificationSettingSuccess(isEnabled: isEnabled));
} catch (_) {
yield Left(GetNotificationSettingFailure());
}
}
}
@@ -0,0 +1,22 @@
import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:dartz/dartz.dart';
import 'package:tmail_ui_user/features/manage_account/domain/repository/notification_repository.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/toggle_notification_setting_state.dart';
class ToggleNotificationSettingInteractor {
final NotificationRepository _notificationRepository;
ToggleNotificationSettingInteractor(this._notificationRepository);
Stream<Either<Failure, Success>> execute() async* {
try {
yield Right(TogglingNotificationSetting());
await _notificationRepository.toggleNotificationSetting();
yield Right(ToggleNotificationSettingSuccess());
} catch (_) {
yield Left(ToggleNotificationSettingFailure());
}
}
}
@@ -0,0 +1,71 @@
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/base/base_bindings.dart';
import 'package:tmail_ui_user/features/manage_account/data/datasource/notification_datasource.dart';
import 'package:tmail_ui_user/features/manage_account/data/datasource_impl/notification_datasource_impl.dart';
import 'package:tmail_ui_user/features/manage_account/data/repository/notification_repository_impl.dart';
import 'package:tmail_ui_user/features/manage_account/domain/repository/notification_repository.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/toggle_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/notification/notification_controller.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/permissions/notification_permission_service.dart';
class NotificationBinding extends BaseBindings {
@override
void bindingsController() {
Get.put(NotificationController(
Get.find<GetNotificationSettingInteractor>(),
Get.find<ToggleNotificationSettingInteractor>()));
}
@override
void bindingsDataSource() {
Get.lazyPut<NotificationDataSource>(
() => Get.find<NotificationDataSourceImpl>());
}
@override
void bindingsDataSourceImpl() {
Get.lazyPut(() => NotificationDataSourceImpl(
Get.find<NotificationPermissionService>(),
Get.find<CacheExceptionThrower>()
));
}
@override
void dependencies() {
Get.lazyPut(() => NotificationPermissionService());
super.dependencies();
}
@override
void bindingsInteractor() {
Get.lazyPut(() => GetNotificationSettingInteractor(
Get.find<NotificationRepository>()));
Get.lazyPut(() => ToggleNotificationSettingInteractor(
Get.find<NotificationRepository>()));
}
@override
void bindingsRepository() {
Get.lazyPut<NotificationRepository>(
() => Get.find<NotificationRepositoryImpl>());
}
@override
void bindingsRepositoryImpl() {
Get.lazyPut(() => NotificationRepositoryImpl(
Get.find<NotificationDataSource>()));
}
void close() {
Get.delete<NotificationController>();
Get.delete<GetNotificationSettingInteractor>();
Get.delete<ToggleNotificationSettingInteractor>();
Get.delete<NotificationRepository>();
Get.delete<NotificationRepositoryImpl>();
Get.delete<NotificationDataSource>();
Get.delete<NotificationDataSourceImpl>();
Get.delete<NotificationPermissionService>();
}
}
@@ -0,0 +1,59 @@
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/get_notification_setting_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/toggle_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/menu/settings/settings_controller.dart';
class NotificationController extends FullLifeCycleController with FullLifeCycleMixin {
final GetNotificationSettingInteractor _getNotificationSettingInteractor;
final ToggleNotificationSettingInteractor _toggleNotificationSettingInteractor;
NotificationController(
this._getNotificationSettingInteractor,
this._toggleNotificationSettingInteractor);
final notificationSettingEnabled = Rxn<bool>();
final settingsController = Get.find<SettingsController>();
@override
Future<void> onInit() async {
super.onInit();
await _getNotificationSetting();
}
Future<void> _getNotificationSetting() async {
final userName = settingsController.manageAccountDashboardController.sessionCurrent?.username;
if (userName == null) {
notificationSettingEnabled.value = false;
return;
}
final getNotificationSettingState = await _getNotificationSettingInteractor
.execute(userName).last;
getNotificationSettingState.fold(
(failure) {
notificationSettingEnabled.value = false;
}, (success) {
notificationSettingEnabled.value = success is GetNotificationSettingSuccess
? success.isEnabled
: false;
});
}
Future<void> toggleNotificationSetting() async {
if (notificationSettingEnabled.value == null) return;
await _toggleNotificationSettingInteractor.execute().last;
}
@override
void onDetached() {}
@override
void onInactive() {}
@override
void onPaused() {}
@override
Future<void> onResumed() => _getNotificationSetting();
}
@@ -0,0 +1,57 @@
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:tmail_ui_user/features/manage_account/domain/repository/notification_repository.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/get_notification_setting_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_notification_setting_interactor.dart';
import 'get_notification_setting_interactor_test.mocks.dart';
@GenerateNiceMocks([MockSpec<NotificationRepository>()])
void main() {
final notificationRepository = MockNotificationRepository();
final getNotificationSettingInteractor
= GetNotificationSettingInteractor(notificationRepository);
final userName = UserName('value');
group('GetNotificationSettingInteractor test:', () {
test(
'should emit expected state '
'when repo return value',
() {
// arrange
const notificationSettingEnabled = true;
when(notificationRepository.getNotificationSetting(any))
.thenAnswer((_) async => notificationSettingEnabled);
// assert
expect(
getNotificationSettingInteractor.execute(userName),
emitsInOrder([
Right(GettingNotificationSetting()),
Right(GetNotificationSettingSuccess(isEnabled: notificationSettingEnabled)),
]),
);
});
test(
'should emit expected state '
'when repo throws exception',
() {
// arrange
when(notificationRepository.getNotificationSetting(any)).thenThrow(Exception());
// assert
expect(
getNotificationSettingInteractor.execute(userName),
emitsInOrder([
Right(GettingNotificationSetting()),
Left(GetNotificationSettingFailure()),
]),
);
});
});
}
@@ -0,0 +1,52 @@
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:tmail_ui_user/features/manage_account/domain/repository/notification_repository.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/toggle_notification_setting_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/toggle_notification_setting_interactor.dart';
import 'toggle_notification_setting_interactor_test.mocks.dart';
@GenerateNiceMocks([MockSpec<NotificationRepository>()])
void main() {
final notificationRepository = MockNotificationRepository();
final toggleNotificationSettingInteractor
= ToggleNotificationSettingInteractor(notificationRepository);
group('ToggleNotificationSettingInteractor test:', () {
test(
'should emit expected state '
'when repo not throw exception',
() {
// arrange
when(notificationRepository.toggleNotificationSetting())
.thenAnswer((_) async => {});
// assert
expect(
toggleNotificationSettingInteractor.execute(),
emitsInOrder([
Right(TogglingNotificationSetting()),
Right(ToggleNotificationSettingSuccess()),
]),
);
});
test(
'should emit expected state '
'when repo throws exception',
() {
// arrange
when(notificationRepository.toggleNotificationSetting()).thenThrow(Exception());
// assert
expect(
toggleNotificationSettingInteractor.execute(),
emitsInOrder([
Right(TogglingNotificationSetting()),
Left(ToggleNotificationSettingFailure()),
]),
);
});
});
}
@@ -0,0 +1,142 @@
import 'package:dartz/dartz.dart' hide State;
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'package:jmap_dart_client/jmap/core/session/session.dart';
import 'package:jmap_dart_client/jmap/core/state.dart';
import 'package:jmap_dart_client/jmap/core/user_name.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/get_notification_setting_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/toggle_notification_setting_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/toggle_notification_setting_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/manage_account_dashboard_controller.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/menu/settings/settings_controller.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/notification/notification_controller.dart';
import 'notification_controller_test.mocks.dart';
mockControllerCallback() => InternalFinalCallback<void>(callback: () {});
const fallbackGenerators = {
#onStart: mockControllerCallback,
#onDelete: mockControllerCallback,
};
@GenerateNiceMocks([
MockSpec<GetNotificationSettingInteractor>(),
MockSpec<ToggleNotificationSettingInteractor>(),
MockSpec<SettingsController>(fallbackGenerators: fallbackGenerators),
MockSpec<ManageAccountDashBoardController>(fallbackGenerators: fallbackGenerators),
])
void main() {
final getNotificationSettingInteractor
= MockGetNotificationSettingInteractor();
final toggleNotificationSettingInteractor
= MockToggleNotificationSettingInteractor();
late NotificationController notificationController;
final settingsController = MockSettingsController();
setUpAll(() {
Get.put<SettingsController>(settingsController);
});
setUp(() {
notificationController = NotificationController(
getNotificationSettingInteractor,
toggleNotificationSettingInteractor);
});
group('notification controller test', () {
test(
'should call execute on getNotificationSettingInteractor '
'when init',
() async {
// arrange
final userName = UserName('value');
final manageAccountDashboardController = MockManageAccountDashBoardController();
when(manageAccountDashboardController.sessionCurrent).thenReturn(
Session(
{}, {}, {},
userName,
Uri(), Uri(), Uri(), Uri(), State('value'))
);
when(settingsController.manageAccountDashboardController).thenReturn(
manageAccountDashboardController);
when(getNotificationSettingInteractor.execute(any))
.thenAnswer((_) => Stream.value(Left(GetNotificationSettingFailure())));
// act
notificationController.onInit();
// assert
verify(getNotificationSettingInteractor.execute(userName)).called(1);
});
test(
'should call execute on toggleNotificationSettingInteractor '
'when toggleNotificationSetting is triggered',
() {
// arrange
notificationController.notificationSettingEnabled.value = true;
when(toggleNotificationSettingInteractor.execute())
.thenAnswer((_) => Stream.value(Left(ToggleNotificationSettingFailure())));
// act
notificationController.toggleNotificationSetting();
// assert
verify(toggleNotificationSettingInteractor.execute()).called(1);
});
test(
'should change the value of notificationSettingEnabled '
'when GetNotificationSettingSuccess is emitted',
() async {
// arrange
const isEnabled = true;
final userName = UserName('value');
final manageAccountDashboardController = MockManageAccountDashBoardController();
when(manageAccountDashboardController.sessionCurrent).thenReturn(
Session(
{}, {}, {},
userName,
Uri(), Uri(), Uri(), Uri(), State('value'))
);
when(settingsController.manageAccountDashboardController).thenReturn(
manageAccountDashboardController);
when(getNotificationSettingInteractor.execute(any))
.thenAnswer((_) => Stream.value(Right(GetNotificationSettingSuccess(isEnabled: isEnabled))));
// act
await notificationController.onInit();
// assert
expect(notificationController.notificationSettingEnabled.value, isEnabled);
});
test(
'should change the value of notificationSettingEnabled to false '
'when GetNotificationSettingFailure is emitted',
() async {
// arrange
final userName = UserName('value');
final manageAccountDashboardController = MockManageAccountDashBoardController();
when(manageAccountDashboardController.sessionCurrent).thenReturn(
Session(
{}, {}, {},
userName,
Uri(), Uri(), Uri(), Uri(), State('value'))
);
when(settingsController.manageAccountDashboardController).thenReturn(
manageAccountDashboardController);
when(getNotificationSettingInteractor.execute(any)).thenAnswer((_) => Stream.value(Left(GetNotificationSettingFailure())));
// act
await notificationController.onInit();
// assert
expect(notificationController.notificationSettingEnabled.value, false);
});
});
}