Cloud Messaging Package #206
| @ -1,15 +1,12 @@ | ||||
| import 'package:firebase_core/firebase_core.dart'; | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | ||||
| import 'package:notification_bloc_example/firebase_options.dart'; | ||||
| import 'package:wyatt_notification_bloc/wyatt_notification_bloc.dart'; | ||||
| 
 | ||||
| void main() async { | ||||
|   WidgetsFlutterBinding.ensureInitialized(); | ||||
|   await Firebase.initializeApp( | ||||
|     options: DefaultFirebaseOptions.currentPlatform, | ||||
|   ); | ||||
|   print("Runnig App"); | ||||
|   runApp(const MyApp()); | ||||
| } | ||||
| 
 | ||||
| @ -57,19 +54,11 @@ class MyHomePage extends StatefulWidget { | ||||
| } | ||||
| 
 | ||||
| class _MyHomePageState extends State<MyHomePage> { | ||||
|   String _content = 'No content'; | ||||
| 
 | ||||
|   void _handlerNotif(RemoteNotificationWrapper notif) { | ||||
|     setState(() { | ||||
|       _content = notif.data.toString(); | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     final notificationFirebaseDataSource = FirebaseMessagingDataSourceImpl(); | ||||
|     final notificationRepository = NotificationRepositoryImpl( | ||||
|         notificationRemoteDataSource: notificationFirebaseDataSource); | ||||
|     // final notificationFirebaseDataSource = FirebaseMessagingDataSourceImpl(); | ||||
|     // final notificationRepository = NotificationRepositoryImpl( | ||||
|     //     notificationRemoteDataSource: notificationFirebaseDataSource); | ||||
|     // This method is rerun every time setState is called, for instance as done | ||||
|     // by the _incrementCounter method above. | ||||
|     // | ||||
| @ -82,8 +71,12 @@ class _MyHomePageState extends State<MyHomePage> { | ||||
|         // the App.build method, and use it to set our appbar title. | ||||
|         title: Text(widget.title), | ||||
|       ), | ||||
| <<<<<<< HEAD | ||||
| <<<<<<< HEAD | ||||
|       body: Center( | ||||
| ======= | ||||
|       body: const Center( | ||||
| >>>>>>> 3dbc809 (feat: implement notification abstraction package) | ||||
|         // Center is a layout widget. It takes a single child and positions it | ||||
|         // in the middle of the parent. | ||||
|         child: Column( | ||||
| @ -103,6 +96,7 @@ class _MyHomePageState extends State<MyHomePage> { | ||||
|           // horizontal). | ||||
|           mainAxisAlignment: MainAxisAlignment.center, | ||||
|           children: <Widget>[ | ||||
| <<<<<<< HEAD | ||||
|             const Text( | ||||
|               'You have pushed the button this many times:', | ||||
|             ), | ||||
| @ -157,6 +151,15 @@ class _MyHomePageState extends State<MyHomePage> { | ||||
|             ], | ||||
|           ), | ||||
| >>>>>>> 5da1cd0 (feat(notification_bloc): add base architecture and start impementing firebase messaging data source. Add example too (#33)) | ||||
| ======= | ||||
|             Text( | ||||
|               'content of notification :', | ||||
|             ), | ||||
|             Text( | ||||
|               'Notif', | ||||
|             ), | ||||
|           ], | ||||
| >>>>>>> 3dbc809 (feat: implement notification abstraction package) | ||||
|         ), | ||||
|       ), | ||||
|     ); | ||||
|  | ||||
| @ -33,7 +33,7 @@ dependencies: | ||||
|     sdk: flutter | ||||
| 
 | ||||
|   flutter_bloc: ^8.1.1 | ||||
|   wyatt_notification_bloc: | ||||
|   wyatt_cloud_messaging_bloc: | ||||
|     path: ../ | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										2
									
								
								packages/wyatt_notification_bloc/lib/src/core/core.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								packages/wyatt_notification_bloc/lib/src/core/core.dart
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| export './exceptions/notification_exeption.dart'; | ||||
| export './extensions/build_context_extension.dart'; | ||||
| @ -1,5 +0,0 @@ | ||||
| enum NotificationStatus { | ||||
|   unknown, | ||||
|   authorized, | ||||
|   unauthorized, | ||||
| } | ||||
| @ -0,0 +1,5 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| 
 | ||||
| class NotificationException extends AppException { | ||||
|   const NotificationException([super.message]); | ||||
| } | ||||
| @ -0,0 +1,7 @@ | ||||
| import 'package:flutter/widgets.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/wyatt_cloud_messaging_bloc.dart'; | ||||
| 
 | ||||
| extension BuildContextExtension on BuildContext { | ||||
|   CloudmessagingCubit get cloudMessaging => read<CloudmessagingCubit>(); | ||||
| } | ||||
| @ -1,2 +1 @@ | ||||
| export 'data_sources/remote/firebase_messaging_data_source_impl.dart'; | ||||
| export 'repositories/notification_repository_impl.dart'; | ||||
|  | ||||
| @ -1,52 +0,0 @@ | ||||
| import 'package:firebase_messaging/firebase_messaging.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/data/models/remote_notification_wrapper_model.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/data_sources/remote/notification_remote_data_source.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| 
 | ||||
| class FirebaseMessagingDataSourceImpl extends NotificationRemoteDataSource { | ||||
|   final _instance = FirebaseMessaging.instance; | ||||
| 
 | ||||
|   @override | ||||
|   Future<String> getToken() async { | ||||
|     final token = await _instance.getToken(); | ||||
|     if (token != null && token.isNotEmpty) { | ||||
|       return token; | ||||
|     } | ||||
|     throw Exception('error on token'); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Stream<RemoteNotificationWrapper> onNotification() => | ||||
|       FirebaseMessaging.onMessage | ||||
|           .map(RemoteNotificationWrapperModel.fromFirebaseModel); | ||||
| 
 | ||||
|   @override | ||||
|   Stream<RemoteNotificationWrapper> onNotificationOpened() => | ||||
|       FirebaseMessaging.onMessageOpenedApp | ||||
|           .map(RemoteNotificationWrapperModel.fromFirebaseModel); | ||||
| 
 | ||||
|   @override | ||||
|   Future<RemoteNotificationWrapper?> getInitialMessage() => | ||||
|       _instance.getInitialMessage().then<RemoteNotificationWrapper?>( | ||||
|             RemoteNotificationWrapperModel.fromFirebaseModel, | ||||
|           ); | ||||
| 
 | ||||
|   @override | ||||
|   Future<void> register() async { | ||||
|     await _instance.requestPermission(); | ||||
|     await FirebaseMessaging.instance | ||||
|         .setForegroundNotificationPresentationOptions( | ||||
|       alert: true, // Required to display a heads up notification | ||||
|       badge: true, | ||||
|       sound: true, | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   Future<void> subscribeToTopic(String topic) => | ||||
|       _instance.subscribeToTopic(topic); | ||||
| 
 | ||||
|   @override | ||||
|   Future<void> unsubscribeFromTopic(String topic) => | ||||
|       _instance.unsubscribeFromTopic(topic); | ||||
| } | ||||
| @ -1,30 +0,0 @@ | ||||
| import 'package:firebase_messaging/firebase_messaging.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| 
 | ||||
| class RemoteNotificationWrapperModel extends RemoteNotificationWrapper { | ||||
|   @override | ||||
|   final String? senderId; | ||||
|   @override | ||||
|   final String? messageId; | ||||
|   @override | ||||
|   final Map<String, dynamic>? data; | ||||
|   @override | ||||
|   final DateTime? sentTime; | ||||
| 
 | ||||
|   const RemoteNotificationWrapperModel({ | ||||
|     this.senderId, | ||||
|     this.messageId, | ||||
|     this.data, | ||||
|     this.sentTime, | ||||
|   }); | ||||
| 
 | ||||
|   factory RemoteNotificationWrapperModel.fromFirebaseModel( | ||||
|     RemoteMessage? message, | ||||
|   ) => | ||||
|       RemoteNotificationWrapperModel( | ||||
|         senderId: message?.senderId, | ||||
|         messageId: message?.senderId, | ||||
|         data: message?.data, | ||||
|         sentTime: message?.sentTime, | ||||
|       ); | ||||
| } | ||||
| @ -1,7 +1,8 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/data_sources/remote/notification_remote_data_source.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/core/exceptions/notification_exeption.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/data_sources/remote/notification_remote_data_source.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | ||||
| 
 | ||||
| class NotificationRepositoryImpl extends NotificationRepository { | ||||
| @ -13,46 +14,68 @@ class NotificationRepositoryImpl extends NotificationRepository { | ||||
|   @override | ||||
|   FutureOrResult<void> register() => Result.tryCatchAsync( | ||||
|         _notificationRemoteDataSource.register, | ||||
|         (error) => ClientException(error.toString()), | ||||
|         (error) => error is AppException | ||||
|             ? error | ||||
|             : NotificationException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<String> getToken() => Result.tryCatchAsync( | ||||
|         _notificationRemoteDataSource.getToken, | ||||
|         (error) => ClientException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotificationWrapper>> onNotification() => | ||||
|       Result.tryCatch( | ||||
|         _notificationRemoteDataSource.onNotification, | ||||
|         (error) => ClientException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<RemoteNotificationWrapper?> getInitialMessage() => | ||||
|       Result.tryCatchAsync( | ||||
|         _notificationRemoteDataSource.getInitialMessage, | ||||
|         (error) => ClientException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotificationWrapper>> onNotificationOpened() => | ||||
|       Result.tryCatch( | ||||
|         _notificationRemoteDataSource.onNotificationOpened, | ||||
|         (error) => ClientException(error.toString()), | ||||
|         (error) => error is AppException | ||||
|             ? error | ||||
|             : NotificationException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> subscribeToTopic(String topic) => Result.tryCatchAsync( | ||||
|         () => _notificationRemoteDataSource.subscribeToTopic(topic), | ||||
|         (error) => ClientException(error.toString()), | ||||
|         (error) => error is AppException | ||||
|             ? error | ||||
|             : NotificationException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> unsubscribeFromTopic(String topic) => | ||||
|       Result.tryCatchAsync( | ||||
|         () => _notificationRemoteDataSource.unsubscribeFromTopic(topic), | ||||
|         (error) => ClientException(error.toString()), | ||||
|         (error) => error is AppException | ||||
|             ? error | ||||
|             : NotificationException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> requestPermissions() => Result.tryCatchAsync( | ||||
|         _notificationRemoteDataSource.requestPermissions, | ||||
|         (error) => error is AppException | ||||
|             ? error | ||||
|             : NotificationException(error.toString()), | ||||
|       ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotification>> onNotificationAccepted() => | ||||
|       Result.tryCatchAsync( | ||||
|         _notificationRemoteDataSource.onNotificationBackgroundAccepted, | ||||
|         (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()), | ||||
|           ); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotification>> | ||||
|       onNotificationForegroundAccepted() => Result.tryCatchAsync( | ||||
|             _notificationRemoteDataSource.onNotificationForegroundAccepted, | ||||
|             (error) => error is AppException | ||||
|                 ? error | ||||
|                 : NotificationException(error.toString()), | ||||
|           ); | ||||
| } | ||||
|  | ||||
| @ -1,14 +1,16 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| 
 | ||||
| abstract class NotificationRemoteDataSource extends BaseRepository { | ||||
|   Future<void> register(); | ||||
| 
 | ||||
|   Stream<RemoteNotificationWrapper> onNotification(); | ||||
|   Future<Stream<RemoteNotification>> onNotificationAccepted(); | ||||
| 
 | ||||
|   Stream<RemoteNotificationWrapper> onNotificationOpened(); | ||||
|   Future<Stream<RemoteNotification>> onNotificationBackgroundAccepted(); | ||||
| 
 | ||||
|   Future<RemoteNotificationWrapper?> getInitialMessage(); | ||||
|   Future<Stream<RemoteNotification>> onNotificationForegroundAccepted(); | ||||
| 
 | ||||
|   Future<void> requestPermissions(); | ||||
| 
 | ||||
|   Future<String> getToken(); | ||||
| 
 | ||||
|  | ||||
| @ -1,9 +1,15 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| 
 | ||||
| abstract class RemoteNotificationWrapper extends Entity { | ||||
|   const RemoteNotificationWrapper(); | ||||
|   String? get senderId; | ||||
|   String? get messageId; | ||||
|   Map<String, dynamic>? get data; | ||||
|   DateTime? get sentTime; | ||||
| class RemoteNotification extends Entity { | ||||
|   const RemoteNotification({ | ||||
|     this.senderId, | ||||
|     this.messageId, | ||||
|     this.data, | ||||
|     this.sentTime, | ||||
|   }); | ||||
| 
 | ||||
|   final String? senderId; | ||||
|   final String? messageId; | ||||
|   final Map<String, dynamic>? data; | ||||
|   final DateTime? sentTime; | ||||
| } | ||||
|  | ||||
| @ -1,16 +1,18 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| 
 | ||||
| abstract class NotificationRepository extends BaseRepository { | ||||
|   FutureOrResult<void> register(); | ||||
| 
 | ||||
|   FutureOrResult<Stream<RemoteNotificationWrapper>> onNotification(); | ||||
|   FutureOrResult<Stream<RemoteNotification>> onNotificationAccepted(); | ||||
| 
 | ||||
|   FutureOrResult<Stream<RemoteNotificationWrapper>> onNotificationOpened(); | ||||
|   FutureOrResult<Stream<RemoteNotification>> onNotificationBackgroundAccepted(); | ||||
| 
 | ||||
|   FutureOrResult<RemoteNotificationWrapper?> getInitialMessage(); | ||||
|   FutureOrResult<Stream<RemoteNotification>> onNotificationForegroundAccepted(); | ||||
| 
 | ||||
|   FutureOrResult<void> requestPermissions(); | ||||
| 
 | ||||
|   FutureOrResult<String> getToken(); | ||||
| 
 | ||||
|  | ||||
| @ -0,0 +1,14 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class GetCloudMessagingTokenUseCase extends AsyncUseCase<void, String> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   GetCloudMessagingTokenUseCase({ | ||||
|     required NotificationRepository notificationRepository, | ||||
|   }) : _notificationRepository = notificationRepository; | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<String> execute(void params) => | ||||
|       _notificationRepository.getToken(); | ||||
| } | ||||
| @ -1,14 +0,0 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/wyatt_notification_bloc.dart'; | ||||
| 
 | ||||
| class GetInitialMessage | ||||
|     extends AsyncUseCase<NoParam, RemoteNotificationWrapper?> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   GetInitialMessage({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<RemoteNotificationWrapper?> call(NoParam? params) => | ||||
|       _notificationRepository.getInitialMessage(); | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class InitCloudmessagingUseCase extends AsyncUseCase<NoParam, void> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   InitCloudmessagingUseCase({ | ||||
|     required NotificationRepository notificationRepository, | ||||
|   }) : _notificationRepository = notificationRepository; | ||||
|   @override | ||||
|   FutureOrResult<void> execute(NoParam? params) => | ||||
|       _notificationRepository.register(); | ||||
| } | ||||
| @ -1,14 +0,0 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class ListenNotification | ||||
|     extends StreamUseCase<NoParam, RemoteNotificationWrapper> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   ListenNotification({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotificationWrapper>> call(NoParam? params) => | ||||
|       _notificationRepository.onNotificationOpened(); | ||||
| } | ||||
| @ -0,0 +1,51 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:rxdart/rxdart.dart'; | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/core/exceptions/notification_exeption.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| import 'package:wyatt_type_utils/wyatt_type_utils.dart'; | ||||
| 
 | ||||
| class ListenNotification extends StreamUseCase<NoParam, RemoteNotification> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   ListenNotification({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
|   @override | ||||
|   FutureOrResult<Stream<RemoteNotification>> execute(NoParam? params) async { | ||||
|     Stream<RemoteNotification>? notificationStream; | ||||
| 
 | ||||
|     final notificationStreamResponse = | ||||
|         await _notificationRepository.onNotificationAccepted(); | ||||
| 
 | ||||
|     if (notificationStreamResponse.isOk) { | ||||
|       notificationStream = notificationStreamResponse.ok; | ||||
|     } else if (notificationStreamResponse.isErr) { | ||||
|       final notificationBackgroundStreamResponse = | ||||
|           await _notificationRepository.onNotificationBackgroundAccepted(); | ||||
| 
 | ||||
|       if (notificationBackgroundStreamResponse.isErr) { | ||||
|         return Err(notificationBackgroundStreamResponse.err!); | ||||
|       } | ||||
| 
 | ||||
|       final notificationForegroundStreamResponse = | ||||
|           await _notificationRepository.onNotificationForegroundAccepted(); | ||||
| 
 | ||||
|       if (notificationForegroundStreamResponse.isErr) { | ||||
|         return Err(notificationBackgroundStreamResponse.err!); | ||||
|       } | ||||
| 
 | ||||
|       notificationStream = Rx.merge([ | ||||
|         notificationBackgroundStreamResponse.ok!, | ||||
|         notificationBackgroundStreamResponse.ok!, | ||||
|       ]); | ||||
|     } | ||||
| 
 | ||||
|     if (notificationStream == null) { | ||||
|       return const Err(NotificationException('Cannot listen notifications')); | ||||
|     } | ||||
| 
 | ||||
|     return Ok(notificationStream); | ||||
|   } | ||||
| } | ||||
| @ -1,16 +0,0 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class Register extends AsyncUseCase<NoParam, void> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   Register({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
|   @override | ||||
|   FutureOrResult<void> call(NoParam? params) async { | ||||
|     final registration = await _notificationRepository.register(); | ||||
|     final userToken = await _notificationRepository.getToken(); | ||||
|     print("User FCM Token : ${userToken.ok}"); | ||||
|     return registration; | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class RequestCloudMessagingPermissionUseCase extends AsyncUseCase<void, void> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| 
 | ||||
|   RequestCloudMessagingPermissionUseCase({ | ||||
|     required NotificationRepository notificationRepository, | ||||
|   }) : _notificationRepository = notificationRepository; | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> execute(void params) => | ||||
|       _notificationRepository.register(); | ||||
| } | ||||
| @ -1,7 +1,7 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class Subscribe extends AsyncUseCase<String, void> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| @ -9,14 +9,14 @@ class Subscribe extends AsyncUseCase<String, void> { | ||||
|   Subscribe({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> call(String? params) => | ||||
|       _notificationRepository.subscribeToTopic(params ?? ''); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOr<void> onStart(String? params) { | ||||
|     if (params == null) { | ||||
|       throw ClientException('Topic is not valid'); | ||||
|       throw const ClientException('Topic is not valid'); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> execute(String? params) => | ||||
|       _notificationRepository.subscribeToTopic(params!); | ||||
| } | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/repositories/notification_repository.dart'; | ||||
| 
 | ||||
| class Unsubscribe extends AsyncUseCase<String, void> { | ||||
|   final NotificationRepository _notificationRepository; | ||||
| @ -9,14 +9,14 @@ class Unsubscribe extends AsyncUseCase<String, void> { | ||||
|   Unsubscribe({required NotificationRepository notificationRepository}) | ||||
|       : _notificationRepository = notificationRepository; | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> call(String? params) => | ||||
|       _notificationRepository.unsubscribeFromTopic(params ?? ''); | ||||
| 
 | ||||
|   @override | ||||
|   FutureOr<void> onStart(String? params) { | ||||
|     if (params == null) { | ||||
|       throw ClientException('Topic is not valid'); | ||||
|       throw const ClientException('Topic is not valid'); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   @override | ||||
|   FutureOrResult<void> execute(String? params) => | ||||
|       _notificationRepository.unsubscribeFromTopic(params!); | ||||
| } | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| export 'get_intitial_message.dart'; | ||||
| export 'listen_notification.dart'; | ||||
| export 'register.dart'; | ||||
| export 'subscribe.dart'; | ||||
| export 'unsubscribe.dart'; | ||||
| export './get_cloud_messaging_token_use_case.dart'; | ||||
| export './init_cloud_messaging_use_case.dart'; | ||||
| export './listen_notification_use_case.dart'; | ||||
| export './request_cloud_messaging_permission_use_case.dart'; | ||||
| export './subscribe.dart'; | ||||
| export './unsubscribe.dart'; | ||||
|  | ||||
| @ -0,0 +1,43 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:equatable/equatable.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/usecases/listen_notification_use_case.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/usecases/subscribe.dart'; | ||||
| import 'package:wyatt_cloud_messaging_bloc/src/domain/usecases/unsubscribe.dart'; | ||||
| 
 | ||||
| part 'cloud_messaging_state.dart'; | ||||
| 
 | ||||
| typedef OnNotification = void Function(RemoteNotification); | ||||
| 
 | ||||
| class CloudmessagingCubit extends Cubit<CloudmessagingState> { | ||||
|   final ListenNotification _listenNotification; | ||||
|   final Subscribe _subscribe; | ||||
|   final Unsubscribe _unsubscribe; | ||||
| 
 | ||||
|   final OnNotification? handleNotification; | ||||
|   CloudmessagingCubit({ | ||||
|     required ListenNotification listenNotification, | ||||
|     required Subscribe subscribe, | ||||
|     required Unsubscribe unsubscribe, | ||||
|     this.handleNotification, | ||||
|   })  : _listenNotification = listenNotification, | ||||
|         _subscribe = subscribe, | ||||
|         _unsubscribe = unsubscribe, | ||||
|         super(const CloudmessagingState()); | ||||
| 
 | ||||
|   Future<void> listenNotification() async { | ||||
|     final notificaitons = await _listenNotification.execute(const NoParam()); | ||||
|     if (notificaitons.isOk) { | ||||
|       notificaitons.ok?.listen((notification) { | ||||
|         emit(CloudmessagingState(notification)); | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   FutureOr<void> subscribe(String topic) => _subscribe.execute(topic); | ||||
| 
 | ||||
|   FutureOr<void> unsubscribe(String topic) => _unsubscribe.execute(topic); | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| part of 'cloud_messaging_cubit.dart'; | ||||
| 
 | ||||
| class CloudmessagingState extends Equatable { | ||||
|   final RemoteNotification? remoteNotification; | ||||
| 
 | ||||
|   const CloudmessagingState([this.remoteNotification]); | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [remoteNotification]; | ||||
| } | ||||
| @ -1,71 +0,0 @@ | ||||
| import 'dart:async'; | ||||
| 
 | ||||
| import 'package:equatable/equatable.dart'; | ||||
| import 'package:flutter_bloc/flutter_bloc.dart'; | ||||
| import 'package:wyatt_architecture/wyatt_architecture.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/core/enum/notification_status.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/entities/remote_notifications.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/usecases/get_intitial_message.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/usecases/listen_notification.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/usecases/register.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/usecases/subscribe.dart'; | ||||
| import 'package:wyatt_notification_bloc/src/domain/usecases/unsubscribe.dart'; | ||||
| 
 | ||||
| part 'notification_state.dart'; | ||||
| 
 | ||||
| typedef OnNotification = void Function(RemoteNotificationWrapper); | ||||
| 
 | ||||
| class NotificationCubit extends Cubit<NotificationState> { | ||||
|   final Register _register; | ||||
|   final ListenNotification _listenNotification; | ||||
|   final Subscribe _subscribe; | ||||
|   final Unsubscribe _unsubscribe; | ||||
|   final GetInitialMessage _getInitialMessage; | ||||
| 
 | ||||
|   final OnNotification? handleNotification; | ||||
|   NotificationCubit({ | ||||
|     required Register register, | ||||
|     required GetInitialMessage getInitialMessage, | ||||
|     required ListenNotification listenNotification, | ||||
|     required Subscribe subscribe, | ||||
|     required Unsubscribe unsubscribe, | ||||
|     this.handleNotification, | ||||
|   })  : _register = register, | ||||
|         _getInitialMessage = getInitialMessage, | ||||
|         _listenNotification = listenNotification, | ||||
|         _subscribe = subscribe, | ||||
|         _unsubscribe = unsubscribe, | ||||
|         super(const NotificationState.unknown()); | ||||
| 
 | ||||
|   FutureOr<void> register() async { | ||||
|     print("Registering Threw cubit\n"); | ||||
|     final response = await _register.execute(const NoParam()); | ||||
| 
 | ||||
|     print(response); | ||||
|     if (response.isOk) { | ||||
|       emit(const NotificationState.authorized()); | ||||
| 
 | ||||
|       final initialMessage = await _getInitialMessage.execute(const NoParam()); | ||||
|       if (initialMessage.isOk && initialMessage.ok != null) { | ||||
|         handleNotification?.call(initialMessage.ok!); | ||||
|       } | ||||
|     } else if (response.isErr) { | ||||
|       emit(const NotificationState.unauthorized()); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   Future<void> listenNotification() async { | ||||
|     print("Listening for notifications\n"); | ||||
|     final notificaitons = await _listenNotification.execute(const NoParam()); | ||||
|     if (notificaitons.isOk) { | ||||
|       print("Get notifications ready to listen"); | ||||
|       notificaitons.ok?.listen((_) { | ||||
|         print("New notif !!\n"); | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   FutureOr<void> subscribe(String topic) => _subscribe.execute(topic); | ||||
| 
 | ||||
|   FutureOr<void> unsubscribe(String topic) => _unsubscribe.execute(topic); | ||||
| } | ||||
| @ -1,31 +0,0 @@ | ||||
| part of 'notification_cubit.dart'; | ||||
| 
 | ||||
| class NotificationState extends Equatable { | ||||
|   final NotificationStatus _notificationStatus; | ||||
| 
 | ||||
|   const NotificationState._({required NotificationStatus notificationStatus}) | ||||
|       : _notificationStatus = notificationStatus; | ||||
| 
 | ||||
|   const NotificationState.unknown() | ||||
|       : this._( | ||||
|           notificationStatus: NotificationStatus.unknown, | ||||
|         ); | ||||
| 
 | ||||
|   const NotificationState.authorized() | ||||
|       : this._( | ||||
|           notificationStatus: NotificationStatus.authorized, | ||||
|         ); | ||||
| 
 | ||||
|   const NotificationState.unauthorized() | ||||
|       : this._( | ||||
|           notificationStatus: NotificationStatus.unauthorized, | ||||
|         ); | ||||
| 
 | ||||
|   NotificationStatus get notificationStatus => _notificationStatus; | ||||
| 
 | ||||
|   @override | ||||
|   List<Object?> get props => [_notificationStatus]; | ||||
| 
 | ||||
|   @override | ||||
|   String toString() => 'NotificationState(status: $notificationStatus)'; | ||||
| } | ||||
| @ -1,3 +1,3 @@ | ||||
| export 'data/data.dart'; | ||||
| export 'domain/domain.dart'; | ||||
| export 'features/notifications/bloc/notification_cubit.dart'; | ||||
| export 'features/notifications/bloc/cloud_messaging_cubit.dart'; | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| // Copyright (C) 2022 WYATT GROUP | ||||
| // Copyright (C) 2023 WYATT GROUP | ||||
| // Please see the AUTHORS file for details. | ||||
| // | ||||
| // This program is free software: you can redistribute it and/or modify | ||||
| @ -15,6 +15,6 @@ | ||||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||
| 
 | ||||
| /// An notification library for BLoC. | ||||
| library wyatt_notification_bloc; | ||||
| library wyatt_cloud_messaging_bloc; | ||||
| 
 | ||||
| export 'src/src.dart'; | ||||
| @ -1,4 +1,4 @@ | ||||
| name: wyatt_notification_bloc | ||||
| name: wyatt_cloud_messaging_bloc | ||||
| description: A starting point for Dart libraries or applications. | ||||
| repository: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/src/branch/master/packages/wyatt_notification_bloc | ||||
| version: 1.0.0 | ||||
| @ -6,22 +6,26 @@ version: 1.0.0 | ||||
| publish_to: none | ||||
| 
 | ||||
| environment: | ||||
|   sdk: ">=2.18.2 <3.0.0" | ||||
|   sdk: ">=3.0.0 <4.0.0" | ||||
| 
 | ||||
| dependencies: | ||||
|   flutter: | ||||
|     sdk: flutter | ||||
|   equatable: ^2.0.5 | ||||
|   firebase_messaging: ^14.1.1 | ||||
|   flutter_bloc: ^8.1.1 | ||||
| 
 | ||||
|   rxdart: ^0.27.7 | ||||
|   wyatt_architecture: | ||||
|     git:  | ||||
|       url: ssh://git@git.wyatt-studio.fr:993/Wyatt-FOSS/wyatt-packages.git | ||||
|       ref: feat/architecture/make_usecases_more_modular_and_adaptable | ||||
|       path: packages/wyatt_architecture | ||||
| 
 | ||||
|     hosted: | ||||
|       url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ | ||||
|       name: wyatt_architecture | ||||
|     version: ^0.2.0 | ||||
|   wyatt_type_utils: | ||||
|     hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ | ||||
|     version: ^0.0.3+1 | ||||
|     hosted: | ||||
|       url: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub/ | ||||
|       name: wyatt_type_utils | ||||
|     version: ^0.0.5 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| dev_dependencies: | ||||
|  | ||||
| @ -0,0 +1,11 @@ | ||||
| // This is a generated file; do not edit or check into version control. | ||||
| FLUTTER_ROOT=/Users/maloleon/Library/flutter | ||||
| FLUTTER_APPLICATION_PATH=/Users/maloleon/Studio/wyatt-packages/packages/wyatt_ui_kit/example | ||||
| COCOAPODS_PARALLEL_CODE_SIGN=true | ||||
| FLUTTER_BUILD_DIR=build | ||||
| FLUTTER_BUILD_NAME=1.0.0 | ||||
| FLUTTER_BUILD_NUMBER=1.0.0 | ||||
| DART_OBFUSCATION=false | ||||
| TRACK_WIDGET_CREATION=true | ||||
| TREE_SHAKE_ICONS=false | ||||
| PACKAGE_CONFIG=.dart_tool/package_config.json | ||||
| @ -0,0 +1,12 @@ | ||||
| #!/bin/sh | ||||
| # This is a generated file; do not edit or check into version control. | ||||
| export "FLUTTER_ROOT=/Users/maloleon/Library/flutter" | ||||
| export "FLUTTER_APPLICATION_PATH=/Users/maloleon/Studio/wyatt-packages/packages/wyatt_ui_kit/example" | ||||
| export "COCOAPODS_PARALLEL_CODE_SIGN=true" | ||||
| export "FLUTTER_BUILD_DIR=build" | ||||
| export "FLUTTER_BUILD_NAME=1.0.0" | ||||
| export "FLUTTER_BUILD_NUMBER=1.0.0" | ||||
| export "DART_OBFUSCATION=false" | ||||
| export "TRACK_WIDGET_CREATION=true" | ||||
| export "TREE_SHAKE_ICONS=false" | ||||
| export "PACKAGE_CONFIG=.dart_tool/package_config.json" | ||||
| @ -0,0 +1,11 @@ | ||||
| // This is a generated file; do not edit or check into version control. | ||||
| FLUTTER_ROOT=/Users/maloleon/Library/flutter | ||||
| FLUTTER_APPLICATION_PATH=/Users/maloleon/Studio/wyatt-packages/packages/wyatt_ui_layout/example | ||||
| COCOAPODS_PARALLEL_CODE_SIGN=true | ||||
| FLUTTER_BUILD_DIR=build | ||||
| FLUTTER_BUILD_NAME=1.0.0 | ||||
| FLUTTER_BUILD_NUMBER=1 | ||||
| DART_OBFUSCATION=false | ||||
| TRACK_WIDGET_CREATION=true | ||||
| TREE_SHAKE_ICONS=false | ||||
| PACKAGE_CONFIG=.dart_tool/package_config.json | ||||
| @ -0,0 +1,12 @@ | ||||
| #!/bin/sh | ||||
| # This is a generated file; do not edit or check into version control. | ||||
| export "FLUTTER_ROOT=/Users/maloleon/Library/flutter" | ||||
| export "FLUTTER_APPLICATION_PATH=/Users/maloleon/Studio/wyatt-packages/packages/wyatt_ui_layout/example" | ||||
| export "COCOAPODS_PARALLEL_CODE_SIGN=true" | ||||
| export "FLUTTER_BUILD_DIR=build" | ||||
| export "FLUTTER_BUILD_NAME=1.0.0" | ||||
| export "FLUTTER_BUILD_NUMBER=1" | ||||
| export "DART_OBFUSCATION=false" | ||||
| export "TRACK_WIDGET_CREATION=true" | ||||
| export "TREE_SHAKE_ICONS=false" | ||||
| export "PACKAGE_CONFIG=.dart_tool/package_config.json" | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user