Skip to content

Commit

Permalink
Merge pull request #76 from skywinder/countDownDatePickerUpdate
Browse files Browse the repository at this point in the history
- General fixes for #74 & #50
  • Loading branch information
skywinder committed Oct 15, 2014
2 parents b234c16 + 9b6e806 commit d404a69
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 70 deletions.
25 changes: 15 additions & 10 deletions Pickers/ActionSheetDatePicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@

@class ActionSheetDatePicker;

typedef void(^ActionDateDoneBlock)(ActionSheetDatePicker *picker, NSDate *selectedDate, id origin);
typedef void(^ActionDateDoneBlock)(ActionSheetDatePicker *picker, id selectedDate, id origin); //selectedDate is NSDate or NSNumber for "UIDatePickerModeCountDownTimer"
typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker);

@interface ActionSheetDatePicker : AbstractActionSheetPicker

@property (nonatomic) NSDate *minimumDate;
@property (nonatomic) NSDate *maximumDate;
@property (nonatomic) NSInteger minuteInterval;
@property (nonatomic) NSCalendar *calendar;
@property (nonatomic) NSTimeZone *timeZone;
@property (nonatomic) NSLocale *locale;
@property (nonatomic, retain) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode
@property (nonatomic, retain) NSDate *maximumDate; // default is nil

@property (nonatomic) NSInteger minuteInterval; // display minutes wheel with interval. interval must be evenly divided into 60. default is 1. min is 1, max is 30

@property (nonatomic, retain) NSLocale *locale; // default is [NSLocale currentLocale]. setting nil returns to default
@property (nonatomic, copy) NSCalendar *calendar; // default is [NSCalendar currentCalendar]. setting nil returns to default
@property (nonatomic, retain) NSTimeZone *timeZone; // default is nil. use current time zone or time zone from calendar

@property (nonatomic, assign) NSTimeInterval countDownDuration; // for UIDatePickerModeCountDownTimer, ignored otherwise. default is 0.0. limit is 23:59 (86,399 seconds). value being set is div 60 (drops remaining seconds).

@property (nonatomic, copy) ActionDateDoneBlock onActionSheetDone;
@property (nonatomic, copy) ActionDateCancelBlock onActionSheetCancel;

+ (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin;

Expand All @@ -54,6 +61,7 @@ typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker);

- (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin cancelAction:(SEL)cancelAction;


- (instancetype)initWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode
selectedDate:(NSDate *)selectedDate
Expand All @@ -63,7 +71,4 @@ typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker);

- (void)eventForDatePicker:(id)sender;

@property (nonatomic, copy) ActionDateDoneBlock onActionSheetDone;
@property (nonatomic, copy) ActionDateCancelBlock onActionSheetCancel;

@end
25 changes: 16 additions & 9 deletions Pickers/ActionSheetDatePicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@
@interface ActionSheetDatePicker()
@property (nonatomic, assign) UIDatePickerMode datePickerMode;
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, assign) NSTimeInterval duration;
@end

@implementation ActionSheetDatePicker
@synthesize selectedDate = _selectedDate;
@synthesize datePickerMode = _datePickerMode;
@synthesize duration = _duration;

+ (id)showPickerWithTitle:(NSString *)title
datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate
Expand Down Expand Up @@ -79,7 +75,6 @@ - (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePicke
self.title = title;
self.datePickerMode = datePickerMode;
self.selectedDate = selectedDate;
self.duration = 60;
}
return self;
}
Expand Down Expand Up @@ -110,7 +105,14 @@ - (UIView *)configuredPickerView {
datePicker.timeZone = self.timeZone;
datePicker.locale = self.locale;

[datePicker setDate:self.selectedDate animated:NO];
// if datepicker is set with a date in countDownMode then
// 1h is added to the initial countdown
if (self.datePickerMode == UIDatePickerModeCountDownTimer) {
datePicker.countDownDuration = self.countDownDuration;
} else {
[datePicker setDate:self.selectedDate animated:NO];
}

[datePicker addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];

//need to keep a reference to the picker so we can clear the DataSource / Delegate when dismissing (not used in this picker, but just in case somebody uses this as a template for another picker)
Expand All @@ -123,14 +125,19 @@ - (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)orig
{
if (self.onActionSheetDone)
{
self.onActionSheetDone(self, self.selectedDate, origin);
if (self.datePickerMode == UIDatePickerModeCountDownTimer)
self.onActionSheetDone(self, @(self.countDownDuration), origin);
else
self.onActionSheetDone(self, self.selectedDate, origin);

return;
}
else if ([target respondsToSelector:action])
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
if (self.datePickerMode == UIDatePickerModeCountDownTimer) {
[target performSelector:action withObject:@(self.duration) withObject:origin];
[target performSelector:action withObject:@(self.countDownDuration) withObject:origin];

} else {
[target performSelector:action withObject:self.selectedDate withObject:origin];
}
Expand Down Expand Up @@ -162,7 +169,7 @@ - (void)eventForDatePicker:(id)sender
return;
UIDatePicker *datePicker = (UIDatePicker *)sender;
self.selectedDate = datePicker.date;
self.duration = datePicker.countDownDuration;
self.countDownDuration = datePicker.countDownDuration;
}

- (void)customButtonPressed:(id)sender {
Expand Down
Loading

0 comments on commit d404a69

Please sign in to comment.