Browse Source

add it while its building and working, added search ability for tvOS

Kevin Bradley 3 years ago
parent
commit
fa85ebd63f

+ 46 - 11
Classes/Core/Controllers/FLEXTableViewController.m

@@ -16,6 +16,7 @@
 #import "FLEXResources.h"
 #import "UIBarButtonItem+FLEX.h"
 #import <objc/runtime.h>
+#import "fakes.h"
 
 @interface Block : NSObject
 - (void)invoke;
@@ -39,6 +40,11 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
 @property (nonatomic) UIBarButtonItem *middleToolbarItem;
 @property (nonatomic) UIBarButtonItem *middleLeftToolbarItem;
 @property (nonatomic) UIBarButtonItem *leftmostToolbarItem;
+
+#if TARGET_OS_TV
+@property (nonatomic) UISearchContainerViewController *searchContainer;
+#endif
+
 @end
 
 @implementation FLEXTableViewController
@@ -104,12 +110,17 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
         self.searchController.searchResultsUpdater = (id)self;
         self.searchController.delegate = (id)self;
         #if !TARGET_OS_TV
+        self.searchController.searchBar.delegate = self;
         self.searchController.dimsBackgroundDuringPresentation = NO;
+        #else
+        KBSearchButton *sb = [KBSearchButton buttonWithType:UIButtonTypeSystem];
+        sb.searchBar = self.searchController.searchBar;
+        UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithCustomView:sb];
+        self.navigationItem.leftBarButtonItem = searchButton;
         #endif
         self.searchController.hidesNavigationBarDuringPresentation = NO;
         /// Not necessary in iOS 13; remove this when iOS 13 is the minimum deployment target
-        self.searchController.searchBar.delegate = self;
-
+        
         self.automaticallyShowsSearchBarCancelButton = YES;
 
         #if FLEX_AT_LEAST_IOS13_SDK
@@ -117,14 +128,10 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
             self.searchController.automaticallyShowsScopeBar = NO;
         }
         #endif
-        #if !TARGET_OS_TV
         [self addSearchController:self.searchController];
-        #endif
     } else {
         // Search already shown and just set to NO, so remove it
-        #if !TARGET_OS_TV
         [self removeSearchController:self.searchController];
-        #endif
     }
 }
 
@@ -279,7 +286,11 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
             #endif
         }
     }
-
+    #if TARGET_OS_TV
+    UIEdgeInsets insets = self.tableView.contentInset;
+    insets.top+=20;
+    self.tableView.contentInset = insets;
+    #endif
     [self setupToolbarItems];
 }
 
@@ -465,18 +476,32 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
         self.tableView.tableHeaderView = nil;
     } else {
         if (self.showsSearchBar) {
-            #if !TARGET_OS_TV
             [self removeSearchController:self.searchController];
             [self addSearchController:self.searchController];
-            #endif
         } else {
             self.tableView.tableHeaderView = nil;
             _tableHeaderViewContainer = nil;
         }
     }
 }
-#if !TARGET_OS_TV
+
+- (void)addChildViewController:(UIViewController *)childController {
+    [super addChildViewController:childController];
+    childController.view.frame = self.view.frame;
+}
+
+
 - (void)addSearchController:(UISearchController *)controller {
+    #if TARGET_OS_TV
+    //self.tableView.tableHeaderView = controller.searchBar;
+    /*
+    self.searchContainer = [[UISearchContainerViewController alloc] initWithSearchController:controller];
+    [self addChildViewController:self.searchContainer];
+    [self.view addSubview:self.searchContainer.view];
+    [self.searchContainer didMoveToParentViewController:self];
+    [self.searchController.searchBar becomeFirstResponder];
+     */
+    #else
     if (@available(iOS 11.0, *)) {
         self.navigationItem.searchController = controller;
     } else {
@@ -498,9 +523,18 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
         self.tableHeaderViewContainer.frame = frame;
         [self layoutTableHeaderIfNeeded];
     }
+    #endif
 }
 
 - (void)removeSearchController:(UISearchController *)controller {
+    #if TARGET_OS_TV
+    /*
+    [self.searchContainer willMoveToParentViewController:nil];
+    [self.searchContainer.view removeFromSuperview];
+    [self.searchContainer removeFromParentViewController];
+     */
+    self.navigationItem.leftBarButtonItem = nil;
+    #else
     if (@available(iOS 11.0, *)) {
         self.navigationItem.searchController = nil;
     } else {
@@ -515,8 +549,9 @@ CGFloat const kFLEXDebounceForExpensiveIO = 0.5;
             _tableHeaderViewContainer = nil;
         }
     }
+    #endif
 }
