XLFormLeftRightSelectorCell.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // XLFormLeftRightSelectorCell.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 "UIView+XLFormAdditions.h"
  26. #import "XLFormRightImageButton.h"
  27. #import "NSObject+XLFormAdditions.h"
  28. #import "XLFormLeftRightSelectorCell.h"
  29. @implementation XLFormLeftRightSelectorCell
  30. {
  31. UITextField * _constraintTextField;
  32. }
  33. @synthesize leftButton = _leftButton;
  34. @synthesize rightLabel = _rightLabel;
  35. #pragma mark - Properties
  36. -(UIButton *)leftButton
  37. {
  38. if (_leftButton) return _leftButton;
  39. _leftButton = [[XLFormRightImageButton alloc] init];
  40. [_leftButton setTranslatesAutoresizingMaskIntoConstraints:NO];
  41. UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XLForm.bundle/forwardarrow.png"]];
  42. [imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
  43. [_leftButton addSubview:imageView];
  44. [_leftButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[image(8)]|" options:0 metrics:0 views:@{@"image": imageView}]];
  45. UIView * separatorTop = [UIView autolayoutView];
  46. UIView * separatorBottom = [UIView autolayoutView];
  47. [_leftButton addSubview:separatorTop];
  48. [_leftButton addSubview:separatorBottom];
  49. [_leftButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[separatorTop(separatorBottom)][image][separatorBottom]|" options:0 metrics:0 views:@{@"image": imageView, @"separatorTop": separatorTop, @"separatorBottom": separatorBottom}]];
  50. _leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 15);
  51. [_leftButton setTitleColor:[UIColor colorWithRed:0.0 green:0.478431 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
  52. [_leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
  53. [_leftButton setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizontal];
  54. return _leftButton;
  55. }
  56. -(UILabel *)rightLabel
  57. {
  58. if (_rightLabel) return _rightLabel;
  59. _rightLabel = [UILabel autolayoutView];
  60. [_rightLabel setTextColor:[UIColor grayColor]];
  61. [_rightLabel setTextAlignment:NSTextAlignmentRight];
  62. return _rightLabel;
  63. }
  64. -(XLFormLeftRightSelectorOption *)leftOptionForDescription:(NSString *)description
  65. {
  66. if (description){
  67. return [[self.rowDescriptor.selectorOptions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  68. return ([description isEqual:[((XLFormLeftRightSelectorOption *)evaluatedObject).leftValue displayText]]);
  69. }]] firstObject];
  70. }
  71. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"" userInfo:nil];
  72. }
  73. -(XLFormLeftRightSelectorOption *)leftOptionForOption:(id)option
  74. {
  75. if (option){
  76. return [[self.rowDescriptor.selectorOptions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
  77. XLFormLeftRightSelectorOption * evaluatedLeftOption = (XLFormLeftRightSelectorOption *)evaluatedObject;
  78. return [evaluatedLeftOption.leftValue isEqual:option];
  79. }]] firstObject];
  80. }
  81. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"" userInfo:nil];
  82. }
  83. #pragma mark - XLFormDescriptorCell
  84. -(void)configure
  85. {
  86. [super configure];
  87. UIView * separatorView = [UIView autolayoutView];
  88. _constraintTextField = [UITextField autolayoutView];
  89. [_constraintTextField setText:@"Option"];
  90. _constraintTextField.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  91. [separatorView setBackgroundColor:[UIColor colorWithWhite:0.85 alpha:1.0]];
  92. [self.contentView addSubview:_constraintTextField];
  93. [_constraintTextField setHidden:YES];
  94. [self.contentView addSubview:self.leftButton];
  95. [self.contentView addSubview:self.rightLabel];
  96. [self.contentView addSubview:separatorView];
  97. [self.leftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  98. NSDictionary * views = @{@"leftButton" : self.leftButton, @"rightLabel": self.rightLabel, @"separatorView": separatorView, @"constraintTextField": _constraintTextField };
  99. [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
  100. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[constraintTextField]" options:0 metrics:nil views:views]];
  101. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[leftButton]-[separatorView(1)]-[rightLabel]-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
  102. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[separatorView(20)]" options:0 metrics:nil views:views]];
  103. [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[constraintTextField]-12-|" options:0 metrics:0 views:views]];
  104. }
  105. -(void)update
  106. {
  107. [super update];
  108. self.leftButton.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  109. self.rightLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  110. [self.leftButton setTitle:[NSString stringWithFormat:@"%@%@", [self.rowDescriptor.leftRightSelectorLeftOptionSelected displayText], self.rowDescriptor.required && self.rowDescriptor.sectionDescriptor.formDescriptor.addAsteriskToRequiredRowsTitle ? @"*" : @""] forState:UIControlStateNormal];
  111. [self.rowDescriptor setTitle:[self.rowDescriptor.leftRightSelectorLeftOptionSelected displayText]];
  112. self.rightLabel.text = [self rightTextLabel];
  113. [self.leftButton setEnabled:!self.rowDescriptor.isDisabled];
  114. self.accessoryView = self.rowDescriptor.isDisabled ? nil : [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"XLForm.bundle/forwardarrow.png"]];
  115. self.editingAccessoryView = self.accessoryView;
  116. self.selectionStyle = self.rowDescriptor.isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
  117. }
  118. -(NSString *)rightTextLabel
  119. {
  120. return (self.rowDescriptor.value ? [self.rowDescriptor.value displayText] : (self.rowDescriptor.leftRightSelectorLeftOptionSelected ? [self leftOptionForOption:self.rowDescriptor.leftRightSelectorLeftOptionSelected].noValueDisplayText : @""));
  121. }
  122. -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  123. {
  124. if (self.rowDescriptor.leftRightSelectorLeftOptionSelected){
  125. XLFormLeftRightSelectorOption * option = [self leftOptionForOption:self.rowDescriptor.leftRightSelectorLeftOptionSelected];
  126. if (option.rightOptions){
  127. XLFormOptionsViewController * optionsViewController = [[XLFormOptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  128. optionsViewController.title = option.selectorTitle;
  129. optionsViewController.rowDescriptor = self.rowDescriptor;
  130. [controller.navigationController pushViewController:optionsViewController animated:YES];
  131. }
  132. else{
  133. XLFormLeftRightSelectorOption * option = [self leftOptionForOption:self.rowDescriptor.leftRightSelectorLeftOptionSelected];
  134. Class selectorClass = option.rightSelectorControllerClass;
  135. UIViewController<XLFormRowDescriptorViewController> *selectorViewController = [[selectorClass alloc] init];
  136. selectorViewController.rowDescriptor = self.rowDescriptor;
  137. selectorViewController.title = self.rowDescriptor.selectorTitle;
  138. [controller.navigationController pushViewController:selectorViewController animated:YES];
  139. }
  140. }
  141. }
  142. -(NSString *)formDescriptorHttpParameterName
  143. {
  144. XLFormLeftRightSelectorOption * option = [self leftOptionForOption:self.rowDescriptor.leftRightSelectorLeftOptionSelected];
  145. return option.httpParameterKey;
  146. }
  147. - (id) chooseNewRightValueFromOption:(XLFormLeftRightSelectorOption*)option
  148. {
  149. switch (option.leftValueChangePolicy) {
  150. case XLFormLeftRightSelectorOptionLeftValueChangePolicyChooseLastOption:
  151. return [option.rightOptions lastObject];
  152. case XLFormLeftRightSelectorOptionLeftValueChangePolicyChooseFirstOption:
  153. return [option.rightOptions firstObject];
  154. case XLFormLeftRightSelectorOptionLeftValueChangePolicyNullifyRightValue:
  155. return nil;
  156. }
  157. return nil;
  158. }
  159. #pragma mark - Actions
  160. -(void)leftButtonPressed:(UIButton *)leftButton
  161. {
  162. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:self.rowDescriptor.selectorTitle
  163. message:nil
  164. preferredStyle:UIAlertControllerStyleActionSheet];
  165. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  166. style:UIAlertActionStyleCancel
  167. handler:nil]];
  168. __weak __typeof(self)weakSelf = self;
  169. for (XLFormLeftRightSelectorOption * leftOption in self.rowDescriptor.selectorOptions) {
  170. [alertController addAction:[UIAlertAction actionWithTitle:[leftOption.leftValue displayText]
  171. style:UIAlertActionStyleDefault
  172. handler:^(UIAlertAction *action) {
  173. weakSelf.rowDescriptor.value = [self chooseNewRightValueFromOption:leftOption];
  174. weakSelf.rowDescriptor.leftRightSelectorLeftOptionSelected = [self leftOptionForDescription:[leftOption.leftValue displayText]].leftValue;
  175. [weakSelf.formViewController updateFormRow:weakSelf.rowDescriptor];
  176. }]];
  177. }
  178. [self.formViewController presentViewController:alertController animated:YES completion:nil];
  179. }
  180. @end