forked from escoz/QuickDialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QDateTimeElement.m
206 lines (163 loc) · 6.52 KB
/
QDateTimeElement.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Copyright 2011 ESCOZ Inc - http://escoz.com
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
// file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// TODO: Needs to be rewritten to use a custom UIViewController with the elements in it.
// the animation is not smooth when using the dateselector as a keyboard
#import "QDateTimeElement.h"
#import "QDateTimeInlineElement.h"
#import "QuickDialogController.h"
#import "QuickDialog.h"
@interface QDateTimeElement ()
- (void)initializeRoot;
- (void)updateElements;
@end
@implementation QDateTimeElement
@synthesize dateValue = _dateValue;
- (void)setMode:(UIDatePickerMode)mode {
_mode = mode;
[[self sections] removeAllObjects];
[self initializeRoot];
}
- (void)setMinuteInterval:(NSInteger)minuteInterval
{
_minuteInterval = minuteInterval;
self.sections = nil;
[self initializeRoot];
}
- (void)setDateValue:(NSDate *)date {
_dateValue = date;
[self updateElements];
}
- (void)setTicksValue:(NSNumber *)ticks {
[self setDateValue:[NSDate dateWithTimeIntervalSince1970:ticks.doubleValue]];
}
-(NSNumber *)ticksValue {
return [NSNumber numberWithDouble:[self.dateValue timeIntervalSince1970]];
}
- (UIDatePickerMode)mode {
return _mode;
}
- (NSInteger)minuteInterval
{
return _minuteInterval;
}
- (QDateTimeElement *)init {
self = [super init];
_grouped = YES;
_mode = UIDatePickerModeDateAndTime;
[self initializeRoot];
return self;
}
- (QDateTimeElement *)initWithTitle:(NSString *)title date:(NSDate *)date {
self = [super init];
if (self!=nil){
_grouped = YES;
_mode = UIDatePickerModeDateAndTime;
_title = title;
_dateValue = date;
[self updateElements];
}
return self;
}
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller {
UITableViewCell *cell = [super getCellForTableView:tableView controller:controller];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
switch (_mode) {
case UIDatePickerModeDate:
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
break;
case UIDatePickerModeTime:
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
break;
case UIDatePickerModeDateAndTime:
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
break;
case UIDatePickerModeCountDownTimer:
break;
}
cell.detailTextLabel.text = [dateFormatter stringFromDate:_dateValue];
return cell;
}
- (void)initializeRoot {
NSDate *dateForSection = _dateValue;
if (dateForSection==nil){
dateForSection = NSDate.date;
}
QSection *section = [[QSection alloc] initWithTitle:(_mode == UIDatePickerModeDateAndTime ? @"\n" : @"\n\n")];
if (_mode == UIDatePickerModeTime || _mode == UIDatePickerModeDateAndTime){
QDateTimeInlineElement *timeElement = (QDateTimeInlineElement *) [[QDateTimeInlineElement alloc] initWithKey:@"time"];
timeElement.dateValue = dateForSection;
timeElement.centerLabel = YES;
timeElement.mode = UIDatePickerModeTime;
timeElement.hiddenToolbar = YES;
timeElement.minuteInterval = _minuteInterval;
[section addElement:timeElement];
}
[self addSection:section];
}
- (void)updateElements
{
QDateTimeInlineElement *dateElement = (QDateTimeInlineElement *)[self elementWithKey:@"date"];
QDateTimeInlineElement *timeElement = (QDateTimeInlineElement *)[self elementWithKey:@"time"];
NSDate *dateForElements = (_dateValue == nil) ? NSDate.date : _dateValue;
if (dateElement != nil) {
dateElement.dateValue = dateForElements;
}
if (timeElement != nil) {
timeElement.dateValue = dateForElements;
}
}
- (void)fetchValueIntoObject:(id)obj {
if (_key==nil)
return;
[obj setValue:_dateValue forKey:_key];
}
- (void)selected:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller indexPath:(NSIndexPath *)indexPath {
if (self.sections==nil)
return;
QuickDialogController * newController = [controller controllerForRoot:self];
newController.quickDialogTableView.scrollEnabled = NO;
[controller displayViewController:newController];
__weak QuickDialogController *controllerForBlock = newController;
newController.willDisappearCallback = ^{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[((QSection *)[controllerForBlock.root.sections objectAtIndex:0]) fetchValueIntoObject:dict];
NSDate *date;
NSDate *time;
if (_mode == UIDatePickerModeTime){
time = [dict valueForKey:@"time"];
date = [NSDate date];
}
else if (_mode == UIDatePickerModeDate){
date = [dict valueForKey:@"date"];
time = [NSDate date];
}
else if (_mode == UIDatePickerModeDateAndTime){
date = [dict valueForKey:@"date"];
time = [dict valueForKey:@"time"];
} else {
NSLog(@"This control was not created to handle this time of UIDatePickerMode");
}
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:kCFCalendarUnitHour | kCFCalendarUnitMinute | kCFCalendarUnitSecond fromDate:time];
[components setHour:[timeComponents hour]];
[components setMinute:[timeComponents minute]];
[components setSecond:[timeComponents second]];
self.dateValue = [[NSCalendar currentCalendar] dateFromComponents:components];
};
[newController.quickDialogTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
}
@end