CalendarViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //
  2. // CalendarViewController.m
  3. // YPB_Calendar
  4. //
  5. // Created by YYF on 16/10/15.
  6. // Copyright © 2016年 YangYfei. All rights reserved.
  7. //
  8. #import "CalendarViewController.h"
  9. #import "LYYSelectSchduleAlerView.h"
  10. @interface CalendarViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
  11. {
  12. int daynumber;//天数
  13. int optiondaynumber;//选择日期数量
  14. NSTimer *timer;
  15. CalendarDayModel *dayModel1;
  16. CalendarDayModel *dayModel2;
  17. NSInteger row;
  18. NSInteger section;
  19. UIView *navigationView;
  20. }
  21. @end
  22. @implementation CalendarViewController
  23. @synthesize typeStr;
  24. -(id)initWithTitleString:(NSString *)string WithTime:(NSString *)time WithType:(NSString *)type
  25. {
  26. self = [super init];
  27. if (self) {
  28. // Custom initialization
  29. typeStr = type;
  30. }
  31. return self;
  32. }
  33. - (void)backView
  34. {
  35. [self.navigationController popViewControllerAnimated:YES];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. self.view.backgroundColor = [UIColor whiteColor];
  41. [self CreateNavigation:@"选择日期"];
  42. [self.view addSubview:self.weekView];
  43. [self.view addSubview:self.collectionView];
  44. [self.weekView mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.top.left.right.equalTo(self.view);
  46. make.height.mas_equalTo(50);
  47. }];
  48. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.top.equalTo(self.weekView.mas_bottom);
  50. make.left.right.bottom.equalTo(self.view);
  51. }];
  52. }
  53. -(void)CreateNavigation:(NSString *)titleString
  54. {
  55. UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
  56. btn2.frame = CGRectMake(0, 5, 22, 22);
  57. UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, iOS11?5:0, 22, 22)];
  58. image.image = [UIImage imageNamed:@"icon_back_nor3"];
  59. [btn2 addSubview:image];
  60. [btn2 addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
  61. UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithCustomView:btn2];
  62. self.navigationItem.leftBarButtonItems = @[bbi2];
  63. self.title = titleString;
  64. }
  65. #pragma mark - nav_back
  66. -(void)backBtnClick
  67. {
  68. [self.navigationController popViewControllerAnimated:YES];
  69. }
  70. -(UICollectionView *)collectionView
  71. {
  72. if (!_collectionView) {
  73. CalendarMonthCollectionViewLayout *layout = [CalendarMonthCollectionViewLayout new];
  74. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(1, 50, SCREEN_WIDTH - 2, SCREEN_HEIGHT - 64 - 50) collectionViewLayout:layout]; //初始化网格视图大小
  75. [_collectionView registerClass:[CalendarDayCell class] forCellWithReuseIdentifier:@"cell"];//cell重用设置ID
  76. [_collectionView registerClass:[CalendarMonthHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
  77. [_collectionView registerClass:[CalendarMonthFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
  78. _collectionView.delegate = self;//实现网格视图的delegate
  79. _collectionView.dataSource = self;//实现网格视图的dataSource
  80. _collectionView.pagingEnabled = NO;
  81. _collectionView.backgroundColor = [UIColor whiteColor];
  82. }
  83. return _collectionView;
  84. }
  85. -(CalendarWeekView *)weekView
  86. {
  87. if (!_weekView) {
  88. _weekView = [[CalendarWeekView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
  89. }
  90. return _weekView;
  91. }
  92. -(NSMutableArray *)selectDaysArray
  93. {
  94. if (!_selectDaysArray) {
  95. _selectDaysArray = [NSMutableArray array];
  96. }
  97. return _selectDaysArray;
  98. }
  99. #pragma mark - 逻辑代码初始化
  100. //飞机初始化方法
  101. - (void)setAirPlaneToDay:(int)day ToDateforString:(NSString *)todate
  102. {
  103. daynumber = day;
  104. optiondaynumber = 1;//选择一个后返回数据对象
  105. self.calendarMonth = [self getMonthArrayOfDayNumber:daynumber ToDateforString:todate];
  106. [self.collectionView reloadData];//刷新
  107. }
  108. //获取时间段内的天数数组
  109. - (NSMutableArray *)getMonthArrayOfDayNumber:(int)day ToDateforString:(NSString *)todate
  110. {
  111. NSDate *date = [NSDate date];
  112. NSDate *selectdate = [NSDate date];
  113. if (todate) {
  114. selectdate = [selectdate dateFromString:todate];
  115. }
  116. self.Logic = [[CalendarLogic alloc]init];
  117. return [self.Logic reloadCalendarView:date selectDate:selectdate needDays:day];
  118. }
  119. #pragma mark - UICollectionViewDataSource
  120. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  121. {
  122. return self.calendarMonth.count;
  123. }
  124. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  125. {
  126. NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:section];
  127. return monthArray.count;
  128. }
  129. //组头
  130. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  131. {
  132. UICollectionReusableView *reusableview = nil;
  133. if (kind == UICollectionElementKindSectionHeader){
  134. NSMutableArray *month_Array = [self.calendarMonth objectAtIndex:indexPath.section];
  135. CalendarDayModel *model = [month_Array objectAtIndex:15];
  136. CalendarMonthHeaderView *monthHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
  137. monthHeader.masterLabel.text = [NSString stringWithFormat:@"%ld年 %ld月",(unsigned long)model.year,(unsigned long)model.month];//@"日期";
  138. monthHeader.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
  139. reusableview = monthHeader;
  140. }
  141. else if (kind == UICollectionElementKindSectionFooter){
  142. CalendarMonthFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
  143. reusableview = footerView;
  144. }
  145. return reusableview;
  146. }
  147. //每个UICollectionView展示的内容
  148. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. CalendarDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  151. NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
  152. CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
  153. cell.model = model;
  154. return cell;
  155. }
  156. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. //过去的和空白不显示的style点击无反应
  159. NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
  160. CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
  161. if (model.style == CellDayTypeFutur || model.style == CellDayTypeWeek ||model.style == CellDayTypeClick || model.style == CellDayFristTypeClick || model.style ==CellDaySecondSTypeClick || model.style == CellDayTypeToday) {
  162. //多程
  163. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"round"]) {
  164. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"TIMEINF"] && [[NSUserDefaults standardUserDefaults] objectForKey:@"TIMEINF2"]) {
  165. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TIMEINF1"];
  166. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TIMEINF2"];
  167. [self setAirPlaneToDay:365 ToDateforString:nil]; //初始化
  168. }
  169. NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
  170. CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
  171. if (self.selectDaysArray.count == 0) {
  172. dayModel1 = model;
  173. dayModel1.style = CellDayFristTypeClick;
  174. [self.selectDaysArray addObject:dayModel1];
  175. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]forKey:@"row1"];
  176. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"section1"];
  177. }
  178. else{
  179. dayModel2 = model;
  180. if (dayModel1.year > dayModel2.year ||
  181. (dayModel1.year == dayModel2.year && dayModel1.month > dayModel2.month) ||
  182. (dayModel1.year == dayModel2.year && dayModel1.month == dayModel2.month && dayModel1.day > dayModel2.day)) {
  183. [self.Logic selectLogic:dayModel1];
  184. [self.selectDaysArray removeAllObjects];
  185. [self.selectDaysArray addObject:dayModel2];
  186. dayModel1 = dayModel2;
  187. dayModel1.style = CellDayFristTypeClick;
  188. }
  189. else{
  190. [self.selectDaysArray addObject:dayModel2];
  191. dayModel2.style = CellDaySecondSTypeClick;
  192. [self.collectionView reloadData];
  193. if (self.block) {
  194. self.block(self.selectDaysArray);
  195. [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[0] date] forKey:@"TIMEINF"];
  196. [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[1]date] forKey:@"TIMEINF2"];
  197. }
  198. timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
  199. }
  200. }
  201. }
  202. //单程
  203. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"single"]) {
  204. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"time3"]) {
  205. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"time3"];
  206. [self setAirPlaneToDay:365 ToDateforString:nil]; //初始化
  207. }
  208. NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
  209. CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
  210. dayModel1 = model;
  211. dayModel1.style = CellDayTypeClick;
  212. [self.selectDaysArray addObject:dayModel1];
  213. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]forKey:@"row2"];
  214. [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.section] forKey:@"section2"];
  215. if (self.block) {
  216. self.block(self.selectDaysArray);
  217. [[NSUserDefaults standardUserDefaults] setObject:[self.selectDaysArray[0] date] forKey:@"time3"];
  218. }
  219. [self onTimer];
  220. // timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
  221. }
  222. [self.collectionView reloadData];
  223. }
  224. }
  225. #pragma mark - 日期传值
  226. -(void)passDate:(CalendarBlock)block
  227. {
  228. self.block = block;
  229. }
  230. -(void)onTimer
  231. {
  232. // [timer invalidate];
  233. // timer = nil;
  234. [MBProgressHUD showMessage:@"加载中...."];
  235. [self loadData];
  236. DLog(@"选择日期:%@",_selectDaysArray[0]);
  237. // 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];
  238. // [view show];
  239. }
  240. - (void)loadData
  241. {
  242. NSDate *date = [NSDate date];
  243. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  244. [formatter setDateStyle:NSDateFormatterMediumStyle];
  245. [formatter setTimeStyle:NSDateFormatterShortStyle];
  246. [formatter setDateFormat:@"YYYYMMdd"];
  247. NSString *DateTime = [formatter stringFromDate:date];
  248. [LYHTTPRequest Get:KQin_schedulingSetting_allDetail page:0 condition:DateTime Completion:^(id ObjectData, NSError *error) {
  249. if (error == nil) {
  250. SLog(@"heihei%@",ObjectData);
  251. NSMutableArray *data = [NSMutableArray array];
  252. NSArray *arr = ObjectData[@"listdata"];
  253. for (NSInteger i = 0; i < arr.count; i ++) {
  254. NSDictionary *dic = arr[i];
  255. JLSchedulingModel *model = [JLSchedulingModel mj_objectWithKeyValues:dic];
  256. SLog(@"%@",model.emdefaultorcodes);
  257. if ([model.emdefaultors isEqualToString:@"null"]) {
  258. model.emdefaultors = @"";
  259. model.emdefaultorcodes = @"";
  260. }else if ([model.emdefaultorcodes isEqualToString:@"(null)"])
  261. {
  262. model.emdefaultors = @"";
  263. model.emdefaultorcodes = @"";
  264. }
  265. if ([model.emnames isEqualToString:@"null"] || [model.emcodes isEqualToString:@"null"]) {
  266. model.emnames = @"";
  267. model.emcodes = @"";
  268. }
  269. [data addObject:model];
  270. }
  271. [MBProgressHUD hideHUD];
  272. LYYSelectSchduleAlerView *aletview = [[LYYSelectSchduleAlerView alloc] initWithFrame:self.view.bounds];
  273. aletview.SelectSchduleArr = data;
  274. [aletview setSelectSchBlock:^(NSInteger tag){
  275. JLSchedulingModel *model;
  276. if (tag < data.count) {
  277. model = data[tag];
  278. [self updateSchdulingAday:model];
  279. }else{
  280. }
  281. }];
  282. [self.view addSubview:aletview];
  283. }else{
  284. [MBProgressHUD hideHUD];
  285. if (error.code==-1009)
  286. {
  287. [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkDisconnect") ToNavi:self.navigationController withColor:nil];
  288. }else if (error.code==-1001)
  289. {
  290. [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkTimeOut") ToNavi:self.navigationController withColor:nil];
  291. }
  292. else{
  293. [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_getDataFaild") ToNavi:self.navigationController withColor:nil];
  294. }
  295. }
  296. }];
  297. }
  298. #pragma mark - 上传每一天数据
  299. -(void)updateSchdulingAday:(JLSchedulingModel *)model
  300. {
  301. NSString *DateString = [[_selectDaysArray[self.selectDaysArray.count-1] toString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
  302. NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
  303. _model.wd_defaultor,@"deptcodes",
  304. _model.wd_emcode, @"emcodes",
  305. DateString,"date",
  306. model.wd_code,@"workcode",
  307. nil];
  308. [LYHTTPRequest Post:KQin_schdulingSetting_fixedADay page:dic Completion:^(id ObjectData, NSError *error) {
  309. if (error== nil) {
  310. [JOShowMessageFromNavi showDropViewWithMessage:@"更改成功" ToNavi:self.navigationController withColor:nil];
  311. }else
  312. {
  313. if (error.code==-1009)
  314. {
  315. [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkDisconnect") ToNavi:self.navigationController withColor:nil];
  316. }else if (error.code==-1001)
  317. {
  318. [JOShowMessageFromNavi showDropViewWithMessage:Localized(@"UU_custom_networkTimeOut") ToNavi:self.navigationController withColor:nil];
  319. }
  320. else{
  321. [JOShowMessageFromNavi showDropViewWithMessage:@"更改数据失败" ToNavi:self.navigationController withColor:nil];
  322. }
  323. }
  324. }];
  325. }
  326. //定位到上次的日期的indexpath
  327. -(void)viewWillAppear:(BOOL)animated
  328. {
  329. [super viewWillAppear:animated];
  330. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"round"]) {
  331. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"row1"] && [[NSUserDefaults standardUserDefaults]objectForKey:@"section1"]) {
  332. row = [[[NSUserDefaults standardUserDefaults] objectForKey:@"row1"] integerValue];
  333. section = [[[NSUserDefaults standardUserDefaults] objectForKey:@"section1"] integerValue];
  334. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"row1"];
  335. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"section1"];
  336. }
  337. else{
  338. row = 0;
  339. section = 0;
  340. }
  341. }
  342. if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"trip"] isEqualToString:@"single"]){
  343. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"row2"] && [[NSUserDefaults standardUserDefaults]objectForKey:@"section2"]) {
  344. row = [[[NSUserDefaults standardUserDefaults] objectForKey:@"row2"] integerValue];
  345. section = [[[NSUserDefaults standardUserDefaults] objectForKey:@"section2"] integerValue];
  346. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"row2"];
  347. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"section2"];
  348. }
  349. else{
  350. row = 0;
  351. section = 0;
  352. }
  353. }
  354. //滑到居中位置
  355. [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
  356. }
  357. - (void)didReceiveMemoryWarning {
  358. [super didReceiveMemoryWarning];
  359. // Dispose of any resources that can be recreated.
  360. }
  361. /*
  362. #pragma mark - Navigation
  363. // In a storyboard-based application, you will often want to do a little preparation before navigation
  364. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  365. // Get the new view controller using [segue destinationViewController].
  366. // Pass the selected object to the new view controller.
  367. }
  368. */
  369. @end