GroupTableController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //
  2. // GroupTableControllerTableViewController.m
  3. // whosay
  4. //
  5. // Created by Fenix Wang on 2017/5/22.
  6. // Copyright © 2017年 DashGame. All rights reserved.
  7. //
  8. #import "GroupTableController.h"
  9. #import "NIMSDK/NIMSDK.h"
  10. #import "User.h"
  11. #import "GroupTableViewCell.h"
  12. #import "NIMSDK/NIMSDK.h"
  13. #import "NTESSessionViewController.h"
  14. #import "SVProgressHUD.h"
  15. #import <CoreLocation/CoreLocation.h>
  16. #import "AFNetworking/AFNetworking.h"
  17. #import "GroupMoreTableViewController.h"
  18. #import "UIAlertView+NTESBlock.h"
  19. #import "HttpRequest.h"
  20. #import "UIView+Toast.h"
  21. @interface GroupTableController () <CLLocationManagerDelegate>
  22. @property (nonatomic, strong) UIButton *localBtn;
  23. @property (nonatomic, strong) NSMutableArray *groupArr;
  24. @end
  25. @implementation GroupTableController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. NSLog(@"check user data is saved %@", [User sharedInfo].mobile);
  29. }
  30. - (void)didReceiveMemoryWarning {
  31. [super didReceiveMemoryWarning];
  32. // Dispose of any resources that can be recreated.
  33. }
  34. -(void) viewWillAppear:(BOOL)animated{
  35. [super viewWillAppear:animated];
  36. NSLog(@"viewWillAppear %@", [User sharedInfo].mobile);
  37. UINavigationItem *navItem = self.parentViewController.navigationItem;
  38. navItem.title = @"谁说";
  39. User *user = [User sharedInfo];
  40. NSString *title = @"请重试定位";
  41. if(![user.district isEqualToString:@""])
  42. {
  43. title = user.district;
  44. if(title.length > 6)
  45. {
  46. title = [NSString stringWithFormat:@"%@%@", [title substringWithRange:NSMakeRange(0, 6)], @"..."];
  47. }
  48. }
  49. _localBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  50. [_localBtn setTitle:title forState:UIControlStateNormal];
  51. [_localBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  52. [_localBtn addTarget:self action:@selector(relocaltion) forControlEvents:UIControlEventTouchUpInside];
  53. [_localBtn setImage:[UIImage imageNamed:@"交易帖_03 (4)"] forState:UIControlStateNormal];
  54. _localBtn.imageEdgeInsets = UIEdgeInsetsMake(2, -5, 2, 5);
  55. _localBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
  56. [_localBtn sizeToFit];
  57. UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  58. spacer.width = -5;
  59. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_localBtn];
  60. [navItem setLeftBarButtonItems:@[spacer, leftItem]];
  61. UIButton *createBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  62. [createBtn setTitle:@"创建" forState:UIControlStateNormal];
  63. [createBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  64. [createBtn addTarget:self action:@selector(createGroup:) forControlEvents:UIControlEventTouchUpInside];
  65. [createBtn sizeToFit];
  66. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:createBtn];
  67. [navItem setRightBarButtonItems:@[rightItem]];
  68. if(!self.groupArr)
  69. [self setDataList];
  70. if([self checkNick])
  71. {
  72. [self checkAutoJoin];
  73. }
  74. }
  75. - (bool)checkNick{
  76. NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:[NIMSDK sharedSDK].loginManager.currentAccount];
  77. if(nimUser.userInfo.nickName && nimUser.userInfo.nickName.length)
  78. {
  79. return true;
  80. }
  81. __weak typeof(self) wself = self;
  82. UIAlertView *nickAlert = [[UIAlertView alloc] initWithTitle:nil message:@"设置昵称" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
  83. nickAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
  84. [nickAlert showAlertWithCompletionHandler:^(NSInteger optIndex) {
  85. NSString *nick = [nickAlert textFieldAtIndex:0].text;
  86. if(nick.length > 0)
  87. {
  88. [SVProgressHUD show];
  89. [[HttpRequest shared] updateNick:nick success:^{
  90. [[NIMSDK sharedSDK].userManager updateMyUserInfo:@{@(NIMUserInfoUpdateTagNick) : nick} completion:^(NSError *error) {
  91. [SVProgressHUD dismiss];
  92. if (!error) {
  93. [wself.view makeToast:@"昵称设置成功"
  94. duration:2
  95. position:CSToastPositionBottom];
  96. [wself checkAutoJoin];
  97. }else{
  98. [wself.view makeToast:@"昵称设置失败,请重试"
  99. duration:2
  100. position:CSToastPositionBottom];
  101. [wself checkNick];
  102. }
  103. }];
  104. } failure:^{
  105. [wself checkNick];
  106. }];
  107. }
  108. else
  109. {
  110. [wself showNeedNickAlert];
  111. }
  112. }];
  113. return false;
  114. }
  115. - (void)checkAutoJoin{
  116. User *user = [User sharedInfo];
  117. if(user.districtRoomId && user.districtRoomId.length > 0 && user.firstTimeJoin == YES)
  118. {
  119. user.firstTimeJoin = NO;
  120. [self gotoTeamChat:user.districtRoomId animated:NO];
  121. }
  122. else
  123. {
  124. user.firstTimeJoin = NO;
  125. }
  126. }
  127. - (void)showNeedNickAlert{
  128. __weak typeof(self) wself = self;
  129. UIAlertView *nickAlert = [[UIAlertView alloc] initWithTitle:nil message:@"昵称不能为空" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
  130. [nickAlert showAlertWithCompletionHandler:^(NSInteger optIndex) {
  131. [wself checkNick];
  132. }];
  133. }
  134. - (void) viewDidAppear:(BOOL)animated{
  135. [super viewDidAppear:animated];
  136. NSLog(@"viewDidAppear %@", [User sharedInfo].mobile);
  137. if([User sharedInfo].newCreateTeamId > 0)
  138. {
  139. [SVProgressHUD dismiss];
  140. [self gotoTeamChat:[NSString stringWithFormat:@"%d", [User sharedInfo].newCreateTeamId] animated:YES];
  141. [User sharedInfo].newCreateTeamId = 0;
  142. }
  143. else
  144. {
  145. [self setDataList];
  146. [self.tableView reloadData];
  147. }
  148. }
  149. - (void) setDataList{
  150. NSMutableArray *myTeams = [[NSMutableArray alloc] init];
  151. User *user = [User sharedInfo];
  152. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.countryRoomId];
  153. NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
  154. NSDictionary *county = @{@"cid":@"Group", @"name":@"国家频道", @"desc":desc, @"sid":user.countryRoomId, @"avatar":@""};
  155. [myTeams addObject:county];
  156. if(![user.provinceRoomId isEqualToString:@""] && user.provinceRoomId != nil)
  157. {
  158. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.provinceRoomId];
  159. NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
  160. NSString *sid = user.provinceRoomId;
  161. [myTeams addObject:@{@"cid":@"Group", @"name":user.province, @"desc":desc, @"sid":sid, @"avatar":@""}];
  162. }
  163. if(![user.cityRoomId isEqualToString:@""] && user.cityRoomId != nil && ![user.cityRoomId isEqualToString:user.provinceRoomId])
  164. {
  165. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.cityRoomId];
  166. NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
  167. NSString *sid = user.cityRoomId;
  168. [myTeams addObject:@{@"cid":@"Group", @"name":user.city, @"desc":desc, @"sid":sid, @"avatar":@""}];
  169. }
  170. if(![user.districtRoomId isEqualToString:@""] && user.districtRoomId != nil && ![user.districtRoomId isEqualToString:user.cityRoomId])
  171. {
  172. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.districtRoomId];
  173. NSString *name = [NSString stringWithFormat:@"%@", user.district];
  174. NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
  175. NSString *sid = user.districtRoomId;
  176. [myTeams addObject:@{@"cid":@"Group", @"name":name, @"desc":desc, @"sid":sid, @"avatar":@""}];
  177. }
  178. NSDictionary *more = @{@"cid":@"More"};
  179. NSDictionary *split = @{@"cid":@"Split"};
  180. NSDictionary *near = @{@"cid":@"Near"};
  181. NSDictionary *act = @{@"cid":@"Activity"};
  182. [myTeams addObjectsFromArray:@[more, split, near, act]];
  183. NSArray<NIMTeam *> *allMyTeams = [[NIMSDK sharedSDK] teamManager].allMyTeams;
  184. for(int i = 0; i < allMyTeams.count; i++ )
  185. {
  186. NIMTeam *team = [allMyTeams objectAtIndex:i];
  187. NSString *name = team.teamName == nil ? @"" : team.teamName;
  188. NSString *desc = [NSString stringWithFormat:@"%ld人热聊中", (long)team.memberNumber];
  189. NSString *sid = team.teamId;
  190. NSDictionary *teamData = @{@"cid":@"Group", @"name":name, @"desc":desc, @"sid":sid, @"team":team};
  191. [myTeams addObject:teamData];
  192. if(i >= 10)
  193. break;
  194. }
  195. self.groupArr = myTeams;
  196. }
  197. #pragma mark - Table view data source
  198. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  199. return 1;
  200. }
  201. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  202. return self.groupArr.count;
  203. }
  204. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  205. NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
  206. NSString *cid = [cellData valueForKey:@"cid"];
  207. if([cid isEqualToString:@"Group"])
  208. return 80;
  209. else if([cid isEqualToString:@"More"])
  210. return 50;
  211. else if([cid isEqualToString:@"Split"])
  212. return 10;
  213. else if([cid isEqualToString:@"Near"])
  214. return 60;
  215. else if([cid isEqualToString:@"Activity"])
  216. return 40;
  217. return 0;
  218. }
  219. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  220. NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
  221. NSString *cid = [cellData valueForKey:@"cid"];
  222. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cid forIndexPath:indexPath];
  223. if([cid isEqualToString:@"Group"])
  224. {
  225. GroupTableViewCell *groupCell = (GroupTableViewCell *)cell;
  226. [groupCell setData:cellData];
  227. }
  228. return cell;
  229. }
  230. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  231. {
  232. NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
  233. NSString *cid = [cellData valueForKey:@"cid"];
  234. if([cid isEqualToString:@"Group"])
  235. {
  236. NSString *sid = [cellData objectForKey:@"sid"];
  237. if([sid isEqualToString:@""])
  238. {
  239. }
  240. else{
  241. [self gotoTeamChat:sid animated:YES];
  242. }
  243. }
  244. else if([cid isEqualToString:@"More"])
  245. {
  246. if([User sharedInfo].province != nil)
  247. {
  248. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  249. GroupMoreTableViewController *next = (GroupMoreTableViewController *)[board instantiateViewControllerWithIdentifier:@"GroupMore"];
  250. [next showAllDistrict];
  251. [self.navigationController pushViewController:next animated:YES];
  252. }
  253. }
  254. else if([cid isEqualToString:@"Near"])
  255. {
  256. if([User sharedInfo].longitude != 0)
  257. {
  258. UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  259. GroupMoreTableViewController *next = (GroupMoreTableViewController *)[board instantiateViewControllerWithIdentifier:@"GroupMore"];
  260. [next showNear:1];
  261. [self.navigationController pushViewController:next animated:YES];
  262. }
  263. }
  264. }
  265. /*
  266. // Override to support conditional editing of the table view.
  267. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  268. // Return NO if you do not want the specified item to be editable.
  269. return YES;
  270. }
  271. */
  272. /*
  273. // Override to support editing the table view.
  274. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  275. if (editingStyle == UITableViewCellEditingStyleDelete) {
  276. // Delete the row from the data source
  277. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  278. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  279. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  280. }
  281. }
  282. */
  283. /*
  284. // Override to support rearranging the table view.
  285. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  286. }
  287. */
  288. /*
  289. // Override to support conditional rearranging of the table view.
  290. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  291. // Return NO if you do not want the item to be re-orderable.
  292. return YES;
  293. }
  294. */
  295. /*
  296. #pragma mark - Navigation
  297. // In a storyboard-based application, you will often want to do a little preparation before navigation
  298. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  299. // Get the new view controller using [segue destinationViewController].
  300. // Pass the selected object to the new view controller.
  301. }
  302. */
  303. - (IBAction)createGroup:(id)sender {
  304. NSArray<NIMTeam *> *allMyTeams = [[NIMSDK sharedSDK].teamManager allMyTeams];
  305. int countTeamJoined = 0;
  306. for(int i=0; i<allMyTeams.count; i++)
  307. {
  308. NIMTeam *myTeam = [allMyTeams objectAtIndex:i];
  309. if(![[User sharedInfo] isSystemTeam:myTeam.teamId])
  310. {
  311. countTeamJoined++;
  312. }
  313. }
  314. if(countTeamJoined >= 6)
  315. {
  316. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"最多只允许创建或加入6个群" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
  317. [alertView show];
  318. return;
  319. }
  320. [self performSegueWithIdentifier:@"createGroup" sender:self];
  321. }
  322. - (void)gotoTeamChat:(NSString *)teamId animated:(BOOL)animated{
  323. if([[NIMSDK sharedSDK].teamManager isMyTeam:teamId])
  324. {
  325. [self startTeamChat:teamId animated:animated];
  326. }
  327. else
  328. {
  329. if([[User sharedInfo] isSystemTeam:teamId])
  330. {
  331. [[NIMSDK sharedSDK].teamManager applyToTeam:teamId message:@"" completion:^(NSError * _Nullable error, NIMTeamApplyStatus applyStatus) {
  332. NSLog(@"applyToTeam error %@", error);
  333. if(!error && applyStatus == NIMTeamApplyStatusAlreadyInTeam)
  334. {
  335. [self startTeamChat:teamId animated:animated];
  336. }
  337. }];
  338. }
  339. else
  340. {
  341. [[User sharedInfo] attempJoinTeam:teamId vc:self];
  342. }
  343. }
  344. }
  345. - (void)startTeamChat:(NSString *)teamId animated:(BOOL)animated
  346. {
  347. NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
  348. NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:session];
  349. [self.navigationController pushViewController:vc animated:animated];
  350. }
  351. - (void)relocaltion{
  352. [self performSegueWithIdentifier:@"cityPicker" sender:self];
  353. }
  354. @end