100 lines
2.5 KiB
Dart
100 lines
2.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:event_bus/event_bus.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter_mm_core/im_core/model/model_conversation.dart';
|
|
import 'package:flutter_mm_core/im_core/model/model_message.dart';
|
|
|
|
import 'im_ad/im_ad_item.dart';
|
|
import 'im_ad/im_ad_manager.dart';
|
|
import 'im_ad/im_ad_provider.dart';
|
|
import 'model/im_message_resp.dart';
|
|
|
|
typedef EventCallback<T> = void Function(T event);
|
|
|
|
class ImMsgProvider with ChangeNotifier {
|
|
static final ImMsgProvider _ins = ImMsgProvider._();
|
|
|
|
factory ImMsgProvider() => _ins;
|
|
|
|
ImMsgProvider._();
|
|
int _unReadCount = 0;
|
|
bool get hasUnreadCount => _unReadCount > 0;
|
|
|
|
ModelConversationListResult? _curConversation;
|
|
|
|
void setUnReadStatus(ModelConversationListResult resp) {
|
|
_curConversation = resp;
|
|
_unReadCount = resp.totalUnreadCount;
|
|
int adUnReadCount = 0;
|
|
int adMaxCount = resp.conversations.length ~/ 3 + 1;
|
|
List<ImAdItem> adList = ImAdProvider().getAdList(ImAdManager.kPosListFeedNative);
|
|
for (int i = 0; i < adMaxCount && i < adList.length; i++) {
|
|
ImAdItem adItem = adList[i];
|
|
if (ImAdProvider().isUnreadAd(adItem.id)) {
|
|
adUnReadCount++;
|
|
}
|
|
}
|
|
_unReadCount += adUnReadCount;
|
|
notifyListeners();
|
|
}
|
|
|
|
//广告数据刷新,我的消息红点状态也要同步更新
|
|
void unReadStatusUpdateByAds() {
|
|
setUnReadStatus(_curConversation ?? ModelConversationListResult());
|
|
}
|
|
|
|
void setUnReadStatusFromAd() {
|
|
setUnReadStatus(_curConversation ?? ModelConversationListResult());
|
|
}
|
|
}
|
|
|
|
class IMMsgEvent {
|
|
final ModelMessage? model;
|
|
final bool isSuccess;
|
|
|
|
IMMessageModel? get msgModel {
|
|
if (model == null) return null;
|
|
IMMessageModel convtModel = IMMessageModel.convertModel(model!);
|
|
return convtModel;
|
|
}
|
|
|
|
IMMsgEvent(this.model, this.isSuccess);
|
|
}
|
|
|
|
class ImEventBus {
|
|
//保存单例
|
|
static final ImEventBus _instance = ImEventBus._internal();
|
|
|
|
//工厂构造函数
|
|
factory ImEventBus() => _instance;
|
|
|
|
//初始化eventBus
|
|
late EventBus _eventBus;
|
|
|
|
ImEventBus._internal() {
|
|
// 初始化
|
|
_eventBus = EventBus();
|
|
}
|
|
|
|
/// 开启eventbus订阅
|
|
StreamSubscription on<T>(EventCallback<T> callback) {
|
|
StreamSubscription stream = _eventBus.on<T>().listen((event) {
|
|
callback(event);
|
|
});
|
|
return stream;
|
|
}
|
|
|
|
/// 发送消息
|
|
void emit(event) {
|
|
_eventBus.fire(event);
|
|
}
|
|
|
|
/// 移除stream
|
|
void off(StreamSubscription steam) {
|
|
steam.cancel();
|
|
}
|
|
}
|
|
|
|
var imEventBus = ImEventBus._instance;
|