123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- //
- // 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"
- @interface GroupTableController () <CLLocationManagerDelegate>
- @property (nonatomic, strong) NSMutableArray *groupArr;
- @property(strong, nonatomic) CLLocationManager *locationManager;
- @end
- @implementation GroupTableController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.locationManager = [[CLLocationManager alloc] init];
- self.locationManager.delegate = self;
- self.locationManager.distanceFilter = 1.0;
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
-
- if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
- {
- [self.locationManager requestAlwaysAuthorization]; // 永久授权
- [self.locationManager requestWhenInUseAuthorization]; //使用中授权
- }
-
- [self.locationManager startUpdatingLocation];
-
- 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:@""])
- {
- if([user.city isEqualToString:user.district])
- title = user.city;
- else
- title = [NSString stringWithFormat:@"%@%@", user.city, user.district];
-
- if(title.length > 6)
- {
- title = [NSString stringWithFormat:@"%@%@", [title substringWithRange:NSMakeRange(0, 6)], @"..."];
- }
- }
-
- UIButton *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(user.districtRoomId && user.firstTimeJoin == YES)
- {
- user.firstTimeJoin = NO;
- [self startTeamChat:user.districtRoomId];
- }
- else
- {
- user.firstTimeJoin = NO;
- }
- }
- - (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]];
- [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;
- NSString *avatar = team.avatarUrl ? team.avatarUrl : @"";
- NSDictionary *teamData = @{@"cid":@"Group", @"name":name, @"desc":desc, @"sid":sid, @"avatar":avatar};
- [myTeams addObject:teamData];
- }
-
- 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];
- }
- }
- 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 {
-
- [self performSegueWithIdentifier:@"createGroup" sender:self];
-
- }
- - (void)gotoTeamChat:(NSString *)teamId{
-
- if([[NIMSDK sharedSDK].teamManager isMyTeam:teamId])
- {
- [self startTeamChat:teamId];
- }
- else
- {
- [[NIMSDK sharedSDK].teamManager applyToTeam:teamId message:@"" completion:^(NSError * _Nullable error, NIMTeamApplyStatus applyStatus) {
- NSLog(@"applyToTeam error %@", error);
- if(!error && applyStatus == NIMTeamApplyStatusAlreadyInTeam)
- {
- [self startTeamChat:teamId];
- }
- }];
- }
- }
- - (void)startTeamChat:(NSString *)teamId
- {
- NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
- NTESSessionViewController *vc = [[NTESSessionViewController alloc] initWithSession:session];
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)relocaltion{
- [self performSegueWithIdentifier:@"cityPicker" sender:self];
- }
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- [self.locationManager stopUpdatingLocation];
-
- NSLog(@"经度:%f", newLocation.coordinate.longitude);
- NSLog(@"纬度:%f", newLocation.coordinate.latitude);
- NSLog(@"速度:%f 米/秒", newLocation.speed);
- CLGeocoder * geocoder = [[CLGeocoder alloc] init];
- [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
-
- NSDictionary *locationInfo = [[NSDictionary alloc]init];
- for (CLPlacemark * placemark in placemarks) {
- locationInfo = [placemark addressDictionary];
- }
- NSLog(@"%@",locationInfo);
- }];
-
-
- User *user = [User sharedInfo];
- if(user.latitude != newLocation.coordinate.latitude || user.longitude != newLocation.coordinate.longitude)
- {
- user.longitude = newLocation.coordinate.longitude;
- user.latitude = newLocation.coordinate.latitude;
-
- [self coordinate2Location];
- }
- }
- - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
- {
- NSLog(@"localtion error %@", error);
- }
- - (void)coordinate2Location
- {
- User *user = [User sharedInfo];
-
- NSDictionary *parameters = @{@"id":[NSString stringWithFormat:@"%d", user.userId],
- @"longitude":[NSString stringWithFormat:@"%F", user.longitude],
- @"latitude":[NSString stringWithFormat:@"%F", user.latitude]
- };
-
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=coordinate_2_location";
- //请求的managers
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer new];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
-
- __weak typeof(self) wself = self;
-
- NSLog(@"%@", parameters);
-
- //请求的方式:POST
- [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
-
- [SVProgressHUD dismiss];
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
- NSDictionary * dic = responseObject;
- int code = [[dic valueForKey:@"c"] intValue];
- NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
-
- NSLog(@"Json解析结果 = %d %@", code, data);
-
- if(code == 0 && ([user.provinceRoomId isEqualToString:@""] || user.provinceRoomId == nil))
- {
- if([data objectForKey:@"province"])
- user.province = [data objectForKey:@"province"];
- if([data objectForKey:@"city"])
- user.city = [data objectForKey:@"city"];
- if([data objectForKey:@"district"])
- user.district = [data objectForKey:@"district"];
-
- if([data objectForKey:@"province_room_id"])
- user.provinceRoomId = [data objectForKey:@"province_room_id"];
- if([data objectForKey:@"city_room_id"])
- user.cityRoomId = [data objectForKey:@"city_room_id"];
- if([data objectForKey:@"district_room_id"])
- user.districtRoomId = [data objectForKey:@"district_room_id"];
-
- [wself setDataList];
- [wself.tableView reloadData];
- }
-
- } failure:^(NSURLSessionDataTask *task, NSError * error) {
- [SVProgressHUD dismiss];
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- }];
- }
- @end
|