| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551 |
- //
- // IMAContactManager.m
- // TIMAdapter
- //
- // Created by AlexiChen on 16/2/18.
- // Copyright © 2016年 AlexiChen. All rights reserved.
- //
- #import "IMAContactManager.h"
- @implementation IMAContactChangedNotifyItem
- - (instancetype)initWith:(IMAContactChangedNotifyType)type
- {
- if (self = [super init])
- {
- _type = type;
- }
- return self;
- }
- - (NSNotification *)changedNotification
- {
- NSNotification *notify = [NSNotification notificationWithName:[self notificationName] object:self];
- return notify;
- }
- - (NSString *)notificationName
- {
- return [NSString stringWithFormat:@"IMAContactChangedNotification_%d", (int)_type];
- }
- @end
- @implementation IMAContactManager
- - (void)saveToLocal
- {
- [self saveSubGroupStateInfoToLocal];
- }
- - (void)asyncConfigContact
- {
- // 分组是FriendShipProxy下的内容
- // 同步分组列表
- [self asyncSubGroupList];
- }
- // 异步配置群相关内容
- - (void)asyncConfigGroup
- {
- // 是GroupAssistant下的内容
- // 同步群列表
- [self syncGroupList];
-
- // 异步黑名单
- [self asyncBlackList];
- }
- - (CLSafeMutableArray *)clearPickedSubGroupList
- {
- NSInteger count = [_subGroupList count];
- for (NSInteger i = 0; i < count; i++)
- {
- IMASubGroup *group = [_subGroupList objectAtIndex:i];
-
- NSInteger fcount = group.friends.count;
- for (NSInteger j = 0; j < fcount; j++)
- {
- IMAUser *user = [group.friends objectAtIndex:j];
- user.isSelected = NO;
- }
- }
-
- return _subGroupList;
- }
- - (IMASubGroup *)getSubGroupOf:(NSString *)sgNAme
- {
- IMASubGroup *temp = [[IMASubGroup alloc] initWithName:sgNAme];
- NSInteger idx = [_subGroupList indexOfObject:temp];
- if (idx >= 0 && idx < _subGroupList.count)
- {
- return [_subGroupList objectAtIndex:idx];
- }
- else
- {
- return [self defaultAddToSubGroup];
- }
- }
- - (IMASubGroup *)subgroupOf:(IMAUser *)user
- {
- if ([user isC2CType])
- {
- for (NSInteger i = 0; i < _subGroupList.count; i++)
- {
- IMASubGroup *sg = [_subGroupList objectAtIndex:i];
- NSInteger index = [sg.friends indexOfObject:user];
- if (index >= 0 && index < sg.friends.count)
- {
- return sg;
- }
- }
- }
- return nil;
- }
- - (IMAUser *)isContainUser:(IMAUser *)user
- {
- if ([user isC2CType])
- {
- for (NSInteger i = 0; i < _subGroupList.count; i++)
- {
- IMASubGroup *sg = [_subGroupList objectAtIndex:i];
- NSInteger index = [sg.friends indexOfObject:user];
- if (index >= 0 && index < sg.friends.count)
- {
- return [sg.friends objectAtIndex:index];
- }
- }
- }
- else if ([user isGroupType])
- {
- NSInteger index = [_groupList indexOfObject:user];
- if (index >= 0 && index < _groupList.count)
- {
- return [_groupList objectAtIndex:index];
- }
- }
-
- return nil;
- }
- - (IMAUser *)getUserByGroupId:(NSString *)groupID
- {
- if ([NSString isEmpty:groupID])
- {
- return nil;
- }
- IMAUser *item = [[IMAGroup alloc] initWith:groupID];
- return [self isContainUser:item];
-
- }
- - (IMAUser *)getUserByUserId:(NSString *)userID
- {
- if ([NSString isEmpty:userID])
- {
- return nil;
- }
- IMAUser *item = [[IMAUser alloc] initWith:userID];
- return [self isContainUser:item];
- }
- - (BOOL)isMyFriend:(IMAUser *)user
- {
- // 检查通讯录中是否含有些人
- IMAUser *u = [self isContainUser:user];
-
- return u != nil;
- }
- - (BOOL)isInBlackListByID:(NSString *)userID
- {
- IMAUser *u = [[IMAUser alloc] initWith:userID];
- return [self isInBlackList:u];
- }
- - (BOOL)isInBlackList:(IMAUser *)user
- {
- return _blackList && [_blackList containsObject:user];
- }
- - (void)addContactChangedObserver:(id)observer handler:(SEL)selector forEvent:(NSUInteger)eventID
- {
- NSUInteger op = EIMAContact_AddNewSubGroup;
- do
- {
- if (op & eventID)
- {
- NSString *notification = [NSString stringWithFormat:@"IMAContactChangedNotification_%d", (int)op];
- [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:notification object:nil];
- eventID -= op;
- }
- op = op << 1;
-
- } while (eventID > 0);
- }
- - (void)removeContactChangedObser:(id)observer
- {
- [[NSNotificationCenter defaultCenter] removeObserver:observer];
- }
- - (void)removeUser:(IMAUser *)user
- {
- if (user)
- {
- [self onDeleteFriend:user];
- [[IMAPlatform sharedInstance].conversationMgr removeConversationWith:user];
- }
- }
- - (void)addUser:(IMAUser *)user toSubGroup:(IMASubGroup *)sg
- {
- if (user && [user isC2CType])
- {
- NSInteger idx = [_subGroupList indexOfObject:sg];
- if (idx >= 0 && idx < _subGroupList.count)
- {
- IMASubGroup *dsg = [_subGroupList objectAtIndex:idx];
-
- NSInteger uidx = [dsg.friends indexOfObject:user];
-
- if (!(uidx >= 0 && uidx < dsg.friends.count))
- {
- [dsg.friends addObject:user];
- [self onAddUser:user toSubGroup:dsg];
- }
-
- }
- }
- else
- {
- DebugLog(@"参数有误");
- }
- }
- - (void)addUserToDefaultSubGroup:(IMAUser *)user
- {
- if (user)
- {
- IMASubGroup *sub = [[IMASubGroup alloc] initDefaultSubGroup];
- NSInteger idx = [_subGroupList indexOfObject:sub];
- if (idx >= 0 && idx < _subGroupList.count)
- {
- IMASubGroup *dsg = [_subGroupList objectAtIndex:idx];
-
- NSInteger uidx = [dsg.friends indexOfObject:user];
-
- if (!(uidx >= 0 && uidx < dsg.friends.count))
- {
- [dsg.friends addObject:user];
- [self onAddUser:user toSubGroup:dsg];
- }
- }
- }
- }
- - (void)removeUserToBlackList:(IMAUser *)user
- {
- // [self removeUser:user];//这里不需要移除,添加到黑名单时,会收到一个删除好友的回调通知,-(void) OnDelFriends:(NSArray*)identifiers,在这个回调通知里面已经处理了
- [self onAddToBlackList:user];
- }
- - (void)removeUserOutBlackList:(IMAUser *)user
- {
- [self onRemoveOutBlackList:user];
- }
- @end
- @implementation IMAContactManager (Protected)
- - (void)onAddNewSubGroup:(IMASubGroup *)group
- {
- NSString * log = [NSString stringWithFormat:@"onAddNewSubGroup1 group.count = %ld,fun = %s",(long)group.itemsCount, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log];
- NSString * log1 = [NSString stringWithFormat:@"onAddNewSubGroup1 subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
- [self saveSubGroupStateInfoToLocal];
- [_subGroupList addObject:group];
- NSInteger index = [self.subGroupList count] - 1;
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddNewSubGroup];
- item.subGroup = group;
- item.index = index;
-
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- NSString * log2 = [NSString stringWithFormat:@"onAddNewSubGroup1 subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
- }
- - (void)onAddUser:(IMAUser *)user toSubGroup:(IMASubGroup *)group
- {
- NSInteger index = [group.friends indexOfObject:user];
-
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddFriend];
- item.subGroup = group;
- item.user = user;
- item.index = index;
-
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- }
- - (void)onDeleteSubGroup:(IMASubGroup *)group
- {
- NSString * log1 = [NSString stringWithFormat:@"onDeleteSubGroup subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
- NSInteger index = [_subGroupList indexOfObject:group];
- // todo-todo-
- [_subGroupList removeObject:group];
-
- [self saveSubGroupStateInfoToLocal];
-
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteSubGroup];
- item.subGroup = group;
- item.index = index;
-
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
-
- if (group.friends.count)
- {
- IMASubGroup *defg = [self defaultAddToSubGroup];
- [defg.friends addObjectsFromArray:group.friends.safeArray];
-
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_SubGroupChanged];
- item.subGroup = defg;
- item.index = [_subGroupList indexOfObject:defg];
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- }
- NSString * log2 = [NSString stringWithFormat:@"onDeleteSubGroup subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
- }
- - (void)onDeleteFriend:(IMAUser *)user
- {
- if ([user isC2CType])
- {
- for (NSInteger i = 0; i < _subGroupList.count; i++)
- {
- IMASubGroup *sg = [_subGroupList objectAtIndex:i];
- NSInteger index = [sg.friends indexOfObject:user];
- if (index >= 0 && index < sg.friends.count)
- {
- IMAUser *u = [sg.friends objectAtIndex:index];
- [sg.friends removeObject:u];
-
- // 更新
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteFriend];
- item.subGroup = sg;
- item.user = u;
- item.index = index;
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- break;
- }
- }
- }
- else if([user isGroupType])
- {
- NSInteger index = [_groupList indexOfObject:user];
- if (index >= 0 && index < _groupList.count)
- {
- IMAGroup *sg = [_groupList objectAtIndex:index];
- [_groupList removeObject:sg];
- // UI退出后上不存在更新逻辑,所以不发送通知
- // // 更新
- // IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteFriend];
- // item.subGroup = sg;
- // item.user = u;
- // item.index = index;
- // [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
-
- }
- }
- }
- - (void)onFriendInfoChanged:(IMAUser *)user remark:(NSString *)remark
- {
- for (NSInteger i = 0; i < _subGroupList.count; i++)
- {
- IMASubGroup *sg = [_subGroupList objectAtIndex:i];
- NSInteger index = [sg.friends indexOfObject:user];
- if (index >= 0 && index < sg.friends.count)
- {
- IMAUser *u = [sg.friends objectAtIndex:index];
- u.remark = remark;
-
- // 更新
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_FriendInfoChanged];
- item.subGroup = sg;
- item.user = u;
- item.index = index;
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- break;
- }
- }
-
- // 有可能不在好友里面
- [[IMAPlatform sharedInstance].conversationMgr updateConversationWith:user];
- }
- - (void)onLoadDefaultSubGroup:(IMASubGroup *)group
- {
- NSString * log = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log];
- // todo-todo-
- [_subGroupList insertObject:group atIndex:0];
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_LoadDefaultFriends];
- item.subGroup = group;
- item.index = 0;
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- NSString * log1 = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
- [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
- }
- - (void)onAddToBlackList:(IMAUser *)user
- {
- if (!_blackList)
- {
- _blackList = [[CLSafeMutableArray alloc] init];
- }
-
- [_blackList addObject:user];
-
-
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_BlackListAddIn];
- item.index = _blackList.count - 1;
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- }
- - (void)onRemoveOutBlackList:(IMAUser *)user
- {
- NSInteger idx = [_blackList indexOfObject:user];
- if (idx >= 0 && idx < _blackList.count)
- {
- [_blackList removeObjectAtIndex:idx];
-
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_BlackListMoveOut];
- item.index = idx;
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- }
- }
- - (void)onMove:(IMAUser *)user from:(IMASubGroup *)fromG to:(IMASubGroup *)toG
- {
- IMAUser *u = [self isContainUser:user];
-
- if (u)
- {
- NSInteger fi = [_subGroupList indexOfObject:fromG];
- NSInteger ti = [_subGroupList indexOfObject:toG];
- NSInteger count = [_subGroupList count];
- if (fi >= 0 && fi < count && ti >= 0 && ti < count)
- {
- IMASubGroup *sg = [_subGroupList objectAtIndex:fi];
- NSInteger index = [sg.friends indexOfObject:user];
- [sg.friends removeObject:u];
-
- // 更新
- IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_MoveFriend];
- item.subGroup = sg;
- item.user = u;
- item.index = index;
-
- IMASubGroup *tg = [_subGroupList objectAtIndex:ti];
- [tg.friends addObject:u];
-
- NSInteger tindex = [tg.friends indexOfObject:u];
-
- IMAContactChangedNotifyItem *titem = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddFriend];
- titem.subGroup = tg;
- titem.user = u;
- titem.index = tindex;
-
- item.toItem = titem;
-
- if (_contactChangedCompletion)
- {
- _contactChangedCompletion(item);
- }
- [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
- }
- else
- {
- DebugLog(@"不存在的分组");
- }
- }
- else
- {
- DebugLog(@"不存在的User");
- }
- }
- - (void)onAddGroup:(IMAGroup *)group
- {
- if (group)
- {
- [_groupList addObject:group];
- }
- }
- @end
|