AddChannelController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. [self postData];
  36. }
  37. - (void)loadChannelModel
  38. {
  39. [self.channelSelectArr removeAllObjects];
  40. // ChannelModel * model = [[ChannelModel alloc] init];
  41. // model.ArticleGroupId = 0;
  42. // model.ArticleGroupName = @"推荐";
  43. // model.IsSelect = YES;
  44. // [self.channelSelectArr addObject:model];
  45. // ChannelModel * sModel = [[ChannelModel alloc] init];
  46. // sModel.ArticleGroupId = 2;
  47. // sModel.ArticleGroupName = @"信源听说";
  48. // sModel.IsSelect = NO;
  49. // [self.channelSelectArr addObject:sModel];
  50. // ChannelModel * mModel = [[ChannelModel alloc] init];
  51. // mModel.ArticleGroupId = 3;
  52. // mModel.ArticleGroupName = @"名校";
  53. // mModel.IsSelect = NO;
  54. // [self.channelSelectArr addObject:mModel];
  55. // ChannelModel * aModel = [[ChannelModel alloc] init];
  56. // aModel.ArticleGroupId = 4;
  57. // aModel.ArticleGroupName = @"会员特供";
  58. // aModel.IsSelect = NO;
  59. // [self.channelSelectArr addObject:aModel];
  60. // ChannelModel * bModel = [[ChannelModel alloc] init];
  61. // bModel.ArticleGroupId = 5;
  62. // bModel.ArticleGroupName = @"每日微课";
  63. // bModel.IsSelect = NO;
  64. // [self.channelSelectArr addObject:bModel];
  65. // ChannelModel * cModel = [[ChannelModel alloc] init];
  66. // cModel.ArticleGroupId = 6;
  67. // cModel.ArticleGroupName = @"每日技能";
  68. // cModel.IsSelect = NO;
  69. // [self.channelSelectArr addObject:cModel];
  70. // ChannelModel * dModel = [[ChannelModel alloc] init];
  71. // dModel.ArticleGroupId = 7;
  72. // dModel.ArticleGroupName = @"每日好书";
  73. // dModel.IsSelect = NO;
  74. // [self.channelSelectArr addObject:dModel];
  75. // ChannelModel * eModel = [[ChannelModel alloc] init];
  76. // eModel.ArticleGroupId = 8;
  77. // eModel.ArticleGroupName = @"每日好文";
  78. // eModel.IsSelect = NO;
  79. // [self.channelSelectArr addObject:eModel];
  80. // ChannelModel * fModel = [[ChannelModel alloc] init];
  81. // fModel.ArticleGroupId = 9;
  82. // fModel.ArticleGroupName = @"名师";
  83. // fModel.IsSelect = NO;
  84. // [self.channelSelectArr addObject:fModel];
  85. // ChannelModel * gModel = [[ChannelModel alloc] init];
  86. // gModel.ArticleGroupId = 10;
  87. // gModel.ArticleGroupName = @"大咖讲座";
  88. // gModel.IsSelect = NO;
  89. // [self.channelSelectArr addObject:gModel];
  90. }
  91. - (void)getData
  92. {
  93. WS(weakSelf);
  94. [self.channelMainArr removeAllObjects];
  95. [self.channelSelectArr removeAllObjects];
  96. [self.channelOtherArr removeAllObjects];
  97. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Pindao_User_Post] parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  98. if ([responseObject isKindOfClass:[NSArray class]]) {
  99. NSArray *array = responseObject;
  100. for (NSInteger i = 0 ; i < array.count; i ++) {
  101. NSDictionary *dic = array[i];
  102. ChannelModel *model = [ChannelModel modelWithDictionary:dic];
  103. [weakSelf.channelSelectArr addObject:model];
  104. }
  105. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Pindao_Post] parameters:@{} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  106. if ([responseObject isKindOfClass:[NSArray class]]) {
  107. NSArray *array = responseObject;
  108. for (NSDictionary * dic in array) {
  109. ChannelModel *model = [ChannelModel modelWithDictionary:dic];
  110. [weakSelf.channelOtherArr addObject:model];
  111. }
  112. [weakSelf.channelMainArr addObject:weakSelf.channelSelectArr];
  113. [weakSelf.channelMainArr addObject:weakSelf.channelOtherArr];
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. [weakSelf.collectionView reloadData];
  116. });
  117. }
  118. } failure:^(NSError * _Nonnull error) {
  119. REMOVESHOW
  120. }];
  121. }
  122. } failure:^(NSError * _Nonnull error) {
  123. REMOVESHOW
  124. }];
  125. }
  126. - (void)createCollectionView {
  127. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  128. // CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  129. // flowLayout.itemSize = CGSizeMake(itemWidth - 8.f, 42.f);
  130. // flowLayout.minimumLineSpacing = 12.f;
  131. // flowLayout.minimumInteritemSpacing = 12.f;
  132. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(12,Height_Layout(74), kGXScreenWidth - 24.f, kGXScreenHeigh - (Height_Layout(74))) collectionViewLayout:flowLayout];
  133. self.collectionView.dataSource = self;
  134. self.collectionView.delegate = self;
  135. self.collectionView.scrollEnabled = YES;
  136. self.collectionView.showsVerticalScrollIndicator = NO;
  137. self.collectionView.showsHorizontalScrollIndicator = NO;
  138. self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 12.f, 0);
  139. [self.collectionView registerNib:[UINib nibWithNibName:@"MyChanelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"MyChanelCollectionViewCell"];
  140. [self.collectionView registerNib:[UINib nibWithNibName:@"AddChanelNoDataCell" bundle:nil] forCellWithReuseIdentifier:@"AddChanelNoDataCell"];
  141. [self.collectionView registerNib:[UINib nibWithNibName:@"MyChanelCollectionReusableViewTop" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyChanelCollectionReusableViewTop"];
  142. self.collectionView.backgroundColor = [UIColor whiteColor];
  143. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  144. [self.collectionView addGestureRecognizer:longPressGesture];
  145. [self.view addSubview:self.collectionView];
  146. }
  147. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  148. return self.channelMainArr.count;
  149. }
  150. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  151. NSArray *array = self.channelMainArr[section];
  152. if (section == 1 && array.count == 0) {
  153. return 1;
  154. }
  155. return array.count;
  156. }
  157. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. if (indexPath.section == 1) {
  160. if (self.channelOtherArr.count > 0) {
  161. CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  162. return CGSizeMake(itemWidth - 8.f, 42.f);
  163. }else{
  164. return CGSizeMake(SCREEN_WIDTH, 120.f);
  165. }
  166. }else{
  167. CGFloat itemWidth = (kGXScreenWidth - (3 * 12.f)) / 4;
  168. return CGSizeMake(itemWidth - 8.f, 42.f);
  169. }
  170. }
  171. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  172. //{
  173. // return UIEdgeInsetsMake(0, 0, 0, 0);;
  174. //}
  175. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  176. {
  177. return 12.f;
  178. }
  179. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  180. {
  181. return 12.f;
  182. }
  183. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  184. MyChanelCollectionReusableViewTop *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyChanelCollectionReusableViewTop" forIndexPath:indexPath];
  185. if (indexPath.section == 0) {
  186. headerView.labelTitle.text = @"我的频道";
  187. headerView.labelDetail.text = @"长按可拖动排序";
  188. }else{
  189. headerView.labelTitle.text = @"更多频道";
  190. headerView.labelDetail.text = @"点击添加到我的频道";
  191. }
  192. return headerView;
  193. }
  194. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  195. return CGSizeMake(kGXScreenWidth, 72);
  196. }
  197. - (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath {
  198. if (indexPath.section == 0 && indexPath.item == 0) {
  199. return false;
  200. }
  201. if (indexPath.section == 1) {
  202. return false;
  203. }
  204. return true;
  205. }
  206. - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
  207. if (indexPath.section == 0 && indexPath.item == 0) {
  208. return NO;
  209. }
  210. if (indexPath.section == 1) {
  211. return NO;
  212. }
  213. return YES;
  214. }
  215. // 在移动结束的时候调用此代理方法
  216. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
  217. if (destinationIndexPath.section == 0 && sourceIndexPath.section == 0) {
  218. ChannelModel * model = self.channelSelectArr[sourceIndexPath.item];
  219. // ChannelModel * smodel = self.channelSelectArr[destinationIndexPath.item];
  220. [self.channelSelectArr removeObject:model];
  221. [self.channelSelectArr insertObject:model atIndex:destinationIndexPath.item];
  222. [self.channelMainArr replaceObjectAtIndex:0 withObject:self.channelSelectArr];
  223. [self.collectionView reloadData];
  224. // }else{
  225. // ChannelModel * model = self.channelSelectArr[sourceIndexPath.item];
  226. // if (model) {
  227. // NSString * url = [NSString stringWithFormat:@"%@%ld",Host(Article_Pindao_User_Delete),(long)model.Id];
  228. // [[HttpManager sharedHttpManager] DeleteUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) {
  229. // [weakSelf getData];
  230. // } failure:^(NSError * _Nonnull error) {
  231. //
  232. // }];
  233. // }
  234. }
  235. // NSString *string = arraySource[sourceIndexPath.row];
  236. // [arraySource removeObjectAtIndex:sourceIndexPath.row];
  237. // [arrayDestination insertObject:string atIndex:destinationIndexPath.row];
  238. // [self.channelMainArr removeAllObjects];
  239. // [self.channelMainArr addObject:self.channelSelectArr];
  240. // [self.channelMainArr addObject:self.channelOtherArr];
  241. // [self.collectionView reloadData];
  242. }
  243. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  244. MyChanelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyChanelCollectionViewCell" forIndexPath:indexPath];
  245. NSArray *arrayTmp = self.channelMainArr[indexPath.section];
  246. ChannelModel *model = arrayTmp[indexPath.row];
  247. cell.labelTitle.text = model.ArticleGroupName.length ? model.ArticleGroupName : model.Name;
  248. if (indexPath.section) {
  249. if (arrayTmp.count > 0) {
  250. cell.deleteButton.hidden = YES;
  251. cell.labelTitle.text = [NSString stringWithFormat:@"+%@",model.ArticleGroupName.length ? model.ArticleGroupName : model.Name];
  252. return cell;
  253. }else{
  254. AddChanelNoDataCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddChanelNoDataCell" forIndexPath:indexPath];
  255. return cell;
  256. }
  257. } else {
  258. if (indexPath.item == 0) {
  259. cell.deleteButton.hidden = YES;
  260. }else{
  261. cell.deleteButton.hidden = NO;
  262. cell.deleteButton.tag = indexPath.item;
  263. [cell.deleteButton addTarget:self action:@selector(deleteChannelAction:) forControlEvents:UIControlEventTouchUpInside];
  264. }
  265. return cell;
  266. }
  267. }
  268. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  269. if (indexPath.section == 1) {
  270. ChannelModel * model = self.channelOtherArr[indexPath.item];
  271. if (model) {
  272. [self.channelOtherArr removeObject:model];
  273. [self.channelSelectArr addObject:model];
  274. [self.collectionView reloadData];
  275. }
  276. }
  277. }
  278. - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
  279. MyChanelCollectionViewCell *cell;
  280. switch (longPress.state) {
  281. case UIGestureRecognizerStateBegan:{
  282. //判断手势落点位置是否在路径上
  283. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
  284. if (indexPath.item == 0 && indexPath.section == 0) {
  285. break;
  286. }
  287. //在路径上则开始移动该路径上的cell
  288. cell = (MyChanelCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
  289. cell.isEditing = YES;
  290. [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
  291. }
  292. break;
  293. case UIGestureRecognizerStateChanged:{
  294. NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
  295. if (indexPath.item == 0 && indexPath.section == 0) {
  296. break;
  297. }
  298. //移动过程当中随时更新cell位置
  299. [self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:self.collectionView]];
  300. }break;
  301. case UIGestureRecognizerStateEnded:
  302. //移动结束后关闭cell移动
  303. cell.isEditing = NO;
  304. [self.collectionView endInteractiveMovement];
  305. break;
  306. default:
  307. cell.isEditing = NO;
  308. [self.collectionView cancelInteractiveMovement];
  309. break;
  310. }
  311. }
  312. - (void)deleteChannelAction:(UIButton *)sender {
  313. ChannelModel * model = self.channelSelectArr[sender.tag];
  314. if (model) {
  315. [self.channelOtherArr addObject:model];
  316. [self.channelSelectArr removeObject:model];
  317. [self.collectionView reloadData];
  318. }
  319. }
  320. #pragma mark - setter
  321. - (TDHorizontalWaterFlowLayout *)layout {
  322. if (!_layout) {
  323. _layout = [[TDHorizontalWaterFlowLayout alloc] init];
  324. _layout.dataList = self.channelMainArr;
  325. }
  326. return _layout;
  327. }
  328. - (NSMutableArray *)channelMainArr {
  329. if (!_channelMainArr) {
  330. _channelMainArr = [NSMutableArray array];
  331. }
  332. return _channelMainArr;
  333. }
  334. - (NSMutableArray *)channelSelectArr {
  335. if (!_channelSelectArr) {
  336. _channelSelectArr = [NSMutableArray array];
  337. }
  338. return _channelSelectArr;
  339. }
  340. - (NSMutableArray *)channelOtherArr {
  341. if (!_channelOtherArr) {
  342. _channelOtherArr = [NSMutableArray array];
  343. }
  344. return _channelOtherArr;
  345. }
  346. - (TDButton *)dismissButton {
  347. if (!_dismissButton) {
  348. _dismissButton = [[TDButton alloc] init];
  349. _dismissButton.frame = CGRectMake(kGXScreenWidth - (Width_Layout(50)),kStatusBarHeight,Width_Layout(40), Height_Layout(40));
  350. [_dismissButton setCurrentButtonHotSize:CGSizeZero];
  351. [_dismissButton setImage:IMG(@"icon_dissmiss") forState:UIControlStateNormal];
  352. [_dismissButton addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
  353. }
  354. return _dismissButton;
  355. }
  356. - (void)postData
  357. {
  358. WS(weakSelf);
  359. NSMutableArray * array = [NSMutableArray array];
  360. NSMutableArray * sArray = self.channelMainArr.firstObject;
  361. for (NSInteger i = 1; i < sArray.count; i ++) {
  362. ChannelModel *model = sArray[i];
  363. [array addObject:[NSNumber numberWithInteger:model.ArticleGroupId]];
  364. }
  365. [[HttpManager sharedHttpManager] POSTUrl:Host(Article_Pindao_User_Delete) parameters:@{@"ArticleGroupId":array} responseStyle:DATA success:^(id _Nonnull responseObject) {
  366. if ([weakSelf.delegate respondsToSelector:@selector(upadateChannel)]) {
  367. [weakSelf.delegate upadateChannel];
  368. }
  369. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  370. } failure:^(NSError * _Nonnull error) {
  371. }];
  372. }
  373. @end