You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
11 KiB

//
// CourseViewController.m
// tongxin
//
// Created by ecell on 2021/11/24.
// Copyright © 2021 xTT. All rights reserved.
//
#import "CourseViewController.h"
#import "GWPCourseListView.h"
#import "CourseModel.h"
#import "AddCourseViewController.h"
#import "CourseSchedule.h"
//#import <GWPKit/GWPKit.h>
@interface CourseViewController ()<GWPCourseListViewDataSource, GWPCourseListViewDelegate, AddCousrDelegate>
@property (nonatomic, strong) GWPCourseListView *courseListView;
@property (nonatomic, strong) UILabel *addBtn;
@property (nonatomic, strong) NSMutableArray<CourseModel*> *courseArr;
@property (nonatomic, strong) CourseSchedule *curSchedule;
@property (nonatomic, strong) NSMutableDictionary *timetableDict;
@property (nonatomic, strong) NSArray *keyArray;
@end
@implementation CourseViewController
- (NSMutableArray<CourseModel *> *)courseArr{
if (!_courseArr) {
//CourseModel *a = [CourseModel courseWithName:@"PHP" dayIndex:1 startCourseIndex:1 endCourseIndex:2];
//CourseModel *b = [CourseModel courseWithName:@"Java" dayIndex:1 startCourseIndex:3 endCourseIndex:3];
//CourseModel *c = [CourseModel courseWithName:@"C++" dayIndex:1 startCourseIndex:4 endCourseIndex:6];
//CourseModel *d = [CourseModel courseWithName:@"C#" dayIndex:2 startCourseIndex:4 endCourseIndex:4];
//CourseModel *e = [CourseModel courseWithName:@"javascript" dayIndex:7 startCourseIndex:5 endCourseIndex:6];
//_courseArr = [NSMutableArray arrayWithArray:@[a,b,c,d,e]];
_courseArr = [NSMutableArray new];
}
return _courseArr;
}
- (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:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveSchedule:)];
}
self.title = @"课程表";
self.curSchedule = [[CourseSchedule alloc] init];
self.keyArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
[self initTimeTable];
self.courseListView = [[GWPCourseListView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
self.courseListView.delegate = self;
self.courseListView.dataSource = self;
[self.view addSubview:self.courseListView];
self.addBtn = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-120, ScreenHeight-240, 68, 68)];
self.addBtn.text = @"+";
self.addBtn.textAlignment = NSTextAlignmentCenter;
self.addBtn.font = [UIFont systemFontOfSize:48];
self.addBtn.textColor = RGB(255, 255, 255);
self.addBtn.userInteractionEnabled = YES;
self.addBtn.layer.cornerRadius = 34;
self.addBtn.clipsToBounds = YES;
self.addBtn.backgroundColor = mainColor;
[self.addBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCourse)]];
[self.view addSubview:self.addBtn];
[self getScheduleFromServer];
}
- (void)saveSchedule:(id)sender {
CourseSchedule *api = [[CourseSchedule alloc] init];
[api save:[self getTimeTableParameter] schedule:[self getScheduleParameter] success:^{
[SVProgressHUD showSuccessWithStatus:@"保存成功"];
} failure:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:@"保存失败"];
}];
NSLog(@"%@ %@",[self getScheduleParameter],[self getTimeTableParameter]);
}
- (NSString *)getTimeTableParameter {
NSString *result = @"";
for (NSString *timeKey in _keyArray) {
NSString *timeValue = [_timetableDict objectForKey:timeKey];
result = [result stringByAppendingString:@"-"];
result = [result stringByAppendingString:timeValue];
}
return [result substringFromIndex:1];
}
- (NSString *)getScheduleParameter {
NSString *result = @"";
for (CourseModel *model in self.courseArr) {
result = [result stringByAppendingString:@"-"];
result = [result stringByAppendingString:[NSString stringWithFormat:@"%zd_%zd_%@",model.dayIndex,model.startCourseIndex,model.courseName]];
}
return [result substringFromIndex:1];
}
- (void)getScheduleFromServer {
WEAKSELF
[self.curSchedule query:^(CourseSchedule *model) {
[weakSelf dealOriginalSchedule:model];
} failure:^(NSError *error) {
}];
}
- (void)dealOriginalSchedule:(CourseSchedule *)model {
if (model.schedule) {
NSArray *array = [model.schedule componentsSeparatedByString:@"-"]; // 星期_节次_课程-...-星期_节次_课程-...-星期_节次_课程
for (NSString *course in array) {
NSArray *cor = [course componentsSeparatedByString:@"_"];
if (cor.count != 3) {
continue;
}
CourseModel *cm = [CourseModel courseWithName:cor[2] dayIndex:[cor[0] intValue] startCourseIndex:[cor[1] intValue] endCourseIndex:[cor[1] intValue]];
[self.courseArr addObject:cm];
}
}
//xxxxxxxx-xxxxxxxx-hhmmhhmm-xxxxxxxx
if (model.timetable && model.timetable.length > 8) {
NSArray *items = [model.timetable componentsSeparatedByString:@"-"];
if (items.count == 8) {
for (int i=0; i<_keyArray.count; i++) {
[self.timetableDict setValue:items[i] forKey:_keyArray[i]];
}
}
}
[self.courseListView reloadData];
}
- (void)initTimeTable {
_timetableDict = [NSMutableDictionary dictionary];
for (int i=0; i<_keyArray.count; i++) {
[_timetableDict setValue:@"00000000" forKey:_keyArray[i]];
}
}
- (NSString *)getStartTimeAndEndTime:(NSString *)key {
NSString *time = [_timetableDict objectForKey:key];
NSString *hh = [time substringWithRange:NSMakeRange(0, 2)];
NSString *mm = [time substringWithRange:NSMakeRange(2, 2)];
NSString *hh2 = [time substringWithRange:NSMakeRange(4, 2)];
NSString *mm2 = [time substringWithRange:NSMakeRange(6, 2)];
return [NSString stringWithFormat:@"%@:%@-%@:%@",hh,mm,hh2,mm2];
}
- (void)addCourse {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
AddCourseViewController *vc =[sb instantiateViewControllerWithIdentifier:@"AddCourseViewController"];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
#pragma call back
- (void)addCourse:(AddCourseViewController *)vc course:(CourseModel *)model time:time {
//CourseModel *a = [CourseModel courseWithName:@"Golang Hello World" dayIndex:3 startCourseIndex:1 endCourseIndex:2];
[self.timetableDict setValue:time forKey:[NSString stringWithFormat:@"%ld",model.startCourseIndex]];
BOOL found = NO;
for (CourseModel *cm in self.courseArr) {
//修改
if ([cm isSameCourse:model]) {
cm.courseName = model.courseName;
found = YES;
break;
}
}
// 新增 化学 物理 自然 劳动 科技 自然 英文 数学 语文
if (!found) {
[self.courseArr addObject:model];
}
[self.courseListView reloadData];
NSLog(@"============%@", time);
}
- (void)goBack:(id)sender {
if (self.navigationController.viewControllers.count > 1) {
[self.navigationController popViewControllerAnimated:YES];
}else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - GWPCourseListViewDataSource
- (NSArray<id<Course>> *)courseForCourseListView:(GWPCourseListView *)courseListView{
return self.courseArr;
}
/** 课程单元背景色自定义 */
- (UIColor *)courseListView:(GWPCourseListView *)courseListView courseTitleBackgroundColorForCourse:(id<Course>)course{
//if ([course isEqual:[self.courseArr firstObject]]) { // 第一个,返回自定义颜色
// return [UIColor blueColor];
//}
// 其他返回默认的随机色
return nil;
}
/** 设置选项卡的title的文字属性,如果实现该方法,该方法返回的attribute将会是attributeString的属性 */
- (NSDictionary*)courseListView:(GWPCourseListView *)courseListView titleAttributesInTopbarAtIndex:(NSInteger)index{
//if (index==0) {
// return @{NSForegroundColorAttributeName:[UIColor greenColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};
//}
return nil;
}
/** 设置选项卡的title的背景颜色,默认白色 */
- (UIColor*)courseListView:(GWPCourseListView *)courseListView titleBackgroundColorInTopbarAtIndex:(NSInteger)index{
//if (index==1) {
// return [UIColor purpleColor];
//}
return nil;
}
#pragma mark - GWPCourseListViewDelegate
/** 选中(点击)某一个课程单元之后的回调 */
- (void)courseListView:(GWPCourseListView *)courseListView didSelectedCourse:(id<Course>)course{
CourseModel *model = (CourseModel *)course;
if (model) {
[self showOperationDialog:model];
}
//NSLog(@"SELECT %zd_%zd", courseListView.selectedIndex, courseListView.selectedRow);
}
- (void)showOperationDialog:(CourseModel *)model {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"修改或删除数据" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"点击了取消");
}];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
NSLog(@"点击了删除");
for (int i=0; i < [self.courseArr count]; i++) {
CourseModel *cm = [self.courseArr objectAtIndex:i];
if ([cm isSameCourse:model]) {
[self.courseArr removeObject:cm];
[self.courseListView reloadData];
break;
}
}
}];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
AddCourseViewController *vc =[sb instantiateViewControllerWithIdentifier:@"AddCourseViewController"];
vc.delegate = self;
vc.curModel = model;
vc.courseTime = [self getStartTimeAndEndTime:[NSString stringWithFormat:@"%zd",model.startCourseIndex]];
[self.navigationController pushViewController:vc animated:YES];
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];
}
@end