diff --git a/README.md b/README.md index daf3953..05774c6 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,14 @@ Install with [CocoaPods](http://cocoapods.org) by adding the following to your P ``` ruby platform :ios, '6.1' -pod 'THCalendarDatePicker', '~> 1.2.5' +pod 'THCalendarDatePicker', '~> 1.2.6' ``` ####Swift ``` ruby platform :ios, '8.0' use_frameworks! -pod 'THCalendarDatePicker', '~> 1.2.5' +pod 'THCalendarDatePicker', '~> 1.2.6' ``` **Note**: We follow http://semver.org for versioning the public API. @@ -116,9 +116,11 @@ or how you could do it in Swift - Fixed issue [#28](https://github.com/hons82/THCalendarDatePicker/issues/28) - Fixed issue [#30](https://github.com/hons82/THCalendarDatePicker/issues/30) +- Fixed issue [#69](https://github.com/hons82/THCalendarDatePicker/issues/69) - Pull request [#40](https://github.com/hons82/THCalendarDatePicker/pull/40) - Pull request [#41](https://github.com/hons82/THCalendarDatePicker/pull/41) - Pull request [#53](https://github.com/hons82/THCalendarDatePicker/pull/53) +- Pull request [#70](https://github.com/hons82/THCalendarDatePicker/pull/70) ### V1.1.X @@ -157,6 +159,7 @@ or how you could do it in Swift - [jeremiescheer](https://github.com/jeremiescheer) - [powfulhong](https://github.com/powfulhong) - [sparkdreamstudio](https://github.com/sparkdreamstudio) +- [Ignacio pascualin](https://github.com/pascualin) # License diff --git a/THCalendarDatePicker.podspec b/THCalendarDatePicker.podspec index 54272c5..6254eda 100644 --- a/THCalendarDatePicker.podspec +++ b/THCalendarDatePicker.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "THCalendarDatePicker" - s.version = "1.2.5" + s.version = "1.2.6" s.summary = "A DatePicker based on a custom calendar view" s.homepage = "https://github.com/hons82/THCalendarDatePicker" s.license = { :type => 'MIT', :file => 'LICENSE.md' } @@ -12,7 +12,7 @@ Pod::Spec.new do |s| s.source_files = 'THCalendarDatePicker/*.{h,m}' s.resources = ["THCalendarDatePicker/*.xib", "THCalendarDatePicker/Images.xcassets"] s.frameworks = 'QuartzCore' - s.dependency 'KNSemiModalViewController_hons82', '~> 0.4.3' + s.dependency 'KNSemiModalViewController_hons82', '~> 0.4.4' s.prefix_header_contents = <<-EOS #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) diff --git a/THCalendarDatePicker/THDateDay.m b/THCalendarDatePicker/THDateDay.m index 99d3ec2..76f9795 100755 --- a/THCalendarDatePicker/THDateDay.m +++ b/THCalendarDatePicker/THDateDay.m @@ -51,8 +51,7 @@ -(void)setLightText:(BOOL)light { UIColor * color = [UIColor colorWithWhite:.84 alpha:1]; [self.dateButton setTitleColor:color forState:UIControlStateNormal]; self.hasItemsIndicator.image = (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") ? [UIImage imageNamed:@"calendar_littledot-disabled" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] : [UIImage imageNamed:@"calendar_littledot-disabled"]); - } - else { + } else { UIColor * color = [UIColor colorWithWhite:.3 alpha:1]; [self.dateButton setTitleColor:color forState:UIControlStateNormal]; self.hasItemsIndicator.image = (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0") ? [UIImage imageNamed:@"calendar_littledot" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] : [UIImage imageNamed:@"calendar_littledot"]); diff --git a/THCalendarDatePicker/THDatePickerViewController.h b/THCalendarDatePicker/THDatePickerViewController.h index f1196d1..2d0e368 100755 --- a/THCalendarDatePicker/THDatePickerViewController.h +++ b/THCalendarDatePicker/THDatePickerViewController.h @@ -23,6 +23,7 @@ @optional -(void)datePicker:(THDatePickerViewController *)datePicker selectedDate:(NSDate *)selectedDate; +-(void)datePicker:(THDatePickerViewController *)datePicker deselectedDate:(NSDate *)deselectedDate; -(void)datePickerDidHide:(THDatePickerViewController *)datePicker; @end @@ -43,6 +44,7 @@ @property (weak, nonatomic) IBOutlet UIView *toolbarBackgroundView; @property (nonatomic) float slideAnimationDuration; @property (strong, nonatomic) NSString* dateTitle; +@property (strong, nonatomic) NSArray * selectedDates; - (void)setDateHasItemsCallback:(BOOL (^)(NSDate * date))callback; @@ -51,6 +53,11 @@ */ - (void)setAllowClearDate:(BOOL)allow; +/*! Enable Multi Day Selection + * \param allow selection of multiple days + */ +- (void)setAllowMultiDaySelection:(BOOL)allow; + /*! Enable Ok Button when selected Date has already been selected * \param allow should show ok button */ diff --git a/THCalendarDatePicker/THDatePickerViewController.m b/THCalendarDatePicker/THDatePickerViewController.m index 8b61465..035891d 100755 --- a/THCalendarDatePicker/THDatePickerViewController.m +++ b/THCalendarDatePicker/THDatePickerViewController.m @@ -29,6 +29,9 @@ @interface THDatePickerViewController () { BOOL _disableYearSwitch; BOOL (^_dateHasItemsCallback)(NSDate *); float _slideAnimationDuration; + NSMutableArray * _selectedDates; + NSMutableArray * _selectedDateViews; + BOOL _allowMultiDaySelection; } @property (nonatomic, strong) NSDate * firstOfCurrentMonth; @property (nonatomic, strong) THDateDay * currentDay; @@ -67,6 +70,7 @@ @implementation THDatePickerViewController @synthesize rounded = _rounded; @synthesize historyFutureBasedOnInternal = _historyFutureBasedOnInternal; @synthesize slideAnimationDuration = _slideAnimationDuration; +@synthesize selectedDates = _selectedDates; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; @@ -82,6 +86,9 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil _autoCloseCancelDelay = 1.0; _dateTimeZone = [NSTimeZone defaultTimeZone]; _slideAnimationDuration = .5; + _selectedDates = [[NSMutableArray alloc] init]; + _selectedDateViews = [[NSMutableArray alloc] init]; + _allowMultiDaySelection = NO; } return self; } @@ -90,6 +97,11 @@ +(THDatePickerViewController *)datePicker { return [[THDatePickerViewController alloc] initWithNibName:@"THDatePickerViewController" bundle:[NSBundle bundleForClass:self.class]]; } +-(void)setAllowMultiDaySelection:(BOOL)allow { + [self setAutoCloseOnSelectDate:!allow]; // Caution possible endless loop + _allowMultiDaySelection = allow; +} + - (void)setAllowClearDate:(BOOL)allow { _allowClearDate = allow; } @@ -108,6 +120,7 @@ - (void)setClearAsToday:(BOOL)beTodayButton { - (void)setAutoCloseOnSelectDate:(BOOL)autoClose { if (!_allowClearDate) [self setAllowClearDate:!autoClose]; + _allowMultiDaySelection = !autoClose; _autoCloseOnSelectDate = autoClose; } @@ -305,6 +318,22 @@ - (void)redrawDays { if (self.selectedBackgroundColor) [day setSelectedBackgroundColor:self.selectedBackgroundColor]; + if(!_allowMultiDaySelection) { + if (_internalDate && ![[self dateWithOutTime:date] timeIntervalSinceDate:_internalDate]) { + self.currentDay = day; + [day setSelected:YES]; + } + } else { + if ([_selectedDates containsObject:date]) { + if ([_selectedDates lastObject] == date) { + _internalDate = date; + } + [day setSelected:YES]; + } else { + [day setSelected:NO]; + } + } + [day setLightText:![self dateInCurrentMonth:date]]; [day setEnabled:![self dateInFutureAndShouldBeDisabled:date]]; [day indicateDayHasItems:(_dateHasItemsCallback && _dateHasItemsCallback(date))]; @@ -313,10 +342,6 @@ - (void)redrawDays { [day.dateButton setTitle:[NSString stringWithFormat:@"%ld",(long)[comps day]] forState:UIControlStateNormal]; [self.calendarDaysView addSubview:day]; - if (_internalDate && ![[date dateWithOutTime] timeIntervalSinceDate:_internalDate]) { - self.currentDay = day; - [day setSelected:YES]; - } // @end date = [_calendar dateByAddingComponents:offsetComponents toDate:date options:0]; curX += cellWidth; @@ -352,20 +377,38 @@ - (void)redrawWeekdays:(int)dayWidth { #pragma mark - Date Set, etc. +- (NSArray *)selectedDates { + return [[NSArray alloc] initWithArray:_selectedDates]; +} + +- (void)setSelectedDates:(NSArray *)selectedDates { + + _selectedDates = [[NSMutableArray alloc] init]; + for (NSDate* selectedDate in selectedDates) { + [_selectedDates addObject:[self dateWithOutTime:selectedDate]]; + } + if ([_selectedDates count] > 0) { + _date = (NSDate*)[_selectedDates objectAtIndex:0]; + _dateNoTime = !_date ? nil : [self dateWithOutTime:_date]; + self.internalDate = [_dateNoTime dateByAddingTimeInterval:0]; + } + [self redrawDays]; +} + - (void)setDate:(NSDate *)date { - _date = date; - _dateNoTime = !date ? nil : [date dateWithOutTime]; - self.internalDate = [_dateNoTime dateByAddingTimeInterval:0]; + [self setSelectedDates:@[date]]; } - (NSDate *)date { - if(!self.internalDate) return nil; - else if(!_date) return self.internalDate; + if(!self.internalDate) + return nil; + else if(!_date) + return self.internalDate; else { int ymd = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay; NSDateComponents* internalComps = [_calendar components:ymd fromDate:self.internalDate]; int time = NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone; - NSDateComponents* origComps = [_calendar components:time fromDate:_date]; + NSDateComponents* origComps = [_calendar components:time fromDate:(_allowMultiDaySelection && [_selectedDates count] > 0 ? [_selectedDates lastObject] : _date)]; [origComps setDay:[internalComps day]]; [origComps setMonth:[internalComps month]]; [origComps setYear:[internalComps year]]; @@ -374,16 +417,21 @@ - (NSDate *)date { } - (BOOL)shouldOkBeEnabled { - if (_autoCloseOnSelectDate) - return YES; - return (self.internalDate && _dateNoTime && (_allowSelectionOfSelectedDate || [self.internalDate timeIntervalSinceDate:_dateNoTime])) - || (self.internalDate && !_dateNoTime) - || (!self.internalDate && _dateNoTime); + if (!_allowMultiDaySelection) { + if (_autoCloseOnSelectDate) + return YES; + NSLog(@"interval %f",[self.internalDate timeIntervalSinceDate:_dateNoTime]); + return (self.internalDate && _dateNoTime && (_allowSelectionOfSelectedDate || [self.internalDate timeIntervalSinceDate:_dateNoTime])) + || (self.internalDate && !_dateNoTime) + || (!self.internalDate && _dateNoTime); + } else { + return ([_selectedDates count] > 0); + } } - (void)setInternalDate:(NSDate *)internalDate{ _internalDate = internalDate; - self.clearBtn.enabled = !!internalDate; + self.clearBtn.enabled = internalDate; self.okBtn.enabled = [self shouldOkBeEnabled]; if(internalDate){ [self setDisplayedMonthFromDate:internalDate]; @@ -450,23 +498,44 @@ - (BOOL)setDateTimeZoneWithName:(NSString *)name { #pragma mark - User Events - (void)dateDayTapped:(THDateDay *)dateDay { - if (!_internalDate || [_internalDate timeIntervalSinceDate:dateDay.date] || _allowSelectionOfSelectedDate) { // new date selected - [self.currentDay setSelected:NO]; - [self.currentDay setLightText:![self dateInCurrentMonth:self.currentDay.date]]; - [dateDay setSelected:YES]; - BOOL dateInDifferentMonth = ![self dateInCurrentMonth:dateDay.date]; - NSDate *firstOfCurrentMonth = self.firstOfCurrentMonth; - [self setInternalDate:dateDay.date]; - [self setCurrentDay:dateDay]; - if (dateInDifferentMonth) { - [self slideTransitionViewInDirection:[dateDay.date timeIntervalSinceDate:firstOfCurrentMonth]<0 ? UISwipeGestureRecognizerDirectionRight : UISwipeGestureRecognizerDirectionLeft]; - } - if ([self.delegate respondsToSelector:@selector(datePicker:selectedDate:)]) { - [self.delegate datePicker:self selectedDate:dateDay.date]; + BOOL dateInDifferentMonth = ![self dateInCurrentMonth:dateDay.date]; + NSDate *firstOfCurrentMonth = self.firstOfCurrentMonth; + if (!_allowMultiDaySelection) { + if (!_internalDate || [_internalDate timeIntervalSinceDate:dateDay.date] || _allowSelectionOfSelectedDate) { // new date selected + [self.currentDay setSelected:NO]; + [self.currentDay setLightText:![self dateInCurrentMonth:self.currentDay.date]]; + [dateDay setSelected:YES]; + [self setInternalDate:dateDay.date]; + [self setCurrentDay:dateDay]; + [_selectedDates removeAllObjects]; + [_selectedDates addObject:[self dateWithOutTime:dateDay.date]]; + if ([self.delegate respondsToSelector:@selector(datePicker:selectedDate:)]) { + [self.delegate datePicker:self selectedDate:dateDay.date]; + } } - if (_autoCloseOnSelectDate) { - [self.delegate datePickerDonePressed:self]; + } else { + [self setInternalDate:dateDay.date]; + if (![_selectedDates containsObject:[self dateWithOutTime:dateDay.date]]){ + [_selectedDates addObject:[self dateWithOutTime:dateDay.date]]; + [dateDay setSelected:YES]; + if ([self.delegate respondsToSelector:@selector(datePicker:selectedDate:)]) { + [self.delegate datePicker:self selectedDate:dateDay.date]; + } + } else { + if ([self.delegate respondsToSelector:@selector(datePicker:deselectedDate:)]) { + [self.delegate datePicker:self deselectedDate:dateDay.date]; + } + [dateDay setLightText:dateInDifferentMonth]; + [dateDay setSelected:NO]; + [_selectedDates removeObject:dateDay.date]; } + self.okBtn.enabled = [self shouldOkBeEnabled]; + } + if (_autoCloseOnSelectDate) { + [self.delegate datePickerDonePressed:self]; + } + if (dateInDifferentMonth) { + [self slideTransitionViewInDirection:[dateDay.date timeIntervalSinceDate:firstOfCurrentMonth]<0 ? UISwipeGestureRecognizerDirectionRight : UISwipeGestureRecognizerDirectionLeft]; } } @@ -613,6 +682,14 @@ - (BOOL)dateInCurrentMonth:(NSDate *)date{ return [comp1 year] == [comp2 year] && [comp1 month] == [comp2 month]; } +- (NSDate *)dateWithOutTime:(NSDate *)datDate { + if(!datDate) { + datDate = [NSDate date]; + } + NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:datDate]; + return [[NSCalendar currentCalendar] dateFromComponents:comps]; +} + #pragma mark - Cleanup - (void)didReceiveMemoryWarning { diff --git a/THCalendarDatePickerExample/Podfile.lock b/THCalendarDatePickerExample/Podfile.lock index d426193..73f766e 100644 --- a/THCalendarDatePickerExample/Podfile.lock +++ b/THCalendarDatePickerExample/Podfile.lock @@ -1,7 +1,7 @@ PODS: - - KNSemiModalViewController_hons82 (0.4.3) - - THCalendarDatePicker (1.2.5): - - KNSemiModalViewController_hons82 (~> 0.4.3) + - KNSemiModalViewController_hons82 (0.4.4) + - THCalendarDatePicker (1.2.6): + - KNSemiModalViewController_hons82 (~> 0.4.4) DEPENDENCIES: - THCalendarDatePicker (from `./../../THCalendarDatePicker`) @@ -11,7 +11,7 @@ EXTERNAL SOURCES: :path: "./../../THCalendarDatePicker" SPEC CHECKSUMS: - KNSemiModalViewController_hons82: 08916c03c9320759006246aa5073b290a5fd5891 - THCalendarDatePicker: 65c63c1814d1a27defc89f4b72ec7a1d01d5503d + KNSemiModalViewController_hons82: 22aead5ef5d8ca749bc1babb43fa7d6f14b030a3 + THCalendarDatePicker: 809e0c11affecb7087eee08ca55a9a7ab083829b COCOAPODS: 0.39.0 diff --git a/THCalendarDatePickerExample/THCalendarDatePickerExample.xcworkspace/xcshareddata/xcschemes/THCalendarDatePickerExample.xcscheme b/THCalendarDatePickerExample/THCalendarDatePickerExample.xcworkspace/xcshareddata/xcschemes/THCalendarDatePickerExample.xcscheme new file mode 100644 index 0000000..0ff9a19 --- /dev/null +++ b/THCalendarDatePickerExample/THCalendarDatePickerExample.xcworkspace/xcshareddata/xcschemes/THCalendarDatePickerExample.xcscheme @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/THCalendarDatePickerExample/THCalendarDatePickerExample/TestViewController.m b/THCalendarDatePickerExample/THCalendarDatePickerExample/TestViewController.m index 6341093..9e8d08b 100644 --- a/THCalendarDatePickerExample/THCalendarDatePickerExample/TestViewController.m +++ b/THCalendarDatePickerExample/THCalendarDatePickerExample/TestViewController.m @@ -40,6 +40,7 @@ - (IBAction)touchedButton:(id)sender { //[self.datePicker setDisableFutureSelection:NO]; [self.datePicker setDaysInHistorySelection:1]; [self.datePicker setDaysInFutureSelection:0]; +// [self.datePicker setAllowMultiDaySelection:YES]; // [self.datePicker setDateTimeZoneWithName:@"UTC"]; //[self.datePicker setAutoCloseCancelDelay:5.0]; [self.datePicker setSelectedBackgroundColor:[UIColor colorWithRed:125/255.0 green:208/255.0 blue:0/255.0 alpha:1.0]]; diff --git a/THCalendarDatePickerFromToSwiftExample/Podfile.lock b/THCalendarDatePickerFromToSwiftExample/Podfile.lock index f4c98fe..7be7b51 100644 --- a/THCalendarDatePickerFromToSwiftExample/Podfile.lock +++ b/THCalendarDatePickerFromToSwiftExample/Podfile.lock @@ -1,7 +1,7 @@ PODS: - - KNSemiModalViewController_hons82 (0.4.3) - - THCalendarDatePicker (1.2.5): - - KNSemiModalViewController_hons82 (~> 0.4.3) + - KNSemiModalViewController_hons82 (0.4.4) + - THCalendarDatePicker (1.2.6): + - KNSemiModalViewController_hons82 (~> 0.4.4) DEPENDENCIES: - THCalendarDatePicker (from `../../THCalendarDatePicker`) @@ -11,7 +11,7 @@ EXTERNAL SOURCES: :path: "../../THCalendarDatePicker" SPEC CHECKSUMS: - KNSemiModalViewController_hons82: 08916c03c9320759006246aa5073b290a5fd5891 - THCalendarDatePicker: 65c63c1814d1a27defc89f4b72ec7a1d01d5503d + KNSemiModalViewController_hons82: 22aead5ef5d8ca749bc1babb43fa7d6f14b030a3 + THCalendarDatePicker: 809e0c11affecb7087eee08ca55a9a7ab083829b COCOAPODS: 0.39.0 diff --git a/THCalendarDatePickerFromToSwiftExample/THCalendarDatePickerFromToSwiftExample/ViewController.swift b/THCalendarDatePickerFromToSwiftExample/THCalendarDatePickerFromToSwiftExample/ViewController.swift index 82dc109..ad1b356 100644 --- a/THCalendarDatePickerFromToSwiftExample/THCalendarDatePickerFromToSwiftExample/ViewController.swift +++ b/THCalendarDatePickerFromToSwiftExample/THCalendarDatePickerFromToSwiftExample/ViewController.swift @@ -47,9 +47,9 @@ class ViewController: UIViewController, THDatePickerDelegate { let picker = THDatePickerViewController.datePicker() picker.delegate = self picker.date = self.curToDate - picker.selectedBackgroundColor = UIColor.brownColor() - picker.currentDateColor = UIColor.orangeColor() - picker.currentDateColorSelected = UIColor.yellowColor() + picker.selectedBackgroundColor = UIColor.yellowColor() + picker.currentDateColor = UIColor.greenColor() + picker.currentDateColorSelected = UIColor.brownColor() return picker }() diff --git a/THCalendarDatePickerSwiftExample/Podfile.lock b/THCalendarDatePickerSwiftExample/Podfile.lock index d426193..73f766e 100644 --- a/THCalendarDatePickerSwiftExample/Podfile.lock +++ b/THCalendarDatePickerSwiftExample/Podfile.lock @@ -1,7 +1,7 @@ PODS: - - KNSemiModalViewController_hons82 (0.4.3) - - THCalendarDatePicker (1.2.5): - - KNSemiModalViewController_hons82 (~> 0.4.3) + - KNSemiModalViewController_hons82 (0.4.4) + - THCalendarDatePicker (1.2.6): + - KNSemiModalViewController_hons82 (~> 0.4.4) DEPENDENCIES: - THCalendarDatePicker (from `./../../THCalendarDatePicker`) @@ -11,7 +11,7 @@ EXTERNAL SOURCES: :path: "./../../THCalendarDatePicker" SPEC CHECKSUMS: - KNSemiModalViewController_hons82: 08916c03c9320759006246aa5073b290a5fd5891 - THCalendarDatePicker: 65c63c1814d1a27defc89f4b72ec7a1d01d5503d + KNSemiModalViewController_hons82: 22aead5ef5d8ca749bc1babb43fa7d6f14b030a3 + THCalendarDatePicker: 809e0c11affecb7087eee08ca55a9a7ab083829b COCOAPODS: 0.39.0