refactor: modify methods on usage

This commit is contained in:
Malo Léon 2023-08-17 20:59:34 +02:00
parent bcc5c0f268
commit 18e8d1c754
14 changed files with 92 additions and 75 deletions

View File

@ -14,4 +14,4 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'repositories/notification_repository_impl.dart'; export 'repositories/cloud_messaging_repository_impl.dart';

View File

@ -16,27 +16,20 @@
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/core/exceptions/notification_exeption.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/core/exceptions/notification_exeption.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/data_sources/remote/notification_remote_data_source.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/data_sources/remote/cloud_messaging_remote_data_source.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart'; import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class NotificationRepositoryImpl extends NotificationRepository { class CloudMessagingRepositoryImpl extends CloudMessagingRepository {
final NotificationRemoteDataSource _notificationRemoteDataSource; final CloudMessagingRemoteDataSource _notificationRemoteDataSource;
NotificationRepositoryImpl({ CloudMessagingRepositoryImpl({
required NotificationRemoteDataSource notificationRemoteDataSource, required CloudMessagingRemoteDataSource notificationRemoteDataSource,
}) : _notificationRemoteDataSource = notificationRemoteDataSource; }) : _notificationRemoteDataSource = notificationRemoteDataSource;
@override
FutureOrResult<void> register() => Result.tryCatchAsync(
_notificationRemoteDataSource.register,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
@override @override
FutureOrResult<String> getToken() => Result.tryCatchAsync( FutureOrResult<String?> getToken() => Result.tryCatchAsync(
_notificationRemoteDataSource.getToken, _notificationRemoteDataSource.getToken,
(error) => error is AppException (error) => error is AppException
? error ? error
@ -69,29 +62,37 @@ class NotificationRepositoryImpl extends NotificationRepository {
); );
@override @override
FutureOrResult<Stream<RemoteNotification>> onNotificationAccepted() => FutureOrResult<Stream<RemoteNotification>> onNotification() =>
Result.tryCatchAsync( Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationBackgroundAccepted, _notificationRemoteDataSource.onNotificationBackground,
(error) => error is AppException (error) => error is AppException
? error ? error
: NotificationException(error.toString()), : NotificationException(error.toString()),
); );
@override @override
FutureOrResult<Stream<RemoteNotification>> FutureOrResult<Stream<RemoteNotification>> onNotificationBackground() =>
onNotificationBackgroundAccepted() => Result.tryCatchAsync( Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationBackgroundAccepted, _notificationRemoteDataSource.onNotificationBackground,
(error) => error is AppException (error) => error is AppException
? error ? error
: NotificationException(error.toString()), : NotificationException(error.toString()),
); );
@override @override
FutureOrResult<Stream<RemoteNotification>> FutureOrResult<Stream<RemoteNotification>> onNotificationForeground() =>
onNotificationForegroundAccepted() => Result.tryCatchAsync( Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationForegroundAccepted, _notificationRemoteDataSource.onNotificationForeground,
(error) => error is AppException (error) => error is AppException
? error ? error
: NotificationException(error.toString()), : NotificationException(error.toString()),
); );
@override
FutureOrResult<void> init() => Result.tryCatchAsync(
_notificationRemoteDataSource.init,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
} }

View File

@ -0,0 +1,17 @@
// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export './remote/cloud_messaging_remote_data_source.dart';

View File

