NearbyVC.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // NearbyVC.m
  3. // YBVideo
  4. //
  5. // Created by YunBao on 2018/7/27.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "NearbyVC.h"
  9. #import "AFNetworking.h"
  10. #import "NearbyVideoModel.h"
  11. #import "NearbyCell.h"
  12. #import "YBGetVideoObj.h"
  13. #import "YBCitySelVC.h"
  14. #import "searchVC.h"
  15. @import CoreLocation;
  16. @interface NearbyVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,CLLocationManagerDelegate>
  17. {
  18. NSInteger _paging;
  19. NSString *_currentCity;
  20. CLLocationManager *_lbsManager;
  21. BOOL _isSelfLoc;
  22. }
  23. @property(nonatomic,strong)NSMutableArray *dataArray;
  24. @property(nonatomic,strong)NSArray *models;
  25. @property(nonatomic,strong)UICollectionView *collectionView;
  26. @property(nonatomic,strong)UIView *headerView;
  27. @property(nonatomic,strong)UILabel *headerTitle;
  28. @end
  29. @implementation NearbyVC
  30. - (void)viewWillAppear:(BOOL)animated{
  31. [super viewWillAppear:animated];
  32. [[PublicObj publicInstance]addCusDelayedRemove];
  33. if (_dataArray.count <= 0) {
  34. [self pullData];
  35. }
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. self.leftBtn.hidden = NO;
  40. [self.leftBtn setImage:[UIImage imageNamed:@"home_search"] forState:0];
  41. self.rightBtn.hidden = NO;
  42. [self.rightBtn setImage:[UIImage imageNamed:@"同城-定位"] forState:0];
  43. self.rightBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 6);
  44. self.rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -6, 0, 0);
  45. [self.rightBtn setTitleColor:UIColor.whiteColor forState:0];
  46. self.titleL.text = YZMsg(@"同城");
  47. self.navigationController.interactivePopGestureRecognizer.delegate = (id) self;
  48. self.view.backgroundColor = Normal_Color;
  49. _currentCity = @"";
  50. _isSelfLoc = NO;
  51. YBWeakSelf;
  52. [[RKLBSManager shareManager] startLocation];
  53. [[RKLBSManager shareManager] locationComplete:^(NSArray *placemarks, NSArray<NSString *> *pcaArray) {
  54. if ([PublicObj checkNull:_currentCity]) {
  55. _currentCity = [PublicObj checkNull:minstr(pcaArray[1])]?@"":pcaArray[1];
  56. if ([_currentCity hasSuffix:@"市"]) {
  57. _currentCity = [_currentCity substringToIndex:_currentCity.length-1];
  58. }
  59. [weakSelf changeName:_currentCity];
  60. [weakSelf pullData];
  61. }
  62. }];
  63. self.dataArray = [NSMutableArray array];
  64. self.models = [NSArray array];
  65. _paging = 1;
  66. // _url = [NSString stringWithFormat:@"Video.getNearby&uid=%@&lng=%@&lat=%@",[Config getOwnID],[cityDefault getLocationLng],[cityDefault getLocationLat]];
  67. _url = @"Video.getNearby";
  68. [self.view addSubview:self.collectionView];
  69. }
  70. - (void)clickNaviLeftBtn{
  71. searchVC *search = [[searchVC alloc]init];
  72. search.searchFrom = SearchFrom_City;
  73. search.curCity = _currentCity;
  74. [[YBBaseAppDelegate sharedAppDelegate]pushViewController:search animated:YES];
  75. }
  76. - (void)clickNaviRightBtn{
  77. [self clickChangeBtn];
  78. }
  79. - (NSArray *)models {
  80. NSMutableArray *m_array = [NSMutableArray array];
  81. for (NSDictionary *dic in _dataArray) {
  82. NearbyVideoModel *model = [NearbyVideoModel modelWithDic:dic];
  83. [m_array addObject:model];
  84. }
  85. _models = m_array;
  86. return _models;
  87. }
  88. -(void)pullData {
  89. _collectionView.userInteractionEnabled = NO;
  90. //NSString *url = [NSString stringWithFormat:@"%@&p=%ld&city=%@",_url,(long)_paging,_currentCity];
  91. NSDictionary *postDic = @{
  92. @"lng":[cityDefault getLocationLng],
  93. @"lat":[cityDefault getLocationLat],
  94. @"p":@(_paging),
  95. @"city":_currentCity,
  96. };
  97. [YBNetworking postWithUrl:_url Dic:postDic Suc:^(int code, id info, NSString *msg) {
  98. [_collectionView.mj_header endRefreshing];
  99. [_collectionView.mj_footer endRefreshing];
  100. _collectionView.userInteractionEnabled = YES;
  101. if (code == 0) {
  102. NSArray *infoA = [NSArray arrayWithArray:info];
  103. if (_paging == 1) {
  104. [_dataArray removeAllObjects];
  105. }
  106. if (infoA.count <= 0) {
  107. [_collectionView.mj_footer endRefreshingWithNoMoreData];
  108. }else{
  109. [_dataArray addObjectsFromArray:infoA];
  110. }
  111. if (_dataArray.count > 0) {
  112. [PublicView hiddenTextNoData:_collectionView];
  113. }else{
  114. [PublicView showTextNoData:_collectionView text1:@"" text2:YZMsg(@"暂无附近视频哦~") centerY:0.8];
  115. }
  116. [_collectionView reloadData];
  117. }else{
  118. if (_dataArray) {
  119. [_dataArray removeAllObjects];
  120. }
  121. [_collectionView reloadData];
  122. [PublicView showTextNoData:_collectionView text1:@"" text2:YZMsg(@"暂无附近视频哦~") centerY:0.8];
  123. }
  124. } Fail:^(id fail) {
  125. _collectionView.userInteractionEnabled = YES;
  126. _collectionView.userInteractionEnabled = YES;
  127. if (_dataArray) {
  128. [_dataArray removeAllObjects];
  129. }
  130. [_collectionView reloadData];
  131. [PublicView showTextNoData:_collectionView text1:@"" text2:YZMsg(@"暂无附近视频哦~") centerY:0.8];
  132. [_collectionView.mj_header endRefreshing];
  133. [_collectionView.mj_footer endRefreshing];
  134. }];
  135. }
  136. #pragma mark - CollectionView 代理
  137. /*
  138. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;{
  139. if (kind == UICollectionElementKindSectionHeader) {
  140. UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NearbyHeader" forIndexPath:indexPath];
  141. [reusableView addSubview:self.headerView];
  142. return reusableView;
  143. }
  144. return nil;
  145. }
  146. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  147. return CGSizeMake(_window_width, 40);
  148. }
  149. */
  150. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  151. return 2;
  152. }
  153. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  154. return 1;
  155. }
  156. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  157. return self.models.count;
  158. }
  159. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  160. NearbyCell *cell = (NearbyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"NearbyCell" forIndexPath:indexPath];
  161. NearbyVideoModel *model = _models[indexPath.row];
  162. cell.isSelfLoc = _isSelfLoc;
  163. cell.model = model;
  164. return cell;
  165. }
  166. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  167. NSString *vidoeID = minstr([_dataArray[indexPath.row] valueForKey:@"id"]);
  168. [YBGetVideoObj lookManeger].fromWhere = @"NearbyVC";
  169. [YBGetVideoObj lookManeger].videoID = vidoeID;
  170. [YBGetVideoObj lookManeger].playIndex = (int)indexPath.row;
  171. [YBGetVideoObj lookManeger].videoList = [_dataArray mutableCopy];
  172. [YBGetVideoObj lookManeger].paging = (int)_paging;
  173. [YBGetVideoObj lookManeger].baseUrl = _url;
  174. [[YBGetVideoObj lookManeger]goLookVC];
  175. }
  176. #pragma mark - set/get
  177. - (UICollectionView *)collectionView {
  178. if (!_collectionView) {
  179. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
  180. flow.scrollDirection = UICollectionViewScrollDirectionVertical;
  181. flow.itemSize = CGSizeMake(_window_width/2-1, (_window_width/2-1) * 1.4);
  182. flow.minimumLineSpacing = 2;
  183. flow.minimumInteritemSpacing = 2;
  184. _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,64+statusbarHeight, _window_width, _window_height-49-ShowDiff-64-statusbarHeight) collectionViewLayout:flow];
  185. [_collectionView registerNib:[UINib nibWithNibName:@"NearbyCell" bundle:nil] forCellWithReuseIdentifier:@"NearbyCell"];
  186. [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"NearbyHeader"];
  187. _collectionView.backgroundColor = Normal_Color;
  188. _collectionView.delegate = self;
  189. _collectionView.dataSource = self;
  190. _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  191. _paging ++;
  192. [self pullData];
  193. }];
  194. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  195. _paging = 1;
  196. [self pullData];
  197. }];
  198. }
  199. return _collectionView;
  200. }
  201. - (void)didReceiveMemoryWarning {
  202. [super didReceiveMemoryWarning];
  203. }
  204. - (UIView *)headerView {
  205. if (!_headerView) {
  206. _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _window_width, 40)];
  207. _headerView.backgroundColor = NaviBg_Color;
  208. _headerTitle = [[UILabel alloc]init];
  209. _headerTitle.text = [NSString stringWithFormat:@"%@:%@",YZMsg(@"定位城市"),_currentCity];
  210. _headerTitle.font = SYS_Font(12);
  211. _headerTitle.textColor = RGB_COLOR(@"#ffffff", 0.53);
  212. [_headerView addSubview:_headerTitle];
  213. UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  214. changeBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
  215. [changeBtn setTitle:YZMsg(@"切换") forState:0];
  216. changeBtn.titleLabel.font = SYS_Font(12);
  217. [changeBtn setTitleColor:_headerTitle.textColor forState:0];
  218. [changeBtn setImage:[UIImage imageNamed:@"更多-箭头"] forState:0];
  219. [changeBtn addTarget:self action:@selector(clickChangeBtn) forControlEvents:UIControlEventTouchUpInside];
  220. [_headerView addSubview:changeBtn];
  221. [_headerTitle mas_makeConstraints:^(MASConstraintMaker *make) {
  222. make.left.equalTo(_headerView.mas_left).offset(15);
  223. make.height.centerY.equalTo(_headerView);
  224. make.right.lessThanOrEqualTo(changeBtn.mas_left).offset(-10);
  225. }];
  226. [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  227. make.height.centerY.equalTo(_headerView);
  228. make.right.equalTo(_headerView.mas_right).offset(-15);
  229. make.width.mas_greaterThanOrEqualTo(46);
  230. }];
  231. [_headerView layoutIfNeeded];
  232. [changeBtn setImageEdgeInsets:UIEdgeInsetsMake(0, changeBtn.titleLabel.size.width, 0, -changeBtn.titleLabel.size.width)];
  233. [changeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -changeBtn.imageView.size.width-1, 0, changeBtn.imageView.size.width+1)];
  234. }
  235. return _headerView;
  236. }
  237. -(void)clickChangeBtn {
  238. YBCitySelVC *selVC = [[YBCitySelVC alloc]init];
  239. YBWeakSelf;
  240. selVC.citySelEvent = ^(NSString *cityName) {
  241. _currentCity = cityName;
  242. [weakSelf changeName:cityName];
  243. _paging = 1;
  244. [weakSelf pullData];
  245. };
  246. [[YBBaseAppDelegate sharedAppDelegate]pushViewController:selVC animated:YES];
  247. }
  248. -(void)changeName:(NSString *)cityName {
  249. if ([cityName containsString:[cityDefault getLocationCity]] || [[cityDefault getLocationCity] containsString:cityName]) {
  250. _isSelfLoc = YES;
  251. }else {
  252. _isSelfLoc = NO;
  253. }
  254. dispatch_async(dispatch_get_main_queue(), ^{
  255. [PublicObj updataNewCity:cityName];
  256. _headerTitle.text = [NSString stringWithFormat:@"%@:%@",YZMsg(@"定位城市"),_currentCity];
  257. self.titleL.text = _currentCity;
  258. [self.rightBtn setTitle:cityName forState:0];
  259. });
  260. }
  261. @end