KBDatePickerView.m 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. #import "KBDatePickerView.h"
  2. #define STACK_VIEW_HEIGHT 128
  3. DEFINE_ENUM(KBTableViewTag, TABLE_TAG)
  4. DEFINE_ENUM(KBDatePickerMode, PICKER_MODE)
  5. @interface UITableView (yep)
  6. - (NSIndexPath *)_focusedCellIndexPath;
  7. @end
  8. @implementation UIView (Helper)
  9. - (void)removeAllSubviews {
  10. [[self subviews] enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  11. [obj removeFromSuperview];
  12. }];
  13. }
  14. @end
  15. @implementation UIStackView (Helper)
  16. - (void)removeAllArrangedSubviews {
  17. [[self arrangedSubviews] enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  18. if ([obj respondsToSelector:@selector(removeAllArrangedSubviews)]){
  19. [obj removeAllArrangedSubviews];
  20. }
  21. [self removeArrangedSubview:obj];
  22. }];
  23. }
  24. - (void)setArrangedViews:(NSArray *)views {
  25. if ([self arrangedSubviews].count > 0){
  26. [self removeAllArrangedSubviews];
  27. }
  28. [views enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  29. [self addArrangedSubview:obj];
  30. }];
  31. }
  32. @end
  33. @interface KBTableView(){
  34. NSIndexPath *_selectedIndexPath;
  35. }
  36. @end
  37. @implementation KBTableView //nothing to implement yet, just getting some properties
  38. - (instancetype)initWithTag:(KBTableViewTag)tag delegate:(id)delegate {
  39. self = [super initWithFrame:CGRectZero style:UITableViewStylePlain];
  40. if (self){
  41. self.tag = tag;
  42. self.dataSource = delegate;
  43. self.delegate = delegate;
  44. }
  45. return self;
  46. }
  47. - (CGSize)intrinsicContentSize {
  48. CGSize og = [super intrinsicContentSize];
  49. if (self.customWidth > 0){
  50. og.width = self.customWidth;
  51. return og;
  52. }
  53. return og;
  54. }
  55. - (NSArray *)visibleValues {
  56. __block NSMutableArray *visibleValues = [NSMutableArray new];
  57. [self.visibleCells enumerateObjectsUsingBlock:^(__kindof UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  58. NSString *value = obj.textLabel.text;
  59. [visibleValues addObject:value];
  60. }];
  61. return visibleValues;;
  62. }
  63. - (NSIndexPath *)selectedIndexPath {
  64. return _selectedIndexPath;
  65. }
  66. - (id)valueForIndexPath:(NSIndexPath *)indexPath {
  67. return [self cellForRowAtIndexPath:indexPath].textLabel.text;
  68. }
  69. - (NSString *)description {
  70. NSString *sup = [super description];
  71. return [NSString stringWithFormat:@"%@ : %@", sup, NSStringFromKBTableViewTag((KBTableViewTag)self.tag)];
  72. }
  73. - (void)setSelectedIndexPath:(NSIndexPath *)selectedIndexPath {
  74. _selectedIndexPath = selectedIndexPath;
  75. id value = [self valueForIndexPath:selectedIndexPath];
  76. if (value){
  77. //DPLog(@"found cell in %lu", self.tag);
  78. _selectedValue = value;
  79. //DPLog(@"selected value set: %@ for index; %lu", _selectedValue, selectedIndexPath.row);
  80. }
  81. }
  82. @end
  83. @interface KBDatePickerView () {
  84. NSDate *_currentDate;
  85. NSDate *_minimumDate;
  86. NSDate *_maximumDate;
  87. NSArray *_tableViews;
  88. BOOL _pmSelected;
  89. NSMutableDictionary *_selectedRowData;
  90. KBDatePickerMode _datePickerMode;
  91. NSInteger _minYear;
  92. NSInteger _maxYear;
  93. NSInteger _yearSelected;
  94. NSInteger _monthSelected;
  95. NSInteger _daySelected;
  96. NSInteger _hourSelected;
  97. NSInteger _minuteSelected;
  98. NSInteger _currentMonthDayCount; //current months
  99. }
  100. @property (nonatomic, strong) NSArray *hourData;
  101. @property (nonatomic, strong) NSArray *minutesData;
  102. @property (nonatomic, strong) NSArray *dayData;
  103. @property (nonatomic, strong) NSArray *dateData;
  104. @property UIStackView *datePickerStackView;
  105. @property KBTableView *monthTable;
  106. @property KBTableView *dayTable;
  107. @property KBTableView *yearTable;
  108. @property KBTableView *hourTable;
  109. @property KBTableView *minuteTable;
  110. @property KBTableView *amPMTable;
  111. @property KBTableView *dateTable; //Sun Jan 3 data
  112. @property UILabel *monthLabel;
  113. @property UILabel *dayLabel;
  114. @property UILabel *yearLabel;
  115. @property UILabel *datePickerLabel;
  116. @property NSLayoutConstraint *widthConstraint;
  117. @property UILabel *unsupportedLabel;
  118. @property UIStackViewDistribution stackDistribution;
  119. @end
  120. @implementation KBDatePickerView
  121. - (void)menuGestureRecognized:(UITapGestureRecognizer *)gestureRecognizer {
  122. if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
  123. id superview = [self superview];
  124. if ([superview respondsToSelector:@selector(delegate)]){
  125. UIViewController *vc = [superview delegate];
  126. DPLog(@"delegateView: %@", vc);
  127. [vc setNeedsFocusUpdate];
  128. [vc updateFocusIfNeeded];
  129. } else {
  130. //[self setPreferredFocusedItem:self.toggleTypeButton]; //PRIVATE_API call, trying to avoid those to stay app store friendly!
  131. UIApplication *sharedApp = [UIApplication sharedApplication];
  132. #pragma clang diagnostic push
  133. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  134. UIWindow *window = [sharedApp keyWindow];
  135. #pragma clang diagnostic pop
  136. UIViewController *rootViewController = [window rootViewController];
  137. if (rootViewController.view == self.superview){
  138. [rootViewController setNeedsFocusUpdate];
  139. [rootViewController updateFocusIfNeeded];
  140. }
  141. }
  142. }
  143. }
  144. + (NSDateFormatter *)sharedMinimumDateFormatter {
  145. static dispatch_once_t minOnceToken;
  146. static NSDateFormatter *sharedMin = nil;
  147. if(sharedMin == nil) {
  148. dispatch_once(&minOnceToken, ^{
  149. sharedMin = [[NSDateFormatter alloc] init];
  150. [sharedMin setTimeZone:[NSTimeZone localTimeZone]];
  151. [sharedMin setDateFormat:@"E MMM d"];
  152. });
  153. }
  154. return sharedMin;
  155. }
  156. + (NSDateFormatter *)sharedDateFormatter {
  157. static dispatch_once_t onceToken;
  158. static NSDateFormatter *shared = nil;
  159. if(shared == nil) {
  160. dispatch_once(&onceToken, ^{
  161. shared = [[NSDateFormatter alloc] init];
  162. [shared setTimeZone:[NSTimeZone localTimeZone]];
  163. [shared setDateFormat:@"E, MMM d, yyyy h:mm a"];
  164. });
  165. }
  166. return shared;
  167. }
  168. - (NSArray *)generateDatesForYear:(NSInteger)year {
  169. NSMutableArray *_days = [NSMutableArray new];
  170. NSDateComponents *dc = [[NSCalendar currentCalendar] components: NSCalendarUnitYear | NSCalendarUnitDay fromDate:[NSDate date]];
  171. NSInteger currentDay = dc.day;
  172. NSInteger currentYear = dc.year;
  173. NSRange days = [[self calendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[KBDatePickerView todayInYear:year]];
  174. for (NSInteger i = 1; i < days.length; i++){//(NSInteger i = 1; i < days.length-1; i++)
  175. dc.day = i;
  176. if (dc.day == currentDay && dc.year == currentYear){
  177. [_days addObject:@"Today"];
  178. } else {
  179. NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:dc];
  180. NSString *currentDay = [[KBDatePickerView sharedMinimumDateFormatter] stringFromDate:newDate];
  181. [_days addObject:currentDay];
  182. }
  183. }
  184. return _days;
  185. }
  186. - (NSDateComponents *)currentComponents:(NSCalendarUnit)unitFlags {
  187. return [[self calendar] components:unitFlags fromDate:self.date];
  188. }
  189. - (NSDate *)date {
  190. if (!_currentDate){
  191. [self setDate:[NSDate date]];
  192. }
  193. return _currentDate;
  194. }
  195. - (NSCalendar *)calendar {
  196. return [NSCalendar currentCalendar];
  197. }
  198. - (void)setDate:(NSDate *)date animated:(BOOL)animated {
  199. _currentDate = date;
  200. [self scrollToCurrentDateAnimated:animated];
  201. }
  202. - (void)setMinimumDate:(NSDate *)minimumDate {
  203. _minimumDate = minimumDate;
  204. [self populateYearsForDateRange];
  205. }
  206. - (NSDate *)minimumDate {
  207. return _minimumDate;
  208. }
  209. - (NSDate *)maximumDate {
  210. return _maximumDate;
  211. }
  212. - (void)setMaximumDate:(NSDate *)maximumDate {
  213. _maximumDate = maximumDate;
  214. [self populateYearsForDateRange];
  215. }
  216. - (void)setDate:(NSDate *)date {
  217. _currentDate = date;
  218. [self setDate:date animated:true];
  219. }
  220. - (BOOL)isEnabled {
  221. return FALSE;
  222. }
  223. - (id)init {
  224. self = [super init];
  225. _pmSelected = false;
  226. _showDateLabel = true;
  227. _topOffset = 20;
  228. if (![self date]){
  229. [self setDate:[NSDate date]];
  230. }
  231. UITapGestureRecognizer *menuTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuGestureRecognized:)];
  232. menuTap.numberOfTapsRequired = 1;
  233. menuTap.allowedPressTypes = @[@(UIPressTypeMenu)];
  234. [self addGestureRecognizer:menuTap];
  235. _selectedRowData = [NSMutableDictionary new];
  236. _datePickerMode = KBDatePickerModeDate;
  237. [self layoutViews];
  238. return self;
  239. }
  240. - (void)layoutForTime {
  241. if (self.hourTable){
  242. [self.hourTable removeFromSuperview];
  243. self.hourTable = nil;
  244. [self.minuteTable removeFromSuperview];
  245. self.minuteTable = nil;
  246. [self.amPMTable removeFromSuperview];
  247. self.amPMTable = nil;
  248. _tableViews = nil;
  249. }
  250. [self setupTimeData];
  251. self.stackDistribution = UIStackViewDistributionFillProportionally;
  252. self.hourTable = [[KBTableView alloc] initWithTag:KBTableViewTagHours delegate:self];
  253. self.minuteTable = [[KBTableView alloc] initWithTag:KBTableViewTagMinutes delegate:self];
  254. self.amPMTable = [[KBTableView alloc] initWithTag:KBTableViewTagAMPM delegate:self];
  255. self.hourTable.customWidth = 80;
  256. self.minuteTable.customWidth = 80;
  257. self.amPMTable.customWidth = 70;
  258. self.amPMTable.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
  259. _tableViews = @[_hourTable, _minuteTable, _amPMTable];
  260. }
  261. - (void)layoutForDate {
  262. if (self.monthLabel){
  263. [self removeDateHeaders];
  264. self.monthTable = nil;
  265. self.yearTable = nil;
  266. self.dayTable = nil;
  267. _tableViews = nil;
  268. }
  269. self.stackDistribution = UIStackViewDistributionFillProportionally;
  270. [self populateDaysForCurrentMonth];
  271. [self populateYearsForDateRange];
  272. self.monthLabel = [[UILabel alloc] init];
  273. self.monthLabel.translatesAutoresizingMaskIntoConstraints = false;
  274. self.monthLabel.text = @"Month";
  275. self.yearLabel = [[UILabel alloc] init];
  276. self.yearLabel.translatesAutoresizingMaskIntoConstraints = false;
  277. self.yearLabel.text = @"Year";
  278. self.dayLabel = [[UILabel alloc] init];
  279. self.dayLabel.translatesAutoresizingMaskIntoConstraints = false;
  280. self.dayLabel.text = @"Day";
  281. self.monthTable = [[KBTableView alloc] initWithTag:KBTableViewTagMonths delegate:self];
  282. self.yearTable = [[KBTableView alloc] initWithTag:KBTableViewTagYears delegate:self];
  283. self.dayTable = [[KBTableView alloc] initWithTag:KBTableViewTagDays delegate:self];
  284. self.monthTable.customWidth = 200;
  285. self.dayTable.customWidth = 80;
  286. self.yearTable.customWidth = 150;
  287. _tableViews = @[_monthTable, _dayTable, _yearTable];
  288. [self addSubview:self.monthLabel];
  289. [self addSubview:self.yearLabel];
  290. [self addSubview:self.dayLabel];
  291. }
  292. - (void)layoutLabelsForDate {
  293. [self.monthLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:_topOffset].active = true;
  294. [self.dayLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:_topOffset].active = true;
  295. [self.dayLabel.centerXAnchor constraintEqualToAnchor:self.dayTable.centerXAnchor].active = true;
  296. [self.monthLabel.centerXAnchor constraintEqualToAnchor:self.monthTable.centerXAnchor].active = true;
  297. [self.yearLabel.centerXAnchor constraintEqualToAnchor:self.yearTable.centerXAnchor].active = true;
  298. [self.yearLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:_topOffset].active = true;
  299. [self.monthLabel.topAnchor constraintEqualToAnchor:self.topAnchor constant:_topOffset].active = true;
  300. }
  301. - (void)layoutUnsupportedView {
  302. if (_datePickerStackView != nil){
  303. [_datePickerStackView removeAllArrangedSubviews];
  304. [_datePickerStackView removeFromSuperview];
  305. _datePickerStackView = nil;
  306. }
  307. self.unsupportedLabel = [[UILabel alloc] init];
  308. [self addSubview:self.unsupportedLabel];
  309. self.unsupportedLabel.translatesAutoresizingMaskIntoConstraints = false;
  310. [self.unsupportedLabel.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = true;
  311. [self.unsupportedLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = true;
  312. self.unsupportedLabel.text = [self kb_stringWithFormat:"Error: currently '%s' is an unsuppported configuration.", [NSStringFromKBDatePickerMode(self.datePickerMode) UTF8String]];
  313. }
  314. - (void)layoutForDateAndTime {
  315. if (self.hourTable){
  316. [self.hourTable removeFromSuperview];
  317. self.hourTable = nil;
  318. [self.minuteTable removeFromSuperview];
  319. self.minuteTable = nil;
  320. [self.amPMTable removeFromSuperview];
  321. self.amPMTable = nil;
  322. [self.dateTable removeFromSuperview];
  323. self.dateTable = nil;
  324. _tableViews = nil;
  325. }
  326. self.stackDistribution = UIStackViewDistributionFillProportionally;
  327. self.dateData = [self generateDatesForYear:2021]; //FIXME: un-hardcode it, this is just for testing
  328. [self setupTimeData];
  329. self.dateTable = [[KBTableView alloc] initWithTag:KBTaleViewWeekday delegate:self];
  330. self.dateTable.customWidth = 200;
  331. self.hourTable = [[KBTableView alloc] initWithTag:KBTableViewTagHours delegate:self];
  332. self.hourTable.customWidth = 80;
  333. self.minuteTable = [[KBTableView alloc] initWithTag:KBTableViewTagMinutes delegate:self];
  334. self.minuteTable.customWidth = 80;
  335. self.amPMTable = [[KBTableView alloc] initWithTag:KBTableViewTagAMPM delegate:self];
  336. self.amPMTable.customWidth = 70;
  337. self.amPMTable.contentInset = UIEdgeInsetsMake(0, 0, 40, 0);
  338. _tableViews = @[_dateTable, _hourTable, _minuteTable, _amPMTable];
  339. DPLog(@"_tableViews: %@", _tableViews);
  340. // [self layoutUnsupportedView];
  341. }
  342. - (void)layoutForCountdownTimer {
  343. [self layoutUnsupportedView];
  344. }
  345. - (void)removeDateHeaders {
  346. [self.dayLabel removeFromSuperview];
  347. self.dayLabel = nil;
  348. [self.monthLabel removeFromSuperview];
  349. self.monthLabel = nil;
  350. [self.yearLabel removeFromSuperview];
  351. self.yearLabel = nil;
  352. }
  353. - (void)layoutLabelsForTime {
  354. [self removeDateHeaders];
  355. }
  356. - (void)layoutLabelsForDateAndTime {
  357. [self removeDateHeaders];
  358. }
  359. - (void)setupLabelsForMode {
  360. switch (self.datePickerMode) {
  361. case KBDatePickerModeTime:
  362. [self layoutLabelsForTime];
  363. break;
  364. case KBDatePickerModeDate:
  365. [self layoutLabelsForDate];
  366. break;
  367. case KBDatePickerModeDateAndTime:
  368. [self layoutLabelsForDateAndTime];
  369. break;
  370. case KBDatePickerModeCountDownTimer:
  371. [self layoutForCountdownTimer];
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. - (void)viewSetupForMode {
  378. if (self.unsupportedLabel){
  379. [self.unsupportedLabel removeFromSuperview];
  380. self.unsupportedLabel = nil;
  381. }
  382. switch (self.datePickerMode) {
  383. case KBDatePickerModeTime:
  384. [self layoutForTime];
  385. break;
  386. case KBDatePickerModeDate:
  387. [self layoutForDate];
  388. break;
  389. case KBDatePickerModeDateAndTime:
  390. [self layoutForDateAndTime];
  391. break;
  392. case KBDatePickerModeCountDownTimer:
  393. [self layoutForCountdownTimer];
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. - (NSArray *)createNumberArray:(NSInteger)count zeroIndex:(BOOL)zeroIndex leadingZero:(BOOL)leadingZero {
  400. __block NSMutableArray *_newArray = [NSMutableArray new];
  401. int startIndex = 1;
  402. if (zeroIndex){
  403. startIndex = 0;
  404. }
  405. for (int i = startIndex; i < count+startIndex; i++){
  406. if (leadingZero){
  407. [_newArray addObject:[self kb_stringWithFormat:"%02i", i]];
  408. } else {
  409. [_newArray addObject:[self kb_stringWithFormat:"%i", i]];
  410. }
  411. }
  412. return _newArray;
  413. }
  414. - (NSArray *)monthData {
  415. return [[self calendar] monthSymbols];
  416. }
  417. - (void)scrollToCurrentDateAnimated:(BOOL)animated {
  418. if (self.datePickerMode == KBDatePickerModeTime){
  419. [self loadTimeFromDateAnimated:animated];
  420. } else if (self.datePickerMode == KBDatePickerModeDate){
  421. NSDateComponents *components = [self currentComponents:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay];
  422. NSInteger monthIndex = components.month-1;
  423. NSString *monthSymbol = self.monthData[monthIndex];
  424. if (![self.monthTable.selectedValue isEqualToString:monthSymbol]){
  425. [self scrollToValue:monthSymbol inTableViewType:KBTableViewTagMonths animated:animated];
  426. }
  427. NSInteger dayIndex = components.day;
  428. NSString *dayString = [self kb_stringWithFormat:"%i",dayIndex];
  429. if (![[_dayTable selectedValue] isEqualToString:dayString]){
  430. [self scrollToValue:dayString inTableViewType:KBTableViewTagDays animated:animated];
  431. }
  432. NSInteger yearIndex = components.year-1;
  433. //DPLog(@"year index: %lu", yearIndex);
  434. NSString *yearString = [self kb_stringWithFormat:"%i",yearIndex];
  435. //DPLog(@"year index: %@", yearString);
  436. if (![[_yearTable selectedValue] isEqualToString:yearString]){
  437. _yearSelected = yearIndex;
  438. [self scrollToValue:yearString inTableViewType:KBTableViewTagYears animated:animated];
  439. }
  440. //[_yearTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] animated:animated scrollPosition:UITableViewScrollPositionTop];
  441. [self delayedUpdateFocus];
  442. } else {
  443. [self loadTimeFromDateAnimated:animated];
  444. //if (self.datePickerMode == KBDatePickerModeDateAndTime){
  445. NSDateComponents *components = [self currentComponents:NSCalendarUnitYear | NSCalendarUnitDay];
  446. NSInteger currentDay = components.day-1;
  447. //NSString *valueForDate = self.dateData[currentDay];
  448. [self.dateTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:currentDay inSection:0 ] atScrollPosition:UITableViewScrollPositionTop animated:animated];
  449. // }
  450. }
  451. }
  452. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  453. if (tableView == _monthTable){
  454. return [self infiniteNumberOfRowsInSection:section];
  455. } else if (tableView == _dayTable){
  456. return [self infiniteNumberOfRowsInSection:section];
  457. } else if (tableView == _hourTable){
  458. return [self infiniteNumberOfRowsInSection:section];
  459. } else if (tableView == _minuteTable){
  460. return [self infiniteNumberOfRowsInSection:section];
  461. } else if (tableView == _amPMTable){
  462. return 2;
  463. } else if (tableView == _yearTable){
  464. return _maxYear - _minYear;
  465. } else if (tableView == _dateTable){
  466. return self.dateData.count;
  467. }
  468. return 0;
  469. }
  470. +(id)todayInYear:(NSInteger)year {
  471. NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];
  472. dc.year = year;
  473. return [[NSCalendar currentCalendar] dateFromComponents:dc];
  474. }
  475. - (void)updateDetailsIfContinuous:(NSIndexPath *)indexPath inTable:(KBTableView *)tableView {
  476. NSDateComponents *components = [self currentComponents:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute];
  477. NSArray *dataSource = nil;
  478. NSInteger normalizedIndex = NSNotFound;
  479. if (tableView == _monthTable){
  480. dataSource = self.monthData;
  481. normalizedIndex = indexPath.row % dataSource.count;
  482. //DPLog(@"normalizedIndex: %lu s: %@", normalizedIndex, [dataSource objectAtIndex: normalizedIndex]);
  483. components.month = normalizedIndex + 1;
  484. _monthSelected = components.month;
  485. NSDate *newDate = [[self calendar] dateFromComponents:components];
  486. _currentDate = newDate; //set ivar so w dont set off any additional UI craziness.
  487. } else if (tableView == _dayTable){
  488. dataSource = self.dayData;
  489. normalizedIndex = indexPath.row % dataSource.count;
  490. //DPLog(@"_dayTable normalizedIndex: %lu s: %@", normalizedIndex, [dataSource objectAtIndex: normalizedIndex]);
  491. components.day = normalizedIndex + 1;
  492. _daySelected = components.day;
  493. NSDate *newDate = [[self calendar] dateFromComponents:components];
  494. _currentDate = newDate; //set ivar so w dont set off any additional UI craziness.
  495. } else if (tableView == _minuteTable){
  496. dataSource = self.minutesData;
  497. normalizedIndex = indexPath.row % dataSource.count;
  498. //DPLog(@"_minuteTable normalizedIndex: %lu s: %@", normalizedIndex, [dataSource objectAtIndex: normalizedIndex]);
  499. components.minute = normalizedIndex;
  500. _minuteSelected = components.minute;
  501. NSDate *newDate = [[self calendar] dateFromComponents:components];
  502. _currentDate = newDate; //set ivar so w dont set off any additional UI craziness.
  503. } else if (tableView == _hourTable){
  504. dataSource = self.hourData;
  505. normalizedIndex = indexPath.row % dataSource.count;
  506. //NSString *s = [dataSource objectAtIndex: normalizedIndex];
  507. if (_pmSelected){
  508. if (normalizedIndex != 11){
  509. normalizedIndex+=12;
  510. }
  511. } else {
  512. if (normalizedIndex == 11){
  513. normalizedIndex+=12;
  514. }
  515. }
  516. //DPLog(@"normalizedIndex: %lu s: %@", normalizedIndex, [dataSource objectAtIndex: normalizedIndex]);
  517. components.hour = normalizedIndex + 1;
  518. _hourSelected = components.hour;
  519. NSDate *newDate = [[self calendar] dateFromComponents:components];
  520. _currentDate = newDate; //set ivar so w dont set off any additional UI craziness.
  521. } else if (tableView == _yearTable){
  522. //NSInteger year = [[self calendar] component:NSCalendarUnitYear fromDate:self.date];
  523. NSInteger year = [_yearTable.selectedIndexPath row];
  524. NSInteger adjustment = 1;
  525. //DPLog(@"_minYear: %lu", _minYear);
  526. if (_minYear > 1){
  527. //DPLog(@"adjust the year, we dont start at 1!");
  528. adjustment = 0;
  529. year = [_yearTable.selectedValue integerValue];
  530. }
  531. //DPLog(@"year: %lu", year);
  532. components.year = year + adjustment;
  533. _yearSelected = components.year;
  534. NSDate *newDate = nil;
  535. do {
  536. newDate = [[self calendar] dateFromComponents:components];
  537. components.day -= 1;
  538. } while (newDate == nil || ([[self calendar] component:NSCalendarUnitMonth fromDate:newDate] != components.month));
  539. _currentDate = newDate;
  540. } else if (tableView == _amPMTable){
  541. BOOL previousState = _pmSelected;
  542. //DPLog(@"_hourSelected: %lu previousState: %d", _hourSelected, previousState);
  543. if (indexPath.row == 0){
  544. _pmSelected = false;
  545. if(_hourSelected != 0){
  546. if (previousState != _pmSelected){
  547. components.hour-=12;
  548. _hourSelected = components.hour;
  549. NSDate *date = [[self calendar] dateFromComponents:components];
  550. if (date){
  551. _currentDate = date;
  552. }
  553. }
  554. }
  555. } else if(indexPath.row == 1) {
  556. _pmSelected = true;
  557. if(_hourSelected != 0 && previousState != _pmSelected){
  558. components.hour+=12;
  559. _hourSelected = components.hour;
  560. NSDate *date = [[self calendar] dateFromComponents:components];
  561. if (date){
  562. _currentDate = date;
  563. }
  564. }
  565. }
  566. } else if (tableView == _dateTable){
  567. NSDateComponents *dc = [self currentComponents:NSCalendarUnitYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute];
  568. dc.day = indexPath.row+1;
  569. _currentDate = [[NSCalendar currentCalendar] dateFromComponents:dc];
  570. }
  571. [self selectionOccured];
  572. }
  573. - (void)selectMonthAtIndex:(NSInteger)index {
  574. NSDateComponents *comp = [self currentComponents:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear];
  575. NSInteger adjustedIndex = index;
  576. if (index > self.monthData.count){
  577. adjustedIndex = index % self.monthData.count;
  578. }
  579. comp.month = adjustedIndex;
  580. [self setDate:[[self calendar] dateFromComponents:comp]];
  581. }
  582. - (BOOL)tableView:(UITableView *)tableView canFocusRowAtIndexPath:(NSIndexPath *)indexPath {
  583. if (tableView.tag == KBTableViewTagDays){
  584. NSInteger normalized = (indexPath.row % self.dayData.count) + 1;
  585. if (normalized > _currentMonthDayCount){
  586. return false;
  587. }
  588. }
  589. return true;
  590. }
  591. - (void)toggleMidnight {
  592. NSInteger index = 1;
  593. if (_pmSelected){
  594. index = 0;
  595. }
  596. [self.amPMTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:true scrollPosition:UITableViewScrollPositionTop];
  597. _pmSelected = !_pmSelected;
  598. }
  599. - (void)toggleMidnightIfNecessaryWithPrevious:(NSInteger)previousRow next:(NSInteger)nextRow {
  600. if (previousRow == 11 && nextRow == 12 && !_pmSelected){
  601. [self toggleMidnight];
  602. }
  603. if (previousRow == 12 && nextRow == 1 && !_pmSelected){
  604. [self toggleMidnight];
  605. }
  606. }
  607. - (BOOL)contextBrothers:(UITableViewFocusUpdateContext *)context {
  608. UIView *previousCell = context.previouslyFocusedView;
  609. UIView *newCell = context.nextFocusedView;
  610. return (previousCell.superview == newCell.superview);
  611. }
  612. - (void)tableView:(UITableView *)tableView didUpdateFocusInContext:(UITableViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
  613. [coordinator addCoordinatedAnimations:^{
  614. NSIndexPath *nextIndexPath = context.nextFocusedIndexPath;
  615. KBTableView *table = (KBTableView *)tableView;
  616. if ([self contextBrothers:context]){
  617. if (table.tag == KBTableViewTagHours){
  618. NSIndexPath *previous = context.previouslyFocusedIndexPath;
  619. NSInteger previousRow = (previous.row % self.hourData.count)+1;
  620. NSInteger nextRow = (nextIndexPath.row % self.hourData.count)+1;
  621. if ((previousRow == 11 && nextRow == 12) || (previousRow == 12 && nextRow == 11)){
  622. [self toggleMidnight];
  623. }
  624. }
  625. }
  626. if ([table respondsToSelector:@selector(setSelectedIndexPath:)]){
  627. if (nextIndexPath != nil){
  628. //DPLog(@"next ip: %lu table: %@", ip.row, NSStringFromKBTableViewTag((KBTableViewTag)tableView.tag));
  629. [table setSelectedIndexPath:nextIndexPath];
  630. [self updateDetailsIfContinuous:nextIndexPath inTable:table];
  631. if (tableView.tag == KBTableViewTagMonths){
  632. [self populateDaysForCurrentMonth];
  633. }
  634. }
  635. }
  636. [tableView selectRowAtIndexPath:nextIndexPath animated:false scrollPosition:UITableViewScrollPositionTop];
  637. } completion:^{
  638. }];
  639. }
  640. - (NSInteger)infiniteNumberOfRowsInSection:(NSInteger)section {
  641. return NUMBER_OF_CELLS;
  642. }
  643. - (void)populateYearsForDateRange {
  644. _minYear = self.minimumDate != nil ? [[self calendar] component:NSCalendarUnitYear fromDate:self.minimumDate] : 1;
  645. _maxYear = self.maximumDate != nil ? [[self calendar] component:NSCalendarUnitYear fromDate:self.maximumDate] : NUMBER_OF_CELLS;
  646. //DPLog(@"minYear: %lu", _minYear);
  647. //DPLog(@"maxYear: %lu", _maxYear);
  648. //DPLog(@"selectedValue: %@", _yearTable.selectedValue);
  649. //DPLog(@"currentYear: %lu", _yearSelected);
  650. if (!_yearTable.selectedValue && _yearSelected != 0){
  651. if (_minYear > 1){
  652. NSInteger yearDifference = _yearSelected - _minYear;
  653. //DPLog(@"year difference: %lu", yearDifference);
  654. [self.yearTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:yearDifference inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:false];
  655. }
  656. }
  657. [self.yearTable reloadData];
  658. }
  659. - (void)populateDaysForCurrentMonth {
  660. //NSDateComponents *comp = [self currentComponents:NSCalendarUnitMonth];
  661. NSRange days = [[self calendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self.date];
  662. //DPLog(@"month : %lu days %lu for: %@", comp.month, days.length, self.date);
  663. _currentMonthDayCount = days.length; //this is used to push a date back if they've gone too far.
  664. if (!self.dayData){ //only need to populate it once
  665. self.dayData = [self createNumberArray:31 zeroIndex:false leadingZero:false];
  666. [self.dayTable reloadData];
  667. }
  668. //NSString *currentDay = [self kb_stringWithFormat:"%lu", _daySelected];
  669. //DPLog(@"_daySelected: %@ _yearTable.selectedValue: %@", currentDay, _yearTable.selectedValue);
  670. //[self scrollToValue:currentDay inTableViewType:KBTableViewTagDays animated:false];
  671. }
  672. - (void)setupTimeData {
  673. self.hourData = [self createNumberArray:12 zeroIndex:false leadingZero:false];
  674. self.minutesData = [self createNumberArray:60 zeroIndex:true leadingZero:true];
  675. }
  676. - (NSInteger)startIndexForHours {
  677. return 24996;
  678. }
  679. - (NSInteger)startIndexForMinutes {
  680. return 24000;
  681. }
  682. - (id)kb_stringWithFormat:(const char*) fmt,... {
  683. va_list args;
  684. char temp[2048];
  685. va_start(args, fmt);
  686. vsprintf(temp, fmt, args);
  687. va_end(args);
  688. return [[NSString alloc] initWithUTF8String:temp];
  689. }
  690. - (void)loadTimeFromDateAnimated:(BOOL)animated {
  691. NSDateComponents *components = [self currentComponents:NSCalendarUnitHour | NSCalendarUnitMinute];
  692. NSInteger hour = components.hour;
  693. NSInteger minutes = components.minute;
  694. BOOL isPM = (hour >= 12);
  695. if (isPM){
  696. _pmSelected = true;
  697. hour = hour-12;
  698. NSIndexPath *amPMIndex = [NSIndexPath indexPathForRow:1 inSection:0];
  699. [self.amPMTable scrollToRowAtIndexPath:amPMIndex atScrollPosition:UITableViewScrollPositionTop animated:false];
  700. }
  701. NSString *hourValue = [self kb_stringWithFormat:"%lu", hour];
  702. NSString *minuteValue = [self kb_stringWithFormat:"%lu", minutes];
  703. DPLog(@"hours %@ minutes %@", hourValue, minuteValue);
  704. if (![[self.hourTable selectedValue] isEqualToString:hourValue]){
  705. [self scrollToValue:hourValue inTableViewType:KBTableViewTagHours animated:animated];
  706. }
  707. if (![[self.minuteTable selectedValue] isEqualToString:minuteValue]){
  708. [self scrollToValue:minuteValue inTableViewType:KBTableViewTagMinutes animated:animated];
  709. }
  710. }
  711. - (void)delayedUpdateFocus {
  712. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  713. [self setNeedsFocusUpdate];
  714. [self updateFocusIfNeeded];
  715. });
  716. }
  717. - (void)scrollToValue:(id)value inTableViewType:(KBTableViewTag)type animated:(BOOL)animated {
  718. NSInteger foundIndex = NSNotFound;
  719. NSIndexPath *ip = nil;
  720. NSInteger dayCount = self.dayData.count;
  721. NSInteger relationalIndex = 0;
  722. CGFloat shiftIndex = 0.0;
  723. NSString *currentValue = nil;
  724. switch (type) {
  725. case KBTableViewTagHours:
  726. foundIndex = [self.hourData indexOfObject:value];
  727. if (foundIndex != NSNotFound){
  728. ip = [NSIndexPath indexPathForRow:[self startIndexForHours]+foundIndex inSection:0];
  729. //DPLog(@"found index: %lu", ip.row);
  730. [self.hourTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  731. [self.hourTable selectRowAtIndexPath:ip animated:animated scrollPosition:UITableViewScrollPositionTop];
  732. [self delayedUpdateFocus];
  733. }
  734. break;
  735. case KBTableViewTagMinutes:
  736. foundIndex = [self.minutesData indexOfObject:value];
  737. if (foundIndex != NSNotFound){
  738. ip = [NSIndexPath indexPathForRow:[self startIndexForMinutes]+foundIndex inSection:0];
  739. //DPLog(@"found index: %lu", ip.row);
  740. [self.minuteTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  741. [self.minuteTable selectRowAtIndexPath:ip animated:animated scrollPosition:UITableViewScrollPositionTop];
  742. [self delayedUpdateFocus];
  743. }
  744. break;
  745. case KBTableViewTagMonths:
  746. //value = "December";
  747. currentValue = self.monthTable.selectedValue; //March
  748. relationalIndex = [self.monthData indexOfObject:currentValue]; //2
  749. foundIndex = [self.monthData indexOfObject:value]; //11
  750. //11 - 2 = 9 : go up 9 indexes
  751. //2 - 11 = -9 : go back 9 indexes
  752. if (foundIndex != NSNotFound){
  753. shiftIndex = foundIndex - relationalIndex;
  754. if (self.monthTable.selectedIndexPath && currentValue){
  755. //DPLog(@"current value: %@ relationalIndex: %lu found index: %lu, shift index: %.0f", currentValue, relationalIndex, foundIndex, shiftIndex);
  756. ip = [NSIndexPath indexPathForRow:self.monthTable.selectedIndexPath.row+shiftIndex inSection:0];
  757. } else {
  758. ip = [NSIndexPath indexPathForRow:[self startIndexForHours]+foundIndex inSection:0];
  759. }
  760. [self.monthTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  761. [_monthTable selectRowAtIndexPath:ip animated:animated scrollPosition:UITableViewScrollPositionTop];
  762. [self delayedUpdateFocus];
  763. //[self.monthTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  764. }
  765. break;
  766. case KBTableViewTagDays:
  767. foundIndex = [self.dayData indexOfObject:value];
  768. if (foundIndex != NSNotFound){
  769. ip = [NSIndexPath indexPathForRow:[self indexForDays:dayCount]+foundIndex inSection:0];
  770. //DPLog(@"found index: %lu", ip.row);
  771. [self.dayTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  772. [_dayTable selectRowAtIndexPath:ip animated:animated scrollPosition:UITableViewScrollPositionTop];
  773. [self delayedUpdateFocus];
  774. }
  775. break;
  776. case KBTableViewTagYears:
  777. foundIndex = [value integerValue]; //FIXME: this will not work when custom min/max dates are set!
  778. if (_minYear > 1){
  779. NSInteger intValue = [value integerValue];
  780. foundIndex = intValue - _minYear;
  781. }
  782. //DPLog(@"foundIndex: %lu from value:%@", foundIndex, value);
  783. ip = [NSIndexPath indexPathForRow:foundIndex inSection:0];
  784. [self.yearTable scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:animated];
  785. [self delayedUpdateFocus];
  786. default:
  787. break;
  788. }
  789. }
  790. - (NSInteger)indexForDays:(NSInteger)days {
  791. switch (days) {
  792. case 28: return 24976;
  793. case 29: return 24969;
  794. case 30: return 24990;
  795. case 31: return 24986;
  796. }
  797. return 25000;
  798. }
  799. - (UITableViewCell *)infiniteCellForTableView:(KBTableView *)tableView atIndexPath:(NSIndexPath *)indexPath dataSource:(NSArray *)dataSource {
  800. static NSString *CellIdentifier = @"Cell";
  801. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  802. // Configure the cell...
  803. if (!cell) {
  804. cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
  805. }
  806. NSString *s = [dataSource objectAtIndex: indexPath.row % dataSource.count];
  807. [cell.textLabel setText: s];
  808. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  809. return cell;
  810. }
  811. - (UITableViewCell *)amPMCellForRowAtIndexPath:(NSIndexPath *)indexPath {
  812. static NSString *CellIdentifier = @"amPMCell";
  813. UITableViewCell *cell = [_amPMTable dequeueReusableCellWithIdentifier:CellIdentifier];
  814. // Configure the cell...
  815. if (!cell) {
  816. cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
  817. }
  818. if (indexPath.row == 0){
  819. cell.textLabel.text = @"AM";
  820. } else {
  821. cell.textLabel.text = @"PM";
  822. }
  823. return cell;
  824. }
  825. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  826. UITableViewCell *cell = nil;
  827. if (tableView == _hourTable){
  828. return [self infiniteCellForTableView:(KBTableView*)tableView atIndexPath:indexPath dataSource:self.hourData];
  829. } else if (tableView == _minuteTable) {
  830. return [self infiniteCellForTableView:(KBTableView*)tableView atIndexPath:indexPath dataSource:self.minutesData];
  831. } else if (tableView == _amPMTable) {
  832. return [self amPMCellForRowAtIndexPath:indexPath];
  833. } else if (tableView == _monthTable) {
  834. return [self infiniteCellForTableView:(KBTableView*)tableView atIndexPath:indexPath dataSource:self.monthData];
  835. } else if (tableView == _dayTable){
  836. return [self infiniteCellForTableView:(KBTableView*)tableView atIndexPath:indexPath dataSource:self.dayData];
  837. } else if (tableView == _yearTable) {
  838. cell = [tableView dequeueReusableCellWithIdentifier:@"year"];
  839. if (!cell){
  840. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"year"];
  841. }
  842. //NSInteger year = [[self calendar] component:NSCalendarUnitYear fromDate:self.date];
  843. if (_minYear > 1){
  844. cell.textLabel.text = [self kb_stringWithFormat:"%lu", _minYear+indexPath.row+1];
  845. } else {
  846. cell.textLabel.text = [self kb_stringWithFormat:"%lu", indexPath.row+1];
  847. }
  848. //cell.textLabel.text = [NSString stringWithFormat:@"%lu", year - 1 + indexPath.row];
  849. cell.textLabel.textAlignment = NSTextAlignmentCenter;
  850. } else if (tableView == _dateTable){
  851. cell = [tableView dequeueReusableCellWithIdentifier:@"date"];
  852. if (!cell){
  853. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"date"];
  854. }
  855. NSString *currentDate = self.dateData[indexPath.row];
  856. cell.textLabel.text = currentDate;
  857. }
  858. return cell;
  859. }
  860. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  861. }
  862. - (void)selectionOccured {
  863. if (self.itemSelectedBlock){
  864. self.itemSelectedBlock(self.date);
  865. }
  866. [self sendActionsForControlEvents:UIControlEventValueChanged];
  867. if (self.showDateLabel){
  868. self.datePickerLabel.hidden = false;
  869. NSDateFormatter *dateFormatter = [KBDatePickerView sharedDateFormatter];
  870. NSString *strDate = [dateFormatter stringFromDate:self.date];
  871. DPLog(@"strDate: %@", strDate); // Result: strDate: 2014/05/19 10:51:50
  872. self.datePickerLabel.text = strDate;
  873. } else {
  874. self.datePickerLabel.hidden = true;
  875. }
  876. }
  877. - (id)valueForTableViewSelectedCell:(KBTableView *)tableView {
  878. id data = nil;
  879. if (!tableView.selectedIndexPath){
  880. return data;
  881. }
  882. switch (tableView.tag) {
  883. case KBTableViewTagMinutes:
  884. data = [self.minutesData objectAtIndex: tableView.selectedIndexPath.row % self.minutesData.count];
  885. break;
  886. case KBTableViewTagHours:
  887. data = [self.hourData objectAtIndex: tableView.selectedIndexPath.row % self.hourData.count];
  888. break;
  889. case KBTableViewTagMonths:
  890. data = [self.monthData objectAtIndex:tableView.selectedIndexPath.row % self.monthData.count];
  891. break;
  892. default:
  893. break;
  894. }
  895. return data;
  896. }
  897. - (KBDatePickerMode)datePickerMode {
  898. return _datePickerMode;
  899. }
  900. - (void)setDatePickerMode:(KBDatePickerMode)datePickerMode {
  901. _datePickerMode = datePickerMode;
  902. [self adaptModeChange];
  903. }
  904. - (void)adaptModeChange {
  905. [self removeAllSubviews];
  906. [self layoutViews];
  907. }
  908. - (CGSize)sizeThatFits:(CGSize)size {
  909. //CGSize sup = [super sizeThatFits:size];
  910. return CGSizeMake([self widthForMode], STACK_VIEW_HEIGHT+81+60+40);
  911. }
  912. - (CGFloat)widthForMode {
  913. switch (self.datePickerMode) {
  914. case KBDatePickerModeDate: return 500;
  915. case KBDatePickerModeTime: return 350;
  916. case KBDatePickerModeDateAndTime: return 650;
  917. case KBDatePickerModeCountDownTimer: return 400;
  918. }
  919. return 720;
  920. }
  921. - (void)layoutViews {
  922. [self viewSetupForMode];
  923. if (!_tableViews){
  924. DPLog(@"no table views, bail!!");
  925. return;
  926. }
  927. if (_datePickerStackView != nil){
  928. [_datePickerStackView removeAllArrangedSubviews];
  929. [_datePickerStackView removeFromSuperview];
  930. _datePickerStackView = nil;
  931. }
  932. self.datePickerStackView = [[UIStackView alloc] initWithArrangedSubviews:_tableViews];
  933. self.datePickerStackView.translatesAutoresizingMaskIntoConstraints = false;
  934. self.datePickerStackView.spacing = 10;
  935. self.datePickerStackView.axis = UILayoutConstraintAxisHorizontal;
  936. self.datePickerStackView.alignment = UIStackViewAlignmentFill;
  937. self.datePickerStackView.distribution = self.stackDistribution;
  938. self.widthConstraint = [self.datePickerStackView.widthAnchor constraintEqualToConstant:self.widthForMode];
  939. self.widthConstraint.active = true;
  940. [self.datePickerStackView.heightAnchor constraintEqualToConstant:STACK_VIEW_HEIGHT].active = true;
  941. [self addSubview:self.datePickerStackView];
  942. [self.datePickerStackView.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = true;
  943. self.datePickerLabel = [[UILabel alloc] init];
  944. self.datePickerLabel.translatesAutoresizingMaskIntoConstraints = false;
  945. self.datePickerLabel.hidden = !_showDateLabel;
  946. [self addSubview:self.datePickerLabel];
  947. [self.datePickerLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = true;
  948. [self.datePickerLabel.topAnchor constraintEqualToAnchor:self.datePickerStackView.bottomAnchor constant:80].active = true;
  949. [self setupLabelsForMode];
  950. if (self.dayLabel){
  951. DPLog(@"day label in mode: %@", NSStringFromKBDatePickerMode(self.datePickerMode));
  952. [self.datePickerStackView.topAnchor constraintEqualToAnchor:self.dayLabel.bottomAnchor constant:60].active = true;
  953. } else {
  954. DPLog(@"no day label in mode: %@", NSStringFromKBDatePickerMode(self.datePickerMode));
  955. [self.datePickerStackView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor].active = true;
  956. }
  957. [self scrollToCurrentDateAnimated:false];
  958. }
  959. @end