AddChannelController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // AddChannelController.m
  3. // TheoryNetwork
  4. //
  5. // Created by tederen on 2019/9/20.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "AddChannelController.h"
  9. #import "TDHorizontalWaterFlowLayout.h"
  10. #import "MyChanelCollectionReusableViewTop.h"
  11. #import "MyChanelCollectionViewCell.h"
  12. #import "ChannelModel.h"
  13. #import "AddChanelNoDataCell.h"
  14. @interface AddChannelController () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  15. @property (strong, nonatomic) UICollectionView *collectionView;
  16. @property (strong, nonatomic) TDHorizontalWaterFlowLayout *layout;
  17. @property (strong, nonatomic) TDButton *dismissButton;
  18. @property (copy, nonatomic) NSMutableArray *channelMainArr;
  19. @property (copy, nonatomic) NSMutableArray *channelSelectArr;
  20. @property (copy, nonatomic) NSMutableArray *channelOtherArr;
  21. @end
  22. @implementation AddChannelController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.view.userInteractionEnabled = YES;
  26. [self createCollectionView];
  27. [self getData];
  28. [self.view addSubview:self.dismissButton];
  29. }
  30. - (void)viewWillLayoutSubviews {
  31. [super viewWillLayoutSubviews];
  32. [self.collectionView.collectionViewLayout invalidateLayout];
  33. }
  34. - (void)dismissAction {
  35. if ([self.delegate respondsToSelector:@selector(upadateChannel)]) {
  36. [self.delegate upadateChannel];
  37. }
  38. [self dismissViewControllerAnimated:YES completion:nil];
  39. }
  40. - (void)loadChannelModel
  41. {
  42. [self.channelSelectArr removeAllObjects];
  43. ChannelModel * model = [[ChannelModel alloc] init];
  44. model.ArticleGroupId = 0;
  45. model.ArticleGroupName = @"推荐";
  46. model.IsSelect = YES;
  47. [self.channelSelectArr addObject:model];
  48. ChannelModel * workModel = [[ChannelModel alloc] init];
  49. workModel.ArticleGroupId = 1;
  50. workModel.ArticleGroupName = @"待办工作";
  51. workModel.IsSelect = NO;
  52. [self.channelSelectArr addObject:workModel];
  53. }
  54. - (void)getData
  55. {
  56. WS(weakSelf);
  57. [self.channelMainArr removeAllObjects];
  58. [self loadChannelModel];
  59. [self.channelOtherArr removeAllObjects];
  60. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Pindao_User_Post] parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  61. if ([responseObject isKindOfClass:[NSArray class]]) {
  62. NSArray *array = responseObject;
  63. for (NSInteger i = 0 ; i < array.count; i ++) {
  64. NSDictionary *dic = array[i];
  65. ChannelModel *model = [ChannelModel modelWithDictionary:dic];
  66. [weakSelf.channelSelectArr addObject:model];
  67. }
  68. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Pindao_Post] parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  69. if ([responseObject isKindOfClass:[NSArray class]]) {
  70. NSArray *array = responseObject;
  71. for (NSDictionary * dic in array) {
  72. ChannelModel *model = [ChannelModel modelWithDictionary:dic];
  73. [weakSelf.channelOtherArr addObject:model];
  74. }
  75. [weakSelf.channelMainArr addObject:weakSelf.channelSelectArr];
  76. [weakSelf.channelMainArr addObject:weakSelf.channelOtherArr];
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. [weakSelf.collectionView reloadData];
  79. });
  80. }
  81. } failure:^(NSError * _Nonnull error) {
  82. REMOVESHOW
  83. }];
  84. }
  85. } failure:^(NSError * _Nonnull error) {
  86. REMOVESHOW
  87. }];
  88. }
  89. - (void)createCollectionView {
  90. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  91. // CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  92. // flowLayout.itemSize = CGSizeMake(itemWidth - 8.f, 42.f);
  93. // flowLayout.minimumLineSpacing = 12.f;
  94. // flowLayout.minimumInteritemSpacing = 12.f;
  95. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(12,Height_Layout(74), kGXScreenWidth - 24.f, kGXScreenHeigh - (Height_Layout(74))) collectionViewLayout:flowLayout];
  96. self.collectionView.dataSource = self;
  97. self.collectionView.delegate = self;
  98. self.collectionView.scrollEnabled = YES;
  99. self.collectionView.showsVerticalScrollIndicator = NO;
  100. self.collectionView.showsHorizontalScrollIndicator = NO;
  101. self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 12.f, 0);
  102. [self.collectionView registerNib:[UINib nibWithNibName:@"MyChanelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MyChanelCollectionViewCell"];
  103. [self.collectionView registerNib:[UINib nibWithNibName:@"AddChanelNoDataCell" bundle:nil] forCellWithReuseIdentifier:@"AddChanelNoDataCell"];
  104. [self.collectionView registerNib:[UINib nibWithNibName:@"MyChanelCollectionReusableViewTop" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyChanelCollectionReusableViewTop"];
  105. self.collectionView.backgroundColor = [UIColor whiteColor];
  106. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  107. [self.collectionView addGestureRecognizer:longPressGesture];
  108. [self.view addSubview:self.collectionView];
  109. }
  110. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  111. return self.channelMainArr.count;
  112. }
  113. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  114. NSArray *array = self.channelMainArr[section];
  115. if (section == 1 && array.count == 0) {
  116. return 1;
  117. }
  118. return array.count;
  119. }
  120. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. if (indexPath.section == 1) {
  123. if (self.channelOtherArr.count > 0) {
  124. CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  125. return CGSizeMake(itemWidth - 8.f, 42.f);
  126. }else{
  127. return CGSizeMake(SCREEN_WIDTH, 120.f);
  128. }
  129. }else{
  130. CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  131. return CGSizeMake(itemWidth - 8.f, 42.f);
  132. }
  133. }
  134. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  135. //{
  136. // return UIEdgeInsetsMake(0, 0, 0, 0);;
  137. //}
  138. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  139. {
  140. return 12.f;
  141. }
  142. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  143. {
  144. return 12.f;
  145. }
  146. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  147. MyChanelCollectionReusableViewTop *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyChanelCollectionReusableViewTop" forIndexPath:indexPath];
  148. if (indexPath.section == 0) {
  149. headerView.labelTitle.text = @"我的频道";
  150. headerView.labelDetail.text = @"长按可拖动排序";
  151. }else{
  152. headerView.labelTitle.text = @"更多频道";
  153. headerView.labelDetail.text = @"点击添加到我的频道";
  154. }
  155. return headerView;
  156. }
  157. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  158. return CGSizeMake(kGXScreenWidth, 72);
  159. }
  160. - (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath {
  161. if (indexPath.section == 0 && indexPath.item == 0) {
  162. return false;
  163. }
  164. if (indexPath.section == 1) {
  165. return false;
  166. }
  167. return true;
  168. }
  169. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
  170. if (indexPath.section == 0 && indexPath.item == 0) {
  171. return false;
  172. }
  173. if (indexPath.section == 1) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. // 在移动结束的时候调用此代理方法
  179. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
  180. NSMutableArray *arraySource = @[].mutableCopy;
  181. NSMutableArray *arrayDestination = @[].mutableCopy;
  182. if (sourceIndexPath.section == 0) {
  183. arraySource = self.channelSelectArr;
  184. } else {
  185. arraySource = self.channelOtherArr;
  186. }
  187. if (destinationIndexPath.section == 0) {
  188. arrayDestination = self.channelSelectArr;
  189. } else {
  190. arrayDestination = self.channelOtherArr;
  191. }
  192. NSString *string = arraySource[sourceIndexPath.row];
  193. [arraySource removeObjectAtIndex:sourceIndexPath.row];
  194. [arrayDestination insertObject:string atIndex:destinationIndexPath.row];
  195. [self.channelMainArr removeAllObjects];
  196. [self.channelMainArr addObject:self.channelSelectArr];
  197. [self.channelMainArr addObject:self.channelOtherArr];
  198. [self.collectionView reloadData];
  199. }
  200. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  201. MyChanelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyChanelCollectionViewCell" forIndexPath:indexPath];
  202. NSArray *arrayTmp = self.channelMainArr[indexPath.section];
  203. ChannelModel *model = arrayTmp[indexPath.row];
  204. cell.labelTitle.text = model.ArticleGroupName.length ? model.ArticleGroupName : model.Name;
  205. if (indexPath.section) {
  206. if (arrayTmp.count > 0) {
  207. cell.deleteButton.hidden = YES;
  208. cell.labelTitle.text = [NSString stringWithFormat:@"+%@",model.ArticleGroupName.length ? model.ArticleGroupName : model.Name];
  209. return cell;
  210. }else{
  211. AddChanelNoDataCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddChanelNoDataCell" forIndexPath:indexPath];
  212. return cell;
  213. }
  214. } else {
  215. cell.deleteButton.hidden = NO;
  216. if ((indexPath.item == 0) || (indexPath.item == 1)) {
  217. cell.deleteButton.hidden = YES;
  218. }
  219. cell.deleteButton.tag = indexPath.row;
  220. [cell.deleteButton addTarget:self action:@selector(deleteChannelAction:) forControlEvents:UIControlEventTouchUpInside];
  221. return cell;
  222. }
  223. }
  224. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  225. if (indexPath.section == 1) {
  226. WS(weakSelf);
  227. ChannelModel * model = self.channelOtherArr[indexPath.item];
  228. if (model) {
  229. [[HttpManager sharedHttpManager] POSTUrl:Host(Article_Pindao_User_add) parameters:@{@"ArticleGroupId":@(model.Id)} responseStyle:DATA success:^(id _Nonnull responseObject) {
  230. [weakSelf getData];
  231. } failure:^(NSError * _Nonnull error) {
  232. }];
  233. }
  234. }
  235. }
  236. - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
  237. MyChanelCollectionViewCell *cell;
  238. switch (longPress.state) {
  239. case UIGestureRecognizerStateBegan:{
  240. //判断手势落点位置是否在路径上
  241. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
  242. if (indexPath == nil||indexPath.item == 0||indexPath.section == 1 || indexPath.item == 1) {
  243. break;
  244. }
  245. //在路径上则开始移动该路径上的cell
  246. cell = (MyChanelCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  247. cell.isEditing = YES;
  248. [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
  249. }
  250. break;
  251. case UIGestureRecognizerStateChanged:{
  252. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
  253. if (indexPath == nil||indexPath.item == 0||indexPath.section == 1 || indexPath.item == 1) {
  254. break;
  255. }
  256. //移动过程当中随时更新cell位置
  257. [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:self.collectionView]];
  258. }break;
  259. case UIGestureRecognizerStateEnded:
  260. //移动结束后关闭cell移动
  261. cell.isEditing = NO;
  262. [self.collectionView endInteractiveMovement];
  263. break;
  264. default:
  265. cell.isEditing = NO;
  266. [self.collectionView cancelInteractiveMovement];
  267. break;
  268. }
  269. }
  270. - (void)deleteChannelAction:(UIButton *)sender {
  271. WS(weakSelf);
  272. ChannelModel * model = self.channelSelectArr[sender.tag];
  273. if (model) {
  274. NSString * url = [NSString stringWithFormat:@"%@%ld",Host(Article_Pindao_User_Delete),(long)model.Id];
  275. [[HttpManager sharedHttpManager] DeleteUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) {
  276. [weakSelf getData];
  277. } failure:^(NSError * _Nonnull error) {
  278. }];
  279. }
  280. }
  281. #pragma mark - setter
  282. - (TDHorizontalWaterFlowLayout *)layout {
  283. if (!_layout) {
  284. _layout = [[TDHorizontalWaterFlowLayout alloc] init];
  285. _layout.dataList = self.channelMainArr;
  286. }
  287. return _layout;
  288. }
  289. - (NSMutableArray *)channelMainArr {
  290. if (!_channelMainArr) {
  291. _channelMainArr = [NSMutableArray array];
  292. }
  293. return _channelMainArr;
  294. }
  295. - (NSMutableArray *)channelSelectArr {
  296. if (!_channelSelectArr) {
  297. _channelSelectArr = [NSMutableArray array];
  298. }
  299. return _channelSelectArr;
  300. }
  301. - (NSMutableArray *)channelOtherArr {
  302. if (!_channelOtherArr) {
  303. _channelOtherArr = [NSMutableArray array];
  304. }
  305. return _channelOtherArr;
  306. }
  307. - (TDButton *)dismissButton {
  308. if (!_dismissButton) {
  309. _dismissButton = [[TDButton alloc] init];
  310. _dismissButton.frame = CGRectMake(kGXScreenWidth - (Width_Layout(30)),kStatusBarHeight,Width_Layout(30), Height_Layout(30));
  311. [_dismissButton setCurrentButtonHotSize:CGSizeZero];
  312. [_dismissButton setImage:IMG(@"icon_dissmiss") forState:UIControlStateNormal];
  313. [_dismissButton addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
  314. }
  315. return _dismissButton;
  316. }
  317. @end