-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathQDateTimeInlineElement.m
More file actions
executable file
·169 lines (139 loc) · 5.42 KB
/
QDateTimeInlineElement.m
File metadata and controls
executable file
·169 lines (139 loc) · 5.42 KB
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
//
// 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.
//
#import "QDateEntryTableViewCell.h"
#import "QuickDialogController.h"
#import "QDateInlineTableViewCell.h"
@implementation QDateTimeInlineElement {
@private
NSDate *_maximumDate;
NSDate *_minimumDate;
__weak QTableViewCell *_cell;
}
@synthesize mode = _mode;
@synthesize timezone = _timezone;
@synthesize centerLabel = _centerLabel;
@synthesize maximumDate = _maximumDate;
@synthesize minimumDate = _minimumDate;
@synthesize onValueChanged = _onValueChanged;
@synthesize minuteInterval = _minuteInterval;
- (QDateTimeInlineElement *)init {
self = [super init];
_dateValue = [NSDate date];
self.keepSelected = YES;
return self;
}
- (QDateTimeInlineElement *)initWithKey:(NSString *)key {
self = [super initWithKey:key];
_dateValue = [NSDate date];
self.keepSelected = YES;
return self;
}
- (QDateTimeInlineElement *)initWithTitle:(NSString *)string date:(NSDate *)date timezone:(NSTimeZone *)timezone andMode:(UIDatePickerMode)mode {
self = [super initWithTitle:string Value:[date description]];
if (self!=nil){
_dateValue = date;
_mode = mode;
_timezone = timezone;
}
return self;
}
- (QDateTimeInlineElement *)initWithTitle:(NSString *)string date:(NSDate *)date andMode:(UIDatePickerMode)mode{
return [self initWithTitle:string date:date timezone:nil andMode:mode];
}
- (QDateTimeInlineElement *)initWithDate:(NSDate *)date andMode:(UIDatePickerMode)mode{
return [self initWithTitle:nil date:date andMode:mode];
}
- (void)setTicksValue:(NSNumber *)ticks {
if (ticks!=nil)
self.dateValue = [NSDate dateWithTimeIntervalSince1970:ticks.doubleValue];
}
- (void)setDateValue:(NSDate *)date {
_dateValue = date;
}
- (NSDate *)dateValue
{
if (self.mode == UIDatePickerModeDate) {
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.timeZone = _timezone;
NSDateComponents *dateComponents = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:_dateValue];
_dateValue = [gregorian dateFromComponents:dateComponents];
}
return _dateValue;
}
-(NSNumber *)ticksValue {
return [NSNumber numberWithDouble:[self.dateValue timeIntervalSince1970]];
}
- (UITableViewCell *)getCellForTableView:(QuickDialogTableView *)tableView controller:(QuickDialogController *)controller {
QTableViewCell *cell= self.showPickerInCell ? [self getInlineCell:tableView] : [self getEntryCell:tableView];
return cell;
}
- (QDateInlineTableViewCell *)getInlineCell:(QuickDialogTableView *)tableView
{
QDateInlineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuickformDateTimeInlineElement"];
if (cell == nil){
cell = [[QDateInlineTableViewCell alloc] init];
}
_cell = cell;
[cell prepareForElement:self inTableView:tableView];
cell.selectionStyle = !self.enabled || self.showPickerInCell ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue;
cell.imageView.image = self.image;
cell.labelingPolicy = self.labelingPolicy;
return cell;
}
- (QDateEntryTableViewCell *)getEntryCell:(QuickDialogTableView *)tableView
{
QDateEntryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QuickformDateTimeInlineElement"];
if (cell == nil){
cell = [[QDateEntryTableViewCell alloc] init];
}
_cell = cell;
[cell prepareForElement:self inTableView:tableView];
cell.selectionStyle = self.enabled ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
cell.textField.enabled = self.enabled;
cell.textField.userInteractionEnabled = self.enabled;
cell.imageView.image = self.image;
cell.labelingPolicy = self.labelingPolicy;
return cell;
}
- (void)performAction
{
if (self.showPickerInCell){
BOOL shouldEdit = !_cell.isEditing;
[((QuickDialogController *)self.controller).quickDialogTableView endEditingOnVisibleCells];
[_cell setEditing:shouldEdit];
[((QuickDialogController *)self.controller).quickDialogTableView reloadRowHeights];
}
}
- (NSString *)textValue {
NSTimeInterval timeInterval = self.dateValue.timeIntervalSinceNow;
NSInteger ti = (NSInteger)timeInterval;
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600);
return [NSString stringWithFormat:@"%02li:%02li:%02li", (long)hours, (long)minutes, (long)seconds];
}
- (void)fetchValueIntoObject:(id)obj {
if (_key==nil)
return;
[obj setValue:_dateValue forKey:_key];
}
- (CGFloat)getRowHeightForTableView:(QuickDialogTableView *)tableView
{
CGFloat height = [super getRowHeightForTableView:tableView];
if (!_cell.isEditing || !self.showPickerInCell) {
return height;
}
return height + 200;
}
@end