| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- //
- // CalendarViewController.m
- // YPB_Calendar
- //
- // Created by YYF on 16/10/15.
- // Copyright © 2016年 YangYfei. All rights reserved.
- //
- #import "CalendarViewController.h"
- #import "LYYSelectSchduleAlerView.h"
- @interface CalendarViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
- {
- int daynumber;//天数
- int optiondaynumber;//选择日期数量
- NSTimer *timer;
-
- CalendarDayModel *dayModel1;
- CalendarDayModel *dayModel2;
- NSInteger row;
- NSInteger section;
-
- UIView *navigationView;
- }
- @end
- @implementation CalendarViewController
- @synthesize typeStr;
- -(id)initWithTitleString:(NSString *)string WithTime:(NSString *)time WithType:(NSString *)type
- {
- self = [super init];
- if (self) {
- // Custom initialization
-
- typeStr = type;
-
- }
- return self;
- }
- - (void)backView
- {
- [self.navigationController popViewControllerAnimated:YES];
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- self.view.backgroundColor = [UIColor whiteColor];
-
- [self CreateNavigation:@"选择日期"];
-
- [self.view addSubview:self.weekView];
- [self.view addSubview:self.collectionView];
- [self.weekView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.top.left.right.equalTo(self.view);
- make.height.mas_equalTo(50);
-
- }];
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.top.equalTo(self.weekView.mas_bottom);
- make.left.right.bottom.equalTo(self.view);
-
-
- }];
- }
- -(void)CreateNavigation:(NSString *)titleString
- {
- UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
- btn2.frame = CGRectMake(0, 5, 22, 22);
- UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, iOS11?5:0, 22, 22)];
- image.image = [UIImage imageNamed:@"icon_back_nor3"];
- [btn2 addSubview:image];
- [btn2 addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
- UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
- self.navigationItem.leftBarButtonItems = @[bbi2];
- self.title = titleString;
- }
- #pragma mark - nav_back
- -(void)backBtnClick
- {
- [self.navigationController popViewControllerAnimated:YES];
- }
- -(UICollectionView *)collectionView
- {
-
- if (!_collectionView) {
-
- CalendarMonthCollectionViewLayout *layout = [CalendarMonthCollectionViewLayout new];
-
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(1, 50, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 64 - 50) collectionViewLayout:layout]; //初始化网格视图大小
-
- [_collectionView registerClass:[CalendarDayCell class] forCellWithReuseIdentifier:@"cell"];//cell重用设置ID
-
- [_collectionView registerClass:[CalendarMonthHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
-
- [_collectionView registerClass:[CalendarMonthFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
-
- _collectionView.delegate = self;//实现网格视图的delegate
-
- _collectionView.dataSource = self;//实现网格视图的dataSource
-
- _collectionView.pagingEnabled = NO;
-
- _collectionView.backgroundColor = [UIColor whiteColor];
-
-
- }
- return _collectionView;
- }
- -(CalendarWeekView *)weekView
- {
- if (!_weekView) {
- _weekView = [[CalendarWeekView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
- }
- return _weekView;
- }
- -(NSMutableArray *)selectDaysArray
- {
- if (!_selectDaysArray) {
- _selectDaysArray = [NSMutableArray array];
- }
- return _selectDaysArray;
- }
- #pragma mark - 逻辑代码初始化
- //飞机初始化方法
- - (void)setAirPlaneToDay:(int)day ToDateforString:(NSString *)todate
- {
- daynumber = day;
- optiondaynumber = 1;//选择一个后返回数据对象
- self.calendarMonth = [self getMonthArrayOfDayNumber:daynumber ToDateforString:todate];
- [self.collectionView reloadData];//刷新
- }
- //获取时间段内的天数数组
- - (NSMutableArray *)getMonthArrayOfDayNumber:(int)day ToDateforString:(NSString *)todate
- {
-
- NSDate *date = [NSDate date];
-
- NSDate *selectdate = [NSDate date];
-
- if (todate) {
-
- selectdate = [selectdate dateFromString:todate];
-
- }
-
- self.Logic = [[CalendarLogic alloc]init];
-
- return [self.Logic reloadCalendarView:date selectDate:selectdate needDays:day];
- }
- #pragma mark - UICollectionViewDataSource
- -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
- {
- return self.calendarMonth.count;
- }
- -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
- NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:section];
-
- return monthArray.count;
- }
- //组头
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
- {
- UICollectionReusableView *reusableview = nil;
-
- if (kind == UICollectionElementKindSectionHeader){
-
- NSMutableArray *month_Array = [self.calendarMonth objectAtIndex:indexPath.section];
- CalendarDayModel *model = [month_Array objectAtIndex:15];
-
- CalendarMonthHeaderView *monthHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
-
- monthHeader.masterLabel.text = [NSString stringWithFormat:@"%ld年 %ld月",(unsigned long)model.year,(unsigned long)model.month];//@"日期";
- monthHeader.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
- reusableview = monthHeader;
- }
- else if (kind == UICollectionElementKindSectionFooter){
-
- CalendarMonthFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
- reusableview = footerView;
- }
- return reusableview;
-
- }
- //每个UICollectionView展示的内容
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
-
- CalendarDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
-
- NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
-
- CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
-
- cell.model = model;
-
-
- return cell;
- }
- -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- {
- //过去的和空白不显示的style点击无反应
- NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
- CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
-
- if (model.style == CellDayTypeFutur || model.style == CellDayTypeWeek ||model.style == CellDayTypeClick || model.style == CellDayFristTypeClick || model.style ==CellDaySecondSTypeClick || model.style == CellDayTypeToday) {
-
-
- //多程
- if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"round"]) {
-
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"TIMEINF"] && [[NSUserDefaults standardUserDefaults] objectForKey:@"TIMEINF2"]) {
-
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TIMEINF1"];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TIMEINF2"];
- [self setAirPlaneToDay:365 ToDateforString:nil]; //初始化
- }
-
- NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
- CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
-
-
-
- if (self.selectDaysArray.count == 0) {
- dayModel1 = model;
- dayModel1.style = CellDayFristTypeClick;
- [self.selectDaysArray addObject:dayModel1];
-
- [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]forKey:@"row1"];
- [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"section1"];
- }
- else{
- dayModel2 = model;
- if (dayModel1.year > dayModel2.year ||
- (dayModel1.year == dayModel2.year && dayModel1.month > dayModel2.month) ||
- (dayModel1.year == dayModel2.year && dayModel1.month == dayModel2.month && dayModel1.day > dayModel2.day)) {
- [self.Logic selectLogic:dayModel1];
- [self.selectDaysArray removeAllObjects];
- [self.selectDaysArray addObject:dayModel2];
- dayModel1 = dayModel2;
- dayModel1.style = CellDayFristTypeClick;
-
- }
- else{
- [self.selectDaysArray addObject:dayModel2];
- dayModel2.style = CellDaySecondSTypeClick;
-
- [self.collectionView reloadData];
- if (self.block) {
- self.block(self.selectDaysArray);
- [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[0] date] forKey:@"TIMEINF"];
- [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[1]date] forKey:@"TIMEINF2"];
- }
-
- timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
- }
- }
- }
-
- //单程
- if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"single"]) {
-
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"time3"]) {
-
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"time3"];
- [self setAirPlaneToDay:365 ToDateforString:nil]; //初始化
- }
-
- NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
- CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
-
-
- dayModel1 = model;
- dayModel1.style = CellDayTypeClick;
- [self.selectDaysArray addObject:dayModel1];
-
- [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]forKey:@"row2"];
- [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"section2"];
-
- if (self.block) {
- self.block(self.selectDaysArray);
- [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[0] date] forKey:@"time3"];
- }
- [self onTimer];
- // timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
-
- }
-
-
- [self.collectionView reloadData];
-
- }
- }
- #pragma mark - 日期传值
- -(void)passDate:(CalendarBlock)block
- {
- self.block = block;
- }
- -(void)onTimer
- {
- // [timer invalidate];
- // timer = nil;
-
- [MBProgressHUD showMessage:@"加载中...."];
- [self loadData];
-
- DLog(@"选择日期:%@",_selectDaysArray[0]);
-
- // UIAlertView *view =[ [UIAlertView alloc] initWithTitle:@"点击是这个时间" message:[NSString stringWithFormat:@"%@-%@",[self.selectDaysArray[self.selectDaysArray.count-1] toString],[self.selectDaysArray[self.selectDaysArray.count-1] getWeek] ] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
- // [view show];
-
-
- }
- - (void)loadData
- {
- NSDate *date = [NSDate date];
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
-
-
- [formatter setDateStyle:NSDateFormatterMediumStyle];
-
- [formatter setTimeStyle:NSDateFormatterShortStyle];
-
- [formatter setDateFormat:@"YYYYMMdd"];
- NSString *DateTime = [formatter stringFromDate:date];
-
-
- [LYHTTPRequest Get:KQin_schedulingSetting_allDetail page:0 condition:DateTime Completion:^(id ObjectData, NSError *error) {
- if (error == nil) {
- SLog(@"heihei%@",ObjectData);
-
- NSMutableArray *data = [NSMutableArray array];
- NSArray *arr = ObjectData[@"listdata"];
-
- for (NSInteger i = 0; i < arr.count; i ++) {
- NSDictionary *dic = arr[i];
- JLSchedulingModel *model = [JLSchedulingModel mj_objectWithKeyValues:dic];
-
- SLog(@"%@",model.emdefaultorcodes);
- if ([model.emdefaultors isEqualToString:@"null"]) {
- model.emdefaultors = @"";
- model.emdefaultorcodes = @"";
- }else if ([model.emdefaultorcodes isEqualToString:@"(null)"])
- {
- model.emdefaultors = @"";
- model.emdefaultorcodes = @"";
-
- }
-
- if ([model.emnames isEqualToString:@"null"] || [model.emcodes isEqualToString:@"null"]) {
- model.emnames = @"";
- model.emcodes = @"";
- }
-
-
- [data addObject:model];
-
- }
- [MBProgressHUD hideHUD];
- LYYSelectSchduleAlerView *aletview = [[LYYSelectSchduleAlerView alloc] initWithFrame:self.view.bounds];
-
-
- aletview.SelectSchduleArr = data;
- [aletview setSelectSchBlock:^(NSInteger tag){
-
- JLSchedulingModel *model;
- if (tag < data.count) {
- model = data[tag];
- [self updateSchdulingAday:model];
- }else{
-
-
- }
-
-
-
- }];
-
- [self.view addSubview:aletview];
-
- }else{
-
- [MBProgressHUD hideHUD];
- if (error.code==-1009)
- {
- [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkDisconnect") ToNavi:self.navigationController withColor:nil];
- }else if (error.code==-1001)
- {
- [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkTimeOut") ToNavi:self.navigationController withColor:nil];
- }
- else{
- [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_getDataFaild") ToNavi:self.navigationController withColor:nil];
-
- }
-
-
- }
-
-
-
- }];
- }
- #pragma mark - 上传每一天数据
- -(void)updateSchdulingAday:(JLSchedulingModel *)model
- {
-
- NSString *DateString = [[_selectDaysArray[self.selectDaysArray.count-1] toString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
- NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
- _model.wd_defaultor,@"deptcodes",
- _model.wd_emcode, @"emcodes",
- DateString,"date",
- model.wd_code,@"workcode",
- nil];
- [LYHTTPRequest Post:KQin_schdulingSetting_fixedADay page:dic Completion:^(id ObjectData, NSError *error) {
- if (error== nil) {
-
- [JOShowMessageFromNavi showDropViewWithMessage:@"更改成功" ToNavi:self.navigationController withColor:nil];
- }else
- {
- if (error.code==-1009)
- {
- [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkDisconnect") ToNavi:self.navigationController withColor:nil];
- }else if (error.code==-1001)
- {
- [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkTimeOut") ToNavi:self.navigationController withColor:nil];
- }
-
- else{
- [JOShowMessageFromNavi showDropViewWithMessage:@"更改数据失败" ToNavi:self.navigationController withColor:nil];
-
- }
- }
-
- }];
-
-
- }
- //定位到上次的日期的indexpath
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
-
- if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"round"]) {
-
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"row1"] && [[NSUserDefaults standardUserDefaults]objectForKey:@"section1"]) {
-
- row = [[[NSUserDefaults standardUserDefaults] objectForKey:@"row1"] integerValue];
- section = [[[NSUserDefaults standardUserDefaults] objectForKey:@"section1"] integerValue];
-
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"row1"];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"section1"];
- }
- else{
- row = 0;
- section = 0;
- }
- }
-
- if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"single"]){
-
- if ([[NSUserDefaults standardUserDefaults] objectForKey:@"row2"] && [[NSUserDefaults standardUserDefaults]objectForKey:@"section2"]) {
-
- row = [[[NSUserDefaults standardUserDefaults] objectForKey:@"row2"] integerValue];
- section = [[[NSUserDefaults standardUserDefaults] objectForKey:@"section2"] integerValue];
-
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"row2"];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"section2"];
- }
- else{
- row = 0;
- section = 0;
- }
- }
-
-
- //滑到居中位置
- [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #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
|