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
// 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_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/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';
class NotificationRepositoryImpl extends NotificationRepository {
final NotificationRemoteDataSource _notificationRemoteDataSource;
class CloudMessagingRepositoryImpl extends CloudMessagingRepository {
final CloudMessagingRemoteDataSource _notificationRemoteDataSource;
NotificationRepositoryImpl({
required NotificationRemoteDataSource notificationRemoteDataSource,
CloudMessagingRepositoryImpl({
required CloudMessagingRemoteDataSource notificationRemoteDataSource,
}) : _notificationRemoteDataSource = notificationRemoteDataSource;
@override
FutureOrResult<void> register() => Result.tryCatchAsync(
_notificationRemoteDataSource.register,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
@override
FutureOrResult<String> getToken() => Result.tryCatchAsync(
FutureOrResult<String?> getToken() => Result.tryCatchAsync(
_notificationRemoteDataSource.getToken,
(error) => error is AppException
? error
@ -69,29 +62,37 @@ class NotificationRepositoryImpl extends NotificationRepository {
);
@override
FutureOrResult<Stream<RemoteNotification>> onNotificationAccepted() =>
FutureOrResult<Stream<RemoteNotification>> onNotification() =>
Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationBackgroundAccepted,
_notificationRemoteDataSource.onNotificationBackground,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
@override
FutureOrResult<Stream<RemoteNotification>>
onNotificationBackgroundAccepted() => Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationBackgroundAccepted,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
FutureOrResult<Stream<RemoteNotification>> onNotificationBackground() =>
Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationBackground,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
@override
FutureOrResult<Stream<RemoteNotification>>
onNotificationForegroundAccepted() => Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationForegroundAccepted,
(error) => error is AppException
? error
: NotificationException(error.toString()),
);
FutureOrResult<Stream<RemoteNotification>> onNotificationForeground() =>
Result.tryCatchAsync(
_notificationRemoteDataSource.onNotificationForeground,
(error) => error is AppException
? error
: 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_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
abstract class NotificationRemoteDataSource extends BaseRepository {
Future<void> register();
abstract class CloudMessagingRemoteDataSource extends BaseRepository {
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<String> getToken();
Future<String?> getToken();
Future<void> subscribeToTopic(String topic);

View File

@ -14,6 +14,7 @@
// 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 'data_sources/data_sources.dart';
export 'entities/entities.dart';
export 'repositories/repositories.dart';
export 'usecases/usecases.dart';

View File

@ -18,14 +18,12 @@ import 'package:wyatt_architecture/wyatt_architecture.dart';
class RemoteNotification extends Entity {
const RemoteNotification({
this.senderId,
this.messageId,
this.title,
this.body,
this.data,
this.sentTime,
});
final String? senderId;
final String? messageId;
final String? title;
final String? body;
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_cloud_messaging_bloc_base/src/domain/entities/remote_notifications.dart';
abstract class NotificationRepository extends BaseRepository {
FutureOrResult<void> register();
abstract class CloudMessagingRepository extends BaseRepository {
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<String> getToken();
FutureOrResult<String?> getToken();
FutureOrResult<void> subscribeToTopic(String topic);

View File

@ -14,4 +14,4 @@
// 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 '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/>.
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> {
final NotificationRepository _notificationRepository;
class GetCloudMessagingTokenUseCase extends AsyncUseCase<void, String?> {
final CloudMessagingRepository _notificationRepository;
GetCloudMessagingTokenUseCase({
required NotificationRepository notificationRepository,
required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository;
@override
FutureOrResult<String> execute(void params) =>
FutureOrResult<String?> execute(void params) =>
_notificationRepository.getToken();
}

View File

@ -15,15 +15,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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> {
final NotificationRepository _notificationRepository;
final CloudMessagingRepository _notificationRepository;
InitCloudmessagingUseCase({
required NotificationRepository notificationRepository,
required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository;
@override
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_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/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';
class ListenNotification extends StreamUseCase<NoParam, RemoteNotification> {
final NotificationRepository _notificationRepository;
final CloudMessagingRepository _notificationRepository;
ListenNotification({required NotificationRepository notificationRepository})
ListenNotification({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository;
@override
FutureOrResult<Stream<RemoteNotification>> execute(NoParam? params) async {
Stream<RemoteNotification>? notificationStream;
final notificationStreamResponse =
await _notificationRepository.onNotificationAccepted();
await _notificationRepository.onNotification();
if (notificationStreamResponse.isOk) {
notificationStream = notificationStreamResponse.ok;
} else if (notificationStreamResponse.isErr) {
final notificationBackgroundStreamResponse =
await _notificationRepository.onNotificationBackgroundAccepted();
await _notificationRepository.onNotificationBackground();
if (notificationBackgroundStreamResponse.isErr) {
return Err(notificationBackgroundStreamResponse.err!);
}
final notificationForegroundStreamResponse =
await _notificationRepository.onNotificationForegroundAccepted();
await _notificationRepository.onNotificationForeground();
if (notificationForegroundStreamResponse.isErr) {
return Err(notificationBackgroundStreamResponse.err!);

View File

@ -15,16 +15,16 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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> {
final NotificationRepository _notificationRepository;
final CloudMessagingRepository _notificationRepository;
RequestCloudMessagingPermissionUseCase({
required NotificationRepository notificationRepository,
required CloudMessagingRepository notificationRepository,
}) : _notificationRepository = notificationRepository;
@override
FutureOrResult<void> execute(void params) =>
_notificationRepository.register();
_notificationRepository.requestPermissions();
}

View File

@ -17,12 +17,12 @@
import 'dart:async';
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> {
final NotificationRepository _notificationRepository;
final CloudMessagingRepository _notificationRepository;
Subscribe({required NotificationRepository notificationRepository})
Subscribe({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository;
@override

View File

@ -17,12 +17,12 @@
import 'dart:async';
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> {
final NotificationRepository _notificationRepository;
final CloudMessagingRepository _notificationRepository;
Unsubscribe({required NotificationRepository notificationRepository})
Unsubscribe({required CloudMessagingRepository notificationRepository})
: _notificationRepository = notificationRepository;
@override