// // AddCourseViewController.m // tongxin // // Created by ecell on 2021/11/25. // Copyright © 2021 xTT. All rights reserved. // #import "AddCourseViewController.h" #import "CourseModel.h" @interface AddCourseViewController () @property (nonatomic, strong)NSMutableArray *weekLabelArr; @property (nonatomic, strong)NSMutableArray *lessionLabelArr; @property (nonatomic, assign)NSInteger curWeek; @property (nonatomic, assign)NSInteger curLession; @end @implementation AddCourseViewController - (void)viewDidLoad { [super viewDidLoad]; if (self.navigationController.viewControllers.count > 1) { self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[myHelper getImageWithName:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)]; } if (_curModel) { self.title = @"修改课程"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(addCourse:)]; } else { self.title = @"添加课程"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addCourse:)]; } [self setUpUI]; [self setUpData]; [self setUpEvent]; } - (void)goBack:(id)sender { if (self.navigationController.viewControllers.count > 1) { [self.navigationController popViewControllerAnimated:YES]; }else{ [self dismissViewControllerAnimated:YES completion:nil]; } } - (void)addCourse:(id)sender{ if (_tfCourseName.text && _tfCourseName.text.length <= 0) { [UICommon MessageErrorText:@"课程名不能为空"]; return; } if (_curWeek == -1) { [UICommon MessageErrorText:@"星期不能为空"]; return; } if (_curLession == -1) { [UICommon MessageErrorText:@"节次不能为空"]; return; } CourseModel *model = [CourseModel courseWithName:_tfCourseName.text dayIndex:_curWeek startCourseIndex:_curLession endCourseIndex:_curLession]; NSString *time = [NSString stringWithFormat:@"%@%@",_tfStartTime.text, _tfEndTime.text]; time = [time stringByReplacingOccurrencesOfString:@":" withString:@""]; if (self.delegate) { [self.delegate addCourse:self course:model time:time]; } [self goBack:nil]; } - (void)setUpData { if (_curModel) { _curWeek = _curModel.dayIndex; _curLession = _curModel.startCourseIndex; UILabel *selectedWeekLab = _weekLabelArr[_curWeek-1]; selectedWeekLab.backgroundColor = mainColor; selectedWeekLab.textColor = [UIColor whiteColor]; selectedWeekLab.layer.borderColor = [UIColor whiteColor].CGColor; UILabel *selectedLession = _lessionLabelArr[_curLession-1]; selectedLession.backgroundColor = mainColor; selectedLession.textColor = [UIColor whiteColor]; selectedLession.layer.borderColor = [UIColor whiteColor].CGColor; _tfCourseName.text = _curModel.courseName; if (_courseTime) { _tfStartTime.text = [_courseTime componentsSeparatedByString:@"-"][0]; _tfEndTime.text = [_courseTime componentsSeparatedByString:@"-"][1]; } } else { _curWeek = -1; _curLession = -1; } } - (void)setUpUI { _weekLabelArr = [NSMutableArray arrayWithArray:@[ _labMon,_labTues,_labWed,_labThur, _labFri,_labSat,_labSun ]]; _lessionLabelArr = [NSMutableArray arrayWithArray:@[ _labFirst,_labSecond,_labThird,_labFourth, _labFifth,_labSixth,_labSeventh,_labEighth ]]; for (int i = 0; i < _weekLabelArr.count; i++) { [self initLabel:_weekLabelArr[i] tag:i+1]; } for (int i = 0; i < _lessionLabelArr.count; i++) { [self initLabel:_lessionLabelArr[i] tag:i+1]; } [self initStartTime:_tfStartTime]; [self initEndTime:_tfEndTime]; } - (void)initLabel:(UILabel *)label tag:(NSInteger)tag { label.userInteractionEnabled = YES; label.textColor = [UIColor blackColor]; label.font = [UIFont systemFontOfSize:12]; label.backgroundColor = [UIColor whiteColor]; label.layer.backgroundColor = [UIColor whiteColor].CGColor; label.layer.cornerRadius = 12; label.layer.borderColor = RGB(240, 240, 240).CGColor; label.layer.borderWidth = 1; label.tag = tag; label.layer.masksToBounds = YES; } - (void)initStartTime:(UITextField *)label { label.layer.cornerRadius = 15; label.layer.borderColor = RGB(240, 240, 240).CGColor; label.layer.borderWidth = 1; label.layer.masksToBounds = YES; UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeTime; datePicker.date = [NSDate new]; if (@available(iOS 13.4, *)) { datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels; } [datePicker addTarget:self action:@selector(startTimeClick:) forControlEvents:UIControlEventValueChanged]; label.inputView = datePicker; } - (void)initEndTime:(UITextField *)label { label.layer.cornerRadius = 15; label.layer.borderColor = RGB(240, 240, 240).CGColor; label.layer.borderWidth = 1; label.layer.masksToBounds = YES; UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeTime; datePicker.date = [NSDate new]; if (@available(iOS 13.4, *)) { datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels; } [datePicker addTarget:self action:@selector(endTimeClick:) forControlEvents:UIControlEventValueChanged]; label.inputView = datePicker; } - (void)startTimeClick:(id)sender{ UIDatePicker * datePic = (UIDatePicker*)sender; NSString *text = [myHelper getDateFormatWithStr:@"HH:mm"date:[datePic date]]; _tfStartTime.text = text; } - (void)endTimeClick:(id)sender{ UIDatePicker * datePic = (UIDatePicker*)sender; NSString *text = [myHelper getDateFormatWithStr:@"HH:mm"date:[datePic date]]; _tfEndTime.text = text; } - (void)setUpEvent { for (UILabel *lab in _weekLabelArr) { UITapGestureRecognizer *uiTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapWeek:)]; [lab addGestureRecognizer:uiTap]; } for (UILabel *lab in _lessionLabelArr) { UITapGestureRecognizer *uiTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLession:)]; [lab addGestureRecognizer:uiTap2]; } } - (void)tapWeek:(UITapGestureRecognizer *)param{ [self resetWeek]; UILabel *lab = (UILabel *)param.view; lab.backgroundColor = mainColor; lab.textColor = [UIColor whiteColor]; lab.layer.borderColor = [UIColor whiteColor].CGColor; _curWeek = lab.tag; NSLog(@"week = %zd",_curWeek); } - (void)tapLession:(UITapGestureRecognizer *)param{ [self resetLession]; UILabel *lab = (UILabel *)param.view; lab.backgroundColor = mainColor; lab.textColor = [UIColor whiteColor]; lab.layer.borderColor = [UIColor whiteColor].CGColor; _curLession = lab.tag; NSLog(@"lession = %zd", _curLession); } - (void) resetWeek { for (UILabel *label in _weekLabelArr) { label.textColor = [UIColor blackColor]; label.backgroundColor = [UIColor whiteColor]; label.layer.backgroundColor = [UIColor whiteColor].CGColor; label.layer.borderColor = RGB(240, 240, 240).CGColor; } } - (void) resetLession { for (UILabel *label in _lessionLabelArr) { label.textColor = [UIColor blackColor]; label.backgroundColor = [UIColor whiteColor]; label.layer.backgroundColor = [UIColor whiteColor].CGColor; label.layer.borderColor = RGB(240, 240, 240).CGColor; } } @end