Skip to content

Commit

Permalink
fix(ios): resolve reload not working
Browse files Browse the repository at this point in the history
refactor hippy demo code
  • Loading branch information
wwwcg committed Aug 28, 2024
1 parent 49fdb34 commit ba2253e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
9C99A05896359E49AE9A8352 /* MyView.m in Sources */ = {isa = PBXBuildFile; fileRef = 90F34E176169143334EDFC28 /* MyView.m */; };
9CF888F990C83ECD4F6C78AA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5EF877A8CB4B2AD68A780A12 /* Assets.xcassets */; };
A3F629CA4C94D65F42BB4B33 /* HippyDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9EB0CD5FCC3F38E3C7DC5E00 /* HippyDemoViewController.m */; };
B8AA2B793BE43710EFC9D4DC /* TestModule.mm in Sources */ = {isa = PBXBuildFile; fileRef = D2241406CEF00D2FF5938BB2 /* TestModule.mm */; };
BD5196B65EE0B712FDA2EB9B /* UIViewController+Title.m in Sources */ = {isa = PBXBuildFile; fileRef = C412B5D210F1E3359523628C /* UIViewController+Title.m */; };
C1A78F8868963E6C790472AA /* PageCreationCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 25B65684EC9C9AF27BB04D21 /* PageCreationCell.xib */; };
C94B7BB8A618F36A1F58C270 /* HomePageViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 90B5552FD45612EC5EC4CB45 /* HomePageViewController.mm */; };
Expand Down Expand Up @@ -96,15 +95,13 @@
9EB0CD5FCC3F38E3C7DC5E00 /* HippyDemoViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HippyDemoViewController.m; sourceTree = "<group>"; };
A066953E3614D26125F617EF /* HippyPageCacheContainerView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HippyPageCacheContainerView.m; sourceTree = "<group>"; };
B2FA2A97C78628634C6AFC9A /* TTTGB-Medium.otf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "TTTGB-Medium.otf"; sourceTree = "<group>"; };
B5A22F93EAA399DE400CED88 /* TestModule.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestModule.h; sourceTree = "<group>"; };
BD54126F3E80B297E2109B40 /* DebugCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DebugCell.m; sourceTree = "<group>"; };
BF84819C1E7CCD88BCBEA4D7 /* HippyPageCache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HippyPageCache.h; sourceTree = "<group>"; };
C412B5D210F1E3359523628C /* UIViewController+Title.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+Title.m"; sourceTree = "<group>"; };
C629FDE26F7B6DBC976FB63A /* TurboBaseModule.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TurboBaseModule.h; sourceTree = "<group>"; };
C6BAC7BC0021E9E21B04E496 /* SettingsInfo.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SettingsInfo.m; sourceTree = "<group>"; };
C7504BC18486120B29F3D9FA /* PageCreationCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PageCreationCell.h; sourceTree = "<group>"; };
CC8AEAE8BF116954FB41BDF9 /* TurboBaseModule.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TurboBaseModule.mm; sourceTree = "<group>"; };
D2241406CEF00D2FF5938BB2 /* TestModule.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TestModule.mm; sourceTree = "<group>"; };
D2EE3455BE6DD7D2ADA20093 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
DC53FC9D9560D6AD989F8540 /* HomePageView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomePageView.xib; sourceTree = "<group>"; };
DDD052D2760E830B1E0095EA /* UIViewController+Title.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIViewController+Title.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -141,8 +138,6 @@
17AB7FB6346FE861C9D953F5 /* IconUtils.m */,
177A9E8373F2682A9A831991 /* Info.plist */,
D2EE3455BE6DD7D2ADA20093 /* main.m */,
B5A22F93EAA399DE400CED88 /* TestModule.h */,
D2241406CEF00D2FF5938BB2 /* TestModule.mm */,
DDD052D2760E830B1E0095EA /* UIViewController+Title.h */,
C412B5D210F1E3359523628C /* UIViewController+Title.m */,
4CF6648308C60D57DD274D30 /* HomePage */,
Expand Down Expand Up @@ -393,7 +388,6 @@
DCCEB3FED1802C0D9C21AB11 /* PageManagerViewController.m in Sources */,
F3F6D205C6FBA36CB262A28E /* SettingsInfo.m in Sources */,
380469787621147052854E8F /* SettingsViewController.m in Sources */,
B8AA2B793BE43710EFC9D4DC /* TestModule.mm in Sources */,
74C51526435B1AB788CBF991 /* TurboBaseModule.mm in Sources */,
72328C3389BA8DD5E54D7D5F /* TurboConfig.m in Sources */,
BD5196B65EE0B712FDA2EB9B /* UIViewController+Title.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
#import "HippyPageCache.h"
#import "DemoConfigs.h"

#import "HippyMethodInterceptorProtocol.h"

#import <hippy/HippyBridge.h>
#import <hippy/HippyRootView.h>
#import <hippy/HippyLog.h>
#import <hippy/HippyAssert.h>
#import <hippy/UIView+Hippy.h>
#import <hippy/HippyMethodInterceptorProtocol.h>

static NSString *const engineKey = @"Demo";

static NSString *formatLog(NSDate *timestamp, HippyLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) {
static NSArray *logLevelMap;
Expand All @@ -48,7 +46,7 @@
NSString *levelStr = level < 0 || level > logLevelMap.count ? logLevelMap[1] : logLevelMap[level];

if(fileName){
return [[NSString alloc] initWithFormat:@"[%@][%@:%d][%@]%@",
return [[NSString alloc] initWithFormat:@"[%@][%@:%d][%@] %@",
[formatter stringFromDate:timestamp],
fileName.lastPathComponent,
lineNumber.intValue,
Expand Down Expand Up @@ -142,89 +140,49 @@ - (void)runHippyCache {
}

- (void)runHippyDemo {
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(_isDebugMode)};
NSString *uniqueEngineKey = [NSString stringWithFormat:@"%@_%u", engineKey, arc4random()];

_hippyBridge = [[HippyBridge alloc] initWithDelegate:self
moduleProvider:nil
launchOptions:launchOptions
executorKey:uniqueEngineKey];
_hippyBridge.methodInterceptor = self;

[_hippyBridge setInspectable:YES];

[self mountConnector:_hippyBridge];
}

- (void)mountConnector:(HippyBridge *)hippyBridge {
BOOL isSimulator = NO;
#if TARGET_IPHONE_SIMULATOR
isSimulator = YES;
#endif

#if USE_NEW_LOAD
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:hippyBridge
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
delegate:self];
// Necessary configuration:
NSString *moduleName = @"Demo";
NSDictionary *launchOptions = @{ @"DebugMode": @(_isDebugMode) };
NSDictionary *initialProperties = @{ @"isSimulator": @(TARGET_OS_SIMULATOR) };

if (_isDebugMode) {
hippyBridge.sandboxDirectory = [_debugURL URLByDeletingLastPathComponent];
[hippyBridge loadBundleURL:_debugURL completion:^(NSURL * _Nullable, NSError * _Nullable) {
[rootView runHippyApplication];
}];
} else {
NSURL *vendorBundleURL = [self vendorBundleURL];
[hippyBridge loadBundleURL:vendorBundleURL completion:^(NSURL * _Nullable, NSError * _Nullable) {
NSLog(@"url %@ load finish", vendorBundleURL);
}];
NSURL *indexBundleURL = [self indexBundleURL];
hippyBridge.sandboxDirectory = [indexBundleURL URLByDeletingLastPathComponent];
[hippyBridge loadBundleURL:indexBundleURL completion:^(NSURL * _Nullable, NSError * _Nullable) {
NSLog(@"url %@ load finish", indexBundleURL);
[rootView runHippyApplication];
}];
}

#else
HippyBridge *bridge = nil;
HippyRootView *rootView = nil;

if (_isDebugMode) {
hippyBridge.sandboxDirectory = [_debugURL URLByDeletingLastPathComponent];
rootView = [[HippyRootView alloc] initWithBridge:hippyBridge
businessURL:_debugURL
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
bridge = [[HippyBridge alloc] initWithDelegate:self
bundleURL:_debugURL
moduleProvider:nil
launchOptions:launchOptions
executorKey:nil];
rootView = [[HippyRootView alloc] initWithBridge:bridge
moduleName:moduleName
initialProperties:initialProperties
delegate:self];
} else {
NSURL *vendorBundleURL = [self vendorBundleURL];
NSURL *indexBundleURL = [self indexBundleURL];
[hippyBridge loadBundleURL:vendorBundleURL
bundleType:HippyBridgeBundleTypeVendor
completion:^(NSURL * _Nullable url, NSError * _Nullable error) {
NSLog(@"url %@ load finish", vendorBundleURL);
}];
hippyBridge.sandboxDirectory = [indexBundleURL URLByDeletingLastPathComponent];
rootView = [[HippyRootView alloc] initWithBridge:hippyBridge
HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self
bundleURL:vendorBundleURL
moduleProvider:nil
launchOptions:launchOptions
executorKey:nil];
rootView = [[HippyRootView alloc] initWithBridge:bridge
businessURL:indexBundleURL
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
moduleName:moduleName
initialProperties:initialProperties
delegate:self];
}

#endif

bridge.methodInterceptor = self;
[bridge setInspectable:YES];
_hippyBridge = bridge;
rootView.frame = self.contentAreaView.bounds;
rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

[self.contentAreaView addSubview:rootView];
_hippyRootView = rootView;
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_hippyRootView.frame = self.contentAreaView.bounds;
}

#pragma mark -

- (NSURL *)vendorBundleURL {
NSString *path = nil;
Expand Down
46 changes: 24 additions & 22 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -234,40 +234,24 @@ - (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
_debugMode = [launchOptions[@"DebugMode"] boolValue];
_enableTurbo = !!launchOptions[@"EnableTurbo"] ? [launchOptions[@"EnableTurbo"] boolValue] : YES;
_engineKey = executorKey.length > 0 ? executorKey : [NSString stringWithFormat:@"%p", self];
_invalidateReason = HippyInvalidateReasonDealloc;
_valid = YES;
_bundlesQueue = [[HippyBundleOperationQueue alloc] init];
_startTime = footstone::TimePoint::SystemNow();

HippyLogInfo(@"HippyBridge init begin, self:%p", self);

// Set the log delegate for hippy core module
registerLogDelegateToHippyCore();

HippyExecuteOnMainThread(^{
self->_isOSNightMode = [HippyDeviceBaseInfo isUIScreenInOSDarkMode];
self.cachedDimensionsInfo = hippyExportedDimensions(self);
}, YES);

// Setup
[self setUp];

[self addImageProviderClass:[HippyDefaultImageProvider class]];
[self setVfsUriLoader:[self createURILoaderIfNeeded]];
[self setUpNativeRenderManager];

// Record bridge instance for RedBox (Debug Only)
[HippyBridge setCurrentBridge:self];

[self loadPendingVendorBundleURLIfNeeded];

// Set the default sandbox directory
[self setSandboxDirectory:[bundleURL URLByDeletingLastPathComponent]];
HippyLogInfo(@"HippyBridge init end, self:%p", self);
}
return self;
}

- (void)dealloc {
/**
* This runs only on the main thread, but crashes the subclass
* HippyAssertMainQueue();
*/
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle],%@ dealloc %p", NSStringFromClass([self class]), self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.invalidateReason = HippyInvalidateReasonDealloc;
Expand Down Expand Up @@ -442,6 +426,14 @@ - (void)requestReload {

- (void)setUp {
_valid = YES;
_startTime = footstone::TimePoint::SystemNow();

// Get global enviroment info
HippyExecuteOnMainThread(^{
self->_isOSNightMode = [HippyDeviceBaseInfo isUIScreenInOSDarkMode];
self.cachedDimensionsInfo = hippyExportedDimensions(self);
}, YES);

self.moduleSemaphore = dispatch_semaphore_create(0);
@try {
__weak HippyBridge *weakSelf = self;
Expand Down Expand Up @@ -478,6 +470,16 @@ - (void)setUp {
} @catch (NSException *exception) {
HippyBridgeHandleException(exception, self);
}

[self addImageProviderClass:[HippyDefaultImageProvider class]];
[self setVfsUriLoader:[self createURILoaderIfNeeded]];
[self setUpNativeRenderManager];

// Load pending js bundles
[self loadPendingVendorBundleURLIfNeeded];

// Set the default sandbox directory
[self setSandboxDirectory:[_pendingLoadingVendorBundleURL URLByDeletingLastPathComponent]];
}


Expand Down Expand Up @@ -1274,7 +1276,7 @@ - (void)setRootView:(UIView *)rootView {
_rootNode->SetRootOrigin(rootView.frame.origin.x, rootView.frame.origin.y);

//set rendermanager for dommanager
if (!domManager->GetRenderManager().lock()) {
if (domManager->GetRenderManager().lock() != _renderManager) {
domManager->SetRenderManager(_renderManager);
}
//bind rootview and root node
Expand Down
6 changes: 0 additions & 6 deletions renderer/native/ios/renderer/HippyRootView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ - (NSNumber *)hippyTag {
return super.hippyTag;
}

- (void)bridgeDidReload {
HippyAssertMainQueue();
// Clear the hippyTag so it can be re-assigned
self.hippyTag = nil;
}


#pragma mark - Notification Handlers

Expand Down
4 changes: 4 additions & 0 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ - (void)invalidate {
});
}

- (void)dealloc {
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle], HippyUIManager(%p) dealloc", self);
}

#pragma mark Setter & Getter

- (void)registRenderManager:(std::weak_ptr<hippy::RenderManager>)renderManager {
Expand Down

0 comments on commit ba2253e

Please sign in to comment.