FLEXArgumentInputSwitchView.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // FLEXArgumentInputSwitchView.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/16/14.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXArgumentInputSwitchView.h"
  9. #import "fakes.h"
  10. @interface FLEXArgumentInputSwitchView ()
  11. #if !TARGET_OS_TV
  12. @property (nonatomic) UISwitch *inputSwitch;
  13. #else
  14. @property (nonatomic) UIFakeSwitch *inputSwitch;
  15. #endif
  16. @end
  17. @implementation FLEXArgumentInputSwitchView
  18. - (instancetype)initWithArgumentTypeEncoding:(const char *)typeEncoding {
  19. self = [super initWithArgumentTypeEncoding:typeEncoding];
  20. if (self) {
  21. #if !TARGET_OS_TV
  22. self.inputSwitch = [UISwitch new];
  23. #else
  24. self.inputSwitch = [UIFakeSwitch new];
  25. #endif
  26. [self.inputSwitch addTarget:self action:@selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];
  27. [self.inputSwitch sizeToFit];
  28. [self addSubview:self.inputSwitch];
  29. }
  30. return self;
  31. }
  32. #pragma mark Input/Output
  33. - (void)setInputValue:(id)inputValue {
  34. BOOL on = NO;
  35. if ([inputValue isKindOfClass:[NSNumber class]]) {
  36. NSNumber *number = (NSNumber *)inputValue;
  37. on = [number boolValue];
  38. } else if ([inputValue isKindOfClass:[NSValue class]]) {
  39. NSValue *value = (NSValue *)inputValue;
  40. if (strcmp([value objCType], @encode(BOOL)) == 0) {
  41. [value getValue:&on];
  42. }
  43. }
  44. self.inputSwitch.on = on;
  45. }
  46. - (id)inputValue {
  47. BOOL isOn = [self.inputSwitch isOn];
  48. NSValue *boxedBool = [NSValue value:&isOn withObjCType:@encode(BOOL)];
  49. return boxedBool;
  50. }
  51. - (void)switchValueDidChange:(id)sender {
  52. [self.delegate argumentInputViewValueDidChange:self];
  53. }
  54. #pragma mark - Layout and Sizing
  55. - (void)layoutSubviews {
  56. [super layoutSubviews];
  57. self.inputSwitch.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
  58. }
  59. - (CGSize)sizeThatFits:(CGSize)size {
  60. CGSize fitSize = [super sizeThatFits:size];
  61. fitSize.height += self.inputSwitch.frame.size.height;
  62. return fitSize;
  63. }
  64. #pragma mark - Class Helpers
  65. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value {
  66. NSParameterAssert(type);
  67. // Only BOOLs. Current value is irrelevant.
  68. return strcmp(type, @encode(BOOL)) == 0;
  69. }
  70. @end