FLEXArgumentInputSwitchView.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. [self.inputSwitch sizeToFit];
  24. [self.inputSwitch addTarget:self action:@selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];
  25. #else
  26. self.inputSwitch = [UIFakeSwitch newSwitch];
  27. [self.inputSwitch addTarget:self action:@selector(changeSwitchValue:) forControlEvents:UIControlEventPrimaryActionTriggered];
  28. #endif
  29. [self addSubview:self.inputSwitch];
  30. }
  31. return self;
  32. }
  33. - (void)changeSwitchValue:(UIFakeSwitch *)switchView {
  34. [switchView setOn:!switchView.isOn];
  35. [self switchValueDidChange:switchView];
  36. }
  37. #pragma mark Input/Output
  38. - (void)setInputValue:(id)inputValue {
  39. BOOL on = NO;
  40. if ([inputValue isKindOfClass:[NSNumber class]]) {
  41. NSNumber *number = (NSNumber *)inputValue;
  42. on = [number boolValue];
  43. } else if ([inputValue isKindOfClass:[NSValue class]]) {
  44. NSValue *value = (NSValue *)inputValue;
  45. if (strcmp([value objCType], @encode(BOOL)) == 0) {
  46. [value getValue:&on];
  47. }
  48. }
  49. self.inputSwitch.on = on;
  50. }
  51. - (id)inputValue {
  52. BOOL isOn = [self.inputSwitch isOn];
  53. NSValue *boxedBool = [NSValue value:&isOn withObjCType:@encode(BOOL)];
  54. return boxedBool;
  55. }
  56. - (void)switchValueDidChange:(id)sender {
  57. [self.delegate argumentInputViewValueDidChange:self];
  58. }
  59. #pragma mark - Layout and Sizing
  60. - (void)layoutSubviews {
  61. [super layoutSubviews];
  62. #if TARGET_OS_TV
  63. self.inputSwitch.frame = CGRectMake(50, 50, 200, 70);
  64. #else
  65. self.inputSwitch.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
  66. #endif
  67. }
  68. - (CGSize)sizeThatFits:(CGSize)size {
  69. CGSize fitSize = [super sizeThatFits:size];
  70. #if TARGET_OS_TV
  71. fitSize.height += 110;
  72. #else
  73. fitSize.height += self.inputSwitch.frame.size.height;
  74. #endif
  75. return fitSize;
  76. }
  77. #pragma mark - Class Helpers
  78. + (BOOL)supportsObjCType:(const char *)type withCurrentValue:(id)value {
  79. NSParameterAssert(type);
  80. // Only BOOLs. Current value is irrelevant.
  81. return strcmp(type, @encode(BOOL)) == 0;
  82. }
  83. @end