fakes.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // fakes.h
  3. // FLEX
  4. //
  5. // Created by Kevin Bradley on 12/22/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "fakes.h"
  9. #import "NSObject+FLEX_Reflection.h"
  10. @interface UIImage (private)
  11. +(UIImage *)symbolImageNamed:(NSString *)symbolName;
  12. @end
  13. @interface KBSearchButton()
  14. @property UITextField *searchField; //helps us get a keyboard onscreen and acts as a proxy to move text to our UISearchBar
  15. @end
  16. @implementation KBSearchButton
  17. + (instancetype)buttonWithType:(UIButtonType)buttonType {
  18. KBSearchButton *button = [super buttonWithType:buttonType];
  19. [button setImage:[UIImage symbolImageNamed:@"magnifyingglass"] forState:UIControlStateNormal];
  20. button.frame = CGRectMake(0, 0, 150, 70);
  21. UITextField *tf = [[UITextField alloc]init];
  22. tf.clearButtonMode = UITextFieldViewModeAlways;
  23. button.searchField = tf;
  24. tf.delegate = button;
  25. [button addSubview:tf];
  26. [button addTarget:button action:@selector(triggerSearchField) forControlEvents:UIControlEventPrimaryActionTriggered];
  27. [button addListeners];
  28. return button;
  29. }
  30. - (void)textChanged:(NSNotification *)n {
  31. self.searchBar.text = self.searchField.text;
  32. }
  33. - (void)addListeners {
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:nil];
  35. }
  36. - (void)dealloc {
  37. [[NSNotificationCenter defaultCenter] removeObserver:self];
  38. }
  39. - (void)triggerSearchField {
  40. self.searchField.text = self.searchBar.text;
  41. //[self.searchBar becomeFirstResponder];
  42. [self.searchField becomeFirstResponder];
  43. //wait for 0.1 seconds and then decrease the opacity of UISystemInputViewController presenting our UIKeyboard
  44. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  45. UIViewController *vc = [self topViewController];
  46. vc.view.alpha = 0.6;
  47. });
  48. }
  49. @end
  50. @interface UIFakeSwitch() {
  51. BOOL _isOn;
  52. }
  53. @end
  54. @implementation UIFakeSwitch
  55. - (BOOL)isOn {
  56. return _isOn;
  57. }
  58. - (void)initDefaults {
  59. self.onTintColor = [UIColor greenColor];
  60. }
  61. - (instancetype)initWithCoder:(NSCoder *)coder {
  62. self = [super initWithCoder:coder];
  63. if (self){
  64. [self initDefaults];
  65. }
  66. return self;
  67. }
  68. - (instancetype)init {
  69. self = [super init];
  70. if (self){
  71. [self initDefaults];
  72. }
  73. return self;
  74. }
  75. - (instancetype)initWithFrame:(CGRect)frame {
  76. self = [super initWithFrame:frame];
  77. if (self){
  78. [self initDefaults];
  79. }
  80. return self;
  81. }
  82. - (UIColor *)backgroundColor {
  83. if ([self isOn]) return self.onTintColor;
  84. return [super backgroundColor];
  85. }
  86. - (void)setOn:(BOOL)on{
  87. [self setOn:on animated:true];
  88. }
  89. - (NSString *)onTitle {
  90. return @"TRUE";
  91. }
  92. - (NSString *)offTitle {
  93. return @"FALSE";
  94. }
  95. - (void)setOn:(BOOL)on animated:(BOOL)animated {
  96. _isOn = on;
  97. if (_isOn){
  98. [self setTitle:[self onTitle] forState:UIControlStateNormal];
  99. } else {
  100. [self setTitle:[self offTitle] forState:UIControlStateNormal];
  101. }
  102. //[self sendActionsForControlEvents:[self allControlEvents]];
  103. }
  104. + (id)newSwitch {
  105. UIFakeSwitch *new = [UIFakeSwitch buttonWithType:UIButtonTypeSystem];
  106. [new initDefaults];
  107. return new;
  108. }
  109. @end