MoreAppVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // MoreAppVC.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/5.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "MoreAppVC.h"
  9. #import "MoreAppCell.h"
  10. @interface MoreAppVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
  11. @property (strong,nonatomic) UICollectionView *collectionView;
  12. @property (strong,nonatomic) NSMutableArray *dataSourceArray;
  13. @property (nonatomic,assign) BOOL isBeganMove;
  14. @end
  15. @implementation MoreAppVC
  16. +(MoreAppVC *)initMoreAppVC{
  17. MoreAppVC *controller = [StoryboardManager.shared.chatMsg instantiateViewControllerWithIdentifier:@"MoreAppVC"];
  18. return controller;
  19. }
  20. - (void)viewWillDisappear:(BOOL)animated
  21. {
  22. [super viewWillDisappear:animated];
  23. if (self.loadMenuBlock) {
  24. self.loadMenuBlock();
  25. }
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.fd_prefersNavigationBarHidden = YES;
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. [self initDataSourceInfo];
  32. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
  33. [self.view addSubview:self.collectionView];
  34. WS(weakSelf);
  35. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.left.right.equalTo(weakSelf.view);
  37. make.bottom.equalTo(@(0));
  38. make.top.equalTo(@(kStatusBarHeight));
  39. }];
  40. [self.collectionView setBackgroundColor:[UIColor whiteColor]];
  41. [self.collectionView registerNib:[UINib nibWithNibName:@"MoreAppCell" bundle:nil] forCellWithReuseIdentifier:@"MoreAppCell"];
  42. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];
  43. self.collectionView.showsVerticalScrollIndicator = NO;
  44. self.collectionView.showsHorizontalScrollIndicator = NO;
  45. self.collectionView.alwaysBounceVertical = YES;
  46. [self.collectionView setDataSource:self];
  47. [self.collectionView setDelegate:self];
  48. [self.collectionView reloadData];
  49. }
  50. #pragma mark - 手势事件
  51. -(void)moveCollectionViewCell:(UILongPressGestureRecognizer *)gesture {
  52. switch (gesture.state) {
  53. case UIGestureRecognizerStateBegan: {
  54. if (!self.isBeganMove) {
  55. self.isBeganMove = YES;
  56. //获取点击的cell的indexPath
  57. NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
  58. //开始移动对应的cell
  59. [self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
  60. for (MoreAppCell *cell in [self.collectionView visibleCells]) {
  61. [self starShake:cell];
  62. }
  63. }
  64. break;
  65. }
  66. case UIGestureRecognizerStateChanged: {
  67. //移动cell
  68. [self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionView]];
  69. break;
  70. }
  71. case UIGestureRecognizerStateEnded: {
  72. self.isBeganMove = NO;
  73. //结束移动
  74. [self.collectionView endInteractiveMovement];
  75. for (MoreAppCell *cell in [self.collectionView visibleCells]) {
  76. [self stopShake:cell];
  77. }
  78. break;
  79. }
  80. default:
  81. [self.collectionView cancelInteractiveMovement];
  82. break;
  83. }
  84. }
  85. - (void)starShake:(MoreAppCell*)cell{
  86. CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
  87. keyAnimaion.keyPath = @"transform.rotation";
  88. keyAnimaion.values = @[@(-3 / 180.0 * M_PI),@(3 /180.0 * M_PI),@(-3/ 180.0 * M_PI)];//度数转弧度
  89. keyAnimaion.removedOnCompletion = NO;
  90. keyAnimaion.fillMode = kCAFillModeForwards;
  91. keyAnimaion.duration = 0.3;
  92. keyAnimaion.repeatCount = MAXFLOAT;
  93. [cell.layer addAnimation:keyAnimaion forKey:@"cellShake"];
  94. }
  95. - (void)stopShake:(MoreAppCell*)cell{
  96. [cell.layer removeAnimationForKey:@"cellShake"];
  97. }
  98. /**********************************************************************/
  99. #pragma mark -构造WQRefreshCollectionView的数据源对象
  100. /**********************************************************************/
  101. -(void)initDataSourceInfo{
  102. NSDictionary * chatMenuDict = [[NSUserDefaults standardUserDefaults] objectForKey:CHATMENUBAR];
  103. for(int m=0;m<2;m++){
  104. MoreAppModel *bean = [[MoreAppModel alloc] init];
  105. switch (m) {
  106. case 0:{
  107. bean.sectionTitle = @"常用应用";
  108. bean.isCloseSectionFlag = YES;
  109. NSArray * array = [chatMenuDict objectForKey:@"common"];
  110. NSMutableArray * muArray = [NSMutableArray array];
  111. for (NSDictionary * dict in array) {
  112. MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:dict];
  113. [muArray addObject:model];
  114. }
  115. bean.moreAppInfoArray = muArray;
  116. }break;
  117. case 1:{
  118. bean.sectionTitle = @"更多应用";
  119. NSArray * array = [chatMenuDict objectForKey:@"more"];
  120. NSMutableArray * muArray = [NSMutableArray array];
  121. for (NSDictionary * dict in array) {
  122. MoreAppInfoModel * model = [MoreAppInfoModel modelWithDictionary:dict];
  123. [muArray addObject:model];
  124. }
  125. bean.moreAppInfoArray = muArray;
  126. }break;
  127. default:
  128. break;
  129. }
  130. [self.dataSourceArray addObject:bean];
  131. }
  132. }
  133. /**********************************************************************/
  134. #pragma mark -UICollectionViewDataSource
  135. /**********************************************************************/
  136. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  137. return self.dataSourceArray.count;
  138. }
  139. /**********************************************************************/
  140. #pragma mark -GHRefreshCollectionView的section的Count
  141. /**********************************************************************/
  142. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  143. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:section];
  144. return bean.moreAppInfoArray.count;
  145. }
  146. #pragma mark - UICollectionViewDelegate
  147. - (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
  148. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:indexPath.section];
  149. MoreAppInfoModel *beanItem = [bean.moreAppInfoArray objectAtIndex:indexPath.item];
  150. MoreAppCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreAppCell" forIndexPath:indexPath];
  151. cell.showTitleLabel.text = beanItem.title;
  152. if (indexPath.section == 0 && indexPath.item == 0) {
  153. }else{
  154. UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveCollectionViewCell:)];
  155. cell.userInteractionEnabled = YES;
  156. [cell addGestureRecognizer:longPressGesture];
  157. }
  158. if(beanItem.showEditFlag){
  159. cell.showIcon.hidden = NO;
  160. cell.showImgView.image = [UIImage imageNamed:beanItem.imgName];
  161. }else{
  162. cell.showIcon.hidden = YES;
  163. cell.showImgView.image = [UIImage imageNamed:beanItem.notImgName];
  164. }
  165. if(indexPath.section == 0){
  166. [cell.showIcon setImage:[UIImage imageNamed:@"chatmsg_edit_del_icon"]];
  167. }
  168. else if (indexPath.section == 1){
  169. [cell.showIcon setImage:[UIImage imageNamed:@"chatmsg_edit_add_icon"]];
  170. }
  171. return cell;
  172. }
  173. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  174. CGFloat w = SCREEN_WIDTH * 0.2;
  175. CGFloat h = 70;
  176. return CGSizeMake(w, h);
  177. }
  178. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  179. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  180. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:indexPath.section];
  181. MoreAppInfoModel *beanItem = [bean.moreAppInfoArray objectAtIndex:indexPath.item];
  182. if(indexPath.section == 0){
  183. if (indexPath.item == 0) {
  184. return;
  185. }
  186. [bean.moreAppInfoArray removeObject:beanItem];
  187. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  188. [moreModel.moreAppInfoArray addObject:beanItem];
  189. NSMutableArray * moreArray = [NSMutableArray array];
  190. for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  191. NSDictionary * dict = [model modelToJSONObject];
  192. [moreArray addObject:dict];
  193. }
  194. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  195. NSMutableArray * commonArray = [NSMutableArray array];
  196. for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  197. NSDictionary * dict = [model modelToJSONObject];
  198. [commonArray addObject:dict];
  199. }
  200. [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  201. [[NSUserDefaults standardUserDefaults] synchronize];
  202. [self.collectionView reloadData];
  203. }else {
  204. [bean.moreAppInfoArray removeObject:beanItem];
  205. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  206. [commonModel.moreAppInfoArray addObject:beanItem];
  207. NSMutableArray * commonArray = [NSMutableArray array];
  208. for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  209. NSDictionary * dict = [model modelToJSONObject];
  210. [commonArray addObject:dict];
  211. }
  212. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  213. NSMutableArray * moreArray = [NSMutableArray array];
  214. for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  215. NSDictionary * dict = [model modelToJSONObject];
  216. [moreArray addObject:dict];
  217. }
  218. [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  219. [[NSUserDefaults standardUserDefaults] synchronize];
  220. [self.collectionView reloadData];
  221. }
  222. }
  223. //footer的size
  224. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  225. {
  226. return CGSizeMake(0, 0);
  227. }
  228. //header的size
  229. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  230. {
  231. return CGSizeMake(SCREEN_WIDTH, 50);
  232. }
  233. //设置每个item的UIEdgeInsets
  234. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  235. {
  236. return UIEdgeInsetsMake(0, 0, 0, 0);
  237. }
  238. //设置每个item水平间距
  239. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
  240. {
  241. return 0.f;
  242. }
  243. //设置每个item垂直间距
  244. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
  245. {
  246. return 0.f;
  247. }
  248. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  249. MoreAppModel *bean = [self.dataSourceArray objectAtIndex:indexPath.section];
  250. if([kind isEqualToString:UICollectionElementKindSectionHeader]){
  251. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
  252. [headerView removeAllSubviews];
  253. if(indexPath.section == 0){
  254. UILabel *label = [[UILabel alloc] init];
  255. [headerView addSubview:label];
  256. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  257. make.centerY.equalTo(headerView).with.offset(0);
  258. make.left.equalTo(@(15));
  259. }];
  260. label.text = bean.sectionTitle;
  261. label.font = [UIFont systemFontOfSize:17];
  262. label.textColor = RGB(10, 10, 10);
  263. [label sizeToFit];
  264. UILabel *label1 = [[UILabel alloc] init];
  265. [headerView addSubview:label1];
  266. [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
  267. make.centerY.mas_equalTo(label);
  268. make.left.mas_equalTo(label.mas_right).offset(15);
  269. }];
  270. label1.text = @"长按可拖动排序";//@"长按可拖动排序";
  271. label1.font = [UIFont systemFontOfSize:13];
  272. label1.textColor = RGB(153, 153, 153);
  273. UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  274. [headerView addSubview:closeBtn];
  275. [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  276. make.centerY.equalTo(headerView).with.offset(0);
  277. make.right.equalTo(@(-17));
  278. }];
  279. [closeBtn setTitle:@"" forState:UIControlStateNormal];
  280. [closeBtn setImage:[UIImage imageNamed:@"chatmsg_moreapp_close_icon"] forState:UIControlStateNormal];
  281. WS(weakSelf);
  282. [closeBtn setAction:^{
  283. [weakSelf.navigationController popViewControllerAnimated:YES];
  284. }];
  285. }
  286. else if (indexPath.section == 1){
  287. UILabel *label = [[UILabel alloc] init];
  288. [headerView addSubview:label];
  289. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  290. make.centerY.equalTo(headerView).with.offset(0);
  291. make.left.equalTo(@(15));
  292. }];
  293. label.text = bean.sectionTitle;
  294. label.font = [UIFont systemFontOfSize:17];
  295. label.textColor = RGB(10, 10, 10);
  296. }
  297. headerView.backgroundColor = [UIColor whiteColor];
  298. return headerView;
  299. }
  300. return nil;
  301. }
  302. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
  303. WS(weakSelf);
  304. if (destinationIndexPath.section == 0 && destinationIndexPath.item == 0) {
  305. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  306. NSMutableArray * commonArray = [NSMutableArray array];
  307. for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  308. NSDictionary * dict = [model modelToJSONObject];
  309. [commonArray addObject:dict];
  310. }
  311. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  312. NSMutableArray * moreArray = [NSMutableArray array];
  313. for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  314. NSDictionary * dict = [model modelToJSONObject];
  315. [moreArray addObject:dict];
  316. }
  317. [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  318. [[NSUserDefaults standardUserDefaults] synchronize];
  319. dispatch_async(dispatch_get_main_queue(), ^{
  320. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  321. [weakSelf.collectionView reloadData];
  322. });
  323. });
  324. }else{
  325. if (sourceIndexPath.section == 0) {
  326. if (sourceIndexPath.item == 0) {
  327. return;
  328. }
  329. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  330. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  331. MoreAppInfoModel * model = commonModel.moreAppInfoArray[sourceIndexPath.item];
  332. [commonModel.moreAppInfoArray removeObjectAtIndex:sourceIndexPath.item];
  333. if (destinationIndexPath.section == 0) {
  334. [commonModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  335. }else{
  336. [moreModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  337. }
  338. }else{
  339. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  340. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  341. MoreAppInfoModel * model = moreModel.moreAppInfoArray[sourceIndexPath.item];
  342. [moreModel.moreAppInfoArray removeObjectAtIndex:sourceIndexPath.item];
  343. if (destinationIndexPath.section == 0) {
  344. [commonModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  345. }else{
  346. [moreModel.moreAppInfoArray insertObject:model atIndex:destinationIndexPath.item];
  347. }
  348. }
  349. MoreAppModel *commonModel = [self.dataSourceArray firstObject];
  350. NSMutableArray * commonArray = [NSMutableArray array];
  351. for (MoreAppInfoModel * model in commonModel.moreAppInfoArray) {
  352. NSDictionary * dict = [model modelToJSONObject];
  353. [commonArray addObject:dict];
  354. }
  355. MoreAppModel *moreModel = [self.dataSourceArray lastObject];
  356. NSMutableArray * moreArray = [NSMutableArray array];
  357. for (MoreAppInfoModel * model in moreModel.moreAppInfoArray) {
  358. NSDictionary * dict = [model modelToJSONObject];
  359. [moreArray addObject:dict];
  360. }
  361. [[NSUserDefaults standardUserDefaults] setObject:@{@"common":commonArray,@"more":moreArray} forKey:CHATMENUBAR];
  362. [[NSUserDefaults standardUserDefaults] synchronize];
  363. dispatch_async(dispatch_get_main_queue(), ^{
  364. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  365. [weakSelf.collectionView reloadData];
  366. });
  367. });
  368. }
  369. }
  370. -(NSMutableArray *)dataSourceArray{
  371. if(!_dataSourceArray){
  372. _dataSourceArray = [[NSMutableArray alloc] init];
  373. }
  374. return _dataSourceArray;
  375. }
  376. @end