From af5dcb1a957629b9b889b3a06ca546635adc870d Mon Sep 17 00:00:00 2001 From: wwwcg Date: Wed, 13 Mar 2024 21:00:11 +0800 Subject: [PATCH] fix(ios): fix module provider not working and code refactor --- framework/ios/base/bridge/HippyBridge.mm | 6 ++-- .../ios/base/modules/HippyModulesSetup.h | 34 ++++++++++++------ ...ppyModulesSetup.mm => HippyModulesSetup.m} | 35 +++++++++---------- renderer/native/ios/renderer/HippyUIManager.h | 3 -- .../native/ios/renderer/HippyUIManager.mm | 27 +++++++------- .../component/scrollview/HippyScrollView.mm | 4 +-- 6 files changed, 58 insertions(+), 51 deletions(-) rename framework/ios/base/modules/{HippyModulesSetup.mm => HippyModulesSetup.m} (91%) diff --git a/framework/ios/base/bridge/HippyBridge.mm b/framework/ios/base/bridge/HippyBridge.mm index 9c611dbe6cb..c4f7bba4321 100644 --- a/framework/ios/base/bridge/HippyBridge.mm +++ b/framework/ios/base/bridge/HippyBridge.mm @@ -360,7 +360,7 @@ - (NSArray *)modulesConformingToProtocol:(Protocol *)protocol { } - (BOOL)moduleIsInitialized:(Class)moduleClass { - return [_moduleSetup moduleIsInitialized:moduleClass]; + return [_moduleSetup isModuleInitialized:moduleClass]; } @@ -412,7 +412,7 @@ - (void)setUp { _displayLink = [[HippyDisplayLink alloc] init]; // Setup all extra and internal modules - [_moduleSetup setupModulesCompletion:^{ + [_moduleSetup setupModulesWithCompletionBlock:^{ HippyBridge *strongSelf = weakSelf; if (strongSelf) { dispatch_semaphore_signal(strongSelf.moduleSemaphore); @@ -942,7 +942,7 @@ - (BOOL)isLoading { } - (BOOL)moduleSetupComplete { - return _moduleSetup.moduleSetupComplete; + return _moduleSetup.isModuleSetupComplete; } - (void)invalidate { diff --git a/framework/ios/base/modules/HippyModulesSetup.h b/framework/ios/base/modules/HippyModulesSetup.h index 27effbd7181..c71dea2d72c 100644 --- a/framework/ios/base/modules/HippyModulesSetup.h +++ b/framework/ios/base/modules/HippyModulesSetup.h @@ -21,14 +21,13 @@ */ #import - #import "HippyBridgeModule.h" #import "HippyDefines.h" #import "HippyInvalidating.h" -NS_ASSUME_NONNULL_BEGIN +@class HippyBridge, HippyModuleData, HippyModuleData; -HIPPY_EXTERN NSArray *HippyGetModuleClasses(void); +NS_ASSUME_NONNULL_BEGIN #if HIPPY_DEBUG HIPPY_EXTERN void HippyVerifyAllModulesExported(NSArray *extraModules); @@ -44,31 +43,44 @@ HIPPY_EXTERN void HippyVerifyAllModulesExported(NSArray *extraModules); */ typedef NSArray> *_Nullable(^HippyBridgeModuleProviderBlock)(void); -@class HippyBridge, HippyModuleData, HippyModuleData; +/// Helper class responsible for managing Modules @interface HippyModulesSetup : NSObject -@property(nonatomic, copy, readonly) HippyBridgeModuleProviderBlock moduleProvider; - -@property(nonatomic, copy, readonly) NSArray *moduleClasses; +/// All Module Classes +@property (nonatomic, copy, readonly) NSArray *moduleClasses; -@property(readonly, assign) BOOL moduleSetupComplete; +/// Is Module setup somplete +@property (nonatomic, readonly) BOOL isModuleSetupComplete; +/// Init Method +/// - Parameters: +/// - bridge: HippyBridge +/// - moduleProvider: provider block - (instancetype)initWithBridge:(HippyBridge *)bridge extraProviderModulesBlock:(HippyBridgeModuleProviderBlock)moduleProvider; -- (void)setupModulesCompletion:(dispatch_block_t)completion; +/// Setup Modules +/// - Parameter completion: block +- (void)setupModulesWithCompletionBlock:(dispatch_block_t)completion; +/// Get module data dictionary - (NSDictionary *)moduleDataByName; +/// Get module data array - (NSArray *)moduleDataByID; +/// Get module object with given name +/// - Parameter moduleName: string - (id)moduleForName:(NSString *)moduleName; +/// Get module with given class +/// - Parameter cls: Class - (id)moduleForClass:(Class)cls; -- (BOOL)moduleIsInitialized:(Class)moduleClass; +/// Whether module is Initialized +/// - Parameter moduleClass: Class +- (BOOL)isModuleInitialized:(Class)moduleClass; -- (void)invalidate; @end diff --git a/framework/ios/base/modules/HippyModulesSetup.mm b/framework/ios/base/modules/HippyModulesSetup.m similarity index 91% rename from framework/ios/base/modules/HippyModulesSetup.mm rename to framework/ios/base/modules/HippyModulesSetup.m index 5740ab567dd..234b7362d17 100644 --- a/framework/ios/base/modules/HippyModulesSetup.mm +++ b/framework/ios/base/modules/HippyModulesSetup.m @@ -27,8 +27,8 @@ #import "HippyTurboModule.h" #import "HippyLog.h" #import "HippyUtils.h" +#import -#include "objc/runtime.h" static NSMutableArray *HippyModuleClasses; NSArray *HippyGetModuleClasses(void) { @@ -129,13 +129,14 @@ @interface HippyModulesSetup () { NSArray *_moduleClassesByID; } -@property(readwrite, assign) BOOL moduleSetupComplete; +@property(readwrite, assign) BOOL isModuleSetupComplete; @end @implementation HippyModulesSetup -- (instancetype)initWithBridge:(HippyBridge *)bridge extraProviderModulesBlock:(HippyBridgeModuleProviderBlock)moduleProvider { +- (instancetype)initWithBridge:(HippyBridge *)bridge + extraProviderModulesBlock:(HippyBridgeModuleProviderBlock)moduleProvider { self = [super init]; if (self) { _bridge = bridge; @@ -145,18 +146,14 @@ - (instancetype)initWithBridge:(HippyBridge *)bridge extraProviderModulesBlock:( } - (void)invalidate { - -} - -- (void)dealloc { - + // do nothing } - (HippyBridgeModuleProviderBlock)moduleProvider { return [_providerBlock copy]; } -- (void)setupModulesCompletion:(dispatch_block_t)completion { +- (void)setupModulesWithCompletionBlock:(dispatch_block_t)completion { HippyLogInfo(@"Begin Modules Setup"); NSArray> *extraModules = _providerBlock ? _providerBlock() : @[]; #if HIPPY_DEBUG @@ -165,8 +162,8 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion { HippyVerifyAllModulesExported(extraModules); }); #endif //HIPPY_DEBUG - NSMutableArray *moduleClassesByID = [NSMutableArray new]; - NSMutableArray *moduleDataByID = [NSMutableArray new]; + NSMutableArray *moduleClasses = [NSMutableArray new]; + NSMutableArray *moduleDataArr = [NSMutableArray new]; NSMutableDictionary *moduleDataByName = [NSMutableDictionary new]; for (id extraModule in extraModules) { @@ -185,8 +182,8 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion { // Instantiate moduleData container HippyModuleData *moduleData = [[HippyModuleData alloc] initWithModuleInstance:extraModule bridge:_bridge]; moduleDataByName[moduleName] = moduleData; - [moduleClassesByID addObject:moduleClass]; - [moduleDataByID addObject:moduleData]; + [moduleClasses addObject:moduleClass]; + [moduleDataArr addObject:moduleData]; } for (Class moduleClass in HippyGetModuleClasses()) { NSString *moduleName = HippyBridgeModuleNameForClass(moduleClass); @@ -211,15 +208,15 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion { // Instantiate moduleData (TODO: can we defer this until config generation?) moduleData = [[HippyModuleData alloc] initWithModuleClass:moduleClass bridge:_bridge]; moduleDataByName[moduleName] = moduleData; - [moduleClassesByID addObject:moduleClass]; - [moduleDataByID addObject:moduleData]; + [moduleClasses addObject:moduleClass]; + [moduleDataArr addObject:moduleData]; } // Store modules - _moduleDataByID = [moduleDataByID copy]; + _moduleDataByID = [moduleDataArr copy]; _moduleDataByName = [moduleDataByName copy]; - _moduleClassesByID = [moduleClassesByID copy]; + _moduleClassesByID = [moduleClasses copy]; [self prepareModules]; - self.moduleSetupComplete = YES; + self.isModuleSetupComplete = YES; HippyLogInfo(@"End Modules Setup"); if (completion) { completion(); @@ -277,7 +274,7 @@ - (id)moduleForClass:(Class)cls { return [self moduleForName:HippyBridgeModuleNameForClass(cls)]; } -- (BOOL)moduleIsInitialized:(Class)moduleClass { +- (BOOL)isModuleInitialized:(Class)moduleClass { HippyModuleData *module = _moduleDataByName[HippyBridgeModuleNameForClass(moduleClass)]; return module.hasInstance; } diff --git a/renderer/native/ios/renderer/HippyUIManager.h b/renderer/native/ios/renderer/HippyUIManager.h index 6b43f28c7c1..64cf902f2b4 100644 --- a/renderer/native/ios/renderer/HippyUIManager.h +++ b/renderer/native/ios/renderer/HippyUIManager.h @@ -126,9 +126,6 @@ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification; /// Update view with props - (void)updateView:(NSNumber *)componentTag onRootTag:(NSNumber *)rootTag props:(NSDictionary *)pros; -/// Get viewManager Name -/// @param viewName NSString -- (__kindof HippyViewManager *)renderViewManagerForViewName:(NSString *)viewName; /** * Manully create views recursively from renderObject diff --git a/renderer/native/ios/renderer/HippyUIManager.mm b/renderer/native/ios/renderer/HippyUIManager.mm index 2e3a72ff56a..ceff718e424 100644 --- a/renderer/native/ios/renderer/HippyUIManager.mm +++ b/renderer/native/ios/renderer/HippyUIManager.mm @@ -65,9 +65,9 @@ using RootNode = hippy::RootNode; static NSMutableArray *HippyViewManagerClasses = nil; -NSArray *HippyGetViewManagerClasses(void) { +NSArray *HippyGetViewManagerClasses(HippyBridge *bridge) { if (!HippyViewManagerClasses) { - NSArray *classes = HippyGetModuleClasses(); + NSArray *classes = bridge.moduleClasses; NSMutableArray *viewManagerClasses = [NSMutableArray array]; for (id aClass in classes) { if ([aClass isSubclassOfClass:HippyViewManager.class]) { @@ -79,7 +79,7 @@ return HippyViewManagerClasses; } -static NSString *GetViewNameFromViewManagerClass(Class cls) { +static NSString *viewNameFromViewManagerClass(Class cls) { HippyAssert([cls respondsToSelector:@selector(moduleName)], @"%@ must respond to selector moduleName", NSStringFromClass(cls)); NSString *viewName = [cls performSelector:@selector(moduleName)]; @@ -221,7 +221,7 @@ - (void)initContext { _viewRegistry.requireInMainThread = YES; _pendingUIBlocks = [NSMutableArray new]; _componentTransactionListeners = [NSHashTable weakObjectsHashTable]; - _componentDataByName = [NSMutableDictionary dictionaryWithCapacity:64]; + _componentDataByName = [NSMutableDictionary dictionary]; HippyScreenScale(); HippyScreenSize(); } @@ -289,7 +289,7 @@ - (HippyComponentData *)componentDataForViewName:(NSString *)viewName { if (viewName) { HippyComponentData *componentData = _componentDataByName[viewName]; if (!componentData) { - HippyViewManager *viewManager = [self renderViewManagerForViewName:viewName]; + HippyViewManager *viewManager = [self viewManagerForViewName:viewName]; NSAssert(viewManager, @"No view manager found for %@", viewName); if (viewManager) { componentData = [[HippyComponentData alloc] initWithViewManager:viewManager viewName:viewName]; @@ -650,14 +650,14 @@ - (void)updateView:(nonnull NSNumber *)componentTag }]; } -#pragma mark Render Context Implementation +#pragma mark - Render Context Implementation -- (__kindof HippyViewManager *)renderViewManagerForViewName:(NSString *)viewName { +- (__kindof HippyViewManager *)viewManagerForViewName:(NSString *)viewName { if (!_viewManagers) { - _viewManagers = [NSMutableDictionary dictionaryWithCapacity:64]; + _viewManagers = [NSMutableDictionary dictionary]; if (_extraComponents) { for (Class cls in _extraComponents) { - NSString *viewName = GetViewNameFromViewManagerClass(cls); + NSString *viewName = viewNameFromViewManagerClass(cls); HippyAssert(![_viewManagers objectForKey:viewName], @"duplicated component %@ for class %@ and %@", viewName, NSStringFromClass(cls), @@ -665,10 +665,10 @@ - (__kindof HippyViewManager *)renderViewManagerForViewName:(NSString *)viewName [_viewManagers setObject:cls forKey:viewName]; } } - NSArray *classes = HippyGetViewManagerClasses(); + NSArray *classes = HippyGetViewManagerClasses(self.bridge); NSMutableDictionary *defaultViewManagerClasses = [NSMutableDictionary dictionaryWithCapacity:[classes count]]; for (Class cls in classes) { - NSString *viewName = GetViewNameFromViewManagerClass(cls); + NSString *viewName = viewNameFromViewManagerClass(cls); if ([_viewManagers objectForKey:viewName]) { continue; } @@ -676,11 +676,12 @@ - (__kindof HippyViewManager *)renderViewManagerForViewName:(NSString *)viewName } [_viewManagers addEntriesFromDictionary:defaultViewManagerClasses]; } + // Get and instantiate the class id object = [_viewManagers objectForKey:viewName]; if (object_isClass(object)) { HippyViewManager *viewManager = [object new]; viewManager.bridge = self.bridge; - NSAssert([viewManager isKindOfClass:[HippyViewManager class]], @"It must be a HippyViewManager instance"); + NSAssert([viewManager isKindOfClass:[HippyViewManager class]], @"Must be a HippyViewManager instance"); [_viewManagers setObject:viewManager forKey:viewName]; object = viewManager; } @@ -1105,7 +1106,7 @@ - (id)dispatchFunction:(const std::string &)functionName } NSString *nativeModuleName = [NSString stringWithUTF8String:viewName.c_str()]; - HippyViewManager *viewManager = [self renderViewManagerForViewName:nativeModuleName]; + HippyViewManager *viewManager = [self viewManagerForViewName:nativeModuleName]; HippyComponentData *componentData = [self componentDataForViewName:nativeModuleName]; NSValue *selectorPointer = [componentData.methodsByName objectForKey:name]; SEL selector = (SEL)[selectorPointer pointerValue]; diff --git a/renderer/native/ios/renderer/component/scrollview/HippyScrollView.mm b/renderer/native/ios/renderer/component/scrollview/HippyScrollView.mm index 157590b7998..968b9fa0dad 100644 --- a/renderer/native/ios/renderer/component/scrollview/HippyScrollView.mm +++ b/renderer/native/ios/renderer/component/scrollview/HippyScrollView.mm @@ -233,7 +233,7 @@ - (void)insertHippySubview:(UIView *)view atIndex:(NSInteger)atIndex { if (view == _contentView && 0 == atIndex) { return; } - NSAssert(0 == atIndex, @"NativeRenderScrollView only contain one subview at index 0"); + NSAssert(0 == atIndex, @"HippyScrollView only contain one subview at index 0"); if (_contentView) { [self removeHippySubview:_contentView]; } @@ -327,7 +327,7 @@ - (void)layoutSubviews { _scrollView.frame = self.bounds; if (CGRectGetWidth(originFrame) > 0) { NSInteger currIndex = _scrollView.contentOffset.x / CGRectGetWidth(originFrame); - //解决NativeRenderScrollView横竖屏切换时 didScrollView没有回调onScroll的问题 + // 解决HippyScrollView横竖屏切换时 didScrollView没有回调onScroll的问题 _allowNextScrollNoMatterWhat = YES; _scrollView.contentOffset = CGPointMake(currIndex * CGRectGetWidth(_scrollView.frame), 0); }