Skip to content

Commit

Permalink
chore(ios): update demo and eliminate some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Sep 11, 2024
1 parent ae09d07 commit 292a8ef
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 263 deletions.
2 changes: 1 addition & 1 deletion driver/js/src/napi/jsc/jsc_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ std::shared_ptr<CtxValue> JSCCtx::DefineClass(const string_view& name,
JSStringRef set_key_name = JSStringCreateWithCharacters(reinterpret_cast<const JSChar *>(kSetStr), ARRAY_SIZE(kSetStr) - 1);
JSStringRef define_property_name = JSStringCreateWithCharacters(reinterpret_cast<const JSChar *>(kDefinePropertyStr), ARRAY_SIZE(kDefinePropertyStr) - 1);
JSStringRef object_name = JSStringCreateWithCharacters(reinterpret_cast<const JSChar *>(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, DriverType) {
DriverTypeReact,
DriverTypeVue,
DriverTypeVue2,
DriverTypeVue3
};

typedef NS_ENUM(NSUInteger, RenderType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()<UITableViewDelegate, UITableViewDataSource> {
NSString *_currentDriver;
DriverType _currentDriver;
NSString *_renderer;
UITableView *_tableView;
BOOL _debugMode;
Expand All @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,6 @@
#import <hippy/HippyMethodInterceptorProtocol.h>


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 () <HippyMethodInterceptorProtocol, HippyBridgeDelegate, HippyRootViewDelegate> {
HippyBridge *_hippyBridge;
HippyRootView *_hippyRootView;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -253,7 +211,7 @@ - (BOOL)shouldStartInspector:(HippyBridge *)bridge {
}


#pragma mark - HippyMethodInterceptorProtocol
#pragma mark - Optional - HippyMethodInterceptorProtocol

- (BOOL)shouldInvokeWithModuleName:(NSString *)moduleName
methodName:(NSString *)methodName
Expand Down
27 changes: 0 additions & 27 deletions framework/examples/ios-demo/HippyDemo/TestModule.h

This file was deleted.

Loading

0 comments on commit 292a8ef

Please sign in to comment.