提交 f12b5b00 authored 作者: MrQi's avatar MrQi

first submit

上级
## 0.0.1
* TODO: Describe initial release.
TODO: Add your license here.
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
## Features
TODO: List what your package can do. Maybe include images, gifs, or videos.
## Getting started
TODO: List prerequisites and provide or point to information on how to
start using the package.
## Usage
TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
```dart
const like = 'sample';
```
## Additional information
TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
include: package:flutter_lints/flutter.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
library clx_notification_center;
export './notification_center/clx_notification_center.dart'
show defaultNotificationCenter;
import 'clx_notification_message.dart';
import 'clx_notification_name.dart';
typedef CLXNotificationChannel = Future Function(CLXNotificationMessage body);
final class CLXNotificationBody {
final CLXNotificationName name;
final List<CLXNotificationChannel> listerenList = [];
CLXNotificationBody({required this.name});
bool addListen(CLXNotificationChannel listen) {
if (listerenList.where((element) => element == listen).isEmpty) {
listerenList.add(listen);
}
return true;
}
bool removeListen(CLXNotificationChannel listen) {
listerenList.removeWhere((element) => element == listen);
return true;
}
Future<bool> notify({dynamic object}) async {
for (final listen in listerenList) {
await listen.call(CLXNotificationMessage(name: name, object: object));
}
return true;
}
}
import 'clx_notification_body.dart';
import 'clx_notification_name.dart';
final defaultNotificationCenter = _CLXNotificationCenter.getInstance();
final class _CLXNotificationCenter {
static _CLXNotificationCenter? _instance;
_CLXNotificationCenter._internal();
static _CLXNotificationCenter getInstance() {
return _instance ?? _CLXNotificationCenter._internal();
}
/// 消息管理器
final Set<CLXNotificationBody> _notificationItems = {};
/// 注册消息监听
void registerListener(
{required String name, required CLXNotificationChannel callback}) {
_notificationItems.firstWhere((element) => element.name.isEqual(name),
orElse: () {
CLXNotificationBody body =
CLXNotificationBody(name: CLXNotificationName(name: name));
_notificationItems.add(body);
return body;
}).addListen(callback);
}
/// 移除消息监听
void removeListener(
{required String name, required CLXNotificationChannel callback}) {
_notificationItems
.firstWhere((element) => element.name.isEqual(name))
.removeListen(callback);
}
/// 发送消息
Future<bool> postNotification(
{required String name,
dynamic object,
Duration delay = Duration.zero}) async {
if (_notificationItems.isEmpty) {
return Future.value(false);
}
if (delay != Duration.zero) {
await Future.delayed(delay);
}
final list =
_notificationItems.where((element) => element.name.isEqual(name));
final resList = list.map((e) => e.notify(object: object)).toList();
return resList.isNotEmpty;
}
}
import 'clx_notification_name.dart';
final class CLXNotificationMessage {
final CLXNotificationName name;
final dynamic object;
CLXNotificationMessage({required this.name, required this.object});
}
final class CLXNotificationName {
final String name;
const CLXNotificationName({required String name})
: name = 'clx_notification_$name';
@override
operator ==(Object other) {
if (other is CLXNotificationName) {
return name == other.name;
} else {
return false;
}
}
@override
int get hashCode => super.hashCode;
bool isEqual(Object other) {
if (other is CLXNotificationName) {
return name == other.name;
} else if (other is String) {
return name == 'clx_notification_$other';
} else {
return false;
}
}
}
name: clx_notification_center
description: "消息管理中心"
version: 0.0.1
homepage:
environment:
sdk: '>=3.3.4 <4.0.0'
flutter: ">=1.17.0"
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^3.0.0
flutter:
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论