SubjectChooseViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // SubjectChooseViewController.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/4/5.
  6. //
  7. #import "SubjectChooseViewController.h"
  8. #import "SubjectChooseBodyView.h"
  9. #import "DZNSegmentedControl.h"
  10. #import <ZKCycleScrollView.h>
  11. #import <CHIPageControl/CHIPageControl-Swift.h>
  12. #import "SubjectImageCell.h"
  13. #import "InstrumentMessageModel.h"
  14. #import "KSSegmentControl.h"
  15. @interface SubjectChooseViewController ()<ZKCycleScrollViewDelegate,ZKCycleScrollViewDataSource,KSSegmentControlDelegate>
  16. @property (nonatomic, strong) ZKCycleScrollView *imageCycle;
  17. @property (nonatomic, strong) CHIPageControlJaloro *pageControl;
  18. @property (nonatomic, strong) SubjectChooseBodyView *bodyView;
  19. @property (nonatomic, strong) NSMutableArray *imageArray;
  20. @property (nonatomic, strong) NSMutableArray *parentArray;
  21. @property (nonatomic, strong) NSMutableArray *instrumentArray;
  22. @property (nonatomic, assign) NSInteger chooseIndex;
  23. @property (nonatomic, strong) KSSegmentControl *segCtrl;
  24. @property (nonatomic, strong) InstrumentMessageModel *chooseInstrument;
  25. @property (nonatomic, copy) SubjectChooseCallback callback;
  26. @end
  27. @implementation SubjectChooseViewController
  28. - (void)chooseSubjectCallback:(SubjectChooseCallback)callback {
  29. if (callback) {
  30. self.callback = callback;
  31. }
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. [self allocTitle:@"乐器选择"];
  37. [self configUI];
  38. [self requestSubjectMessage];
  39. }
  40. - (void)requestSubjectMessage {
  41. [self showhud];
  42. [KSNetworkingManager studentQuerySubject:KS_GET success:^(NSDictionary * _Nonnull dic) {
  43. [self removehub];
  44. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  45. [self evaluateSource:[dic arrayValueForKey:@"data"]];
  46. }
  47. else {
  48. [self MBPShow:MESSAGEKEY];
  49. }
  50. } faliure:^(NSError * _Nonnull error) {
  51. [self removehub];
  52. }];
  53. }
  54. - (void)evaluateSource:(NSArray *)sourceArray {
  55. @autoreleasepool {
  56. NSMutableArray *valueArray = [sourceArray mutableCopy];
  57. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  58. NSMutableArray *parentNodeArray = [NSMutableArray array];
  59. NSMutableArray *instrumentArray = [NSMutableArray array];
  60. for (NSInteger i = 0; i < valueArray.count; i++) {
  61. NSMutableDictionary *parentNodeDic = [NSMutableDictionary dictionary];
  62. NSDictionary *parm = valueArray[i];
  63. [parentNodeDic setValue:[parm stringValueForKey:@"id"] forKey:@"subjectId"];
  64. [parentNodeDic setValue:[parm stringValueForKey:@"name"] forKey:@"subjectName"];
  65. NSArray *subjects = [parm arrayValueForKey:@"subjects"];
  66. if (subjects.count > 0) { // 存在子节点,才添加父节点
  67. // 添加父节点分类
  68. [parentNodeArray addObject:parentNodeDic];
  69. NSMutableArray *subjectModelArray = [NSMutableArray array];
  70. for (NSDictionary *dic in subjects) {
  71. InstrumentMessageModel *model = [[InstrumentMessageModel alloc] initWithDictionary:dic];
  72. [subjectModelArray addObject:model];
  73. }
  74. [instrumentArray addObject:subjectModelArray];
  75. }
  76. }
  77. self.parentArray = [NSMutableArray arrayWithArray:parentNodeArray];
  78. self.instrumentArray = [NSMutableArray arrayWithArray:instrumentArray];
  79. dispatch_main_async_safe(^{
  80. [self resetChooseStatus];
  81. [self refreshView];
  82. });
  83. });
  84. };
  85. }
  86. - (void)resetChooseStatus {
  87. self.chooseIndex = 0;
  88. NSMutableArray *sourceArray = self.instrumentArray[self.chooseIndex];
  89. self.imageArray = sourceArray;
  90. self.chooseInstrument = self.imageArray[self.chooseIndex];
  91. }
  92. // 刷新页面
  93. - (void)refreshView {
  94. if (self.parentArray.count <= 0) {
  95. return;
  96. }
  97. NSMutableArray *titleArray = [NSMutableArray array];
  98. for (NSDictionary *parm in self.parentArray) {
  99. [titleArray addObject:[parm stringValueForKey:@"subjectName"]];
  100. }
  101. [self.segCtrl configWithArray:titleArray];
  102. NSMutableArray *sourceArray = self.instrumentArray[self.chooseIndex];
  103. self.imageArray = sourceArray;
  104. [self.imageCycle reloadData];
  105. [self.imageCycle scrollToItemAtIndex:0 animated:YES];
  106. [self refreshBottomView:0];
  107. }
  108. - (void)refreshBottomView:(NSInteger)index {
  109. InstrumentMessageModel *model = self.imageArray[index];
  110. self.bodyView.subjectName.text = [NSString returnNoNullStringWithString:model.name];
  111. self.bodyView.subjectDesc.text = [NSString returnNoNullStringWithString:model.desc];
  112. [self.bodyView.sureButton setTitle:[NSString stringWithFormat:@"开启%@学习之路",model.name] forState:UIControlStateNormal];
  113. self.chooseInstrument = self.imageArray[index];
  114. }
  115. - (void)configUI {
  116. [self.view addSubview:self.segCtrl];
  117. [self.segCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
  118. make.left.right.top.mas_equalTo(self.view);
  119. make.height.mas_equalTo(60);
  120. }];
  121. [self.view addSubview:self.bodyView];
  122. [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.left.right.mas_equalTo(self.view);
  124. make.top.mas_equalTo(self.segCtrl.mas_bottom);
  125. make.bottom.mas_equalTo(self.view.mas_bottom);
  126. }];
  127. [self.bodyView.chooseView addSubview:self.imageCycle];
  128. [self.imageCycle mas_makeConstraints:^(MASConstraintMaker *make) {
  129. make.left.right.top.bottom.mas_equalTo(self.bodyView.chooseView);
  130. }];
  131. self.imageCycle.itemSize = CGSizeMake(kScreenWidth - 132, 300);
  132. [self.bodyView addSubview:self.pageControl];
  133. [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.left.right.mas_equalTo(self.bodyView);
  135. make.top.mas_equalTo(self.bodyView.chooseView.mas_bottom).offset(10);
  136. make.height.mas_equalTo(15.0f);
  137. }];
  138. }
  139. #pragma mark ----- segCtrl delegate
  140. - (void)didSelectIndex:(NSInteger)chooseIndex {
  141. self.chooseIndex = chooseIndex;
  142. [self refreshView];
  143. }
  144. #pragma mark -- ZKCycleScrollView DataSource
  145. - (NSInteger)numberOfItemsInCycleScrollView:(ZKCycleScrollView *)cycleScrollView {
  146. return self.imageArray.count;
  147. }
  148. - (__kindof UICollectionViewCell *)cycleScrollView:(ZKCycleScrollView *)cycleScrollView cellForItemAtIndex:(NSInteger)index {
  149. SubjectImageCell *cell = [cycleScrollView dequeueReusableCellWithReuseIdentifier:@"SubjectImageCell" forIndex:index];
  150. InstrumentMessageModel *model = self.imageArray[index];
  151. [cell.instrumentImage sd_setImageWithURL:[NSURL URLWithString:[model.img getUrlEndcodeString]]];
  152. return cell;
  153. }
  154. #pragma mark -- ZKCycleScrollView Delegate
  155. - (void)cycleScrollView:(ZKCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
  156. // 对应当前的数据源刷新数据
  157. NSLog(@"index = %zd",index);
  158. }
  159. - (void)cycleScrollViewDidScroll:(ZKCycleScrollView *)cycleScrollView progress:(CGFloat)progress {
  160. // 对应当前的数据源刷新数据
  161. _pageControl.progress = progress;
  162. NSLog(@"progress = %f", progress);
  163. }
  164. - (void)cycleScrollView:(ZKCycleScrollView *)cycleScrollView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
  165. [self refreshBottomView:toIndex];
  166. }
  167. #pragma mark ---- lazying
  168. - (ZKCycleScrollView *)imageCycle {
  169. if (!_imageCycle) {
  170. _imageCycle = [[ZKCycleScrollView alloc] initWithFrame:CGRectZero shouldInfiniteLoop:NO];
  171. _imageCycle.delegate = self;
  172. _imageCycle.dataSource = self;
  173. _imageCycle.hidesPageControl = YES;
  174. _imageCycle.autoScroll = NO;
  175. _imageCycle.itemSpacing = 6.0f;
  176. _imageCycle.itemZoomScale = 0.85;
  177. [_imageCycle registerCellNib:[UINib nibWithNibName:@"SubjectImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"SubjectImageCell"];
  178. }
  179. return _imageCycle;
  180. }
  181. - (CHIPageControlJaloro *)pageControl {
  182. if (!_pageControl) {
  183. _pageControl = [[CHIPageControlJaloro alloc] initWithFrame:CGRectZero];
  184. _pageControl.radius = 3;
  185. _pageControl.padding = 8;
  186. _pageControl.inactiveTransparency = 0.8f;// 未命中点的不透明度
  187. _pageControl.tintColor = HexRGB(0xd8d8d8);
  188. _pageControl.currentPageTintColor = THEMECOLOR;
  189. _pageControl.numberOfPages = self.imageArray.count;
  190. _pageControl.transform = CGAffineTransformMakeScale(0.5, 0.5);
  191. }
  192. return _pageControl;
  193. }
  194. - (KSSegmentControl *)segCtrl {
  195. if (!_segCtrl) {
  196. _segCtrl = [[KSSegmentControl alloc] init];
  197. _segCtrl.delegate = self;
  198. }
  199. return _segCtrl;
  200. }
  201. - (SubjectChooseBodyView *)bodyView {
  202. if (!_bodyView) {
  203. _bodyView = [SubjectChooseBodyView shareInstance];
  204. MJWeakSelf;
  205. [_bodyView chooseSubjectCallback:^{
  206. [weakSelf sureChooseSubject];
  207. }];
  208. }
  209. return _bodyView;
  210. }
  211. - (void)sureChooseSubject {
  212. NSString *subjectId = self.chooseInstrument.internalBaseClassIdentifier;
  213. [self showhud];
  214. [KSNetworkingManager studentSetSubject:KS_GET subjectIds:subjectId success:^(NSDictionary * _Nonnull dic) {
  215. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  216. MJWeakSelf;
  217. [self KSShowMsg:@"设置成功" promptCompletion:^{
  218. if (weakSelf.callback) {
  219. weakSelf.callback();
  220. }
  221. [weakSelf backAction];
  222. }];
  223. }
  224. else {
  225. [self MBPShow:MESSAGEKEY];
  226. }
  227. } faliure:^(NSError * _Nonnull error) {
  228. [self removehub];
  229. }];
  230. }
  231. - (void)backAction {
  232. if (self.isModalPresent) {
  233. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  234. }
  235. else {
  236. [self.navigationController popViewControllerAnimated:YES];
  237. }
  238. }
  239. /*
  240. #pragma mark - Navigation
  241. // In a storyboard-based application, you will often want to do a little preparation before navigation
  242. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  243. // Get the new view controller using [segue destinationViewController].
  244. // Pass the selected object to the new view controller.
  245. }
  246. */
  247. @end