Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix module provider not working and code refactor #3785

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ - (NSArray *)modulesConformingToProtocol:(Protocol *)protocol {
}

- (BOOL)moduleIsInitialized:(Class)moduleClass {
return [_moduleSetup moduleIsInitialized:moduleClass];
return [_moduleSetup isModuleInitialized:moduleClass];
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -942,7 +942,7 @@ - (BOOL)isLoading {
}

- (BOOL)moduleSetupComplete {
return _moduleSetup.moduleSetupComplete;
return _moduleSetup.isModuleSetupComplete;
}

- (void)invalidate {
Expand Down
34 changes: 23 additions & 11 deletions framework/ios/base/modules/HippyModulesSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
*/

#import <Foundation/Foundation.h>

#import "HippyBridgeModule.h"
#import "HippyDefines.h"
#import "HippyInvalidating.h"

NS_ASSUME_NONNULL_BEGIN
@class HippyBridge, HippyModuleData, HippyModuleData;

HIPPY_EXTERN NSArray<Class> *HippyGetModuleClasses(void);
NS_ASSUME_NONNULL_BEGIN

#if HIPPY_DEBUG
HIPPY_EXTERN void HippyVerifyAllModulesExported(NSArray *extraModules);
Expand All @@ -44,31 +43,44 @@ HIPPY_EXTERN void HippyVerifyAllModulesExported(NSArray *extraModules);
*/
typedef NSArray<id<HippyBridgeModule>> *_Nullable(^HippyBridgeModuleProviderBlock)(void);

@class HippyBridge, HippyModuleData, HippyModuleData;

/// Helper class responsible for managing Modules
@interface HippyModulesSetup : NSObject<HippyInvalidating>

@property(nonatomic, copy, readonly) HippyBridgeModuleProviderBlock moduleProvider;

@property(nonatomic, copy, readonly) NSArray<Class> *moduleClasses;
/// All Module Classes
@property (nonatomic, copy, readonly) NSArray<Class> *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<NSString *, HippyModuleData *> *)moduleDataByName;

/// Get module data array
- (NSArray<HippyModuleData *> *)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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#import "HippyTurboModule.h"
#import "HippyLog.h"
#import "HippyUtils.h"
#import <objc/runtime.h>

#include "objc/runtime.h"

static NSMutableArray<Class> *HippyModuleClasses;
NSArray<Class> *HippyGetModuleClasses(void) {
Expand Down Expand Up @@ -129,13 +129,14 @@ @interface HippyModulesSetup () {
NSArray<Class> *_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;
Expand All @@ -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<id<HippyBridgeModule>> *extraModules = _providerBlock ? _providerBlock() : @[];
#if HIPPY_DEBUG
Expand All @@ -165,8 +162,8 @@ - (void)setupModulesCompletion:(dispatch_block_t)completion {
HippyVerifyAllModulesExported(extraModules);
});
#endif //HIPPY_DEBUG
NSMutableArray<Class> *moduleClassesByID = [NSMutableArray new];
NSMutableArray<HippyModuleData *> *moduleDataByID = [NSMutableArray new];
NSMutableArray<Class> *moduleClasses = [NSMutableArray new];
NSMutableArray<HippyModuleData *> *moduleDataArr = [NSMutableArray new];
NSMutableDictionary<NSString *, HippyModuleData *> *moduleDataByName = [NSMutableDictionary new];

for (id<HippyBridgeModule> extraModule in extraModules) {
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 0 additions & 3 deletions renderer/native/ios/renderer/HippyUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
using RootNode = hippy::RootNode;

static NSMutableArray<Class> *HippyViewManagerClasses = nil;
NSArray<Class> *HippyGetViewManagerClasses(void) {
NSArray<Class> *HippyGetViewManagerClasses(HippyBridge *bridge) {
if (!HippyViewManagerClasses) {
NSArray<Class> *classes = HippyGetModuleClasses();
NSArray<Class> *classes = bridge.moduleClasses;
NSMutableArray<Class> *viewManagerClasses = [NSMutableArray array];
for (id aClass in classes) {
if ([aClass isSubclassOfClass:HippyViewManager.class]) {
Expand All @@ -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)];
Expand Down Expand Up @@ -221,7 +221,7 @@ - (void)initContext {
_viewRegistry.requireInMainThread = YES;
_pendingUIBlocks = [NSMutableArray new];
_componentTransactionListeners = [NSHashTable weakObjectsHashTable];
_componentDataByName = [NSMutableDictionary dictionaryWithCapacity:64];
_componentDataByName = [NSMutableDictionary dictionary];
HippyScreenScale();
HippyScreenSize();
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -650,37 +650,38 @@ - (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),
NSStringFromClass([_viewManagers objectForKey:viewName]));
[_viewManagers setObject:cls forKey:viewName];
}
}
NSArray<Class> *classes = HippyGetViewManagerClasses();
NSArray<Class> *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;
}
[defaultViewManagerClasses setObject:cls forKey: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;
}
Expand Down Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading