Skip to content

Commit

Permalink
refactor(ios): add a uiManager getter for UIView
Browse files Browse the repository at this point in the history
For compatibility,
some third-party components want to get UIManager
or HippyBridge directly through UIView,
although there is a slight performance penalty doing this.
  • Loading branch information
wwwcg committed Sep 10, 2024
1 parent c9ddbc1 commit c22b60a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
3 changes: 3 additions & 0 deletions renderer/native/ios/renderer/HippyUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
@class HippyComponentMap;
@protocol HippyImageProviderProtocol;

NS_ASSUME_NONNULL_BEGIN

/**
* Posted whenever a new root view is registered with HippyUIManager. The userInfo property
Expand Down Expand Up @@ -156,3 +157,5 @@ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification;
@property (nonatomic, strong, readonly) id<HippyCustomTouchHandlerProtocol> customTouchHandler;

@end

NS_ASSUME_NONNULL_END
19 changes: 15 additions & 4 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ static void NativeRenderTraverseViewNodes(id<HippyComponent> view, void (^block)
}
}


@interface UIView (HippyUIManagerPrivate)

/// Bind UIView with HippyUIManager
/// This is a convenient method for UIView to get HippyUIManager instance.
/// - Parameter uiManager: HippyUIManager instance
- (void)setUiManager:(HippyUIManager *)uiManager;

@end

#pragma mark -

#define AssertMainQueue() NSAssert(HippyIsMainQueue(), @"This function must be called on the main thread")

NSString *const HippyUIManagerDidRegisterRootViewNotification = @"HippyUIManagerDidRegisterRootViewNotification";
Expand Down Expand Up @@ -318,8 +330,8 @@ - (void)registerRootView:(UIView *)rootView asRootNode:(std::weak_ptr<RootNode>)
// Register view
[_viewRegistry addRootComponent:rootView rootNode:rootNode forTag:hippyTag];
[rootView addObserver:self forKeyPath:@"frame"
rootView.uiManager = self;
[rootView addObserver:self forKeyPath:@"frame"
options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew)
context:NULL];
CGRect frame = rootView.frame;
Expand Down Expand Up @@ -516,6 +528,7 @@ - (UIView *)createViewFromShadowView:(HippyShadowView *)shadowView {
view.viewName = viewName;
view.rootTag = rootTag;
view.hippyShadowView = shadowView;
view.uiManager = self;
[componentData setProps:props forView:view]; // Must be done before bgColor to prevent wrong default
}
}
Expand Down Expand Up @@ -1523,7 +1536,5 @@ - (void)setCustomTouchHandler:(id<HippyCustomTouchHandlerProtocol>)customTouchHa
objc_setAssociatedObject(self, @selector(customTouchHandler), customTouchHandler, OBJC_ASSOCIATION_RETAIN);
}


@end


37 changes: 37 additions & 0 deletions renderer/native/ios/renderer/component/view/UIView+Render.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@class HippyUIManager;
/// UIView's UIManager category
@interface UIView (HippyUIManager)

/// Convenient method to get HippyUIManager instance,
/// UIView's uiManager property is set when created.
- (nullable HippyUIManager *)uiManager;

@end

NS_ASSUME_NONNULL_END
38 changes: 38 additions & 0 deletions renderer/native/ios/renderer/component/view/UIView+Render.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
* iOS SDK
*
* Tencent is pleased to support the open source community by making
* Hippy available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "UIView+Render.h"
#import <objc/runtime.h>

@implementation UIView (HippyUIManager)

- (HippyUIManager *)uiManager {
NSValue *weakValue = objc_getAssociatedObject(self, @selector(uiManager));
return [weakValue nonretainedObjectValue];
}

- (void)setUiManager:(HippyUIManager *)uiManager {
NSValue *weakValue = [NSValue valueWithNonretainedObject:uiManager];
objc_setAssociatedObject(self, @selector(uiManager), weakValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
22 changes: 20 additions & 2 deletions tests/ios/HippyUIViewCategoryTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@
*/

#import <XCTest/XCTest.h>
#import "UIView+Hippy.h"
#import <hippy/UIView+Hippy.h>
#import <hippy/UIView+Render.h>
#import <hippy/HippyUIManager.h>

@interface UIView (HippyUIManagerUnitTest)

/// Bind UIView with HippyUIManager
/// This is a convenient method for UIView to get HippyUIManager instance.
/// - Parameter uiManager: HippyUIManager instance
- (void)setUiManager:(HippyUIManager *)uiManager;

@end

@interface HippyUIViewCategoryTest : XCTestCase

Expand All @@ -47,8 +58,15 @@ - (void)testGetHippyRootView {
XCTAssert([testView hippyRootView] == testSuperView);
testView.hippyTag = @(11);
XCTAssert([testView hippyRootView] == nil);
}

- (void)testGetHippyUIManager {
UIView *testView = [UIView new];
XCTAssertNil([testView uiManager]);


HippyUIManager *uiManager = [[HippyUIManager alloc] init];
XCTAssertNoThrow(testView.uiManager = uiManager);
XCTAssertTrue(testView.uiManager == uiManager);
}


Expand Down

0 comments on commit c22b60a

Please sign in to comment.