|
| 1 | +#ifdef RCT_NEW_ARCH_ENABLED |
| 2 | +#import "RCTTabViewComponentView.h" |
| 3 | + |
| 4 | +#import <react/renderer/components/RNCTabViewSpec/ComponentDescriptors.h> |
| 5 | +#import <react/renderer/components/RNCTabViewSpec/EventEmitters.h> |
| 6 | +#import <react/renderer/components/RNCTabViewSpec/Props.h> |
| 7 | +#import <react/renderer/components/RNCTabViewSpec/RCTComponentViewHelpers.h> |
| 8 | + |
| 9 | +#import <React/RCTFabricComponentsPlugins.h> |
| 10 | + |
| 11 | +#if __has_include("react_native_bottom_tabs/react_native_bottom_tabs-Swift.h") |
| 12 | +#import "react_native_bottom_tabs/react_native_bottom_tabs-Swift.h" |
| 13 | +#else |
| 14 | +#import "react_native_bottom_tabs-Swift.h" |
| 15 | +#endif |
| 16 | + |
| 17 | +#import <React/RCTImageLoader.h> |
| 18 | +#import <React/RCTImageSource.h> |
| 19 | +#import <React/RCTBridge+Private.h> |
| 20 | +#import "RCTImagePrimitivesConversions.h" |
| 21 | +#import "RCTConversions.h" |
| 22 | + |
| 23 | + |
| 24 | +using namespace facebook::react; |
| 25 | + |
| 26 | +@interface RCTTabViewComponentView () <RCTRNCTabViewViewProtocol, TabViewProviderDelegate> { |
| 27 | +} |
| 28 | + |
| 29 | +@end |
| 30 | + |
| 31 | +@implementation RCTTabViewComponentView { |
| 32 | + TabViewProvider *_tabViewProvider; |
| 33 | + NSMutableArray<UIView *> *_reactSubviews; |
| 34 | +} |
| 35 | + |
| 36 | ++ (ComponentDescriptorProvider)componentDescriptorProvider |
| 37 | +{ |
| 38 | + return concreteComponentDescriptorProvider<RNCTabViewComponentDescriptor>(); |
| 39 | +} |
| 40 | + |
| 41 | +- (instancetype)initWithFrame:(CGRect)frame |
| 42 | +{ |
| 43 | + if (self = [super initWithFrame:frame]) { |
| 44 | + static const auto defaultProps = std::make_shared<const RNCTabViewProps>(); |
| 45 | + _reactSubviews = [NSMutableArray new]; |
| 46 | + RCTImageLoader *imageLoader = [[RCTBridge currentBridge] moduleForName:@"ImageLoader"]; |
| 47 | + _tabViewProvider = [[TabViewProvider alloc] initWithDelegate:self imageLoader:imageLoader]; |
| 48 | + self.contentView = _tabViewProvider; |
| 49 | + _props = defaultProps; |
| 50 | + } |
| 51 | + |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +// Opt out of recycling for now, it's not working properly. |
| 56 | ++ (BOOL)shouldBeRecycled |
| 57 | +{ |
| 58 | + return NO; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)layoutSubviews { |
| 62 | + [super layoutSubviews]; |
| 63 | + _tabViewProvider.children = [self reactSubviews]; |
| 64 | +} |
| 65 | + |
| 66 | +- (NSArray<UIView *> *)reactSubviews |
| 67 | +{ |
| 68 | + return _reactSubviews; |
| 69 | +} |
| 70 | + |
| 71 | +- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index { |
| 72 | + [_reactSubviews insertObject:childComponentView atIndex:index]; |
| 73 | + _tabViewProvider.children = [self reactSubviews]; |
| 74 | +} |
| 75 | + |
| 76 | +- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index { |
| 77 | + [_reactSubviews removeObjectAtIndex:index]; |
| 78 | + |
| 79 | + [childComponentView removeFromSuperview]; |
| 80 | +} |
| 81 | + |
| 82 | +- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps |
| 83 | +{ |
| 84 | + const auto &oldViewProps = *std::static_pointer_cast<RNCTabViewProps const>(_props); |
| 85 | + const auto &newViewProps = *std::static_pointer_cast<RNCTabViewProps const>(props); |
| 86 | + |
| 87 | + if (haveTabItemsChanged(oldViewProps.items, newViewProps.items)) { |
| 88 | + _tabViewProvider.itemsData = convertItemsToArray(newViewProps.items); |
| 89 | + } |
| 90 | + |
| 91 | + if (oldViewProps.translucent != newViewProps.translucent) { |
| 92 | + _tabViewProvider.translucent = newViewProps.translucent; |
| 93 | + } |
| 94 | + |
| 95 | + if (oldViewProps.icons != newViewProps.icons) { |
| 96 | + auto iconsArray = [[NSMutableArray alloc] init]; |
| 97 | + for (auto &source: newViewProps.icons) { |
| 98 | + auto imageSource = [[RCTImageSource alloc] initWithURLRequest:NSURLRequestFromImageSource(source) size:CGSizeMake(source.size.width, source.size.height) scale:source.scale]; |
| 99 | + [iconsArray addObject:imageSource]; |
| 100 | + } |
| 101 | + |
| 102 | + _tabViewProvider.icons = iconsArray; |
| 103 | + } |
| 104 | + |
| 105 | + if (oldViewProps.sidebarAdaptable != newViewProps.sidebarAdaptable) { |
| 106 | + _tabViewProvider.sidebarAdaptable = newViewProps.sidebarAdaptable; |
| 107 | + } |
| 108 | + |
| 109 | + if (oldViewProps.disablePageAnimations != newViewProps.disablePageAnimations) { |
| 110 | + _tabViewProvider.disablePageAnimations = newViewProps.disablePageAnimations; |
| 111 | + } |
| 112 | + |
| 113 | + if (oldViewProps.labeled != newViewProps.labeled) { |
| 114 | + _tabViewProvider.labeled = newViewProps.labeled; |
| 115 | + } |
| 116 | + |
| 117 | + if (oldViewProps.ignoresTopSafeArea != newViewProps.ignoresTopSafeArea) { |
| 118 | + _tabViewProvider.ignoresTopSafeArea = newViewProps.ignoresTopSafeArea; |
| 119 | + } |
| 120 | + |
| 121 | + if (oldViewProps.selectedPage != newViewProps.selectedPage) { |
| 122 | + _tabViewProvider.selectedPage = RCTNSStringFromString(newViewProps.selectedPage); |
| 123 | + } |
| 124 | + |
| 125 | + if (oldViewProps.scrollEdgeAppearance != newViewProps.scrollEdgeAppearance) { |
| 126 | + _tabViewProvider.scrollEdgeAppearance = RCTNSStringFromString(newViewProps.scrollEdgeAppearance); |
| 127 | + } |
| 128 | + |
| 129 | + if (oldViewProps.labeled != newViewProps.labeled) { |
| 130 | + _tabViewProvider.labeled = newViewProps.labeled; |
| 131 | + } |
| 132 | + |
| 133 | + if (oldViewProps.barTintColor != newViewProps.barTintColor) { |
| 134 | + _tabViewProvider.barTintUIColor = RCTUIColorFromSharedColor(newViewProps.barTintColor); |
| 135 | + } |
| 136 | + |
| 137 | + if (oldViewProps.activeTintColor != newViewProps.activeTintColor) { |
| 138 | + _tabViewProvider.activeTintUIColor = RCTUIColorFromSharedColor(newViewProps.activeTintColor); |
| 139 | + } |
| 140 | + |
| 141 | + if (oldViewProps.inactiveTintColor != newViewProps.inactiveTintColor) { |
| 142 | + _tabViewProvider.inactiveTintUIColor = RCTUIColorFromSharedColor(newViewProps.inactiveTintColor); |
| 143 | + } |
| 144 | + |
| 145 | + [super updateProps:props oldProps:oldProps]; |
| 146 | +} |
| 147 | + |
| 148 | +bool areTabItemsEqual(const RNCTabViewItemsStruct& lhs, const RNCTabViewItemsStruct& rhs) { |
| 149 | + return lhs.key == rhs.key && |
| 150 | + lhs.title == rhs.title && |
| 151 | + lhs.sfSymbol == rhs.sfSymbol && |
| 152 | + lhs.badge == rhs.badge && |
| 153 | + lhs.activeTintColor == rhs.activeTintColor; |
| 154 | +} |
| 155 | + |
| 156 | +bool haveTabItemsChanged(const std::vector<RNCTabViewItemsStruct>& oldItems, |
| 157 | + const std::vector<RNCTabViewItemsStruct>& newItems) { |
| 158 | + |
| 159 | + if (oldItems.size() != newItems.size()) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + for (size_t i = 0; i < oldItems.size(); ++i) { |
| 164 | + if (!areTabItemsEqual(oldItems[i], newItems[i])) { |
| 165 | + return true; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return false; |
| 170 | +} |
| 171 | + |
| 172 | +NSArray* convertItemsToArray(const std::vector<RNCTabViewItemsStruct>& items) { |
| 173 | + NSMutableArray<TabInfo *> *result = [NSMutableArray array]; |
| 174 | + |
| 175 | + for (const auto& item : items) { |
| 176 | + auto tabInfo = [[TabInfo alloc] initWithKey:RCTNSStringFromString(item.key) title:RCTNSStringFromString(item.title) badge:RCTNSStringFromString(item.badge) sfSymbol:RCTNSStringFromString(item.sfSymbol) activeTintColor:RCTUIColorFromSharedColor(item.activeTintColor)]; |
| 177 | + |
| 178 | + [result addObject:tabInfo]; |
| 179 | + } |
| 180 | + |
| 181 | + return result; |
| 182 | +} |
| 183 | + |
| 184 | +// MARK: TabViewProviderDelegate |
| 185 | + |
| 186 | +- (void)onPageSelectedWithKey:(NSString *)key reactTag:(NSNumber *)reactTag { |
| 187 | + auto eventEmitter = std::static_pointer_cast<const RNCTabViewEventEmitter>(_eventEmitter); |
| 188 | + if (eventEmitter) { |
| 189 | + eventEmitter->onPageSelected(RNCTabViewEventEmitter::OnPageSelected{ |
| 190 | + .key = [key cStringUsingEncoding:kCFStringEncodingUTF8] |
| 191 | + }); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +- (void)onLongPressWithKey:(NSString *)key reactTag:(NSNumber *)reactTag { |
| 196 | + auto eventEmitter = std::static_pointer_cast<const RNCTabViewEventEmitter>(_eventEmitter); |
| 197 | + if (eventEmitter) { |
| 198 | + eventEmitter->onTabLongPress(RNCTabViewEventEmitter::OnTabLongPress { |
| 199 | + .key = [key cStringUsingEncoding:kCFStringEncodingUTF8] |
| 200 | + }); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +@end |
| 205 | + |
| 206 | +Class<RCTComponentViewProtocol> RNCTabViewCls(void) |
| 207 | +{ |
| 208 | + return RCTTabViewComponentView.class; |
| 209 | +} |
| 210 | + |
| 211 | +#endif // RCT_NEW_ARCH_ENABLED |
| 212 | + |
| 213 | + |
0 commit comments