From 292a8ef713da2093bbf6aca212077bb815869202 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Wed, 28 Aug 2024 16:49:37 +0800 Subject: [PATCH] chore(ios): update demo and eliminate some warnings --- driver/js/src/napi/jsc/jsc_ctx.cc | 2 +- .../HippyDemo/PageManager/HippyPageCache.h | 3 +- .../PageManager/PageCreationViewController.m | 34 +++-- .../RenderPage/HippyDemoViewController.m | 102 ++++--------- .../examples/ios-demo/HippyDemo/TestModule.h | 27 ---- .../examples/ios-demo/HippyDemo/TestModule.mm | 136 ------------------ modules/ios/base/HippyLog.mm | 15 +- .../waterfalllist/HippyWaterfallView.mm | 5 +- 8 files changed, 61 insertions(+), 263 deletions(-) delete mode 100644 framework/examples/ios-demo/HippyDemo/TestModule.h delete mode 100644 framework/examples/ios-demo/HippyDemo/TestModule.mm diff --git a/driver/js/src/napi/jsc/jsc_ctx.cc b/driver/js/src/napi/jsc/jsc_ctx.cc index 0610c96d2f5..51152479073 100644 --- a/driver/js/src/napi/jsc/jsc_ctx.cc +++ b/driver/js/src/napi/jsc/jsc_ctx.cc @@ -320,7 +320,7 @@ std::shared_ptr JSCCtx::DefineClass(const string_view& name, JSStringRef set_key_name = JSStringCreateWithCharacters(reinterpret_cast(kSetStr), ARRAY_SIZE(kSetStr) - 1); JSStringRef define_property_name = JSStringCreateWithCharacters(reinterpret_cast(kDefinePropertyStr), ARRAY_SIZE(kDefinePropertyStr) - 1); JSStringRef object_name = JSStringCreateWithCharacters(reinterpret_cast(kObjectStr), ARRAY_SIZE(kObjectStr) - 1); - for (auto i = 0; i < property_count; ++i) { + for (size_t i = 0; i < property_count; ++i) { auto property_descriptor = properties[i]; auto property_object = JSObjectMake(context_, parent_class_ref, nullptr); if (property_descriptor->getter) { diff --git a/framework/examples/ios-demo/HippyDemo/PageManager/HippyPageCache.h b/framework/examples/ios-demo/HippyDemo/PageManager/HippyPageCache.h index 3aa05d6be61..1bc08be8aea 100644 --- a/framework/examples/ios-demo/HippyDemo/PageManager/HippyPageCache.h +++ b/framework/examples/ios-demo/HippyDemo/PageManager/HippyPageCache.h @@ -27,7 +27,8 @@ NS_ASSUME_NONNULL_BEGIN typedef NS_ENUM(NSUInteger, DriverType) { DriverTypeReact, - DriverTypeVue, + DriverTypeVue2, + DriverTypeVue3 }; typedef NS_ENUM(NSUInteger, RenderType) { diff --git a/framework/examples/ios-demo/HippyDemo/PageManager/PageCreationViewController.m b/framework/examples/ios-demo/HippyDemo/PageManager/PageCreationViewController.m index 52cf8226b02..b1ccf371e1b 100644 --- a/framework/examples/ios-demo/HippyDemo/PageManager/PageCreationViewController.m +++ b/framework/examples/ios-demo/HippyDemo/PageManager/PageCreationViewController.m @@ -32,14 +32,15 @@ static NSString *const kDebugCell = @"debugCell"; static NSString *const kDriverTypeReact = @"JS React"; -static NSString *const kDriverTypeVue = @"JS Vue"; +static NSString *const kDriverTypeVue2 = @"JS Vue2"; +static NSString *const kDriverTypeVue3 = @"JS Vue3"; static NSString *const kRenderTypeNative = @"Native"; static NSString *const kCancel = @"取消"; @interface PageCreationViewController () { - NSString *_currentDriver; + DriverType _currentDriver; NSString *_renderer; UITableView *_tableView; BOOL _debugMode; @@ -54,7 +55,7 @@ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setNavigationItemTitle:@"Page Managerment"]; - _currentDriver = kDriverTypeReact; + _currentDriver = DriverTypeReact; _renderer = kRenderTypeNative; [self setNavigationAreaBackground:[UIColor whiteColor]]; CGFloat ratio = 229.f / 255.f; @@ -120,13 +121,14 @@ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPa } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + if (0 == [indexPath section]) { PageCreationCell *cell = (PageCreationCell *)[tableView dequeueReusableCellWithIdentifier:kNormalCell forIndexPath:indexPath]; cell.summaryImageView.image = [UIImage imageFromIconName:@"driver_icon"]; cell.typeLabel.text = @"Driver"; - cell.subTypeLabel.text = _currentDriver; + cell.subTypeLabel.text = @[kDriverTypeReact, kDriverTypeVue2, kDriverTypeVue3][_currentDriver]; return cell; } else if (1 == [indexPath section]) { @@ -202,19 +204,25 @@ - (void)showDriverSelectMenu { [alert addAction:[UIAlertAction actionWithTitle:kDriverTypeReact style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { PageCreationViewController *strongVC = weakVC; if (strongVC) { - strongVC->_currentDriver = action.title; + strongVC->_currentDriver = DriverTypeReact; [strongVC->_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic]; } }]]; - [alert addAction:[UIAlertAction actionWithTitle:kDriverTypeVue style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + [alert addAction:[UIAlertAction actionWithTitle:kDriverTypeVue2 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { PageCreationViewController *strongVC = weakVC; if (strongVC) { - strongVC->_currentDriver = action.title; + strongVC->_currentDriver = DriverTypeVue2; [strongVC->_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; } }]]; - [alert addAction:[UIAlertAction actionWithTitle:kCancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { + [alert addAction:[UIAlertAction actionWithTitle:kDriverTypeVue3 style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { + PageCreationViewController *strongVC = weakVC; + if (strongVC) { + strongVC->_currentDriver = DriverTypeVue3; + [strongVC->_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; + } }]]; + [alert addAction:[UIAlertAction actionWithTitle:kCancel style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:NULL]; } @@ -251,18 +259,18 @@ - (void)showRenderSelectMenu { } - (void)createDemoAction { - PageCreationCell *cell0 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; - DriverType driverType = [[cell0 subTypeLabel].text isEqualToString:@"JS React"]?DriverTypeReact:DriverTypeVue; -// PageCreationCell *cell1 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; + DriverType driverType = _currentDriver; RenderType renderType = RenderTypeNative; - //[cell1.subTypeLabel.text isEqualToString:@"Native"]?RenderTypeNative:RenderTypeNative; NSURL *debugURL = nil; if (_debugMode) { DebugCell *cell2 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]]; NSString *debugString = [cell2 debugURLString]; debugURL = [NSURL URLWithString:debugString]; } - HippyDemoViewController *vc = [[HippyDemoViewController alloc] initWithDriverType:driverType renderType:renderType debugURL:debugURL isDebugMode:_debugMode]; + HippyDemoViewController *vc = [[HippyDemoViewController alloc] initWithDriverType:driverType + renderType:renderType + debugURL:debugURL + isDebugMode:_debugMode]; NSMutableArray<__kindof UIViewController *> *viewControllers = [[self.navigationController viewControllers] mutableCopy]; [viewControllers removeLastObject]; [viewControllers addObject:vc]; diff --git a/framework/examples/ios-demo/HippyDemo/RenderPage/HippyDemoViewController.m b/framework/examples/ios-demo/HippyDemo/RenderPage/HippyDemoViewController.m index 1dcf1a8bde3..dde46817a75 100644 --- a/framework/examples/ios-demo/HippyDemo/RenderPage/HippyDemoViewController.m +++ b/framework/examples/ios-demo/HippyDemo/RenderPage/HippyDemoViewController.m @@ -33,34 +33,6 @@ #import -static NSString *formatLog(NSDate *timestamp, HippyLogLevel level, NSString *fileName, NSNumber *lineNumber, NSString *message) { - static NSArray *logLevelMap; - static NSDateFormatter *formatter; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - logLevelMap = @[@"TRACE", @"INFO", @"WARN", @"ERROR", @"FATAL"]; - formatter = [NSDateFormatter new]; - formatter.dateFormat = formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS"; - }); - - NSString *levelStr = level < 0 || level > logLevelMap.count ? logLevelMap[1] : logLevelMap[level]; - - if(fileName){ - return [[NSString alloc] initWithFormat:@"[%@][%@:%d][%@] %@", - [formatter stringFromDate:timestamp], - fileName.lastPathComponent, - lineNumber.intValue, - levelStr, - message - ]; - }else{ - return [[NSString alloc] initWithFormat:@"[%@]%@", - [formatter stringFromDate:timestamp], - message - ]; - } -} - @interface HippyDemoViewController () { HippyBridge *_hippyBridge; HippyRootView *_hippyRootView; @@ -118,22 +90,19 @@ - (void)viewDidLoad { } } -- (void)registerLogFunction { - HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) { - NSString *log = formatLog([NSDate date], level, fileName, lineNumber, message); - if([log hasSuffix:@"\n"]){ - fprintf(stderr, "%s", log.UTF8String); - }else{ - fprintf(stderr, "%s\n", log.UTF8String); - } - }); -} - - (void)runHippyCache { _hippyRootView.frame = self.contentAreaView.bounds; [self.contentAreaView addSubview:_hippyRootView]; } +#pragma mark - Hippy Setup + +- (void)registerLogFunction { + // Register your custom log function for Hippy, + // use HippyDefaultLogFunction as an example, it outputs logs to stderr. + HippySetLogFunction(HippyDefaultLogFunction); +} + - (void)runHippyDemo { // Necessary configuration: NSString *moduleName = @"Demo"; @@ -167,54 +136,43 @@ - (void)runHippyDemo { delegate:self]; } - bridge.methodInterceptor = self; + // // Config whether jsc is inspectable, Highly recommended setting, + // since inspectable of JSC is disabled by default since iOS 16.4 [bridge setInspectable:YES]; _hippyBridge = bridge; rootView.frame = self.contentAreaView.bounds; rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; [self.contentAreaView addSubview:rootView]; _hippyRootView = rootView; + + + // Optional configs: + bridge.methodInterceptor = self; // see HippyMethodInterceptorProtocol } -#pragma mark - +#pragma mark - Helpers -- (NSURL *)vendorBundleURL { - NSString *path = nil; - if (DriverTypeReact == _driverType) { - path = [[NSBundle mainBundle] pathForResource:@"vendor.ios" ofType:@"js" inDirectory:@"res/react"]; +- (NSString *)currentJSBundleDir { + NSString *dir = nil; + if (DriverTypeVue2 == _driverType) { + dir = @"res/vue2"; + } else if (DriverTypeVue3 == _driverType) { + dir = @"res/vue3"; + } else if (DriverTypeReact == _driverType) { + dir = @"res/react"; } - else if (DriverTypeVue == _driverType) { - path = [[NSBundle mainBundle] pathForResource:@"vendor.ios" ofType:@"js" inDirectory:@"res/vue3"]; - } - return [NSURL fileURLWithPath:path]; + return dir; } -- (NSURL *)indexBundleURL { - NSString *path = nil; - if (DriverTypeReact == _driverType) { - path = [[NSBundle mainBundle] pathForResource:@"index.ios" ofType:@"js" inDirectory:@"res/react"]; - } - else if (DriverTypeVue == _driverType) { - path = [[NSBundle mainBundle] pathForResource:@"index.ios" ofType:@"js" inDirectory:@"res/vue3"]; - } +- (NSURL *)vendorBundleURL { + NSString *path = [[NSBundle mainBundle] pathForResource:@"vendor.ios" ofType:@"js" inDirectory:[self currentJSBundleDir]]; return [NSURL fileURLWithPath:path]; } -- (DriverType)driverType { - return _driverType; -} - -- (RenderType)renderType { - return _renderType; -} - -- (NSURL *)debugURL { - return _debugURL; -} - -- (BOOL)isDebugMode { - return _isDebugMode; +- (NSURL *)indexBundleURL { + NSString *path = [[NSBundle mainBundle] pathForResource:@"index.ios" ofType:@"js" inDirectory:[self currentJSBundleDir]]; + return [NSURL fileURLWithPath:path]; } - (void)removeRootView:(NSNumber *)rootTag bridge:(HippyBridge *)bridge { @@ -253,7 +211,7 @@ - (BOOL)shouldStartInspector:(HippyBridge *)bridge { } -#pragma mark - HippyMethodInterceptorProtocol +#pragma mark - Optional - HippyMethodInterceptorProtocol - (BOOL)shouldInvokeWithModuleName:(NSString *)moduleName methodName:(NSString *)methodName diff --git a/framework/examples/ios-demo/HippyDemo/TestModule.h b/framework/examples/ios-demo/HippyDemo/TestModule.h deleted file mode 100644 index b461e122363..00000000000 --- a/framework/examples/ios-demo/HippyDemo/TestModule.h +++ /dev/null @@ -1,27 +0,0 @@ -/*! -* 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 "HippyBridgeModule.h" - -@interface TestModule : NSObject - -@end diff --git a/framework/examples/ios-demo/HippyDemo/TestModule.mm b/framework/examples/ios-demo/HippyDemo/TestModule.mm deleted file mode 100644 index 69a0e711364..00000000000 --- a/framework/examples/ios-demo/HippyDemo/TestModule.mm +++ /dev/null @@ -1,136 +0,0 @@ -/*! -* 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 "AppDelegate.h" -#import "TestModule.h" -#import "HippyBundleURLProvider.h" -#import "HippyJSEnginesMapper.h" -#import "HippyRootView.h" -#import "UIView+Hippy.h" -#import "HippyLog.h" -#import "HippyRedBox.h" -#import "DemoConfigs.h" -#import "HippyMethodInterceptorProtocol.h" -#import "HippyAssert.h" - -static NSString *const engineKey = @"Demo"; - -@interface TestModule () { - HippyBridge *_connector; -} - -@end - -@implementation TestModule - -HIPPY_EXPORT_MODULE() - -- (dispatch_queue_t)methodQueue { - return dispatch_get_main_queue(); -} - -HIPPY_EXPORT_METHOD(debug:(nonnull NSNumber *)instanceId) { -} - -HIPPY_EXPORT_METHOD(remoteDebug:(nonnull NSNumber *)instanceId bundleUrl:(nonnull NSString *)bundleUrl) { - [self runCommonDemo:bundleUrl]; -} - -- (void)runCommonDemo:(nonnull NSString *)bundleUrl { - BOOL isSimulator = NO; - #if TARGET_IPHONE_SIMULATOR - isSimulator = YES; - #endif - AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; - UIViewController *rootViewController = delegate.window.rootViewController; - UIViewController *vc = [[UIViewController alloc] init]; - //JS Contexts holding the same engine key will share VM - NSURL *url = [NSURL URLWithString:bundleUrl]; - NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(YES), @"DebugURL": url}; - NSURL *sandboxDirectory = [url URLByDeletingLastPathComponent]; - _connector = [[HippyBridge alloc] initWithDelegate:self - moduleProvider:nil - launchOptions:launchOptions - executorKey:engineKey]; - [_connector setInspectable:YES]; - //set custom vfs loader - _connector.sandboxDirectory = sandboxDirectory; - _connector.contextName = @"Demo"; - _connector.moduleName = @"Demo"; - _connector.methodInterceptor = self; - vc.modalPresentationStyle = UIModalPresentationFullScreen; - [self mountConnector:_connector onView:vc.view]; - [rootViewController presentViewController:vc animated:YES completion:NULL]; -} - -- (void)mountConnector:(HippyBridge *)connector onView:(UIView *)view { - BOOL isSimulator = NO; -#if TARGET_IPHONE_SIMULATOR - isSimulator = YES; -#endif - - NSString *bundleStr = [HippyBundleURLProvider sharedInstance].bundleURLString; - NSURL *bundleUrl = [NSURL URLWithString:bundleStr]; - - HippyRootView *rootView = [[HippyRootView alloc] initWithFrame:view.bounds]; - rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; - [_connector setRootView:rootView]; - NSNumber *rootTag = [rootView hippyTag]; - [connector loadBundleURL:bundleUrl - bundleType:HippyBridgeBundleTypeBusiness - completion:^(NSURL * _Nullable bundleURL, NSError * _Nullable error) { - NSLog(@"url %@ load finish", bundleStr); - [connector loadInstanceForRootView:rootTag withProperties:@{@"isSimulator": @(isSimulator)}]; - }]; - [view addSubview:rootView]; -} - -- (void)reload:(HippyBridge *)bridge { - AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; - UIViewController *rootViewController = delegate.window.rootViewController; - UIViewController *vc = rootViewController.presentedViewController; - [self mountConnector:_connector onView:vc.view]; -} - -- (void)removeRootView:(NSNumber *)rootTag bridge:(HippyBridge *)bridge { - AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; - UIViewController *rootViewController = delegate.window.rootViewController; - UIViewController *vc = rootViewController.presentedViewController; - [[[vc.view subviews] firstObject] removeFromSuperview]; -} - -- (BOOL)shouldStartInspector:(HippyBridge *)bridge { - return bridge.debugMode; -} - -- (BOOL)shouldInvokeWithModuleName:(NSString *)moduleName methodName:(NSString *)methodName arguments:(NSArray> *)arguments argumentsValues:(NSArray *)argumentsValue containCallback:(BOOL)containCallback { - HippyAssert(moduleName, @"module name must not be null"); - HippyAssert(methodName, @"method name must not be null"); - return YES; -} - -- (BOOL)shouldCallbackBeInvokedWithModuleName:(NSString *)moduleName methodName:(NSString *)methodName callbackId:(NSNumber *)cbId arguments:(id)arguments { - HippyAssert(moduleName, @"module name must not be null"); - HippyAssert(methodName, @"method name must not be null"); - return YES; -} -@end diff --git a/modules/ios/base/HippyLog.mm b/modules/ios/base/HippyLog.mm index 33d4489932d..3bdf57eae3d 100644 --- a/modules/ios/base/HippyLog.mm +++ b/modules/ios/base/HippyLog.mm @@ -151,19 +151,12 @@ void HippyPerformBlockWithLogPrefix(void (^block)(void), NSString *prefix) { }); [log appendString:[formatter stringFromDate:timestamp]]; } - if (level) { - [log appendFormat:@"[%s]", HippyLogLevels[level]]; - } - - [log appendFormat:@"[tid:%@]", currentThreadName()]; - + [log appendFormat:@"[%s]", HippyLogLevels[level]]; + [log appendFormat:@"[%@]", currentThreadName()]; + if (fileName) { fileName = fileName.lastPathComponent; - if (lineNumber) { - [log appendFormat:@"[%@:%@]", fileName, lineNumber]; - } else { - [log appendFormat:@"[%@]", fileName]; - } + [log appendFormat:@"[%@:%@]", fileName, lineNumber]; } if (message) { [log appendString:@" "]; diff --git a/renderer/native/ios/renderer/component/waterfalllist/HippyWaterfallView.mm b/renderer/native/ios/renderer/component/waterfalllist/HippyWaterfallView.mm index 215fe4ed042..019302e1e85 100644 --- a/renderer/native/ios/renderer/component/waterfalllist/HippyWaterfallView.mm +++ b/renderer/native/ios/renderer/component/waterfalllist/HippyWaterfallView.mm @@ -520,8 +520,9 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { } } -- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; -{ return nil; } +- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { + return nil; +} - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view { for (NSObject *scrollViewListener in [self scrollListeners]) {