XLFormOptionsViewController.m 8.8 KB

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