InstrumentChooseViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // InstrumentChooseViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/4/11.
  6. //
  7. #import "InstrumentChooseViewController.h"
  8. #import "InstrumentMessageModel.h"
  9. #import "InstrumentChooseCell.h"
  10. #import "InstrumentChooseBottonView.h"
  11. #import "InstrumentHeaderView.h"
  12. @interface InstrumentChooseViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  13. @property (nonatomic, copy) ChooseInstrumentCallback callback;
  14. @property (nonatomic, copy) UICollectionView *collectionView;
  15. @property (nonatomic, strong) NSMutableArray *parentArray;
  16. @property (nonatomic, strong) NSMutableArray *instrumentArray;
  17. @property (nonatomic, copy) InstrumentChooseBottonView *bottomView;
  18. @property (nonatomic, strong) NSMutableArray *chooseArray;
  19. @end
  20. @implementation InstrumentChooseViewController
  21. - (void)chooseCallback:(ChooseInstrumentCallback)callback {
  22. if (callback) {
  23. self.callback = callback;
  24. }
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. [self allocTitle:@"乐器选择"];
  30. [self configUI];
  31. [self requestInstrumentMessage];
  32. }
  33. - (void)configUI {
  34. [self.scrollView removeFromSuperview];
  35. [self.view addSubview:self.bottomView];
  36. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.right.mas_equalTo(self.view);
  38. make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
  39. make.height.mas_equalTo(90);
  40. }];
  41. UIView *headView = [[UIView alloc] init];
  42. headView.layer.backgroundColor = [UIColor whiteColor].CGColor;
  43. [self.view addSubview:headView];
  44. [headView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
  46. make.left.mas_equalTo(self.view.mas_left).offset(14.0f);
  47. make.right.mas_equalTo(self.view.mas_right).offset(-14.0f);
  48. make.height.mas_equalTo(36.0f);
  49. }];
  50. CAGradientLayer * gradientLayer = [CAGradientLayer layer];
  51. gradientLayer.colors = @[(__bridge id)HexRGB(0xFFF6EE).CGColor,(__bridge id)HexRGB(0xFFECDD).CGColor];
  52. gradientLayer.startPoint = CGPointMake(0.04, 0);
  53. gradientLayer.endPoint = CGPointMake(0.97, 1.07);
  54. gradientLayer.frame = CGRectMake(0, 0, kScreenWidth - 28, 36);
  55. gradientLayer.locations = @[@(0),@(1)];
  56. [headView.layer addSublayer:gradientLayer];
  57. headView.layer.cornerRadius = 10.0f;
  58. headView.layer.masksToBounds = YES;
  59. UILabel *tipsLabel = [[UILabel alloc] init];
  60. tipsLabel.text = @"最多可选择1个乐器";
  61. tipsLabel.textColor = HexRGB(0xFF9E5A);
  62. tipsLabel.font = [UIFont systemFontOfSize:14.0f];
  63. [headView addSubview:tipsLabel];
  64. [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.left.mas_equalTo(headView.mas_left).offset(11.0f);
  66. make.right.mas_equalTo(headView.mas_right).offset(-11.0f);
  67. make.height.mas_equalTo(22.0f);
  68. make.centerY.mas_equalTo(headView.mas_centerY);
  69. }];
  70. [self.view addSubview:self.collectionView];
  71. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.left.mas_equalTo(self.view.mas_left).offset(15);
  73. make.right.mas_equalTo(self.view.mas_right).offset(-15);
  74. make.top.mas_equalTo(headView.mas_bottom);
  75. make.bottom.mas_equalTo(self.bottomView.mas_top);
  76. }];
  77. }
  78. - (void)requestInstrumentMessage {
  79. [self showhud];
  80. [KSNetworkingManager studentQuerySubject:KS_GET success:^(NSDictionary * _Nonnull dic) {
  81. [self removehub];
  82. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  83. [self evaluateSource:[dic arrayValueForKey:@"data"]];
  84. }
  85. else {
  86. [self MBPShow:MESSAGEKEY];
  87. }
  88. } faliure:^(NSError * _Nonnull error) {
  89. [self removehub];
  90. }];
  91. }
  92. - (void)evaluateSource:(NSArray *)sourceArray {
  93. @autoreleasepool {
  94. NSMutableArray *valueArray = [sourceArray mutableCopy];
  95. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  96. NSMutableArray *parentNodeArray = [NSMutableArray array];
  97. NSMutableArray *instrumentArray = [NSMutableArray array];
  98. for (NSInteger i = 0; i < valueArray.count; i++) {
  99. NSMutableDictionary *parentNodeDic = [NSMutableDictionary dictionary];
  100. NSDictionary *parm = valueArray[i];
  101. [parentNodeDic setValue:[parm stringValueForKey:@"id"] forKey:@"subjectId"];
  102. [parentNodeDic setValue:[parm stringValueForKey:@"name"] forKey:@"subjectName"];
  103. NSArray *subjects = [parm arrayValueForKey:@"subjects"];
  104. if (subjects.count > 0) {
  105. // 添加父节点分类
  106. [parentNodeArray addObject:parentNodeDic];
  107. NSMutableArray *subjectModelArray = [NSMutableArray array];
  108. for (NSDictionary *dic in subjects) {
  109. InstrumentMessageModel *model = [[InstrumentMessageModel alloc] initWithDictionary:dic];
  110. [subjectModelArray addObject:model];
  111. }
  112. [instrumentArray addObject:subjectModelArray];
  113. }
  114. }
  115. self.parentArray = [NSMutableArray arrayWithArray:parentNodeArray];
  116. self.instrumentArray = [NSMutableArray arrayWithArray:instrumentArray];
  117. dispatch_main_async_safe(^{
  118. // 刷新
  119. [self refreshView];
  120. });
  121. });
  122. };
  123. }
  124. - (void)refreshView {
  125. [self.collectionView reloadData];
  126. }
  127. #pragma mark ----- collection view data source
  128. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  129. return self.parentArray.count;
  130. }
  131. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  132. NSMutableArray *instrumentArray = self.instrumentArray[section];
  133. return instrumentArray.count;
  134. }
  135. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  136. NSMutableArray *instrumentArray = self.instrumentArray[indexPath.section];
  137. InstrumentMessageModel *model = instrumentArray[indexPath.item];
  138. InstrumentChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InstrumentChooseCell" forIndexPath:indexPath];
  139. [cell configCellWithModel:model];
  140. return cell;
  141. }
  142. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  143. NSMutableArray *instrumentArray = self.instrumentArray[indexPath.section];
  144. InstrumentMessageModel *model = instrumentArray[indexPath.item];
  145. model.isChoose = !model.isChoose;
  146. [self countMessageWithModel:model indexPath:indexPath];
  147. }
  148. - (void)countMessageWithModel:(InstrumentMessageModel *)model indexPath:(NSIndexPath *)indexPath {
  149. if (model.isChoose) {
  150. if (![self judgeContainModel:model]) {
  151. if (self.chooseArray.count == 1) {
  152. [self MBPShow:@"最多只能选择1个乐器"];
  153. return;
  154. }
  155. [self.chooseArray addObject:model];
  156. }
  157. }
  158. else {
  159. if ([self judgeContainModel:model]) {
  160. [self removeWithInstrumentId:model.internalBaseClassIdentifier];
  161. }
  162. }
  163. [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
  164. }
  165. - (BOOL)judgeContainModel:(InstrumentMessageModel *)model {
  166. BOOL contain = NO;
  167. for (InstrumentMessageModel *chooseModel in self.chooseArray) {
  168. if ([chooseModel.internalBaseClassIdentifier isEqualToString:model.internalBaseClassIdentifier]) {
  169. contain = YES;
  170. break;
  171. }
  172. }
  173. return contain;
  174. }
  175. - (void)removeWithInstrumentId:(NSString *)instrumentId {
  176. for (InstrumentMessageModel *chooseModel in self.chooseArray) {
  177. if ([chooseModel.internalBaseClassIdentifier isEqualToString:instrumentId]) {
  178. [self.chooseArray removeObject:chooseModel];
  179. break;
  180. }
  181. }
  182. }
  183. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  184. NSDictionary *parm = self.parentArray[indexPath.section];
  185. NSString *subjectName = [parm stringValueForKey:@"subjectName"];
  186. InstrumentHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"InstrumentHeaderView" forIndexPath:indexPath];
  187. [headView evaluateWithMessage:subjectName];
  188. return headView;
  189. }
  190. #pragma mark ------ lazying
  191. - (NSMutableArray *)chooseArray {
  192. if (!_chooseArray) {
  193. _chooseArray = [NSMutableArray array];
  194. }
  195. return _chooseArray;
  196. }
  197. - (UICollectionView *)collectionView {
  198. if (!_collectionView) {
  199. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  200. CGFloat width = (kScreen_Width - 15 * 2 - 20) / 3.0f;
  201. CGFloat height = width;
  202. layout.itemSize = CGSizeMake(width, height);
  203. layout.headerReferenceSize = CGSizeMake(kScreen_Width - 30, 40);
  204. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  205. _collectionView.backgroundColor = [UIColor clearColor];
  206. _collectionView.delegate = self;
  207. _collectionView.dataSource = self;
  208. _collectionView.showsVerticalScrollIndicator = NO;
  209. _collectionView.showsHorizontalScrollIndicator = NO;
  210. [_collectionView registerNib:[UINib nibWithNibName:@"InstrumentChooseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"InstrumentChooseCell"];
  211. [_collectionView registerNib:[UINib nibWithNibName:@"InstrumentHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"InstrumentHeaderView"];
  212. }
  213. return _collectionView;
  214. }
  215. - (InstrumentChooseBottonView *)bottomView {
  216. if (!_bottomView) {
  217. _bottomView = [InstrumentChooseBottonView shareInstance];
  218. MJWeakSelf;
  219. [_bottomView sureCallback:^{
  220. [weakSelf chooseInstrumentAction];
  221. }];
  222. }
  223. return _bottomView;
  224. }
  225. - (void)chooseInstrumentAction {
  226. if (self.chooseArray.count == 0) {
  227. [self MBPShow:@"请选择乐器"];
  228. return;
  229. }
  230. NSMutableArray *instrumentArray = [NSMutableArray array];
  231. for (InstrumentMessageModel *model in self.chooseArray) {
  232. NSMutableDictionary *parm = [NSMutableDictionary dictionary];
  233. [parm setValue:model.name forKey:@"subjectName"];
  234. [parm setValue:model.internalBaseClassIdentifier forKey:@"subjectId"];
  235. [instrumentArray addObject:parm];
  236. }
  237. [self sureChooseSubject];
  238. }
  239. - (void)sureChooseSubject {
  240. InstrumentMessageModel *model = [self.chooseArray firstObject];
  241. NSString *subjectId = model.internalBaseClassIdentifier;
  242. [self showhud];
  243. [KSNetworkingManager studentSetSubject:KS_GET subjectIds:subjectId success:^(NSDictionary * _Nonnull dic) {
  244. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  245. MJWeakSelf;
  246. [self KSShowMsg:@"设置成功" promptCompletion:^{
  247. if (weakSelf.callback) {
  248. weakSelf.callback();
  249. }
  250. [weakSelf backAction];
  251. }];
  252. }
  253. else {
  254. [self MBPShow:MESSAGEKEY];
  255. }
  256. } faliure:^(NSError * _Nonnull error) {
  257. [self removehub];
  258. }];
  259. }
  260. - (void)backAction {
  261. if (self.isModalPresent) {
  262. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  263. }
  264. else {
  265. [self.navigationController popViewControllerAnimated:YES];
  266. }
  267. }
  268. /*
  269. #pragma mark - Navigation
  270. // In a storyboard-based application, you will often want to do a little preparation before navigation
  271. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  272. // Get the new view controller using [segue destinationViewController].
  273. // Pass the selected object to the new view controller.
  274. }
  275. */
  276. @end