AddChannelController.m 15 KB

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