XLFormDateCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. NSDateFormatter *_dateFormatter;
  35. }
  36. - (UIView *)inputView
  37. {
  38. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer]){
  39. if (self.rowDescriptor.value){
  40. [self.datePicker setDate:self.rowDescriptor.value animated:[self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer]];
  41. }
  42. [self setModeToDatePicker:self.datePicker];
  43. return self.datePicker;
  44. }
  45. return [super inputView];
  46. }
  47. - (BOOL)canBecomeFirstResponder
  48. {
  49. return !self.rowDescriptor.isDisabled;
  50. }
  51. -(BOOL)becomeFirstResponder
  52. {
  53. if (self.isFirstResponder){
  54. return [super becomeFirstResponder];
  55. }
  56. _beforeChangeColor = self.detailTextLabel.textColor;
  57. BOOL result = [super becomeFirstResponder];
  58. if (result){
  59. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  60. {
  61. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  62. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:(selectedRowPath.row + 1) inSection:selectedRowPath.section];
  63. XLFormSectionDescriptor * formSection = [self.formViewController.form.formSections objectAtIndex:nextRowPath.section];
  64. XLFormRowDescriptor * datePickerRowDescriptor = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:XLFormRowDescriptorTypeDatePicker];
  65. XLFormDatePickerCell * datePickerCell = (XLFormDatePickerCell *)[datePickerRowDescriptor cellForFormController:self.formViewController];
  66. [self setModeToDatePicker:datePickerCell.datePicker];
  67. if (self.rowDescriptor.value){
  68. [datePickerCell.datePicker setDate:self.rowDescriptor.value animated:[self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]];
  69. }
  70. NSAssert([datePickerCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)], @"inline cell must conform to XLFormInlineRowDescriptorCell");
  71. UITableViewCell<XLFormInlineRowDescriptorCell> * inlineCell = (UITableViewCell<XLFormInlineRowDescriptorCell> *)datePickerCell;
  72. inlineCell.inlineRowDescriptor = self.rowDescriptor;
  73. [formSection addFormRow:datePickerRowDescriptor afterRow:self.rowDescriptor];
  74. [self.formViewController ensureRowIsVisible:datePickerRowDescriptor];
  75. }
  76. }
  77. return result;
  78. }
  79. -(BOOL)resignFirstResponder
  80. {
  81. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  82. {
  83. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  84. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
  85. XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
  86. BOOL result = [super resignFirstResponder];
  87. if ([nextFormRow.rowType isEqualToString:XLFormRowDescriptorTypeDatePicker]){
  88. [self.rowDescriptor.sectionDescriptor removeFormRow:nextFormRow];
  89. }
  90. return result;
  91. }
  92. return [super resignFirstResponder];
  93. }
  94. #pragma mark - XLFormDescriptorCell
  95. -(void)configure
  96. {
  97. [super configure];
  98. self.formDatePickerMode = XLFormDateDatePickerModeGetFromRowDescriptor;
  99. _dateFormatter = [[NSDateFormatter alloc] init];
  100. }
  101. -(void)update
  102. {
  103. [super update];
  104. self.accessoryType = UITableViewCellAccessoryNone;
  105. self.editingAccessoryType = UITableViewCellAccessoryNone;
  106. [self.textLabel setText:self.rowDescriptor.title];
  107. self.selectionStyle = self.rowDescriptor.isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
  108. self.textLabel.text = [NSString stringWithFormat:@"%@%@", self.rowDescriptor.title, self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""];
  109. self.detailTextLabel.text = [self valueDisplayText];
  110. }
  111. -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  112. {
  113. [self.formViewController.tableView deselectRowAtIndexPath:[controller.form indexPathOfFormRow:self.rowDescriptor] animated:YES];
  114. }
  115. -(BOOL)formDescriptorCellCanBecomeFirstResponder
  116. {
  117. return [self canBecomeFirstResponder];
  118. }
  119. -(BOOL)formDescriptorCellBecomeFirstResponder
  120. {
  121. if ([self isFirstResponder]){
  122. return [self resignFirstResponder];
  123. }
  124. return [self becomeFirstResponder];
  125. }
  126. -(void)highlight
  127. {
  128. [super highlight];
  129. self.detailTextLabel.textColor = self.tintColor;
  130. }
  131. -(void)unhighlight
  132. {
  133. [super unhighlight];
  134. self.detailTextLabel.textColor = _beforeChangeColor;
  135. }
  136. #pragma mark - helpers
  137. -(NSString *)valueDisplayText
  138. {
  139. return self.rowDescriptor.value ? [self formattedDate:self.rowDescriptor.value] : self.rowDescriptor.noValueDisplayText;
  140. }
  141. - (NSString *)formattedDate:(NSDate *)date
  142. {
  143. if (self.rowDescriptor.valueTransformer){
  144. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  145. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  146. NSString * tranformedValue = [valueTransformer transformedValue:self.rowDescriptor.value];
  147. if (tranformedValue){
  148. return tranformedValue;
  149. }
  150. }
  151. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline]){
  152. _dateFormatter.dateStyle = NSDateFormatterMediumStyle;
  153. _dateFormatter.timeStyle = NSDateFormatterNoStyle;
  154. return [_dateFormatter stringFromDate:date];
  155. }
  156. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline]){
  157. _dateFormatter.dateStyle = NSDateFormatterNoStyle;
  158. _dateFormatter.timeStyle = NSDateFormatterShortStyle;
  159. return [_dateFormatter stringFromDate:date];
  160. }
  161. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]){
  162. NSCalendar *calendar = [NSCalendar currentCalendar];
  163. [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  164. NSDateComponents *time = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
  165. return [NSString stringWithFormat:@"%ld%@ %ldmin", (long)[time hour], (long)[time hour] == 1 ? @"hour" : @"hours", (long)[time minute]];
  166. }
  167. _dateFormatter.dateStyle = NSDateFormatterShortStyle;
  168. _dateFormatter.timeStyle = NSDateFormatterShortStyle;
  169. return [_dateFormatter stringFromDate:date];
  170. }
  171. -(void)setModeToDatePicker:(UIDatePicker *)datePicker
  172. {
  173. if ((([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDate]) && self.formDatePickerMode == XLFormDateDatePickerModeGetFromRowDescriptor) || self.formDatePickerMode == XLFormDateDatePickerModeDate){
  174. datePicker.datePickerMode = UIDatePickerModeDate;
  175. }
  176. else if ((([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTime]) && self.formDatePickerMode == XLFormDateDatePickerModeGetFromRowDescriptor) || self.formDatePickerMode == XLFormDateDatePickerModeTime){
  177. datePicker.datePickerMode = UIDatePickerModeTime;
  178. }
  179. else if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimer] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline]){
  180. datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
  181. datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  182. }
  183. else{
  184. datePicker.datePickerMode = UIDatePickerModeDateAndTime;
  185. }
  186. if (self.minuteInterval)
  187. datePicker.minuteInterval = self.minuteInterval;
  188. if (self.minimumDate)
  189. datePicker.minimumDate = self.minimumDate;
  190. if (self.maximumDate)
  191. datePicker.maximumDate = self.maximumDate;
  192. if (self.locale) {
  193. datePicker.locale = self.locale;
  194. }
  195. }
  196. #pragma mark - Properties
  197. -(UIDatePicker *)datePicker
  198. {
  199. if (_datePicker) return _datePicker;
  200. _datePicker = [[UIDatePicker alloc] init];
  201. [self setModeToDatePicker:_datePicker];
  202. [_datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
  203. return _datePicker;
  204. }
  205. -(void)setLocale:(NSLocale *)locale
  206. {
  207. _locale = locale;
  208. _dateFormatter.locale = locale;
  209. }
  210. #pragma mark - Target Action
  211. - (void)datePickerValueChanged:(UIDatePicker *)sender
  212. {
  213. self.rowDescriptor.value = sender.date;
  214. [self.formViewController updateFormRow:self.rowDescriptor];
  215. }
  216. -(void)setFormDatePickerMode:(XLFormDateDatePickerMode)formDatePickerMode
  217. {
  218. _formDatePickerMode = formDatePickerMode;
  219. if ([self isFirstResponder]){
  220. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeDateTimeInline] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeCountDownTimerInline])
  221. {
  222. NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
  223. NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
  224. XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
  225. if ([nextFormRow.rowType isEqualToString:XLFormRowDescriptorTypeDatePicker]){
  226. XLFormDatePickerCell * datePickerCell = (XLFormDatePickerCell *)[nextFormRow cellForFormController:self.formViewController];
  227. [self setModeToDatePicker:datePickerCell.datePicker];
  228. }
  229. }
  230. }
  231. }
  232. @end