YOUPAIMineLabelEditVC.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // YOUPAIMineLabelEditVC.m
  3. // MSYOUPAI
  4. //
  5. // Created by admin on 2022/3/2.
  6. // Copyright © 2022 MS. All rights reserved.
  7. //
  8. #import "YOUPAIMineLabelEditVC.h"
  9. #import "YOUPAIZYLabelModel.h"
  10. @interface YOUPAIMineLabelEditVC ()
  11. @property(strong,nonatomic) NSMutableArray*selectids;
  12. @property(strong,nonatomic) NSMutableArray<YOUPAIZYLabelModel*>*selectData;
  13. @property(strong,nonatomic) NSMutableArray<YOUPAIZYLabelModel*>*dataSouce;
  14. @end
  15. @implementation YOUPAIMineLabelEditVC
  16. -(UICollectionViewFlowLayout *)youpaipflowLayout{
  17. if (_youpaipflowLayout == nil){
  18. _youpaipflowLayout = [UICollectionViewFlowLayout new];
  19. _youpaipflowLayout.minimumLineSpacing = 4;
  20. _youpaipflowLayout.minimumInteritemSpacing = 4;
  21. _youpaipflowLayout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
  22. }
  23. return _youpaipflowLayout;
  24. }
  25. -(UICollectionViewFlowLayout *)youpaipSelectflowLayout{
  26. if (_youpaipSelectflowLayout == nil){
  27. _youpaipSelectflowLayout = [UICollectionViewFlowLayout new];
  28. _youpaipSelectflowLayout.minimumLineSpacing = 4;
  29. _youpaipSelectflowLayout.minimumInteritemSpacing = 4;
  30. _youpaipSelectflowLayout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
  31. }
  32. return _youpaipSelectflowLayout;
  33. }
  34. -(UICollectionView *)youpaipcollectionView{
  35. if (_youpaipcollectionView == nil){
  36. _youpaipcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.youpaipflowLayout];
  37. _youpaipcollectionView.dataSource = self;
  38. _youpaipcollectionView.delegate = self;
  39. _youpaipcollectionView.backgroundColor = UIColor.whiteColor;
  40. [_youpaipcollectionView registerClass:[YOUPAIMineLabelEditCell class] forCellWithReuseIdentifier:@"YOUPAIMineLabelEditCell"];
  41. }
  42. return _youpaipcollectionView;
  43. }
  44. -(UICollectionView *)youpaipSelectcollectionView{
  45. if (_youpaipSelectcollectionView == nil){
  46. _youpaipSelectcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.youpaipSelectflowLayout];
  47. _youpaipSelectcollectionView.dataSource = self;
  48. _youpaipSelectcollectionView.delegate = self;
  49. [_youpaipSelectcollectionView registerClass:[YOUPAIMineLabelEditCell class] forCellWithReuseIdentifier:@"YOUPAIMineLabelEditCell"];
  50. _youpaipSelectcollectionView.backgroundColor = UIColor.clearColor;
  51. }
  52. return _youpaipSelectcollectionView;
  53. }
  54. -(UIBarButtonItem *)rightItem{
  55. if (_rightItem == nil){
  56. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  57. [button addTarget:self action:@selector(youpaifdidSave) forControlEvents:UIControlEventTouchUpInside];
  58. button.frame = CGRectMake(0, 0, 56, 24);
  59. [button setTitle:@"保存" forState:0];
  60. [button setTitleColor:LZA3AABEColor forState:0];
  61. // [button setBackgroundColor:HexColorFromRGB(0xFF5CA2)];
  62. button.titleLabel.font = LCFont14;
  63. button.layer.cornerRadius = 12;
  64. button.layer.masksToBounds = YES;
  65. [self.view addSubview:button];
  66. // 设置rightBarButtonItem
  67. _rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
  68. }
  69. return _rightItem;
  70. }
  71. -(void)youpaifdidSave{
  72. if(_didFinishSaveBlock){
  73. _didFinishSaveBlock(self.selectData);
  74. [self.navigationController popViewControllerAnimated:true];
  75. }
  76. }
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. self.title = @"我的标签";
  80. self.navigationItem.rightBarButtonItem = self.rightItem;
  81. self.selectData = [NSMutableArray new];
  82. self.selectids = [NSMutableArray new];
  83. self.dataSouce = [NSMutableArray new];
  84. [self ypupaifgetuserLable];
  85. [self configUI];
  86. [self makeLayout];
  87. }
  88. -(void)reloadData{
  89. [self.selectData removeAllObjects];
  90. for (YOUPAIZYLabelModel *item in self.dataSouce) {
  91. if ([self.selectids containsObject:item.youpaiptag_id]){
  92. [self.selectData addObject:item];
  93. }
  94. }
  95. [_youpaipSelectcollectionView reloadData];
  96. [_youpaipcollectionView reloadData];
  97. }
  98. -(void)configUI{
  99. [self.view addSubview:self.youpaipSelectcollectionView];
  100. [self.view addSubview:self.youpaipcollectionView];
  101. }
  102. -(void)makeLayout{
  103. [_youpaipSelectcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.top.mas_equalTo(0).mas_offset(NavBarHeight);
  105. make.left.right.mas_equalTo(0);
  106. make.height.mas_equalTo(224);
  107. }];
  108. [_youpaipcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.top.mas_equalTo(self.youpaipSelectcollectionView.mas_bottom);
  110. make.left.right.mas_equalTo(0);
  111. make.bottom.mas_equalTo(0);
  112. }];
  113. [self.view layoutIfNeeded];
  114. [LCTools clipCorner:UIRectCornerTopLeft|UIRectCornerTopRight View:_youpaipcollectionView size:CGSizeMake(16, 16)];
  115. }
  116. -(void)ypupaifgetuserLable{
  117. @weakify(self);
  118. NSDictionary *params = @{};
  119. [LCHttpHelper requestWithURLString:api_getUserTag
  120. parameters:params
  121. needToken:YES
  122. type:(HttpRequestTypePost)
  123. success:^(id responseObject) {
  124. @strongify(self);
  125. NSDictionary* dict = (NSDictionary*)responseObject;
  126. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  127. if (code==0) {//成功
  128. NSDictionary *dic = [dict objectForKey:@"data"];
  129. [self.dataSouce removeAllObjects];
  130. for (NSDictionary *item in dic[@"tag_list"]) {
  131. YOUPAIZYLabelModel *data = [YOUPAIZYLabelModel mj_objectWithKeyValues:item];
  132. NSArray *colors = [item[@"color"] componentsSeparatedByString:@","];
  133. data.youpaipstart_color = colors.firstObject;
  134. data.youpaipover_color = colors.lastObject;
  135. [self.dataSouce addObject:data];
  136. }
  137. for (NSDictionary *item in dic[@"user_tag"]) {
  138. [self.selectids addObject:[NSString stringWithFormat:@"%@",item[@"id"]]];
  139. }
  140. [self reloadData];
  141. }
  142. } failure:^(NSError *error) {
  143. [ZCHUDHelper showTitle:error.localizedDescription];
  144. }];
  145. }
  146. // MARK: delegate
  147. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  148. if (collectionView == _youpaipSelectcollectionView){
  149. return _selectData.count;
  150. }else{
  151. return _dataSouce.count;
  152. }
  153. }
  154. -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  155. if (collectionView == _youpaipSelectcollectionView){
  156. YOUPAIMineLabelEditCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAIMineLabelEditCell" forIndexPath:indexPath];
  157. YOUPAIZYLabelModel*dic = _selectData[indexPath.row];
  158. cell.youpaipLabel.text = dic.youpaipname;
  159. cell.youpaipLabel.textColor = [UIColor colorWithHexString:dic.youpaipover_color];
  160. cell.youpaipLabel.backgroundColor = [UIColor colorWithHexString:dic.youpaipstart_color];
  161. [cell.youpaipXBtn setHidden:NO];
  162. return cell;
  163. }else{
  164. YOUPAIMineLabelEditCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YOUPAIMineLabelEditCell" forIndexPath:indexPath];
  165. YOUPAIZYLabelModel*dic = _dataSouce[indexPath.row];
  166. cell.youpaipLabel.text = dic.youpaipname;
  167. if([self.selectids containsObject:dic.youpaiptag_id]){
  168. cell.youpaipLabel.textColor = [UIColor colorWithHexString:dic.youpaipover_color];
  169. cell.youpaipLabel.backgroundColor = [UIColor colorWithHexString:dic.youpaipstart_color];
  170. }else{
  171. cell.youpaipLabel.backgroundColor = LZFAFAFCColor;
  172. cell.youpaipLabel.textColor = LZ273145Color;
  173. }
  174. [cell.youpaipXBtn setHidden:YES];
  175. return cell;
  176. }
  177. }
  178. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  179. YOUPAIZYLabelModel *item = nil;
  180. if (collectionView == _youpaipcollectionView){
  181. item = _dataSouce[indexPath.row];
  182. }else{
  183. item = _selectData[indexPath.row];
  184. }
  185. NSString *title = item.youpaipname;
  186. CGSize size = [title sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]}];
  187. size.width += 24;
  188. size.height += 12;
  189. return size;
  190. }
  191. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  192. if (collectionView == _youpaipSelectcollectionView){
  193. YOUPAIZYLabelModel* item = _selectData[indexPath.row];
  194. [self.selectids removeObject:item.youpaiptag_id];
  195. [self reloadData];
  196. }else{
  197. YOUPAIZYLabelModel* item = _dataSouce[indexPath.row];
  198. if ([_selectData containsObject:item]){
  199. return;
  200. }
  201. [self.selectids addObject:item.youpaiptag_id];
  202. [self reloadData];
  203. }
  204. }
  205. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  206. if (collectionView == _youpaipSelectcollectionView){
  207. return UIEdgeInsetsMake(20, 12, 0, 12);
  208. }else{
  209. return UIEdgeInsetsMake(20, 12, 0, 12);
  210. }
  211. }
  212. @end
  213. @implementation YOUPAIMineLabelEditCell
  214. -(UGLabel *)youpaipLabel{
  215. if (_youpaipLabel == nil){
  216. _youpaipLabel = [UGLabel new];
  217. _youpaipLabel.textColor = UIColor.whiteColor;
  218. _youpaipLabel.textAlignment = NSTextAlignmentCenter;
  219. _youpaipLabel.font = [UIFont systemFontOfSize:12];
  220. [_youpaipLabel ms_radius:14];
  221. }
  222. return _youpaipLabel;
  223. }
  224. -(UIButton *)youpaipXBtn{
  225. if(_youpaipXBtn == nil){
  226. _youpaipXBtn = [UIButton new];
  227. [_youpaipXBtn setImage:[UIImage imageNamed:@"vqu_images_editmineinfo_delete"] forState:UIControlStateNormal];
  228. [_youpaipXBtn setEnabled:NO];
  229. }
  230. return _youpaipXBtn;
  231. }
  232. -(instancetype)initWithFrame:(CGRect)frame{
  233. if (self == [super initWithFrame:frame]){
  234. [self.contentView addSubview:self.youpaipLabel];
  235. [self.contentView addSubview:self.youpaipXBtn];
  236. [self makeLayout];
  237. }
  238. return self;
  239. }
  240. -(void)makeLayout{
  241. [_youpaipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  242. make.top.left.bottom.right.mas_equalTo(0);
  243. }];
  244. [_youpaipXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  245. make.top.right.mas_equalTo(0);
  246. make.size.mas_equalTo(13);
  247. }];
  248. }
  249. @end