| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- //
- // GroupTableControllerTableViewController.m
- // whosay
- //
- // Created by Fenix Wang on 2017/5/22.
- // Copyright © 2017年 DashGame. All rights reserved.
- //
- #import "GroupTableController.h"
- #import "NIMSDK/NIMSDK.h"
- #import "User.h"
- #import "GroupTableViewCell.h"
- #import "NIMSDK/NIMSDK.h"
- #import "NTESSessionViewController.h"
- #import "SVProgressHUD.h"
- #import <CoreLocation/CoreLocation.h>
- #import "AFNetworking/AFNetworking.h"
- #import "GroupMoreTableViewController.h"
- #import "UIAlertView+NTESBlock.h"
- #import "HttpRequest.h"
- #import "UIView+Toast.h"
- @interface GroupTableController () <CLLocationManagerDelegate>
- @property (nonatomic, strong) UIButton *localBtn;
- @property (nonatomic, strong) NSMutableArray *groupArr;
- @end
- @implementation GroupTableController
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSLog(@"check user data is saved %@", [User sharedInfo].mobile);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- -(void) viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- NSLog(@"viewWillAppear %@", [User sharedInfo].mobile);
-
- UINavigationItem *navItem = self.parentViewController.navigationItem;
-
- navItem.title = @"谁说";
-
- User *user = [User sharedInfo];
- NSString *title = @"请重试定位";
- if(![user.district isEqualToString:@""])
- {
- title = user.district;
- if(title.length > 6)
- {
- title = [NSString stringWithFormat:@"%@%@", [title substringWithRange:NSMakeRange(0, 6)], @"..."];
- }
- }
-
- _localBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_localBtn setTitle:title forState:UIControlStateNormal];
- [_localBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [_localBtn addTarget:self action:@selector(relocaltion) forControlEvents:UIControlEventTouchUpInside];
- [_localBtn setImage:[UIImage imageNamed:@"交易帖_03 (4)"] forState:UIControlStateNormal];
- _localBtn.imageEdgeInsets = UIEdgeInsetsMake(2, -5, 2, 5);
- _localBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
- [_localBtn sizeToFit];
-
- UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
- spacer.width = -5;
-
- UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_localBtn];
- [navItem setLeftBarButtonItems:@[spacer, leftItem]];
-
- UIButton *createBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [createBtn setTitle:@"创建" forState:UIControlStateNormal];
- [createBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [createBtn addTarget:self action:@selector(createGroup:) forControlEvents:UIControlEventTouchUpInside];
- [createBtn sizeToFit];
-
- UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:createBtn];
- [navItem setRightBarButtonItems:@[rightItem]];
-
- if(!self.groupArr)
- [self setDataList];
-
- if([self checkNick])
- {
- [self checkAutoJoin];
- }
- }
- - (bool)checkNick{
-
- NIMUser *nimUser = [[NIMSDK sharedSDK].userManager userInfo:[NIMSDK sharedSDK].loginManager.currentAccount];
- if(nimUser.userInfo.nickName && nimUser.userInfo.nickName.length)
- {
- return true;
- }
-
- __weak typeof(self) wself = self;
- UIAlertView *nickAlert = [[UIAlertView alloc] initWithTitle:nil message:@"设置昵称" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
- nickAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
- [nickAlert showAlertWithCompletionHandler:^(NSInteger optIndex) {
- NSString *nick = [nickAlert textFieldAtIndex:0].text;
- if(nick.length > 0)
- {
- [SVProgressHUD show];
- [[HttpRequest shared] updateNick:nick success:^{
- [[NIMSDK sharedSDK].userManager updateMyUserInfo:@{@(NIMUserInfoUpdateTagNick) : nick} completion:^(NSError *error) {
- [SVProgressHUD dismiss];
- if (!error) {
- [wself.view makeToast:@"昵称设置成功"
- duration:2
- position:CSToastPositionBottom];
- [wself checkAutoJoin];
- }else{
- [wself.view makeToast:@"昵称设置失败,请重试"
- duration:2
- position:CSToastPositionBottom];
- [wself checkNick];
- }
- }];
- } failure:^{
- [wself checkNick];
- }];
- }
- else
- {
- [wself showNeedNickAlert];
- }
- }];
-
- return false;
- }
- - (void)checkAutoJoin{
- User *user = [User sharedInfo];
- if(user.districtRoomId && user.districtRoomId.length > 0 && user.firstTimeJoin == YES)
- {
- user.firstTimeJoin = NO;
- [self gotoTeamChat:user.districtRoomId animated:NO];
- }
- else
- {
- user.firstTimeJoin = NO;
- }
- }
- - (void)showNeedNickAlert{
- __weak typeof(self) wself = self;
- UIAlertView *nickAlert = [[UIAlertView alloc] initWithTitle:nil message:@"昵称不能为空" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
- [nickAlert showAlertWithCompletionHandler:^(NSInteger optIndex) {
- [wself checkNick];
- }];
- }
- - (void) viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
-
- NSLog(@"viewDidAppear %@", [User sharedInfo].mobile);
- if([User sharedInfo].newCreateTeamId > 0)
- {
- [SVProgressHUD dismiss];
- [self gotoTeamChat:[NSString stringWithFormat:@"%d", [User sharedInfo].newCreateTeamId] animated:YES];
- [User sharedInfo].newCreateTeamId = 0;
- }
- else
- {
- [self setDataList];
- [self.tableView reloadData];
- }
- }
- - (void) setDataList{
-
- NSMutableArray *myTeams = [[NSMutableArray alloc] init];
- User *user = [User sharedInfo];
-
- NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.countryRoomId];
- NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
- NSDictionary *county = @{@"cid":@"Group", @"name":@"国家频道", @"desc":desc, @"sid":user.countryRoomId, @"avatar":@""};
- [myTeams addObject:county];
-
- if(![user.provinceRoomId isEqualToString:@""] && user.provinceRoomId != nil)
- {
- NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.provinceRoomId];
- NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
- NSString *sid = user.provinceRoomId;
- [myTeams addObject:@{@"cid":@"Group", @"name":user.province, @"desc":desc, @"sid":sid, @"avatar":@""}];
- }
-
- if(![user.cityRoomId isEqualToString:@""] && user.cityRoomId != nil && ![user.cityRoomId isEqualToString:user.provinceRoomId])
- {
- NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.cityRoomId];
- NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
- NSString *sid = user.cityRoomId;
- [myTeams addObject:@{@"cid":@"Group", @"name":user.city, @"desc":desc, @"sid":sid, @"avatar":@""}];
- }
-
- if(![user.districtRoomId isEqualToString:@""] && user.districtRoomId != nil && ![user.districtRoomId isEqualToString:user.cityRoomId])
- {
- NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:user.districtRoomId];
- NSString *name = [NSString stringWithFormat:@"%@", user.district];
- NSString *desc = [NSString stringWithFormat:@"%ld%@", (long)team.memberNumber, @"人热聊中"];
- NSString *sid = user.districtRoomId;
- [myTeams addObject:@{@"cid":@"Group", @"name":name, @"desc":desc, @"sid":sid, @"avatar":@""}];
- }
-
-
- NSDictionary *more = @{@"cid":@"More"};
- NSDictionary *split = @{@"cid":@"Split"};
- NSDictionary *near = @{@"cid":@"Near"};
- NSDictionary *act = @{@"cid":@"Activity"};
-
- [myTeams addObjectsFromArray:@[more, split, near, act]];
-
- NSArray<NIMTeam *> *allMyTeams = [[NIMSDK sharedSDK] teamManager].allMyTeams;
- for(int i = 0; i < allMyTeams.count; i++ )
- {
- NIMTeam *team = [allMyTeams objectAtIndex:i];
- NSString *name = team.teamName == nil ? @"" : team.teamName;
- NSString *desc = [NSString stringWithFormat:@"%ld人热聊中", (long)team.memberNumber];
- NSString *sid = team.teamId;
- NSDictionary *teamData = @{@"cid":@"Group", @"name":name, @"desc":desc, @"sid":sid, @"team":team};
- [myTeams addObject:teamData];
-
- if(i >= 10)
- break;
- }
-
- self.groupArr = myTeams;
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.groupArr.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
- NSString *cid = [cellData valueForKey:@"cid"];
-
- if([cid isEqualToString:@"Group"])
- return 80;
- else if([cid isEqualToString:@"More"])
- return 50;
- else if([cid isEqualToString:@"Split"])
- return 10;
- else if([cid isEqualToString:@"Near"])
- return 60;
- else if([cid isEqualToString:@"Activity"])
- return 40;
- return 0;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
- NSString *cid = [cellData valueForKey:@"cid"];
-
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cid forIndexPath:indexPath];
-
- if([cid isEqualToString:@"Group"])
- {
- GroupTableViewCell *groupCell = (GroupTableViewCell *)cell;
- [groupCell setData:cellData];
- }
-
- return cell;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSDictionary *cellData = [self.groupArr objectAtIndex:indexPath.row];
- NSString *cid = [cellData valueForKey:@"cid"];
- if([cid isEqualToString:@"Group"])
- {
- NSString *sid = [cellData objectForKey:@"sid"];
- if([sid isEqualToString:@""])
- {
-
- }
- else{
- [self gotoTeamChat:sid animated:YES];
- }
- }
- else if([cid isEqualToString:@"More"])
- {
- if([User sharedInfo].province != nil)
- {
- UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- GroupMoreTableViewController *next = (GroupMoreTableViewController *)[board instantiateViewControllerWithIdentifier:@"GroupMore"];
- [next showAllDistrict];
- [self.navigationController pushViewController:next animated:YES];
- }
- }
- else if([cid isEqualToString:@"Near"])
- {
- if([User sharedInfo].longitude != 0)
- {
- UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
- GroupMoreTableViewController *next = (GroupMoreTableViewController *)[board instantiateViewControllerWithIdentifier:@"GroupMore"];
- [next showNear:1];
- [self.navigationController pushViewController:next animated:YES];
- }
- }
- }
- /*
- // Override to support conditional editing of the table view.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the specified item to be editable.
- return YES;
- }
- */
- /*
- // Override to support editing the table view.
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- // Delete the row from the data source
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- } else if (editingStyle == UITableViewCellEditingStyleInsert) {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the item to be re-orderable.
- return YES;
- }
- */
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- - (IBAction)createGroup:(id)sender {
-
- NSArray<NIMTeam *> *allMyTeams = [[NIMSDK sharedSDK].teamManager allMyTeams];
- int countTeamJoined = 0;
- for(int i=0; i<allMyTeams.count; i++)
- {
- NIMTeam *myTeam = [allMyTeams objectAtIndex:i];
- if(![[User sharedInfo] isSystemTeam:myTeam.teamId])
- {
- countTeamJoined++;
- }
- }
-
- if(countTeamJoined >= 6)
- {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"最多只允许创建或加入6个群" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
- [alertView show];
- return;
- }
-
- [self performSegueWithIdentifier:@"createGroup" sender:self];
-
- }
- - (void)gotoTeamChat:(NSString *)teamId animated:(BOOL)animated{
-
- if([[NIMSDK sharedSDK].teamManager isMyTeam:teamId])
- {
- [self startTeamChat:teamId animated:animated];
- }
- else
- {
- if([[User sharedInfo] isSystemTeam:teamId])
- {
- [[NIMSDK sharedSDK].teamManager applyToTeam:teamId message:@"" completion:^(NSError * _Nullable error, NIMTeamApplyStatus applyStatus) {
- NSLog(@"applyToTeam error %@", error);
- if(!error && applyStatus == NIMTeamApplyStatusAlreadyInTeam)
- {
- [self startTeamChat:teamId animated:animated];
- }
- }];
- }
- else
- {
- [[User sharedInfo] attempJoinTeam:teamId vc:self];
- }
- }
- }
- - (void)startTeamChat:(NSString *)teamId animated:(BOOL)animated
- {
- NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
- NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:session];
- [self.navigationController pushViewController:vc animated:animated];
- }
- - (void)relocaltion{
- [self performSegueWithIdentifier:@"cityPicker" sender:self];
- }
- @end
|