@ -17,18 +17,18 @@
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
abstract class NotificationRemoteDataSource extends BaseRepository { abstract class CloudMessagingRemoteDataSource extends BaseRepository {
Future<void> register(); Future<void> init() async {}
Future<Stream<RemoteNotification>> onNotificationAccepted(); Future<Stream<RemoteNotification>> onNotification();
Future<Stream<RemoteNotification>> onNotificationBackgroundAccepted(); Future<Stream<RemoteNotification>> onNotificationBackground();
Future<Stream<RemoteNotification>> onNotificationForegroundAccepted(); Future<Stream<RemoteNotification>> onNotificationForeground();
Future<void> requestPermissions(); Future<void> requestPermissions();
Future<String> getToken(); Future<String?> getToken();
Future<void> subscribeToTopic(String topic); Future<void> subscribeToTopic(String topic);

View File

@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'data_sources/data_sources.dart';
export 'entities/entities.dart'; export 'entities/entities.dart';
export 'repositories/repositories.dart'; export 'repositories/repositories.dart';
export 'usecases/usecases.dart'; export 'usecases/usecases.dart';

View File

@ -18,14 +18,12 @@ import 'package:wyatt_architecture/wyatt_architecture.dart';
class RemoteNotification extends Entity { class RemoteNotification extends Entity {
const RemoteNotification({ const RemoteNotification({
this.senderId, this.title,
this.messageId, this.body,
this.data, this.data,
this.sentTime,
}); });
final String? senderId; final String? title;
final String? messageId; final String? body;
final Map<String, dynamic>? data; final Map<String, dynamic>? data;
final DateTime? sentTime;
} }

View File

@ -19,18 +19,18 @@ import 'dart:async';
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
abstract class NotificationRepository extends BaseRepository { abstract class CloudMessagingRepository extends BaseRepository {
FutureOrResult<void> register(); FutureOrResult<void> init();
FutureOrResult<Stream<RemoteNotification>> onNotificationAccepted(); FutureOrResult<Stream<RemoteNotification>> onNotification();
FutureOrResult<Stream<RemoteNotification>> onNotificationBackgroundAccepted(); FutureOrResult<Stream<RemoteNotification>> onNotificationBackground();
FutureOrResult<Stream<RemoteNotification>> onNotificationForegroundAccepted(); FutureOrResult<Stream<RemoteNotification>> onNotificationForeground();
FutureOrResult<void> requestPermissions(); FutureOrResult<void> requestPermissions();
FutureOrResult<String> getToken(); FutureOrResult<String?> getToken();
FutureOrResult<void> subscribeToTopic(String topic); FutureOrResult<void> subscribeToTopic(String topic);

View File

@ -14,4 +14,4 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'notification_repository.dart'; export 'cloud_messaging_repository.dart';

View File

@ -15,16 +15,16 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
class GetCloudMessagingTokenUseCase extends AsyncUseCase<void, String> { class GetCloudMessagingTokenUseCase extends AsyncUseCase<void, String?> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
GetCloudMessagingTokenUseCase({ GetCloudMessagingTokenUseCase({
required NotificationRepository notificationRepository, required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository; }) : _notificationRepository = notificationRepository;
@override @override
FutureOrResult<String> execute(void params) => FutureOrResult<String?> execute(void params) =>
_notificationRepository.getToken(); _notificationRepository.getToken();
} }

View File

@ -15,15 +15,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
class InitCloudmessagingUseCase extends AsyncUseCase<NoParam, void> { class InitCloudmessagingUseCase extends AsyncUseCase<NoParam, void> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
InitCloudmessagingUseCase({ InitCloudmessagingUseCase({
required NotificationRepository notificationRepository, required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository; }) : _notificationRepository = notificationRepository;
@override @override
FutureOrResult<void> execute(NoParam? params) => FutureOrResult<void> execute(NoParam? params) =>
_notificationRepository.register(); _notificationRepository.init();
} }

View File

@ -20,33 +20,33 @@ import 'package:rxdart/rxdart.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/core/exceptions/notification_exeption.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/core/exceptions/notification_exeption.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
import 'package:wyatt_type_utils/wyatt_type_utils.dart'; import 'package:wyatt_type_utils/wyatt_type_utils.dart';
class ListenNotification extends StreamUseCase<NoParam, RemoteNotification> { class ListenNotification extends StreamUseCase<NoParam, RemoteNotification> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
ListenNotification({required NotificationRepository notificationRepository}) ListenNotification({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository; : _notificationRepository = notificationRepository;
@override @override
FutureOrResult<Stream<RemoteNotification>> execute(NoParam? params) async { FutureOrResult<Stream<RemoteNotification>> execute(NoParam? params) async {
Stream<RemoteNotification>? notificationStream; Stream<RemoteNotification>? notificationStream;
final notificationStreamResponse = final notificationStreamResponse =
await _notificationRepository.onNotificationAccepted(); await _notificationRepository.onNotification();
if (notificationStreamResponse.isOk) { if (notificationStreamResponse.isOk) {
notificationStream = notificationStreamResponse.ok; notificationStream = notificationStreamResponse.ok;
} else if (notificationStreamResponse.isErr) { } else if (notificationStreamResponse.isErr) {
final notificationBackgroundStreamResponse = final notificationBackgroundStreamResponse =
await _notificationRepository.onNotificationBackgroundAccepted(); await _notificationRepository.onNotificationBackground();
if (notificationBackgroundStreamResponse.isErr) { if (notificationBackgroundStreamResponse.isErr) {
return Err(notificationBackgroundStreamResponse.err!); return Err(notificationBackgroundStreamResponse.err!);
} }
final notificationForegroundStreamResponse = final notificationForegroundStreamResponse =
await _notificationRepository.onNotificationForegroundAccepted(); await _notificationRepository.onNotificationForeground();
if (notificationForegroundStreamResponse.isErr) { if (notificationForegroundStreamResponse.isErr) {
return Err(notificationBackgroundStreamResponse.err!); return Err(notificationBackgroundStreamResponse.err!);

View File

@ -15,16 +15,16 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
class RequestCloudMessagingPermissionUseCase extends AsyncUseCase<void, void> { class RequestCloudMessagingPermissionUseCase extends AsyncUseCase<void, void> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
RequestCloudMessagingPermissionUseCase({ RequestCloudMessagingPermissionUseCase({
required NotificationRepository notificationRepository, required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository; }) : _notificationRepository = notificationRepository;
@override @override
FutureOrResult<void> execute(void params) => FutureOrResult<void> execute(void params) =>
_notificationRepository.register(); _notificationRepository.requestPermissions();
} }

View File

@ -17,12 +17,12 @@
import 'dart:async'; import 'dart:async';
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
class Subscribe extends AsyncUseCase<String, void> { class Subscribe extends AsyncUseCase<String, void> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
Subscribe({required NotificationRepository notificationRepository}) Subscribe({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository; : _notificationRepository = notificationRepository;
@override @override

View File

@ -17,12 +17,12 @@
import 'dart:async'; import 'dart:async';
import 'package:wyatt_architecture/wyatt_architecture.dart'; import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/notification_repository.dart'; import 'package:wyatt_cloud_messaging_bloc_base/src/domain/repositories/cloud_messaging_repository.dart';
class Unsubscribe extends AsyncUseCase<String, void> { class Unsubscribe extends AsyncUseCase<String, void> {
final NotificationRepository _notificationRepository; final CloudMessagingRepository _notificationRepository;
Unsubscribe({required NotificationRepository notificationRepository}) Unsubscribe({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository; : _notificationRepository = notificationRepository;
@override @override