2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
#import "RCTTabBar.h"
|
|
|
|
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTTabBarItem.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "RCTView.h"
|
|
|
|
#import "RCTViewControllerProtocol.h"
|
|
|
|
#import "RCTWrapperViewController.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
@interface RCTTabBar() <UITabBarControllerDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTabBar
|
|
|
|
{
|
|
|
|
BOOL _tabsChanged;
|
|
|
|
UITabBarController *_tabController;
|
|
|
|
NSMutableArray *_tabViews;
|
|
|
|
}
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_tabViews = [NSMutableArray new];
|
|
|
|
_tabController = [UITabBarController new];
|
2015-03-06 00:36:41 +00:00
|
|
|
_tabController.delegate = self;
|
|
|
|
[self addSubview:_tabController.view];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-07-31 18:23:29 +00:00
|
|
|
- (UIViewController *)reactViewController
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
|
|
|
return _tabController;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
_tabController.delegate = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)reactSubviews
|
|
|
|
{
|
|
|
|
return _tabViews;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
|
|
|
|
{
|
|
|
|
if (![view isKindOfClass:[RCTTabBarItem class]]) {
|
|
|
|
RCTLogError(@"subview should be of type RCTTabBarItem");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_tabViews insertObject:view atIndex:atIndex];
|
|
|
|
_tabsChanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
|
|
|
{
|
|
|
|
if (_tabViews.count == 0) {
|
|
|
|
RCTLogError(@"should have at least one view to remove a subview");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_tabViews removeObject:subview];
|
|
|
|
_tabsChanged = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
_tabController.view.frame = self.bounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reactBridgeDidFinishTransaction
|
|
|
|
{
|
|
|
|
// we can't hook up the VC hierarchy in 'init' because the subviews aren't
|
|
|
|
// hooked up yet, so we do it on demand here whenever a transaction has finished
|
2015-07-31 18:23:29 +00:00
|
|
|
[self reactAddControllerToClosestParent:_tabController];
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
if (_tabsChanged) {
|
|
|
|
|
|
|
|
NSMutableArray *viewControllers = [NSMutableArray array];
|
|
|
|
for (RCTTabBarItem *tab in [self reactSubviews]) {
|
2015-07-31 18:23:29 +00:00
|
|
|
UIViewController *controller = tab.reactViewController;
|
2015-03-06 00:36:41 +00:00
|
|
|
if (!controller) {
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
controller = [[RCTWrapperViewController alloc] initWithContentView:tab];
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
[viewControllers addObject:controller];
|
|
|
|
}
|
|
|
|
|
|
|
|
_tabController.viewControllers = viewControllers;
|
|
|
|
_tabsChanged = NO;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
[[self reactSubviews] enumerateObjectsUsingBlock:
|
|
|
|
^(RCTTabBarItem *tab, NSUInteger index, __unused BOOL *stop) {
|
2015-03-06 00:36:41 +00:00
|
|
|
UIViewController *controller = _tabController.viewControllers[index];
|
|
|
|
controller.tabBarItem = tab.barItem;
|
|
|
|
if (tab.selected) {
|
|
|
|
_tabController.selectedViewController = controller;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (UIColor *)barTintColor
|
|
|
|
{
|
|
|
|
return _tabController.tabBar.barTintColor;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:40:56 +00:00
|
|
|
- (void)setBarTintColor:(UIColor *)barTintColor
|
|
|
|
{
|
|
|
|
_tabController.tabBar.barTintColor = barTintColor;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (UIColor *)tintColor
|
|
|
|
{
|
|
|
|
return _tabController.tabBar.tintColor;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:40:56 +00:00
|
|
|
- (void)setTintColor:(UIColor *)tintColor
|
|
|
|
{
|
|
|
|
_tabController.tabBar.tintColor = tintColor;
|
|
|
|
}
|
|
|
|
|
2015-07-14 15:05:08 +00:00
|
|
|
- (BOOL)translucent {
|
|
|
|
return _tabController.tabBar.isTranslucent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTranslucent:(BOOL)translucent {
|
|
|
|
_tabController.tabBar.translucent = translucent;
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
#pragma mark - UITabBarControllerDelegate
|
|
|
|
|
|
|
|
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
|
|
|
|
{
|
|
|
|
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
|
|
|
|
RCTTabBarItem *tab = [self reactSubviews][index];
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if (tab.onPress) tab.onPress(nil);
|
2015-03-06 00:36:41 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|