XLFormDateCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // XLFormDateCell.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLForm.h"
  26. #import "XLFormRowDescriptor.h"
  27. #import "XLFormDateCell.h"
  28. @interface XLFormDateCell()
  29. @property (nonatomic) UIDatePicker *datePicker;
  30. @end
  31. @implementation XLFormDateCell
  32. {
  33. UIColor * _beforeChangeColor;
  34. }
  35. - (UIView *)inputView
  36. {
  37. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer]){
  38. if (self.rowDescriptor.value){
  39. [self.datePicker setDate:self.rowDescriptor.value animated:[self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer]];
  40. }
  41. [self setModeToDatePicker:self.datePicker];
  42. return self.datePicker;
  43. }
  44. return [super inputView];
  45. }
  46. - (BOOL)canBecomeFirstResponder
  47. {
  48. return !self.rowDescriptor.isDisabled;
  49. }
  50. -(BOOL)becomeFirstResponder
  51. {
  52. if (self.isFirstResponder){
  53. return [super becomeFirstResponder];
  54. }
  55. _beforeChangeColor = self.detailTextLabel.textColor;
  56. BOOL result = [super becomeFirstResponder];
  57. if (result){
  58. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  59. {
  60. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  61. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:(selectedRowPath.row + 1) inSection:selectedRowPath.section];
  62. XLFormSectionDescriptor * formSection = [self.formViewController.form.formSections objectAtIndex:nextRowPath.section];
  63. XLFormRowDescriptor * datePickerRowDescriptor = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:XLFormRowDescriptorTypeDatePicker];
  64. XLFormDatePickerCell * datePickerCell = (XLFormDatePickerCell *)[datePickerRowDescriptor cellForFormController:self.formViewController];
  65. [self setModeToDatePicker:datePickerCell.datePicker];
  66. if (self.rowDescriptor.value){
  67. [datePickerCell.datePicker setDate:self.rowDescriptor.value animated:[self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]];
  68. }
  69. NSAssert([datePickerCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)], @"inline cell must conform to XLFormInlineRowDescriptorCell");
  70. UITableViewCell<XLFormInlineRowDescriptorCell> * inlineCell = (UITableViewCell<XLFormInlineRowDescriptorCell> *)datePickerCell;
  71. inlineCell.inlineRowDescriptor = self.rowDescriptor;
  72. [formSection addFormRow:datePickerRowDescriptor afterRow:self.rowDescriptor];
  73. [self.formViewController ensureRowIsVisible:datePickerRowDescriptor];
  74. }
  75. }
  76. return result;
  77. }
  78. -(BOOL)resignFirstResponder
  79. {
  80. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  81. {
  82. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  83. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
  84. XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
  85. BOOL result = [super resignFirstResponder];
  86. if ([nextFormRow.rowType isEqualToString:XLFormRowDescriptorTypeDatePicker]){
  87. [self.rowDescriptor.sectionDescriptor removeFormRow:nextFormRow];
  88. }
  89. return result;
  90. }
  91. return [super resignFirstResponder];
  92. }
  93. #pragma mark - XLFormDescriptorCell
  94. -(void)configure
  95. {
  96. [super configure];
  97. self.formDatePickerMode = XLFormDateDatePickerModeGetFromRowDescriptor;
  98. }
  99. -(void)update
  100. {
  101. [super update];
  102. self.accessoryType = UITableViewCellAccessoryNone;
  103. self.editingAccessoryType = UITableViewCellAccessoryNone;
  104. [self.textLabel setText:self.rowDescriptor.title];
  105. self.selectionStyle = self.rowDescriptor.isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
  106. self.textLabel.text = [NSString stringWithFormat:@"%@%@", self.rowDescriptor.title, self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""];
  107. self.detailTextLabel.text = [self valueDisplayText];
  108. }
  109. -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  110. {
  111. [self.formViewController.tableView deselectRowAtIndexPath:[controller.form indexPathOfFormRow:self.rowDescriptor] animated:YES];
  112. }
  113. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  114. {
  115. return [self canBecomeFirstResponder];
  116. }
  117. -(BOOL)formDescriptorCellBecomeFirstResponder
  118. {
  119. if ([self isFirstResponder]){
  120. return [self resignFirstResponder];
  121. }
  122. return [self becomeFirstResponder];
  123. }
  124. -(void)highlight
  125. {
  126. [super highlight];
  127. self.detailTextLabel.textColor = self.tintColor;
  128. }
  129. -(void)unhighlight
  130. {
  131. [super unhighlight];
  132. self.detailTextLabel.textColor = _beforeChangeColor;
  133. }
  134. #pragma mark - helpers
  135. -(NSString *)valueDisplayText
  136. {
  137. return self.rowDescriptor.value ? [self formattedDate:self.rowDescriptor.value] : self.rowDescriptor.noValueDisplayText;
  138. }
  139. - (NSString *)formattedDate:(NSDate *)date
  140. {
  141. if (self.rowDescriptor.valueTransformer){
  142. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  143. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  144. NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
  145. if (tranformedValue){
  146. return tranformedValue;
  147. }
  148. }
  149. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline]){
  150. return [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
  151. }
  152. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline]){
  153. return [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
  154. }
  155. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]){
  156. NSDateComponents *time = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
  157. return [NSString stringWithFormat:@"%ld%@ %ldmin", (long)[time hour], (long)[time hour] == 1 ? @"hour" : @"hours", (long)[time minute]];
  158. }
  159. return [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle];
  160. }
  161. -(void)setModeToDatePicker:(UIDatePicker *)datePicker
  162. {
  163. if ((([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate]) && self.formDatePickerMode == XLFormDateDatePickerModeGetFromRowDescriptor) || self.formDatePickerMode == XLFormDateDatePickerModeDate){
  164. datePicker.datePickerMode = UIDatePickerModeDate;
  165. }
  166. else if ((([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime]) && self.formDatePickerMode == XLFormDateDatePickerModeGetFromRowDescriptor) || self.formDatePickerMode == XLFormDateDatePickerModeTime){
  167. datePicker.datePickerMode = UIDatePickerModeTime;
  168. }
  169. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]){
  170. datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
  171. }
  172. else{
  173. datePicker.datePickerMode = UIDatePickerModeDateAndTime;
  174. }
  175. if (self.minuteInterval)
  176. datePicker.minuteInterval = self.minuteInterval;
  177. if (self.minimumDate)
  178. datePicker.minimumDate = self.minimumDate;
  179. if (self.maximumDate)
  180. datePicker.maximumDate = self.maximumDate;
  181. if (self.locale) {
  182. datePicker.locale = self.locale;
  183. }
  184. }
  185. #pragma mark - Properties
  186. -(UIDatePicker *)datePicker
  187. {
  188. if (_datePicker) return _datePicker;
  189. _datePicker = [[UIDatePicker alloc] init];
  190. [self setModeToDatePicker:_datePicker];
  191. [_datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
  192. return _datePicker;
  193. }
  194. #pragma mark - Target Action
  195. - (void)datePickerValueChanged:(UIDatePicker *)sender
  196. {
  197. self.rowDescriptor.value = sender.date;
  198. [self.formViewController updateFormRow:self.rowDescriptor];
  199. }
  200. -(void)setFormDatePickerMode:(XLFormDateDatePickerMode)formDatePickerMode
  201. {
  202. _formDatePickerMode = formDatePickerMode;
  203. if ([self isFirstResponder]){
  204. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  205. {
  206. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  207. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
  208. XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
  209. if ([nextFormRow.rowType isEqualToString:XLFormRowDescriptorTypeDatePicker]){
  210. XLFormDatePickerCell * datePickerCell = (XLFormDatePickerCell *)[nextFormRow cellForFormController:self.formViewController];
  211. [self setModeToDatePicker:datePickerCell.datePicker];
  212. }
  213. }
  214. }
  215. }
  216. @end