NIMAdvancedTeamCardViewController.m 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //
  2. // NIMAdvancedTeamCardViewController.m
  3. // NIM
  4. //
  5. // Created by chris on 15/3/25.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NIMAdvancedTeamCardViewController.h"
  9. #import "NIMTeamCardRowItem.h"
  10. #import "UIView+NIM.h"
  11. #import "UIImage+NIMKit.h"
  12. #import "NIMKitColorButtonCell.h"
  13. #import "NIMAdvancedTeamMemberCell.h"
  14. #import "NIMKitDependency.h"
  15. #import "NIMTeamMemberCardViewController.h"
  16. #import "NIMCardMemberItem.h"
  17. #import "NIMContactSelectViewController.h"
  18. #import "NIMGroupedUsrInfo.h"
  19. #import "NIMTeamMemberListViewController.h"
  20. #import "NIMTeamAnnouncementListViewController.h"
  21. #import "NIMKitUtil.h"
  22. #import "NIMTeamSwitchTableViewCell.h"
  23. #import "NIMAvatarImageView.h"
  24. #import "NIMKitProgressHUD.h"
  25. #import "NIMTeamNotifyUpdateViewController.h"
  26. #pragma mark - Team Header View
  27. #define CardHeaderHeight 89
  28. @protocol NIMAdvancedTeamCardHeaderViewDelegate <NSObject>
  29. - (void)onTouchAvatar:(id)sender;
  30. @end
  31. @interface NIMAdvancedTeamCardHeaderView : UIView
  32. @property (nonatomic,strong) NIMAvatarImageView *avatar;
  33. @property (nonatomic,strong) UILabel *titleLabel;
  34. @property (nonatomic,strong) UILabel *numberLabel;
  35. @property (nonatomic,strong) UILabel *createTimeLabel;
  36. @property (nonatomic,strong) NIMTeam *team;
  37. @property (nonatomic,weak) id<NIMAdvancedTeamCardHeaderViewDelegate> delegate;
  38. - (void)refresh;
  39. @end
  40. @implementation NIMAdvancedTeamCardHeaderView
  41. - (instancetype)initWithTeam:(NIMTeam*)team{
  42. self = [super initWithFrame:CGRectZero];
  43. if (self) {
  44. _team = [[NIMSDK sharedSDK].teamManager teamById:team.teamId];
  45. _avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  46. [_avatar addTarget:self action:@selector(onTouchAvatar:) forControlEvents:UIControlEventTouchUpInside];
  47. _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
  48. _titleLabel.backgroundColor = [UIColor clearColor];
  49. _titleLabel.font = [UIFont systemFontOfSize:17.f];
  50. _titleLabel.textColor = NIMKit_UIColorFromRGB(0x333333);
  51. _numberLabel = [[UILabel alloc]initWithFrame:CGRectZero];
  52. _numberLabel.backgroundColor = [UIColor clearColor];
  53. _numberLabel.font = [UIFont systemFontOfSize:14.f];
  54. _numberLabel.textColor = NIMKit_UIColorFromRGB(0x999999);
  55. _createTimeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
  56. _createTimeLabel.backgroundColor = [UIColor clearColor];
  57. _createTimeLabel.font = [UIFont systemFontOfSize:14.f];
  58. _createTimeLabel.textColor = NIMKit_UIColorFromRGB(0x999999);
  59. [self addSubview:_avatar];
  60. [self addSubview:_titleLabel];
  61. [self addSubview:_numberLabel];
  62. [self addSubview:_createTimeLabel];
  63. self.backgroundColor = NIMKit_UIColorFromRGB(0xecf1f5);
  64. self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  65. [self refresh];
  66. }
  67. return self;
  68. }
  69. - (CGSize)sizeThatFits:(CGSize)size{
  70. return CGSizeMake(size.width, CardHeaderHeight);
  71. }
  72. - (void)refresh
  73. {
  74. // team 缓存可能发生改变,需要重新从 SDK 里拿一遍
  75. _team = [[NIMSDK sharedSDK].teamManager teamById:_team.teamId];
  76. NSURL *avatarUrl = _team.thumbAvatarUrl.length? [NSURL URLWithString:_team.thumbAvatarUrl] : nil;
  77. [_avatar nim_setImageWithURL:avatarUrl placeholderImage:[UIImage nim_imageInKit:@"avatar_team"]];
  78. }
  79. - (NSString*)formartCreateTime{
  80. NSTimeInterval timestamp = self.team.createTime;
  81. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  82. [dateFormatter setDateFormat:@"yyyy/MM/dd"];
  83. NSString *dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timestamp]];
  84. if (!dateString.length) {
  85. return @"未知时间创建";
  86. }
  87. return [NSString stringWithFormat:@"于%@创建",dateString];
  88. }
  89. - (void)onTouchAvatar:(id)sender
  90. {
  91. if ([self.delegate respondsToSelector:@selector(onTouchAvatar:)]) {
  92. [self.delegate onTouchAvatar:sender];
  93. }
  94. }
  95. #define AvatarLeft 20
  96. #define AvatarTop 25
  97. #define TitleAndAvatarSpacing 10
  98. #define NumberAndTimeSpacing 10
  99. #define MaxTitleLabelWidth 200
  100. - (void)layoutSubviews{
  101. [super layoutSubviews];
  102. _titleLabel.text = self.team.teamName;
  103. _numberLabel.text = self.team.teamId;
  104. _createTimeLabel.text = [self formartCreateTime];
  105. [_titleLabel sizeToFit];
  106. [_createTimeLabel sizeToFit];
  107. [_numberLabel sizeToFit];
  108. self.titleLabel.nim_width = self.titleLabel.nim_width > MaxTitleLabelWidth ? MaxTitleLabelWidth : self.titleLabel.nim_width;
  109. self.avatar.nim_left = AvatarLeft;
  110. self.avatar.nim_top = AvatarTop;
  111. self.titleLabel.nim_left = self.avatar.nim_right + TitleAndAvatarSpacing;
  112. self.titleLabel.nim_top = self.avatar.nim_top;
  113. self.numberLabel.nim_left = self.titleLabel.nim_left;
  114. self.numberLabel.nim_bottom = self.avatar.nim_bottom;
  115. self.createTimeLabel.nim_left = self.numberLabel.nim_right + NumberAndTimeSpacing;
  116. self.createTimeLabel.nim_bottom = self.numberLabel.nim_bottom;
  117. }
  118. @end
  119. #pragma mark - Card VC
  120. #define TableCellReuseId @"tableCell"
  121. #define TableButtonCellReuseId @"tableButtonCell"
  122. #define TableMemberCellReuseId @"tableMemberCell"
  123. #define TableSwitchReuseId @"tableSwitchCell"
  124. @interface NIMAdvancedTeamCardViewController ()<NIMAdvancedTeamMemberCellActionDelegate,NIMContactSelectDelegate,NIMTeamSwitchProtocol,NIMAdvancedTeamCardHeaderViewDelegate,NIMTeamManagerDelegate,UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
  125. UIAlertView *_updateTeamNameAlertView;
  126. UIAlertView *_updateTeamNickAlertView;
  127. UIAlertView *_updateTeamIntroAlertView;
  128. UIAlertView *_quitTeamAlertView;
  129. UIAlertView *_dismissTeamAlertView;
  130. UIActionSheet *_moreActionSheet;
  131. UIActionSheet *_authActionSheet;
  132. UIActionSheet *_inviteActionSheet;
  133. UIActionSheet *_beInviteActionSheet;
  134. UIActionSheet *_updateInfoActionSheet;
  135. UIActionSheet *_avatarActionSheet;
  136. }
  137. @property (nonatomic,strong) UITableView *tableView;
  138. @property(nonatomic,strong) NIMTeam *team;
  139. @property(nonatomic,strong) NIMTeamMember *myTeamInfo;
  140. @property(nonatomic,copy) NSArray *bodyData;
  141. @property(nonatomic,copy) NSArray *memberData;
  142. @end
  143. @implementation NIMAdvancedTeamCardViewController
  144. - (instancetype)initWithTeam:(NIMTeam *)team{
  145. self = [super initWithNibName:nil bundle:nil];
  146. if (self) {
  147. _team = team;
  148. }
  149. return self;
  150. }
  151. - (void)dealloc
  152. {
  153. [[NIMSDK sharedSDK].teamManager removeDelegate:self];
  154. }
  155. - (void)viewDidLoad {
  156. [super viewDidLoad];
  157. NIMAdvancedTeamCardHeaderView *headerView = [[NIMAdvancedTeamCardHeaderView alloc] initWithTeam:self.team];
  158. headerView.delegate = self;
  159. headerView.nim_size = [headerView sizeThatFits:self.view.nim_size];
  160. self.navigationItem.title = self.team.teamName;
  161. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  162. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  163. if (@available(iOS 15.0, *)) {
  164. self.tableView.sectionHeaderTopPadding = 0;
  165. }
  166. [self.view addSubview:self.tableView];
  167. self.tableView.tableHeaderView = headerView;
  168. self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
  169. self.tableView.delegate = self;
  170. self.tableView.dataSource = self;
  171. self.tableView.backgroundColor = NIMKit_UIColorFromRGB(0xecf1f5);
  172. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  173. __weak typeof(self) wself = self;
  174. [self requestData:^(NSError *error) {
  175. if (!error) {
  176. [wself reloadData];
  177. }
  178. }];
  179. [[NIMSDK sharedSDK].teamManager addDelegate:self];
  180. }
  181. - (void)viewWillAppear:(BOOL)animated {
  182. [super viewWillAppear:animated];
  183. [self reloadData];
  184. }
  185. - (void)reloadData{
  186. self.myTeamInfo = [[NIMSDK sharedSDK].teamManager teamMember:self.myTeamInfo.userId inTeam:self.myTeamInfo.teamId];
  187. [self buildBodyData];
  188. [self.tableView reloadData];
  189. NIMAdvancedTeamCardHeaderView *headerView = (NIMAdvancedTeamCardHeaderView*)self.tableView.tableHeaderView;
  190. headerView.titleLabel.text = self.team.teamName;;
  191. self.navigationItem.title = self.team.teamName;
  192. if (self.myTeamInfo.type == NIMTeamMemberTypeOwner) {
  193. UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(onMore:)];
  194. self.navigationItem.rightBarButtonItem = buttonItem;
  195. }else{
  196. self.navigationItem.rightBarButtonItem = nil;
  197. }
  198. }
  199. - (void)requestData:(void(^)(NSError *error)) handler{
  200. __weak typeof(self) wself = self;
  201. [[NIMSDK sharedSDK].teamManager fetchTeamMembers:self.team.teamId completion:^(NSError *error, NSArray *members) {
  202. if (!error) {
  203. for (NIMTeamMember *member in members) {
  204. if ([member.userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount]) {
  205. wself.myTeamInfo = member;
  206. break;
  207. }
  208. }
  209. wself.memberData = members;
  210. }else if(error.code == NIMRemoteErrorCodeTeamNotMember){
  211. [wself.view makeToast:@"你已经不在群里" duration:2
  212. position:CSToastPositionCenter];
  213. }else{
  214. [wself.view makeToast:[NSString stringWithFormat:@"拉好友失败 error: %zd",error.code] duration:2
  215. position:CSToastPositionCenter];
  216. }
  217. handler(error);
  218. }];
  219. }
  220. - (void)buildBodyData{
  221. BOOL canEdit = [NIMKitUtil canEditTeamInfo:self.myTeamInfo];
  222. BOOL isOwner = self.myTeamInfo.type == NIMTeamMemberTypeOwner;
  223. BOOL isManager = self.myTeamInfo.type == NIMTeamMemberTypeManager;
  224. NIMTeamCardRowItem *teamMember = [[NIMTeamCardRowItem alloc] init];
  225. teamMember.title = @"群成员";
  226. teamMember.rowHeight = 111.f;
  227. teamMember.action = @selector(enterMemberCard);
  228. teamMember.type = TeamCardRowItemTypeTeamMember;
  229. NIMTeamCardRowItem *teamName = [[NIMTeamCardRowItem alloc] init];
  230. teamName.title = @"群名称";
  231. teamName.subTitle = self.team.teamName;
  232. teamName.action = @selector(updateTeamName);
  233. teamName.rowHeight = 50.f;
  234. teamName.type = TeamCardRowItemTypeCommon;
  235. teamName.actionDisabled = !canEdit;
  236. NIMTeamCardRowItem *teamNick = [[NIMTeamCardRowItem alloc] init];
  237. teamNick.title = @"群昵称";
  238. teamNick.subTitle = self.myTeamInfo.nickname;
  239. teamNick.action = @selector(updateTeamNick);
  240. teamNick.rowHeight = 50.f;
  241. teamNick.type = TeamCardRowItemTypeCommon;
  242. NIMTeamCardRowItem *teamIntro = [[NIMTeamCardRowItem alloc] init];
  243. teamIntro.title = @"群介绍";
  244. teamIntro.subTitle = self.team.intro.length ? self.team.intro : (canEdit ? @"点击填写群介绍" : @"");
  245. teamIntro.action = @selector(updateTeamIntro);
  246. teamIntro.rowHeight = 50.f;
  247. teamIntro.type = TeamCardRowItemTypeCommon;
  248. teamIntro.actionDisabled = !canEdit;
  249. NIMTeamCardRowItem *teamAnnouncement = [[NIMTeamCardRowItem alloc] init];
  250. teamAnnouncement.title = @"群公告";
  251. teamAnnouncement.subTitle = @"点击查看群公告";//self.team.announcement.length ? self.team.announcement : (isManager ? @"点击填写群公告" : @"");
  252. teamAnnouncement.action = @selector(updateTeamAnnouncement);
  253. teamAnnouncement.rowHeight = 50.f;
  254. teamAnnouncement.type = TeamCardRowItemTypeCommon;
  255. NIMTeamCardRowItem *teamNotify = [[NIMTeamCardRowItem alloc] init];
  256. teamNotify.title = @"消息提醒";
  257. teamNotify.action = @selector(updateTeamNotify);
  258. teamNotify.rowHeight = 50.f;
  259. teamNotify.type = TeamCardRowItemTypeCommon;
  260. NIMTeamCardRowItem *itemQuit = [[NIMTeamCardRowItem alloc] init];
  261. itemQuit.title = @"退出高级群";
  262. itemQuit.action = @selector(quitTeam);
  263. itemQuit.rowHeight = 60.f;
  264. itemQuit.type = TeamCardRowItemTypeRedButton;
  265. NIMTeamCardRowItem *itemDismiss = [[NIMTeamCardRowItem alloc] init];
  266. itemDismiss.title = @"解散群聊";
  267. itemDismiss.action = @selector(dismissTeam);
  268. itemDismiss.rowHeight = 60.f;
  269. itemDismiss.type = TeamCardRowItemTypeRedButton;
  270. NIMTeamCardRowItem *itemAuth = [[NIMTeamCardRowItem alloc] init];
  271. itemAuth.title = @"身份验证";
  272. itemAuth.subTitle = [self joinModeText:self.team.joinMode];
  273. itemAuth.action = @selector(changeAuthMode);
  274. itemAuth.actionDisabled = !canEdit;
  275. itemAuth.rowHeight = 60.f;
  276. itemAuth.type = TeamCardRowItemTypeCommon;
  277. NIMTeamCardRowItem *itemInvite = [[NIMTeamCardRowItem alloc] init];
  278. itemInvite.title = @"邀请他人权限";
  279. itemInvite.subTitle = [self inviteModeText:self.team.inviteMode];
  280. itemInvite.action = @selector(changeInviteMode);
  281. itemInvite.actionDisabled = !canEdit;
  282. itemInvite.rowHeight = 60.f;
  283. itemInvite.type = TeamCardRowItemTypeCommon;
  284. NIMTeamCardRowItem *itemUpdateInfo = [[NIMTeamCardRowItem alloc] init];
  285. itemUpdateInfo.title = @"群资料修改权限";
  286. itemUpdateInfo.subTitle = [self updateInfoModeText:self.team.updateInfoMode];
  287. itemUpdateInfo.action = @selector(changeUpdateInfoMode);
  288. itemUpdateInfo.actionDisabled = !canEdit;
  289. itemUpdateInfo.rowHeight = 60.f;
  290. itemUpdateInfo.type = TeamCardRowItemTypeCommon;
  291. NIMTeamCardRowItem *itemBeInvite = [[NIMTeamCardRowItem alloc] init];
  292. itemBeInvite.title = @"被邀请人身份验证";
  293. itemBeInvite.subTitle = [self beInviteModeText:self.team.beInviteMode];
  294. itemBeInvite.action = @selector(changeBeInviteMode);
  295. itemBeInvite.actionDisabled = !canEdit;
  296. itemBeInvite.rowHeight = 60.f;
  297. itemBeInvite.type = TeamCardRowItemTypeCommon;
  298. if (isOwner) {
  299. self.bodyData = @[
  300. @[teamMember],
  301. @[teamName,teamNick,teamIntro,teamAnnouncement,teamNotify],
  302. @[itemAuth],
  303. @[itemInvite,itemUpdateInfo,itemBeInvite],
  304. @[itemDismiss],
  305. ];
  306. }else if(isManager){
  307. self.bodyData = @[
  308. @[teamMember],
  309. @[teamName,teamNick,teamIntro,teamAnnouncement,teamNotify],
  310. @[itemAuth],
  311. @[itemInvite,itemUpdateInfo,itemBeInvite],
  312. @[itemQuit],
  313. ];
  314. }else{
  315. self.bodyData = @[
  316. @[teamMember],
  317. @[teamName,teamNick,teamIntro,teamAnnouncement,teamNotify],
  318. @[itemQuit],
  319. ];
  320. }
  321. }
  322. - (id<NTESCardBodyData>)bodyDataAtIndexPath:(NSIndexPath*)indexpath{
  323. NSArray *sectionData = self.bodyData[indexpath.section];
  324. return sectionData[indexpath.row];
  325. }
  326. - (NSIndexPath *)cellIndexPathByTitle:(NSString *)title {
  327. __block NSInteger section = 0;
  328. __block NSInteger row = 0;
  329. [self.bodyData enumerateObjectsUsingBlock:^(NSArray *rows, NSUInteger s, BOOL *stop) {
  330. __block BOOL stopped = NO;
  331. [rows enumerateObjectsUsingBlock:^(NIMTeamCardRowItem *item, NSUInteger r, BOOL *stop) {
  332. if([item.title isEqualToString:title]) {
  333. section = s;
  334. row = r;
  335. *stop = YES;
  336. stopped = YES;
  337. }
  338. }];
  339. *stop = stopped;
  340. }];
  341. return [NSIndexPath indexPathForRow:row inSection:section];
  342. }
  343. #pragma mark - NIMTeamManagerDelegate
  344. - (void)onTeamMemberChanged:(NIMTeam *)team
  345. {
  346. __weak typeof(self) weakSelf = self;
  347. [self requestData:^(NSError *error) {
  348. [weakSelf reloadData];
  349. }];
  350. }
  351. #pragma mark - UITableViewDelegate
  352. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  353. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  354. id<NTESCardBodyData> bodyData = [self bodyDataAtIndexPath:indexPath];
  355. if ([bodyData respondsToSelector:@selector(actionDisabled)] && bodyData.actionDisabled) {
  356. return;
  357. }
  358. if ([bodyData respondsToSelector:@selector(action)]) {
  359. if (bodyData.action) {
  360. NIMKit_SuppressPerformSelectorLeakWarning([self performSelector:bodyData.action]);
  361. }
  362. }
  363. }
  364. #pragma mark - UITableViewDataSource
  365. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  366. id<NTESCardBodyData> bodyData = [self bodyDataAtIndexPath:indexPath];
  367. return bodyData.rowHeight;
  368. }
  369. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  370. return self.bodyData.count;
  371. }
  372. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  373. NSArray *sectionData = self.bodyData[section];
  374. return sectionData.count;
  375. }
  376. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  377. id<NTESCardBodyData> bodyData = [self bodyDataAtIndexPath:indexPath];
  378. UITableViewCell * cell;
  379. NIMKitTeamCardRowItemType type = bodyData.type;
  380. switch (type) {
  381. case TeamCardRowItemTypeCommon:
  382. cell = [self builidCommonCell:bodyData];
  383. break;
  384. case TeamCardRowItemTypeRedButton:
  385. cell = [self builidRedButtonCell:bodyData];
  386. break;
  387. case TeamCardRowItemTypeTeamMember:
  388. cell = [self builidTeamMemberCell:bodyData];
  389. break;
  390. case TeamCardRowItemTypeSwitch:
  391. cell = [self buildTeamSwitchCell:bodyData];
  392. break;
  393. default:
  394. break;
  395. }
  396. return cell;
  397. }
  398. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  399. if (section == 0) {
  400. return 0.0f;
  401. }
  402. return 20.f;
  403. }
  404. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  405. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  406. view.backgroundColor = NIMKit_UIColorFromRGB(0xecf1f5);
  407. return view;
  408. }
  409. - (UITableViewCell*)builidCommonCell:(id<NTESCardBodyData>) bodyData{
  410. UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:TableCellReuseId];
  411. if (!cell) {
  412. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableCellReuseId];
  413. CGFloat left = 15.f;
  414. UIView *sep = [[UIView alloc] initWithFrame:CGRectMake(left, cell.nim_height-1, cell.nim_width, 1.f)];
  415. sep.backgroundColor = NIMKit_UIColorFromRGB(0xebebeb);
  416. [cell addSubview:sep];
  417. sep.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  418. }
  419. cell.textLabel.text = bodyData.title;
  420. if ([bodyData respondsToSelector:@selector(subTitle)]) {
  421. cell.detailTextLabel.text = bodyData.subTitle;
  422. }
  423. if ([bodyData respondsToSelector:@selector(actionDisabled)] && bodyData.actionDisabled) {
  424. cell.accessoryType = UITableViewCellAccessoryNone;
  425. }else{
  426. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  427. }
  428. return cell;
  429. }
  430. - (UITableViewCell*)builidRedButtonCell:(id<NTESCardBodyData>) bodyData{
  431. NIMKitColorButtonCell * cell = [self.tableView dequeueReusableCellWithIdentifier:TableButtonCellReuseId];
  432. if (!cell) {
  433. cell = [[NIMKitColorButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableButtonCellReuseId];
  434. }
  435. cell.button.style = NIMKitColorButtonCellStyleRed;
  436. [cell.button setTitle:bodyData.title forState:UIControlStateNormal];
  437. return cell;
  438. }
  439. - (UITableViewCell*)builidTeamMemberCell:(id<NTESCardBodyData>) bodyData{
  440. NIMAdvancedTeamMemberCell * cell = [self.tableView dequeueReusableCellWithIdentifier:TableMemberCellReuseId];
  441. if (!cell) {
  442. cell = [[NIMAdvancedTeamMemberCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:TableMemberCellReuseId];
  443. cell.delegate = self;
  444. }
  445. [cell rereshWithTeam:self.team members:self.memberData width:self.tableView.nim_width];
  446. cell.textLabel.text = bodyData.title;
  447. cell.detailTextLabel.text = bodyData.subTitle;
  448. if ([bodyData respondsToSelector:@selector(actionDisabled)] && bodyData.actionDisabled) {
  449. cell.accessoryType = UITableViewCellAccessoryNone;
  450. }else{
  451. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  452. }
  453. return cell;
  454. }
  455. - (UITableViewCell *)buildTeamSwitchCell:(id<NTESCardBodyData>)bodyData
  456. {
  457. NIMTeamSwitchTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:TableSwitchReuseId];
  458. if (!cell) {
  459. cell = [[NIMTeamSwitchTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NIMTeamSwitchTableViewCell"];
  460. }
  461. cell.textLabel.text = bodyData.title;
  462. cell.switcher.on = bodyData.switchOn;
  463. cell.switchDelegate = self;
  464. return cell;
  465. }
  466. #pragma mark - Action
  467. - (void)onMore:(id)sender{
  468. _moreActionSheet = [[UIActionSheet alloc] initWithTitle:@"请操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"转让群",@"转让群并退出",nil];
  469. [_moreActionSheet showInView:self.view];
  470. }
  471. - (void)onTouchAvatar:(id)sender{
  472. if([NIMKitUtil canEditTeamInfo:self.myTeamInfo])
  473. {
  474. _avatarActionSheet = [[UIActionSheet alloc] initWithTitle:@"设置群头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册", nil];
  475. [_avatarActionSheet showInView:self.view];
  476. }
  477. }
  478. - (void)updateTeamName{
  479. _updateTeamNameAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"修改群名称" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  480. _updateTeamNameAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
  481. [_updateTeamNameAlertView show];
  482. }
  483. - (void)updateTeamNick{
  484. _updateTeamNickAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"修改群昵称" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  485. _updateTeamNickAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
  486. [_updateTeamNickAlertView show];
  487. }
  488. - (void)updateTeamNotify
  489. {
  490. NIMTeamNotifyUpdateViewController *vc = [[NIMTeamNotifyUpdateViewController alloc] initTeam:self.team];
  491. [self.navigationController pushViewController:vc animated:YES];
  492. }
  493. - (void)updateTeamIntro{
  494. _updateTeamIntroAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"修改群介绍" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  495. _updateTeamIntroAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
  496. [_updateTeamIntroAlertView show];
  497. }
  498. - (void)updateTeamAnnouncement{
  499. NIMTeamAnnouncementListViewController *vc = [[NIMTeamAnnouncementListViewController alloc] initWithNibName:nil bundle:nil];
  500. vc.team = self.team;
  501. vc.canCreateAnnouncement = [NIMKitUtil canEditTeamInfo:self.myTeamInfo];
  502. [self.navigationController pushViewController:vc animated:YES];
  503. }
  504. - (void)quitTeam{
  505. _quitTeamAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"确认退出群聊?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  506. [_quitTeamAlertView show];
  507. }
  508. - (void)dismissTeam{
  509. _dismissTeamAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"确认解散群聊?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  510. [_dismissTeamAlertView show];
  511. }
  512. - (void)changeAuthMode{
  513. _authActionSheet = [[UIActionSheet alloc] initWithTitle:@"更改验证方式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"允许任何人",@"需要验证",@"拒绝任何人", nil];
  514. [_authActionSheet showInView:self.view];
  515. }
  516. - (void)changeInviteMode{
  517. _inviteActionSheet = [[UIActionSheet alloc] initWithTitle:@"邀请他人入群" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"管理员邀请",@"所有人邀请", nil];
  518. [_inviteActionSheet showInView:self.view];
  519. }
  520. - (void)changeUpdateInfoMode{
  521. _updateInfoActionSheet = [[UIActionSheet alloc] initWithTitle:@"群资料修改权限" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"管理员修改",@"所有人修改", nil];
  522. [_updateInfoActionSheet showInView:self.view];
  523. }
  524. - (void)changeBeInviteMode{
  525. _beInviteActionSheet = [[UIActionSheet alloc] initWithTitle:@"被邀请人身份验证" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"需要验证",@"不需要验证", nil];
  526. [_beInviteActionSheet showInView:self.view];
  527. }
  528. - (NSString*)joinModeText:(NIMTeamJoinMode)mode{
  529. switch (mode) {
  530. case NIMTeamJoinModeNoAuth:
  531. return @"允许任何人";
  532. case NIMTeamJoinModeNeedAuth:
  533. return @"需要验证";
  534. case NIMTeamJoinModeRejectAll:
  535. return @"拒绝任何人";
  536. default:
  537. break;
  538. }
  539. }
  540. - (NSString*)inviteModeText:(NIMTeamInviteMode)mode{
  541. switch (mode) {
  542. case NIMTeamInviteModeManager:
  543. return @"管理员";
  544. case NIMTeamInviteModeAll:
  545. return @"所有人";
  546. default:
  547. return @"未知权限";
  548. }
  549. }
  550. - (NSString*)updateInfoModeText:(NIMTeamUpdateInfoMode)mode{
  551. switch (mode) {
  552. case NIMTeamUpdateInfoModeManager:
  553. return @"管理员";
  554. case NIMTeamUpdateInfoModeAll:
  555. return @"所有人";
  556. default:
  557. return @"未知权限";
  558. }
  559. }
  560. - (NSString*)beInviteModeText:(NIMTeamBeInviteMode)mode{
  561. switch (mode) {
  562. case NIMTeamBeInviteModeNeedAuth:
  563. return @"需要验证";
  564. case NIMTeamBeInviteModeNoAuth:
  565. return @"不需要验证";
  566. default:
  567. return @"未知";
  568. }
  569. }
  570. - (void)enterMemberCard{
  571. NIMTeamMemberListViewController *vc = [[NIMTeamMemberListViewController alloc] initTeam:self.team members:self.memberData];
  572. [self.navigationController pushViewController:vc animated:YES];
  573. }
  574. - (void)onStateChanged:(BOOL)on
  575. {
  576. __weak typeof(self) weakSelf = self;
  577. [[[NIMSDK sharedSDK] teamManager] updateNotifyState:on
  578. inTeam:[self.team teamId]
  579. completion:^(NSError *error) {
  580. if (error) {
  581. [weakSelf.view makeToast:[NSString stringWithFormat:@"修改失败 error:%zd",error.code]
  582. duration:2
  583. position:CSToastPositionCenter];
  584. }
  585. [weakSelf reloadData];
  586. }];
  587. }
  588. #pragma mark - NIMAdvancedTeamMemberCellActionDelegate
  589. - (void)didSelectAddOpeartor{
  590. NSMutableArray *users = [[NSMutableArray alloc] init];
  591. for (NIMTeamMember *member in self.memberData) {
  592. [users addObject:member.userId];
  593. }
  594. NSString *currentUserID = [[[NIMSDK sharedSDK] loginManager] currentAccount];
  595. [users addObject:currentUserID];
  596. NIMContactFriendSelectConfig *config = [[NIMContactFriendSelectConfig alloc] init];
  597. config.filterIds = users;
  598. config.needMutiSelected = YES;
  599. NIMContactSelectViewController *vc = [[NIMContactSelectViewController alloc] initWithConfig:config];
  600. vc.delegate = self;
  601. [vc show];
  602. }
  603. #pragma mark - ContactSelectDelegate
  604. - (void)didFinishedSelect:(NSArray *)selectedContacts{
  605. //modify by leo v1.0.7 云信新sdk
  606. /*
  607. if (!selectedContacts.count) {
  608. return;
  609. }
  610. NSString *postscript = @"邀请你加入群组";
  611. [[NIMSDK sharedSDK].teamManager addUsers:selectedContacts toTeam:self.team.teamId postscript:postscript completion:^(NSError *error, NSArray *members) {
  612. if (!error) {
  613. [self.view makeToast:@"邀请成功"
  614. duration:2
  615. position:CSToastPositionCenter];
  616. }else{
  617. [self.view makeToast:[NSString stringWithFormat:@"邀请失败 code:%zd",error.code]
  618. duration:2
  619. position:CSToastPositionCenter];
  620. }
  621. }];
  622. */
  623. }
  624. - (void)didCancelledSelect{
  625. }
  626. #pragma mark - UIAlertViewDelegate
  627. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  628. if (alertView == _updateTeamNameAlertView) {
  629. [self updateTeamNameAlert:buttonIndex];
  630. }
  631. else if (alertView == _updateTeamNickAlertView) {
  632. [self updateTeamNickAlert:buttonIndex];
  633. }
  634. else if (alertView == _updateTeamIntroAlertView) {
  635. [self updateTeamIntroAlert:buttonIndex];
  636. }
  637. else if (alertView == _quitTeamAlertView) {
  638. [self quitTeamAlert:buttonIndex];
  639. }
  640. else if (alertView == _dismissTeamAlertView) {
  641. [self dismissTeamAlert:buttonIndex];
  642. }
  643. }
  644. - (void)updateTeamNameAlert:(NSInteger)index{
  645. switch (index) {
  646. case 0://取消
  647. break;
  648. case 1:{
  649. NSString *name = [_updateTeamNameAlertView textFieldAtIndex:0].text;
  650. if (name.length) {
  651. [[NIMSDK sharedSDK].teamManager updateTeamName:name teamId:self.team.teamId completion:^(NSError *error) {
  652. if (!error) {
  653. self.team.teamName = name;
  654. [self.view makeToast:@"修改成功" duration:2
  655. position:CSToastPositionCenter];
  656. [self reloadData];
  657. }else{
  658. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code] duration:2
  659. position:CSToastPositionCenter];
  660. }
  661. }];
  662. }
  663. break;
  664. }
  665. default:
  666. break;
  667. }
  668. }
  669. - (void)updateTeamNickAlert:(NSInteger)index{
  670. switch (index) {
  671. case 0://取消
  672. break;
  673. case 1:{
  674. NSString *name = [_updateTeamNickAlertView textFieldAtIndex:0].text;
  675. NSString *currentUserId = [NIMSDK sharedSDK].loginManager.currentAccount;
  676. [[NIMSDK sharedSDK].teamManager updateUserNick:currentUserId newNick:name inTeam:self.team.teamId completion:^(NSError *error) {
  677. if (!error) {
  678. self.myTeamInfo.nickname = name;
  679. [self.view makeToast:@"修改成功"];
  680. [self reloadData];
  681. }else{
  682. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  683. }
  684. }];
  685. break;
  686. }
  687. default:
  688. break;
  689. }
  690. }
  691. - (void)updateTeamIntroAlert:(NSInteger)index{
  692. switch (index) {
  693. case 0://取消
  694. break;
  695. case 1:{
  696. NSString *intro = [_updateTeamIntroAlertView textFieldAtIndex:0].text;
  697. [[NIMSDK sharedSDK].teamManager updateTeamIntro:intro teamId:self.team.teamId completion:^(NSError *error) {
  698. if (!error) {
  699. self.team.intro = intro;
  700. [self.view makeToast:@"修改成功"];
  701. [self reloadData];
  702. }else{
  703. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  704. }
  705. }];
  706. break;
  707. }
  708. default:
  709. break;
  710. }
  711. }
  712. - (void)quitTeamAlert:(NSInteger)index{
  713. switch (index) {
  714. case 0://取消
  715. break;
  716. case 1:{
  717. [[NIMSDK sharedSDK].teamManager quitTeam:self.team.teamId completion:^(NSError *error) {
  718. if (!error) {
  719. [self.navigationController popToRootViewControllerAnimated:YES];
  720. }else{
  721. [self.view makeToast:[NSString stringWithFormat:@"退出失败 code:%zd",error.code]];
  722. }
  723. }];
  724. break;
  725. }
  726. default:
  727. break;
  728. }
  729. }
  730. - (void)dismissTeamAlert:(NSInteger)index{
  731. switch (index) {
  732. case 0://取消
  733. break;
  734. case 1:{
  735. [[NIMSDK sharedSDK].teamManager dismissTeam:self.team.teamId completion:^(NSError *error) {
  736. if (!error) {
  737. [self.navigationController popToRootViewControllerAnimated:YES];
  738. }else{
  739. [self.view makeToast:[NSString stringWithFormat:@"解散失败 code:%zd",error.code]];
  740. }
  741. }];
  742. break;
  743. }
  744. default:
  745. break;
  746. }
  747. }
  748. #pragma mark - UIActionSheetDelegate
  749. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)index{
  750. if (actionSheet == _moreActionSheet) {
  751. [self ontransferActionSheet:actionSheet index:index];
  752. }
  753. else if (actionSheet == _authActionSheet) {
  754. [self onAuthActionSheet:actionSheet index:index];
  755. }
  756. else if (actionSheet == _inviteActionSheet) {
  757. [self onInviteActionSheet:actionSheet index:index];
  758. }
  759. else if (actionSheet == _updateInfoActionSheet) {
  760. [self onUpdateInfoActionSheet:actionSheet index:index];
  761. }
  762. else if (actionSheet == _beInviteActionSheet)
  763. {
  764. [self onBeInviteActionSheet:actionSheet index:index];
  765. }
  766. else if (actionSheet == _avatarActionSheet)
  767. {
  768. [self onAvatarActionSheet:actionSheet index:index];
  769. }
  770. }
  771. - (void)onAvatarActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  772. {
  773. switch (index) {
  774. case 0:
  775. [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
  776. break;
  777. case 1:
  778. [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
  779. break;
  780. default:
  781. break;
  782. }
  783. }
  784. - (void)showImagePicker:(UIImagePickerControllerSourceType)type{
  785. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  786. picker.delegate = self;
  787. picker.sourceType = type;
  788. picker.allowsEditing = YES;
  789. [self presentViewController:picker animated:YES completion:nil];
  790. }
  791. - (void)ontransferActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  792. {
  793. BOOL isLeave = NO;
  794. switch (index) {
  795. case 0:{
  796. isLeave = NO;
  797. break;
  798. case 1:
  799. isLeave = YES;
  800. break;
  801. }
  802. default:
  803. return;
  804. break;
  805. }
  806. __weak typeof(self) wself = self;
  807. __block ContactSelectFinishBlock finishBlock = ^(NSArray * memeber){
  808. [[NIMSDK sharedSDK].teamManager transferManagerWithTeam:wself.team.teamId newOwnerId:memeber.firstObject isLeave:isLeave completion:^(NSError *error) {
  809. if (!error) {
  810. [wself.view makeToast:@"转移成功!" duration:2.0 position:CSToastPositionCenter];
  811. if (isLeave) {
  812. [wself.navigationController popToRootViewControllerAnimated:YES];
  813. }else{
  814. [wself reloadData];
  815. }
  816. }else{
  817. [wself.view makeToast:[NSString stringWithFormat:@"转移失败!code:%zd",error.code] duration:2.0 position:CSToastPositionCenter];
  818. }
  819. }];
  820. };
  821. NSString *currentUserID = [[[NIMSDK sharedSDK] loginManager] currentAccount];
  822. NIMContactTeamMemberSelectConfig *config = [[NIMContactTeamMemberSelectConfig alloc] init];
  823. config.teamId = self.team.teamId;
  824. config.filterIds = @[currentUserID];
  825. NIMContactSelectViewController *vc = [[NIMContactSelectViewController alloc] initWithConfig:config];
  826. vc.finshBlock = finishBlock;
  827. [vc show];
  828. }
  829. - (void)onAuthActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  830. {
  831. if (index == _authActionSheet.cancelButtonIndex) {
  832. return;
  833. }
  834. [[NIMSDK sharedSDK].teamManager updateTeamJoinMode:index teamId:self.team.teamId completion:^(NSError *error) {
  835. if (!error) {
  836. self.team.joinMode = index;
  837. [self.view makeToast:@"修改成功"];
  838. [self reloadData];
  839. }else{
  840. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  841. }
  842. }];
  843. }
  844. - (void)onInviteActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  845. {
  846. NIMTeamInviteMode mode;
  847. switch (index) {
  848. case 0:
  849. mode = NIMTeamInviteModeManager;
  850. break;
  851. case 1:
  852. mode = NIMTeamInviteModeAll;
  853. break;
  854. default:
  855. return;
  856. }
  857. [[NIMSDK sharedSDK].teamManager updateTeamInviteMode:mode teamId:self.team.teamId completion:^(NSError *error) {
  858. if (!error) {
  859. self.team.inviteMode = mode;
  860. [self.view makeToast:@"修改成功"];
  861. [self reloadData];
  862. }else{
  863. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  864. }
  865. }];
  866. }
  867. - (void)onUpdateInfoActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  868. {
  869. NIMTeamUpdateInfoMode mode;
  870. switch (index) {
  871. case 0:
  872. mode = NIMTeamUpdateInfoModeManager;
  873. break;
  874. case 1:
  875. mode = NIMTeamUpdateInfoModeAll;
  876. break;
  877. default:
  878. return;
  879. }
  880. [[NIMSDK sharedSDK].teamManager updateTeamUpdateInfoMode:mode teamId:self.team.teamId completion:^(NSError *error) {
  881. if (!error) {
  882. self.team.updateInfoMode = mode;
  883. [self.view makeToast:@"修改成功"];
  884. [self reloadData];
  885. }else{
  886. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  887. }
  888. }];
  889. }
  890. - (void)onBeInviteActionSheet:(UIActionSheet *)actionSheet index:(NSInteger)index
  891. {
  892. NIMTeamBeInviteMode mode;
  893. switch (index) {
  894. case 0:
  895. mode = NIMTeamBeInviteModeNeedAuth;
  896. break;
  897. case 1:
  898. mode = NIMTeamBeInviteModeNoAuth;
  899. break;
  900. default:
  901. return;
  902. }
  903. [[NIMSDK sharedSDK].teamManager updateTeamBeInviteMode:mode teamId:self.team.teamId completion:^(NSError *error) {
  904. if (!error) {
  905. self.team.beInviteMode = mode;
  906. [self.view makeToast:@"修改成功"];
  907. [self reloadData];
  908. }else{
  909. [self.view makeToast:[NSString stringWithFormat:@"修改失败 code:%zd",error.code]];
  910. }
  911. }];
  912. }
  913. #pragma mark - UIImagePickerControllerDelegate
  914. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  915. UIImage *image = info[UIImagePickerControllerEditedImage];
  916. [picker dismissViewControllerAnimated:YES completion:^{
  917. [self uploadImage:image];
  918. }];
  919. }
  920. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  921. [picker dismissViewControllerAnimated:YES completion:nil];
  922. }
  923. #pragma mark - Private
  924. - (void)uploadImage:(UIImage *)image{
  925. UIImage *imageForAvatarUpload = [image nim_imageForAvatarUpload];
  926. NSString *fileName = [[[[NSUUID UUID] UUIDString] lowercaseString] stringByAppendingPathExtension:@"jpg"];
  927. NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
  928. NSData *data = UIImageJPEGRepresentation(imageForAvatarUpload, 1.0);
  929. BOOL success = data && [data writeToFile:filePath atomically:YES];
  930. __weak typeof(self) wself = self;
  931. if (success) {
  932. [NIMKitProgressHUD show];
  933. [[NIMSDK sharedSDK].resourceManager upload:filePath scene:NIMNOSSceneTypeAvatar progress:nil completion:^(NSString *urlString, NSError *error) {
  934. [NIMKitProgressHUD dismiss];
  935. if (!error && wself) {
  936. [[NIMSDK sharedSDK].teamManager updateTeamAvatar:urlString teamId:wself.team.teamId completion:^(NSError *error) {
  937. if (!error) {
  938. wself.team.avatarUrl = urlString;
  939. [[SDWebImageManager sharedManager] saveImageToCache:imageForAvatarUpload forURL:[NSURL URLWithString:urlString]];
  940. NIMAdvancedTeamCardHeaderView *headerView = (NIMAdvancedTeamCardHeaderView *)wself.tableView.tableHeaderView;
  941. [headerView refresh];
  942. }else{
  943. [wself.view makeToast:@"设置头像失败,请重试"
  944. duration:2
  945. position:CSToastPositionCenter];
  946. }
  947. }];
  948. }else{
  949. [wself.view makeToast:@"图片上传失败,请重试"
  950. duration:2
  951. position:CSToastPositionCenter];
  952. }
  953. }];
  954. }else{
  955. [self.view makeToast:@"图片保存失败,请重试"
  956. duration:2
  957. position:CSToastPositionCenter];
  958. }
  959. }
  960. #pragma mark - 旋转处理 (iOS7)
  961. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  962. {
  963. NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  964. [self.tableView reloadRowsAtIndexPaths:@[reloadIndexPath] withRowAnimation:UITableViewRowAnimationNone];
  965. }
  966. #pragma mark - 旋转处理 (iOS8 or above)
  967. - (void)viewWillTransitionToSize:(CGSize)size
  968. withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  969. {
  970. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  971. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  972. NSIndexPath *reloadIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  973. [self.tableView reloadRowsAtIndexPaths:@[reloadIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  974. } completion:nil];
  975. }
  976. @end