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

feat(ios): add NightMode & improve method of getting UI state #3411

Merged
merged 3 commits into from
Jul 26, 2023
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
7 changes: 5 additions & 2 deletions examples/ios-demo/HippyDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 53;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -170,7 +170,8 @@
0612F01623A8BE320079E622 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1130;
BuildIndependentTargetsInParallel = YES;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = tencent;
TargetAttributes = {
0612F01D23A8BE320079E622 = {
Expand Down Expand Up @@ -278,6 +279,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -336,6 +338,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
21 changes: 13 additions & 8 deletions examples/ios-demo/HippyDemo/TestModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,26 @@ - (dispatch_queue_t)methodQueue

HIPPY_EXPORT_METHOD(debug:(nonnull NSNumber *)instanceId)
{
//#define REMOTEDEBUG

#ifdef REMOTEDEBUG
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *nav = delegate.window.rootViewController;
UIViewController *vc = [[UIViewController alloc] init];
BOOL isSimulator = NO;
#if TARGET_IPHONE_SIMULATOR
isSimulator = YES;
#endif


//#define REMOTEDEBUG

#ifdef REMOTEDEBUG
NSURL *url = [NSURL URLWithString:@"your server ip address"];
HippyBridge *bridge = [[HippyBridge alloc] initWithBundleURL:url moduleProvider:nil launchOptions:nil];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"your module name" initialProperties:@{@"isSimulator": @(isSimulator) shareOptions:nil delegate:nil];
#else
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:nil businessURL:nil moduleName:@"Demo" initialProperties:@{@"isSimulator": @(isSimulator)} launchOptions:nil shareOptions:nil debugMode:YES delegate:nil];
rootView.bridge.enableTurbo = YES; // keep the same logic with Android
#endif
rootView.backgroundColor = [UIColor whiteColor];
rootView.frame = vc.view.bounds;
[vc.view addSubview:rootView];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[nav presentViewController:vc animated:YES completion:NULL];
#endif
}

HIPPY_EXPORT_METHOD(remoteDebug:(nonnull NSNumber *)instanceId bundleUrl:(nonnull NSString *)bundleUrl)
Expand Down Expand Up @@ -100,4 +96,13 @@ - (NSURL *)inspectorSourceURLForBridge:(HippyBridge *)bridge {
return bridge.bundleURL;
}

#pragma mark - Hippy Bridge Delegate

- (BOOL)shouldUseViewWillTransitionMethodToMonitorOrientation {
// 是否使用viewWillTransitionToSize方法替换UIApplicationDidChangeStatusBarOrientationNotification通知,
// 推荐设置 (该系统通知已废弃,且部分场景下存在异常)
// 注意,设置后必须实现viewWillTransitionToSize方法,并调用onHostControllerTransitionedToSize向hippy同步事件。
return NO;
}

@end
115 changes: 84 additions & 31 deletions examples/ios-demo/HippyDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@
#import "HippyLog.h"
#import "HippyBundleURLProvider.h"
#import "DemoConfigs.h"
#import <hippy/HippyBridge.h>
#import <sys/utsname.h>

@interface ViewController ()<HippyBridgeDelegate, HippyMethodInterceptorProtocol>

//release macro below if use debug mode
//#define HIPPYDEBUG


@interface ViewController () <HippyBridgeDelegate, HippyMethodInterceptorProtocol>

/// Hippy Root View
@property (nonatomic, strong) HippyRootView *hippyRootView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
NSLog(@"hippy says:%@ in file [%@:%d]", message, fileName, lineNumber.intValue);
Expand All @@ -46,10 +55,7 @@ - (void)viewDidLoad {
isSimulator = YES;
#endif


//release macro below if use debug mode
//#define HIPPYDEBUG


#ifdef HIPPYDEBUG
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO), @"DebugMode": @(YES)};
NSString *bundleStr = [HippyBundleURLProvider sharedInstance].bundleURLString;
Expand All @@ -59,8 +65,13 @@ - (void)viewDidLoad {
moduleProvider:nil
launchOptions:launchOptions
executorKey:@"Demo"];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"Demo" initialProperties:@{@"isSimulator": @(isSimulator)} shareOptions:@{@"DebugMode": @(YES)} delegate:nil];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
shareOptions:@{@"DebugMode": @(YES)}
delegate:nil];
#else

NSString *commonBundlePath = [[NSBundle mainBundle] pathForResource:@"vendor.ios" ofType:@"js" inDirectory:@"res"];
NSString *businessBundlePath = [[NSBundle mainBundle] pathForResource:@"index.ios" ofType:@"js" inDirectory:@"res"];
NSDictionary *launchOptions = @{@"EnableTurbo": @(DEMO_ENABLE_TURBO)};
Expand All @@ -69,26 +80,43 @@ - (void)viewDidLoad {
moduleProvider:nil
launchOptions:launchOptions
executorKey:@"Demo"];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge businessURL:[NSURL fileURLWithPath:businessBundlePath] moduleName:@"Demo" initialProperties: @{@"isSimulator": @(isSimulator)} launchOptions:nil shareOptions:nil debugMode:NO delegate:nil];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge
businessURL:[NSURL fileURLWithPath:businessBundlePath]
moduleName:@"Demo"
initialProperties:@{@"isSimulator": @(isSimulator)}
launchOptions:nil
shareOptions:nil
delegate:nil];
#endif
bridge.methodInterceptor = self;


