123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- //
- // SubjectChooseViewController.m
- // KulexiuForStudent
- //
- // Created by 王智 on 2022/4/5.
- //
- #import "SubjectChooseViewController.h"
- #import "SubjectChooseBodyView.h"
- #import "DZNSegmentedControl.h"
- #import <ZKCycleScrollView.h>
- #import <CHIPageControl/CHIPageControl-Swift.h>
- #import "SubjectImageCell.h"
- #import "InstrumentMessageModel.h"
- #import "KSSegmentControl.h"
- @interface SubjectChooseViewController ()<ZKCycleScrollViewDelegate,ZKCycleScrollViewDataSource,KSSegmentControlDelegate>
- @property (nonatomic, strong) ZKCycleScrollView *imageCycle;
- @property (nonatomic, strong) CHIPageControlJaloro *pageControl;
- @property (nonatomic, strong) SubjectChooseBodyView *bodyView;
- @property (nonatomic, strong) NSMutableArray *imageArray;
- @property (nonatomic, strong) NSMutableArray *parentArray;
- @property (nonatomic, strong) NSMutableArray *instrumentArray;
- @property (nonatomic, assign) NSInteger chooseIndex;
- @property (nonatomic, strong) KSSegmentControl *segCtrl;
- @property (nonatomic, strong) InstrumentMessageModel *chooseInstrument;
- @property (nonatomic, copy) SubjectChooseCallback callback;
- @end
- @implementation SubjectChooseViewController
- - (void)chooseSubjectCallback:(SubjectChooseCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self allocTitle:@"乐器选择"];
- [self configUI];
- [self requestSubjectMessage];
- }
- - (void)requestSubjectMessage {
- [self showhud];
- [KSNetworkingManager studentQuerySubject:KS_GET success:^(NSDictionary * _Nonnull dic) {
- [self removehub];
- if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
- [self evaluateSource:[dic arrayValueForKey:@"data"]];
- }
- else {
- [self MBPShow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [self removehub];
- }];
- }
- - (void)evaluateSource:(NSArray *)sourceArray {
- @autoreleasepool {
- NSMutableArray *valueArray = [sourceArray mutableCopy];
-
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- NSMutableArray *parentNodeArray = [NSMutableArray array];
- NSMutableArray *instrumentArray = [NSMutableArray array];
- for (NSInteger i = 0; i < valueArray.count; i++) {
- NSMutableDictionary *parentNodeDic = [NSMutableDictionary dictionary];
- NSDictionary *parm = valueArray[i];
- [parentNodeDic setValue:[parm stringValueForKey:@"id"] forKey:@"subjectId"];
- [parentNodeDic setValue:[parm stringValueForKey:@"name"] forKey:@"subjectName"];
-
- NSArray *subjects = [parm arrayValueForKey:@"subjects"];
- if (subjects.count > 0) { // 存在子节点,才添加父节点
- // 添加父节点分类
- [parentNodeArray addObject:parentNodeDic];
-
- NSMutableArray *subjectModelArray = [NSMutableArray array];
- for (NSDictionary *dic in subjects) {
- InstrumentMessageModel *model = [[InstrumentMessageModel alloc] initWithDictionary:dic];
- [subjectModelArray addObject:model];
- }
- [instrumentArray addObject:subjectModelArray];
- }
- }
- self.parentArray = [NSMutableArray arrayWithArray:parentNodeArray];
- self.instrumentArray = [NSMutableArray arrayWithArray:instrumentArray];
- dispatch_main_async_safe(^{
- [self resetChooseStatus];
- [self refreshView];
- });
- });
- };
- }
- - (void)resetChooseStatus {
- self.chooseIndex = 0;
- NSMutableArray *sourceArray = self.instrumentArray[self.chooseIndex];
- self.imageArray = sourceArray;
- self.chooseInstrument = self.imageArray[self.chooseIndex];
- }
- // 刷新页面
- - (void)refreshView {
- if (self.parentArray.count <= 0) {
- return;
- }
- NSMutableArray *titleArray = [NSMutableArray array];
- for (NSDictionary *parm in self.parentArray) {
- [titleArray addObject:[parm stringValueForKey:@"subjectName"]];
- }
- [self.segCtrl configWithArray:titleArray];
- NSMutableArray *sourceArray = self.instrumentArray[self.chooseIndex];
- self.imageArray = sourceArray;
- [self.imageCycle reloadData];
- [self.imageCycle scrollToItemAtIndex:0 animated:YES];
- [self refreshBottomView:0];
- }
- - (void)refreshBottomView:(NSInteger)index {
- InstrumentMessageModel *model = self.imageArray[index];
- self.bodyView.subjectName.text = [NSString returnNoNullStringWithString:model.name];
- self.bodyView.subjectDesc.text = [NSString returnNoNullStringWithString:model.desc];
- [self.bodyView.sureButton setTitle:[NSString stringWithFormat:@"开启%@学习之路",model.name] forState:UIControlStateNormal];
- self.chooseInstrument = self.imageArray[index];
- }
- - (void)configUI {
- [self.view addSubview:self.segCtrl];
- [self.segCtrl mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.mas_equalTo(self.view);
- make.height.mas_equalTo(60);
- }];
-
- [self.view addSubview:self.bodyView];
- [self.bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self.view);
- make.top.mas_equalTo(self.segCtrl.mas_bottom);
- make.bottom.mas_equalTo(self.view.mas_bottom);
- }];
- [self.bodyView.chooseView addSubview:self.imageCycle];
- [self.imageCycle mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.bottom.mas_equalTo(self.bodyView.chooseView);
- }];
- self.imageCycle.itemSize = CGSizeMake(kScreenWidth - 132, 300);
- [self.bodyView addSubview:self.pageControl];
- [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self.bodyView);
- make.top.mas_equalTo(self.bodyView.chooseView.mas_bottom).offset(10);
- make.height.mas_equalTo(15.0f);
- }];
-
- }
- #pragma mark ----- segCtrl delegate
- - (void)didSelectIndex:(NSInteger)chooseIndex {
- self.chooseIndex = chooseIndex;
- [self refreshView];
- }
- #pragma mark -- ZKCycleScrollView DataSource
- - (NSInteger)numberOfItemsInCycleScrollView:(ZKCycleScrollView *)cycleScrollView {
- return self.imageArray.count;
- }
- - (__kindof UICollectionViewCell *)cycleScrollView:(ZKCycleScrollView *)cycleScrollView cellForItemAtIndex:(NSInteger)index {
- SubjectImageCell *cell = [cycleScrollView dequeueReusableCellWithReuseIdentifier:@"SubjectImageCell" forIndex:index];
- InstrumentMessageModel *model = self.imageArray[index];
- [cell.instrumentImage sd_setImageWithURL:[NSURL URLWithString:[model.img getUrlEndcodeString]]];
- return cell;
- }
- #pragma mark -- ZKCycleScrollView Delegate
- - (void)cycleScrollView:(ZKCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
- // 对应当前的数据源刷新数据
- NSLog(@"index = %zd",index);
- }
- - (void)cycleScrollViewDidScroll:(ZKCycleScrollView *)cycleScrollView progress:(CGFloat)progress {
- // 对应当前的数据源刷新数据
-
- _pageControl.progress = progress;
- NSLog(@"progress = %f", progress);
- }
- - (void)cycleScrollView:(ZKCycleScrollView *)cycleScrollView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
-
- [self refreshBottomView:toIndex];
-
- }
- #pragma mark ---- lazying
- - (ZKCycleScrollView *)imageCycle {
- if (!_imageCycle) {
- _imageCycle = [[ZKCycleScrollView alloc] initWithFrame:CGRectZero shouldInfiniteLoop:NO];
- _imageCycle.delegate = self;
- _imageCycle.dataSource = self;
- _imageCycle.hidesPageControl = YES;
- _imageCycle.autoScroll = NO;
- _imageCycle.itemSpacing = 6.0f;
- _imageCycle.itemZoomScale = 0.85;
- [_imageCycle registerCellNib:[UINib nibWithNibName:@"SubjectImageCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"SubjectImageCell"];
- }
- return _imageCycle;
- }
- - (CHIPageControlJaloro *)pageControl {
- if (!_pageControl) {
- _pageControl = [[CHIPageControlJaloro alloc] initWithFrame:CGRectZero];
- _pageControl.radius = 3;
- _pageControl.padding = 8;
- _pageControl.inactiveTransparency = 0.8f;// 未命中点的不透明度
- _pageControl.tintColor = HexRGB(0xd8d8d8);
- _pageControl.currentPageTintColor = THEMECOLOR;
- _pageControl.numberOfPages = self.imageArray.count;
- _pageControl.transform = CGAffineTransformMakeScale(0.5, 0.5);
- }
- return _pageControl;
- }
- - (KSSegmentControl *)segCtrl {
- if (!_segCtrl) {
- _segCtrl = [[KSSegmentControl alloc] init];
- _segCtrl.delegate = self;
- }
- return _segCtrl;
- }
- - (SubjectChooseBodyView *)bodyView {
- if (!_bodyView) {
- _bodyView = [SubjectChooseBodyView shareInstance];
- MJWeakSelf;
- [_bodyView chooseSubjectCallback:^{
- [weakSelf sureChooseSubject];
- }];
- }
- return _bodyView;
- }
- - (void)sureChooseSubject {
- NSString *subjectId = self.chooseInstrument.internalBaseClassIdentifier;
- [self showhud];
- [KSNetworkingManager studentSetSubject:KS_GET subjectIds:subjectId success:^(NSDictionary * _Nonnull dic) {
- if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
- MJWeakSelf;
- [self KSShowMsg:@"设置成功" promptCompletion:^{
- if (weakSelf.callback) {
- weakSelf.callback();
- }
- [weakSelf backAction];
- }];
- }
- else {
- [self MBPShow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [self removehub];
- }];
- }
- - (void)backAction {
- if (self.isModalPresent) {
- [self.navigationController dismissViewControllerAnimated:YES completion:nil];
- }
- else {
- [self.navigationController popViewControllerAnimated:YES];
- }
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|