DataPickerState.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "DataPickerState.h"
  17. NSString * const kMultipleSelectKey = @"multiple-select";
  18. NSString * const kElementsKey = @"elements";
  19. NSString * const kLabelKey = @"label";
  20. NSString * const kShortLabelKey = @"shortLabel";
  21. NSString * const kSelectedKey = @"selected";
  22. @implementation DataPickerState
  23. - (id)initWithDictionary:(NSDictionary *)dict {
  24. self = [super init];
  25. if (self) {
  26. _multipleSelectEnabled =
  27. [[dict objectForKey:kMultipleSelectKey] boolValue];
  28. NSMutableArray *cellLabels = [[NSMutableArray alloc] init];
  29. NSMutableSet *selectedCells = [[NSMutableSet alloc] init];
  30. NSArray *elements = [dict objectForKey:kElementsKey];
  31. for (NSDictionary *elementDict in elements) {
  32. NSMutableDictionary *cellLabelDict = [NSMutableDictionary dictionary];
  33. NSString *label = [elementDict objectForKey:kLabelKey];
  34. cellLabelDict[kLabelKey] = label;
  35. if ([elementDict objectForKey:kShortLabelKey]) {
  36. cellLabelDict[kShortLabelKey] = [elementDict objectForKey:kShortLabelKey];
  37. }
  38. [cellLabels addObject:cellLabelDict];
  39. // Default selection mode is unselected, unless specified in plist.
  40. if ([[elementDict objectForKey:kSelectedKey] boolValue]) {
  41. [selectedCells addObject:label];
  42. }
  43. }
  44. self.cellLabels = cellLabels;
  45. self.selectedCells = selectedCells;
  46. }
  47. return self;
  48. }
  49. @end