// HippyBridge *bridge = [[HippyBridge alloc] initWithDelegate:self bundleURL:[NSURL URLWithString:@"http://localhost:38989/index.bundle?platform=ios&dev=true&minify=false"] moduleProvider:nil launchOptions:nil executorKey:@"Demo"];
// HippyRootView *rootView = [[HippyRootView alloc] initWithBridge:bridge moduleName:@"Demo" initialProperties:nil shareOptions:nil delegate:nil];;


rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
rootView.frame = self.view.bounds;
[self.view addSubview:rootView];

self.hippyRootView = rootView;
}


#pragma mark - HippyBridgeDelegate

- (CGFloat)defaultStatusBarHeightNoMatterHiddenOrNot {
// 当hippy无法获取状态栏高度时返回的默认值
// 说明:状态栏高度通常为动态获取,且不可作为页面布局时的依赖,然而部分页面初始化时依赖状态栏高度进行布局,
// 为兼容部分旧代码布局,特提供此设置接口,作为默认的兜底值。
return [ViewController defaultStatusBarHeight];
}

- (BOOL)shouldUseViewWillTransitionMethodToMonitorOrientation {
// 是否使用viewWillTransitionToSize方法替换UIApplicationDidChangeStatusBarOrientationNotification通知,
// 推荐设置 (该系统通知已废弃,且部分场景下存在异常)
// 注意,设置后必须实现viewWillTransitionToSize方法,并调用onHostControllerTransitionedToSize向hippy同步事件。
return YES;
}

- (NSDictionary *)objectsBeforeExecuteCode {
NSDictionary *dic1 = @{@"name": @"zs", @"gender": @"male"};
NSDictionary *dic2 = @{@"name": @"ls", @"gender": @"male"};
NSDictionary *dic3 = @{@"name": @"ww", @"gender": @"female"};

NSDictionary *ret = @{@"info1": dic1, @"info2": dic2, @"info3": dic3};
return ret;
}
Expand All @@ -102,20 +130,9 @@ - (NSDictionary *)objectsBeforeExecuteSecondaryCode {
}

- (BOOL)dynamicLoad:(HippyBridge *)bridge URI:(NSString *)uri completion:(void (^)(NSString *))completion {
// NSURL *url = [NSURL URLWithString:uri];
// NSURLRequest *req = [NSURLRequest requestWithURL:url];
// [[NSURLSession sharedSession] dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// if (error) {
// NSLog(@"dynamic load error: %@", [error description]);
// }
// else {
// NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// completion(result);
// }
// }];;
//简单处理,直接返回。
// completion(@"var a = 1");
return false;
// Demo,直接返回。
completion(@"var a = 1");
return YES;
}

- (BOOL)shouldStartInspector:(HippyBridge *)bridge {
Expand All @@ -126,16 +143,52 @@ - (NSURL *)inspectorSourceURLForBridge:(HippyBridge *)bridge {
return bridge.bundleURL;
}

- (BOOL)shouldInvokeWithModuleName:(NSString *)moduleName methodName:(NSString *)methodName arguments:(NSArray<id<HippyBridgeArgument>> *)arguments argumentsValues:(NSArray *)argumentsValue containCallback:(BOOL)containCallback {

#pragma mark - HippyMethodInterceptorProtocol

- (BOOL)shouldInvokeWithModuleName:(NSString *)moduleName
methodName:(NSString *)methodName
arguments:(NSArray<id<HippyBridgeArgument>> *)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 {
- (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;
}


#pragma mark - View Controller Size Change

- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Note that `useViewWillTransitionMethodToMonitorOrientation` flag must be set when init bridge,
// otherwise calling this function takes no effect.
[self.hippyRootView onHostControllerTransitionedToSize:size];
}];
}


#pragma mark - Helper Methods

+ (CGFloat)defaultStatusBarHeight {
// Hippy旧版本SDK内部的默认实现,由于不同设备状态栏高度不同,该方法存在缺陷
// 注意:强烈不建议布局依赖状态栏高度,新版本Hippy SDK内部不再提供默认值
// 如仍坚持依赖状态栏高度进行布局,为避免特殊场景下获取不到导致异常,请根据机型自行判断并提供默认值

// Hippy旧版本内部实现如下:
BOOL isAboveIPhoneX = ([[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0);
return isAboveIPhoneX ? 44.0 : 20.0; //注意,不同机型状态栏高度不同,此实现不正确, 仅作DEMO说明
}

@end
Loading