123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // InstrumentChooseViewController.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2022/4/11.
- //
- #import "InstrumentChooseViewController.h"
- #import "InstrumentMessageModel.h"
- #import "InstrumentChooseCell.h"
- #import "InstrumentChooseBottonView.h"
- #import "InstrumentHeaderView.h"
- #define INSTRUMENT_ROWCOUNT (IS_IPAD ? 5 : 3)
- @interface InstrumentChooseViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
- @property (nonatomic, copy) ChooseInstrumentCallback callback;
- @property (nonatomic, copy) UICollectionView *collectionView;
- @property (nonatomic, strong) NSMutableArray *parentArray;
- @property (nonatomic, strong) NSMutableArray *instrumentArray;
- @property (nonatomic, copy) InstrumentChooseBottonView *bottomView;
- @property (nonatomic, strong) NSMutableArray *chooseArray;
- @property (nonatomic, strong) InstrumentMessageModel *chooseModel;
- @property (nonatomic, strong) NSIndexPath *chooseIndexPath;
- @end
- @implementation InstrumentChooseViewController
- - (void)chooseCallback:(ChooseInstrumentCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- NSString *headTitle = self.isSingleChoose ? @"乐器选择" : @"可教授乐器";
- [self allocTitle:headTitle];
- [self configUI];
- [self requestInstrumentMessage];
- }
- - (void)configUI {
- [self.scrollView removeFromSuperview];
- [self.view addSubview:self.bottomView];
- [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(self.view);
- make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin);
- make.height.mas_equalTo(90);
- }];
-
- UIView *headView = [[UIView alloc] init];
- headView.layer.backgroundColor = [UIColor whiteColor].CGColor;
- [self.view addSubview:headView];
- [headView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.mas_equalTo(self.view.mas_top).mas_offset(10);
- make.left.mas_equalTo(self.view.mas_left).offset(14.0f);
- make.right.mas_equalTo(self.view.mas_right).offset(-14.0f);
- make.height.mas_equalTo(36.0f);
- }];
-
- headView.layer.cornerRadius = 10.0f;
- headView.layer.masksToBounds = YES;
-
- UILabel *tipsLabel = [[UILabel alloc] init];
- tipsLabel.text = self.isSingleChoose ? @"最多可选择1个乐器" : @"最多可选择5个乐器";
- tipsLabel.textColor = HexRGB(0xFF9E5A);
- tipsLabel.font = [UIFont systemFontOfSize:14.0f];
- [headView addSubview:tipsLabel];
- [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(headView.mas_left).offset(11.0f);
- make.right.mas_equalTo(headView.mas_right).offset(-11.0f);
- make.height.mas_equalTo(22.0f);
- make.centerY.mas_equalTo(headView.mas_centerY);
- }];
-
- [self.view addSubview:self.collectionView];
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.view.mas_left).offset(15);
- make.right.mas_equalTo(self.view.mas_right).offset(-15);
- make.top.mas_equalTo(headView.mas_bottom);
- make.bottom.mas_equalTo(self.bottomView.mas_top);
- }];
- }
- - (void)requestInstrumentMessage {
- [LOADING_MANAGER showHUD];
- [KSNetworkingManager querySubjectAllRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
- [LOADING_MANAGER removeHUD];
- if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
- [self evaluateSource:[dic ks_arrayValueForKey:@"data"]];
- }
- else {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
- }
- } faliure:^(NSError * _Nonnull error) {
- [LOADING_MANAGER removeHUD];
-
- }];
- }
- - (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 ks_stringValueForKey:@"id"] forKey:@"subjectId"];
- [parentNodeDic setValue:[parm ks_stringValueForKey:@"name"] forKey:@"subjectName"];
-
- NSArray *subjects = [parm ks_arrayValueForKey:@"subjects"];
- if (subjects.count > 0) {
- // 添加父节点分类
- [parentNodeArray addObject:parentNodeDic];
-
- NSMutableArray *subjectModelArray = [NSMutableArray array];
- for (NSDictionary *dic in subjects) {
- InstrumentMessageModel *model = [[InstrumentMessageModel alloc] initWithDictionary:dic];
- if (self.preChooseArray.count && [self judgePreChooseInstrument:model.internalBaseClassIdentifier]) {
- model.isChoose = YES;
- [self.chooseArray addObject:model];
- }
- [subjectModelArray addObject:model];
- }
- [instrumentArray addObject:subjectModelArray];
- }
- }
- self.parentArray = [NSMutableArray arrayWithArray:parentNodeArray];
- self.instrumentArray = [NSMutableArray arrayWithArray:instrumentArray];
- dispatch_main_async_safe(^{
- // 刷新
- [self refreshView];
- });
- });
- };
- }
- - (BOOL)judgePreChooseInstrument:(NSString *)instrumentId {
- BOOL contain = NO;
- for (NSDictionary *parm in self.preChooseArray) {
- if ([[parm valueForKey:@"subjectId"] isEqualToString:instrumentId]) {
- contain = YES;
- break;
- }
- }
- return contain;
- }
- - (void)refreshView {
- [self.collectionView reloadData];
- }
- #pragma mark ----- collection view data source
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return self.parentArray.count;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- NSMutableArray *instrumentArray = self.instrumentArray[section];
- return instrumentArray.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- NSMutableArray *instrumentArray = self.instrumentArray[indexPath.section];
- InstrumentMessageModel *model = instrumentArray[indexPath.item];
- InstrumentChooseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"InstrumentChooseCell" forIndexPath:indexPath];
- [cell configCellWithModel:model];
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSMutableArray *instrumentArray = self.instrumentArray[indexPath.section];
- InstrumentMessageModel *model = instrumentArray[indexPath.item];
- model.isChoose = !model.isChoose;
- [self countMessageWithModel:model indexPath:indexPath];
- }
- - (void)countMessageWithModel:(InstrumentMessageModel *)model indexPath:(NSIndexPath *)indexPath {
- if (self.isSingleChoose) { // 单选
-
- if (model.isChoose) {
- // 取消上一个选中
- if (self.chooseModel) {
- self.chooseModel.isChoose = NO;
- [self.collectionView reloadItemsAtIndexPaths:@[indexPath,self.chooseIndexPath]];
- }
- self.chooseModel = model;
- self.chooseIndexPath = indexPath;
- }
- else {
- self.chooseModel = nil;
- self.chooseIndexPath = nil;
- }
-
- if (self.chooseIndexPath) {
- [self.collectionView reloadItemsAtIndexPaths:@[indexPath,self.chooseIndexPath]];
- }
- else {
- [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
- }
- }
- else { // 多选
- if (model.isChoose) {
- if (![self judgeContainModel:model]) {
- if (self.chooseArray.count == 5) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"最多只能选择5个乐器"];
- model.isChoose = NO;
- return;
- }
- [self.chooseArray addObject:model];
- }
- }
- else {
- if ([self judgeContainModel:model]) {
- [self removeWithInstrumentId:model.internalBaseClassIdentifier];
- }
- }
- [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
- }
-
- }
- - (BOOL)judgeContainModel:(InstrumentMessageModel *)model {
- BOOL contain = NO;
- for (InstrumentMessageModel *chooseModel in self.chooseArray) {
- if ([chooseModel.internalBaseClassIdentifier isEqualToString:model.internalBaseClassIdentifier]) {
- contain = YES;
- break;
- }
- }
- return contain;
- }
- - (void)removeWithInstrumentId:(NSString *)instrumentId {
- for (InstrumentMessageModel *chooseModel in self.chooseArray) {
- if ([chooseModel.internalBaseClassIdentifier isEqualToString:instrumentId]) {
- [self.chooseArray removeObject:chooseModel];
- break;
- }
- }
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
- NSDictionary *parm = self.parentArray[indexPath.section];
- NSString *subjectName = [parm ks_stringValueForKey:@"subjectName"];
- InstrumentHeaderView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"InstrumentHeaderView" forIndexPath:indexPath];
- [headView evaluateWithMessage:subjectName];
- return headView;
- }
- #pragma mark ------ lazying
- - (NSMutableArray *)chooseArray {
- if (!_chooseArray) {
- _chooseArray = [NSMutableArray array];
- }
- return _chooseArray;
- }
- - (UICollectionView *)collectionView {
- if (!_collectionView) {
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- CGFloat width = (NSInteger)((KPortraitWidth - 15 * 2 - (INSTRUMENT_ROWCOUNT-1)* 10) / INSTRUMENT_ROWCOUNT);
- CGFloat height = width;
- layout.itemSize = CGSizeMake(width, height);
- layout.headerReferenceSize = CGSizeMake(kScreen_Width - 30, 40);
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
- _collectionView.backgroundColor = [UIColor clearColor];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.showsVerticalScrollIndicator = NO;
- _collectionView.showsHorizontalScrollIndicator = NO;
- [_collectionView registerNib:[UINib nibWithNibName:@"InstrumentChooseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"InstrumentChooseCell"];
- [_collectionView registerNib:[UINib nibWithNibName:@"InstrumentHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"InstrumentHeaderView"];
- }
- return _collectionView;
- }
- - (InstrumentChooseBottonView *)bottomView {
- if (!_bottomView) {
- _bottomView = [InstrumentChooseBottonView shareInstance];
- MJWeakSelf;
- [_bottomView sureCallback:^{
- [weakSelf chooseInstrumentAction];
- }];
- }
- return _bottomView;
- }
- - (void)chooseInstrumentAction {
- if (self.isSingleChoose) {
- if (self.chooseModel == nil) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请选择乐器"];
- return;
- }
- NSMutableArray *instrumentArray = [NSMutableArray array];
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- [parm setValue:self.chooseModel.name forKey:@"subjectName"];
- [parm setValue:self.chooseModel.internalBaseClassIdentifier forKey:@"subjectId"];
- [instrumentArray addObject:parm];
- if (self.callback) {
- self.callback(instrumentArray);
- }
- }
- else {
- if (self.chooseArray.count == 0) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:@"请选择可教授乐器"];
- return;
- }
- NSMutableArray *instrumentArray = [NSMutableArray array];
- for (InstrumentMessageModel *model in self.chooseArray) {
- NSMutableDictionary *parm = [NSMutableDictionary dictionary];
- [parm setValue:model.name forKey:@"subjectName"];
- [parm setValue:model.internalBaseClassIdentifier forKey:@"subjectId"];
- [instrumentArray addObject:parm];
- }
- if (self.callback) {
- self.callback(instrumentArray);
- }
- }
-
- [self backAction];
- }
- /*
- #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
|