-#endif
+
 - (UIView *)tableHeaderViewContainer {
     if (!_tableHeaderViewContainer) {
         _tableHeaderViewContainer = [UIView new];

+ 1 - 1
Classes/Editing/ArgumentInputViews/FLEXArgumentInputSwitchView.m

@@ -72,7 +72,7 @@
 - (void)layoutSubviews {
     [super layoutSubviews];
 #if TARGET_OS_TV
-    self.inputSwitch.frame = CGRectMake(50, 50, 200, 60);
+    self.inputSwitch.frame = CGRectMake(50, 50, 200, 70);
 #else
     self.inputSwitch.frame = CGRectMake(0, self.topInputFieldVerticalLayoutGuide, self.inputSwitch.frame.size.width, self.inputSwitch.frame.size.height);
 #endif

+ 1 - 1
Classes/Editing/FLEXFieldEditorViewController.m

@@ -74,7 +74,7 @@
     _getterButton = [UIButton buttonWithType:UIButtonTypeSystem];
     [_getterButton setTitle:@"Get" forState:UIControlStateNormal];
     [_getterButton addTarget:self action:@selector(getterButtonPressed:) forControlEvents:UIControlEventPrimaryActionTriggered];
-    _getterButton.frame = CGRectMake(100, 600, 200, 60);
+    _getterButton.frame = CGRectMake(100, 600, 200, 70);
     [self.view addSubview:_getterButton];
     UIFocusGuide *focusGuide = [[UIFocusGuide alloc] init];
     [self.view addLayoutGuide:focusGuide];

+ 1 - 1
Classes/Editing/FLEXVariableEditorViewController.m

@@ -113,7 +113,7 @@
     _actionButton = [UIButton buttonWithType:UIButtonTypeSystem];
     [_actionButton setTitle:@"Set" forState:UIControlStateNormal];
     [_actionButton addTarget:self action:@selector(actionButtonPressed:) forControlEvents:UIControlEventPrimaryActionTriggered];
-    _actionButton.frame = CGRectMake(500, 600, 200, 60);
+    _actionButton.frame = CGRectMake(500, 600, 200, 70);
     [self.view addSubview:_actionButton];
 #endif
 }

+ 5 - 0
Classes/tvOS/fakes.h

@@ -11,6 +11,11 @@
 #import "KBSlider.h"
 #import "KBDatePickerView.h"
 
+@interface KBSearchButton: UIButton <UITextFieldDelegate>
+- (void)triggerSearchField;
+@property UISearchBar *searchBar;
+@end
+
 @interface UIFakeSwitch : UIButton <NSCoding>
 @property(nullable, nonatomic, strong) UIColor *onTintColor;
 @property(nullable, nonatomic, strong) UIColor *thumbTintColor;

+ 48 - 0
Classes/tvOS/fakes.m

@@ -7,6 +7,54 @@
 //
 
 #import "fakes.h"
+#import "NSObject+FLEX_Reflection.h"
+@interface UIImage (private)
++(UIImage *)symbolImageNamed:(NSString *)symbolName;
+@end
+@interface KBSearchButton()
+@property UITextField *searchField;
+@end
+
+@implementation KBSearchButton
+
+
++ (instancetype)buttonWithType:(UIButtonType)buttonType {
+    KBSearchButton *button = [super buttonWithType:buttonType];
+    [button setImage:[UIImage symbolImageNamed:@"magnifyingglass"] forState:UIControlStateNormal];
+    button.frame = CGRectMake(0, 0, 150, 70);
+    UITextField *tf = [[UITextField alloc]init];
+    tf.clearButtonMode = UITextFieldViewModeAlways;
+    button.searchField = tf;
+    tf.delegate = button;
+    [button addSubview:tf];
+    [button addTarget:button action:@selector(triggerSearchField) forControlEvents:UIControlEventPrimaryActionTriggered];
+    [button addListeners];
+    return button;
+}
+
+- (void)textChanged:(NSNotification *)n {
+    self.searchBar.text = self.searchField.text;
+}
+
+- (void)addListeners {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextFieldTextDidChangeNotification object:nil];
+}
+
+- (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+- (void)triggerSearchField {
+    self.searchField.text = self.searchBar.text;
+    //[self.searchBar becomeFirstResponder];
+    [self.searchField becomeFirstResponder];
+    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+        UIViewController *vc = [self topViewController];
+        vc.view.alpha = 0.6;
+    });
+}
+
+@end
 
 @interface UIFakeSwitch() {
     BOOL _isOn;

+ 2 - 2
FLEX.xcodeproj/xcshareddata/xcschemes/FLEX-tvOS.xcscheme

@@ -15,7 +15,7 @@
             <BuildableReference
                BuildableIdentifier = "primary"
                BlueprintIdentifier = "323F6BC5259B9DB70055BDD6"
-               BuildableName = "FLEX-tvOS.framework"
+               BuildableName = "FLEX.framework"
                BlueprintName = "FLEX-tvOS"
                ReferencedContainer = "container:FLEX.xcodeproj">
             </BuildableReference>
@@ -51,7 +51,7 @@
          <BuildableReference
             BuildableIdentifier = "primary"
             BlueprintIdentifier = "323F6BC5259B9DB70055BDD6"
-            BuildableName = "FLEX-tvOS.framework"
+            BuildableName = "FLEX.framework"
             BlueprintName = "FLEX-tvOS"
             ReferencedContainer = "container:FLEX.xcodeproj">
          </BuildableReference>