initListener method
初始化联系人变化的监听,调用initListener之后才能使用onContactInfoUpdated
Implementation
@override
void initListener() {
onContactInfoUpdated = _onContactUpdated.stream;
subscriptions.add(
NimCore.instance.userService.onFriendAddedOrUpdated.listen((event) {
for (var e in event) {
String userId = e.userId!;
if (contactMap[userId] != null) {
contactMap[userId]!.friend = e;
_onContactUpdated.add(contactMap[userId]);
} else {
_getUser(userId).then((value) {
if (value != null) {
contactMap[userId] = ContactInfo(value, friend: e);
_onContactUpdated.add(contactMap[userId]);
}
});
}
}
}));
subscriptions
.add(NimCore.instance.userService.onUserInfoChange.listen((event) {
for (var e in event) {
String userId = e.userId!;
if (contactMap[userId] != null) {
contactMap[userId]!.user = e;
} else {
contactMap[userId] = ContactInfo(e);
}
_onContactUpdated.add(contactMap[userId]);
}
}));
subscriptions
.add(NimCore.instance.userService.onFriendDeleted.listen((event) {
for (var e in event) {
contactMap.removeWhere((key, value) => e == key);
}
}));
subscriptions
.add(NimCore.instance.userService.onBlackListChanged.listen((event) {
NimCore.instance.userService.getBlackList().then((value) {
if (value.isSuccess && value.data != null) {
var blackListAccIds = value.data!;
for (var contact in contactMap.values) {
if (blackListAccIds.contains(contact?.user.userId)) {
//在目录表示在黑名单,如果本来不在黑名单则需要重置
if (contact?.isInBlack != true) {
contact?.isInBlack = true;
_onContactUpdated.add(contact);
}
} else {
if (contact?.isInBlack == true) {
contact?.isInBlack = false;
_onContactUpdated.add(contact);
}
}
}
}
});
}));
}