XLFormOptionsViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // XLFormOptionsViewController.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 "NSObject+XLFormAdditions.h"
  26. #import "XLFormOptionsViewController.h"
  27. #import "XLFormRightDetailCell.h"
  28. #import "XLForm.h"
  29. #import "NSObject+XLFormAdditions.h"
  30. #import "NSArray+XLFormAdditions.h"
  31. #define CELL_REUSE_IDENTIFIER @"OptionCell"
  32. @interface XLFormOptionsViewController () <UITableViewDataSource>
  33. @property NSString * titleHeaderSection;
  34. @property NSString * titleFooterSection;
  35. @end
  36. @implementation XLFormOptionsViewController
  37. @synthesize titleHeaderSection = _titleHeaderSection;
  38. @synthesize titleFooterSection = _titleFooterSection;
  39. @synthesize rowDescriptor = _rowDescriptor;
  40. @synthesize popoverController = __popoverController;
  41. - (instancetype)initWithStyle:(UITableViewStyle)style
  42. {
  43. self = [super initWithStyle:style];
  44. if (self){
  45. _titleFooterSection = nil;
  46. _titleHeaderSection = nil;
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithStyle:(UITableViewStyle)style titleHeaderSection:(NSString *)titleHeaderSection titleFooterSection:(NSString *)titleFooterSection
  51. {
  52. self = [self initWithStyle:style];
  53. if (self){
  54. _titleFooterSection = titleFooterSection;
  55. _titleHeaderSection = titleHeaderSection;
  56. }
  57. return self;
  58. }
  59. - (void)viewDidLoad
  60. {
  61. [super viewDidLoad];
  62. // register option cell
  63. [self.tableView registerClass:[XLFormRightDetailCell class] forCellReuseIdentifier:CELL_REUSE_IDENTIFIER];
  64. }
  65. #pragma mark - UITableViewDataSource
  66. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  67. {
  68. return [[self selectorOptions] count];
  69. }
  70. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72. XLFormRightDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:CELL_REUSE_IDENTIFIER forIndexPath:indexPath];
  73. id cellObject = [[self selectorOptions] objectAtIndex:indexPath.row];
  74. [self.rowDescriptor.cellConfigForSelector enumerateKeysAndObjectsUsingBlock:^(NSString *keyPath, id value, __unused BOOL *stop) {
  75. [cell setValue:(value == [NSNull null]) ? nil : value forKeyPath:keyPath];
  76. }];
  77. cell.textLabel.text = [self valueDisplayTextForOption:cellObject];
  78. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelector] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelectorPopover]){
  79. cell.accessoryType = ([self selectedValuesContainsOption:cellObject] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone);
  80. }
  81. else{
  82. if ([[self.rowDescriptor.value valueData] isEqual:[cellObject valueData]]){
  83. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  84. }
  85. else{
  86. cell.accessoryType = UITableViewCellAccessoryNone;
  87. }
  88. }
  89. return cell;
  90. }
  91. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  92. {
  93. return self.titleFooterSection;
  94. }
  95. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  96. {
  97. return self.titleHeaderSection;
  98. }
  99. #pragma mark - UITableViewDelegate
  100. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  101. {
  102. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  103. id cellObject = [[self selectorOptions] objectAtIndex:indexPath.row];
  104. if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelector] || [self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeMultipleSelectorPopover]){
  105. if ([self selectedValuesContainsOption:cellObject]){
  106. self.rowDescriptor.value = [self selectedValuesRemoveOption:cellObject];
  107. cell.accessoryType = UITableViewCellAccessoryNone;
  108. }
  109. else{
  110. self.rowDescriptor.value = [self selectedValuesAddOption:cellObject];
  111. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  112. }
  113. }
  114. else{
  115. if ([[self.rowDescriptor.value valueData] isEqual:[cellObject valueData]]){
  116. if (!self.rowDescriptor.required){
  117. self.rowDescriptor.value = nil;
  118. cell.accessoryType = UITableViewCellAccessoryNone;
  119. }
  120. }
  121. else{
  122. if (self.rowDescriptor.value){
  123. NSInteger index = [[self selectorOptions] formIndexForItem:self.rowDescriptor.value];
  124. if (index != NSNotFound){
  125. NSIndexPath * oldSelectedIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
  126. UITableViewCell *oldSelectedCell = [tableView cellForRowAtIndexPath:oldSelectedIndexPath];
  127. oldSelectedCell.accessoryType = UITableViewCellAccessoryNone;
  128. }
  129. }
  130. self.rowDescriptor.value = cellObject;
  131. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  132. }
  133. if (self.popoverController){
  134. [self.popoverController dismissPopoverAnimated:YES];
  135. [self.popoverController.delegate popoverControllerDidDismissPopover:self.popoverController];
  136. }
  137. else if ([self.parentViewController isKindOfClass:[UINavigationController class]]){
  138. [self.navigationController popViewControllerAnimated:YES];
  139. }
  140. }
  141. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  142. }
  143. #pragma mark - Helper
  144. -(NSMutableArray *)selectedValues
  145. {
  146. if (self.rowDescriptor.value == nil){
  147. return [NSMutableArray array];
  148. }
  149. NSAssert([self.rowDescriptor.value isKindOfClass:[NSArray class]], @"XLFormRowDescriptor value must be NSMutableArray");
  150. return [NSMutableArray arrayWithArray:self.rowDescriptor.value];
  151. }
  152. -(BOOL)selectedValuesContainsOption:(id)option
  153. {
  154. return ([self.selectedValues formIndexForItem:option] != NSNotFound);
  155. }
  156. -(NSMutableArray *)selectedValuesRemoveOption:(id)option
  157. {
  158. for (id selectedValueItem in self.selectedValues) {
  159. if ([[selectedValueItem valueData] isEqual:[option valueData]]){
  160. NSMutableArray * result = self.selectedValues;
  161. [result removeObject:selectedValueItem];
  162. return result;
  163. }
  164. }
  165. return self.selectedValues;
  166. }
  167. -(NSMutableArray *)selectedValuesAddOption:(id)option
  168. {
  169. NSAssert([self.selectedValues formIndexForItem:option] == NSNotFound, @"XLFormRowDescriptor value must not contain the option");
  170. NSMutableArray * result = self.selectedValues;
  171. [result addObject:option];
  172. return result;
  173. }
  174. -(NSString *)valueDisplayTextForOption:(id)option
  175. {
  176. if (self.rowDescriptor.valueTransformer){
  177. NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
  178. NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
  179. NSString * transformedValue = [valueTransformer transformedValue:option];
  180. if (transformedValue){
  181. return transformedValue;
  182. }
  183. }
  184. return [option displayText];
  185. }
  186. #pragma mark - Helpers
  187. -(NSArray *)selectorOptions
  188. {
  189. if (self.rowDescriptor.rowType == XLFormRowDescriptorTypeSelectorLeftRight){
  190. XLFormLeftRightSelectorOption * option = [self leftOptionForOption:self.rowDescriptor.leftRightSelectorLeftOptionSelected];
  191. return option.rightOptions;
  192. }
  193. else{
  194. return self.rowDescriptor.selectorOptions;
  195. }
  196. }
  197. -(XLFormLeftRightSelectorOption *)leftOptionForOption:(id)option
  198. {
  199. return [[self.rowDescriptor.selectorOptions filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary * __unused bindings) {
  200. XLFormLeftRightSelectorOption * evaluatedLeftOption = (XLFormLeftRightSelectorOption *)evaluatedObject;
  201. return [evaluatedLeftOption.leftValue isEqual:option];
  202. }]] firstObject];
  203. }
  204. @end