feat(ios): add interface and demo to third party (#1377)

* feat(ios): add interface and demo to third party

* feat(ios): use test mock data for demo
This commit is contained in:
luomy
2022-01-05 19:47:10 +08:00
committed by GitHub
parent 26132956c9
commit a9bc741a33
10 changed files with 209 additions and 30 deletions
@@ -14,7 +14,7 @@
06082A7B24AB222000AF85BC /* HippyFooterRefresh.m in Sources */ = {isa = PBXBuildFile; fileRef = 06082A7424AB222000AF85BC /* HippyFooterRefresh.m */; };
060F6F6D25F1DE0C0038F8F9 /* HippyVirtualList.mm in Sources */ = {isa = PBXBuildFile; fileRef = 060F6F6B25F1DE0C0038F8F9 /* HippyVirtualList.mm */; };
0612F02323A8BE320079E622 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0612F02223A8BE320079E622 /* AppDelegate.m */; };
0612F02923A8BE320079E622 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0612F02823A8BE320079E622 /* ViewController.m */; };
0612F02923A8BE320079E622 /* ViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0612F02823A8BE320079E622 /* ViewController.mm */; };
0612F03423A8BE330079E622 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 0612F03323A8BE330079E622 /* main.m */; };
0640D25824AC75DD00F122A0 /* UIView+AppearEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = 0640D25724AC75DD00F122A0 /* UIView+AppearEvent.m */; };
0649F42725A5ADE900E8F485 /* HippyBaseListViewCell.mm in Sources */ = {isa = PBXBuildFile; fileRef = 0649F42625A5ADE900E8F485 /* HippyBaseListViewCell.mm */; };
@@ -189,6 +189,7 @@
F417B5532727B31200894090 /* HippyDevCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F417B5522727B31200894090 /* HippyDevCommand.m */; };
F419C383274634E9003E242A /* taitank_layout_node.cc in Sources */ = {isa = PBXBuildFile; fileRef = F419C382274634E9003E242A /* taitank_layout_node.cc */; };
F419C38A27464047003E242A /* NativeRenderManager.mm in Sources */ = {isa = PBXBuildFile; fileRef = F419C38927464047003E242A /* NativeRenderManager.mm */; };
F41E3F6127859AB600BD3935 /* create_node.json in Resources */ = {isa = PBXBuildFile; fileRef = F41E3F6027859AB600BD3935 /* create_node.json */; };
F42EA79A277C354C007874A2 /* TurboConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = F42EA796277C354C007874A2 /* TurboConfig.m */; };
F42EA79B277C354C007874A2 /* TurboBaseModule.mm in Sources */ = {isa = PBXBuildFile; fileRef = F42EA799277C354C007874A2 /* TurboBaseModule.mm */; };
F43640BC275DE608007E1301 /* HippyBaseListShadowView.mm in Sources */ = {isa = PBXBuildFile; fileRef = F43640BB275DE608007E1301 /* HippyBaseListShadowView.mm */; };
@@ -233,7 +234,7 @@
0612F02123A8BE320079E622 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
0612F02223A8BE320079E622 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
0612F02723A8BE320079E622 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
0612F02823A8BE320079E622 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
0612F02823A8BE320079E622 /* ViewController.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ViewController.mm; sourceTree = "<group>"; };
0612F03223A8BE330079E622 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0612F03323A8BE330079E622 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
0640D25624AC75DD00F122A0 /* UIView+AppearEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIView+AppearEvent.h"; sourceTree = "<group>"; };
@@ -621,6 +622,7 @@
F419C382274634E9003E242A /* taitank_layout_node.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = taitank_layout_node.cc; sourceTree = "<group>"; };
F419C38927464047003E242A /* NativeRenderManager.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = NativeRenderManager.mm; sourceTree = "<group>"; };
F419C38B27464061003E242A /* NativeRenderManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NativeRenderManager.h; sourceTree = "<group>"; };
F41E3F6027859AB600BD3935 /* create_node.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = create_node.json; path = ../../../../../dom/test/create_node.json; sourceTree = "<group>"; };
F42EA796277C354C007874A2 /* TurboConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TurboConfig.m; sourceTree = "<group>"; };
F42EA797277C354C007874A2 /* TurboConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TurboConfig.h; sourceTree = "<group>"; };
F42EA798277C354C007874A2 /* TurboBaseModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TurboBaseModule.h; sourceTree = "<group>"; };
@@ -743,6 +745,7 @@
0612F02023A8BE320079E622 /* HippyDemo */ = {
isa = PBXGroup;
children = (
F41E3F6027859AB600BD3935 /* create_node.json */,
F42EA795277C354C007874A2 /* turbomodule */,
067AB97023B5F309009D5EE2 /* myview */,
067AB96E23B5EEF3009D5EE2 /* TestModule.h */,
@@ -750,7 +753,7 @@
0612F02123A8BE320079E622 /* AppDelegate.h */,
0612F02223A8BE320079E622 /* AppDelegate.m */,
0612F02723A8BE320079E622 /* ViewController.h */,
0612F02823A8BE320079E622 /* ViewController.m */,
0612F02823A8BE320079E622 /* ViewController.mm */,
0612F03223A8BE330079E622 /* Info.plist */,
0612F03323A8BE330079E622 /* main.m */,
);
@@ -1811,6 +1814,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F41E3F6127859AB600BD3935 /* create_node.json in Resources */,
AC63515623F92C99007FABA5 /* TTTGB-Medium.otf in Resources */,
064C5AF023AB1A70001E80DD /* res in Resources */,
);
@@ -1839,7 +1843,7 @@
064C5A4923AB1A51001E80DD /* HippyEventDispatcher.mm in Sources */,
F400C2E1274F41EC008BD74B /* ios_loader.cc in Sources */,
064C5A0123AB1A51001E80DD /* HippyExceptionModule.m in Sources */,
0612F02923A8BE320079E622 /* ViewController.m in Sources */,
0612F02923A8BE320079E622 /* ViewController.mm in Sources */,
0612F02323A8BE320079E622 /* AppDelegate.m in Sources */,
064C5A5123AB1A51001E80DD /* HippyJSCWrapper.mm in Sources */,
064C5A3A23AB1A51001E80DD /* HippyParserUtils.m in Sources */,
@@ -25,7 +25,15 @@
#import "HippyLog.h"
#import "HippyBundleURLProvider.h"
@interface ViewController ()<HippyBridgeDelegate>
#include "dom/dom_manager.h"
#include "NativeRenderManager.h"
#include "dom/dom_node.h"
#include "dom/dom_value.h"
@interface ViewController ()<HippyBridgeDelegate> {
std::shared_ptr<hippy::DomManager> _domManager;
std::shared_ptr<NativeRenderManager> _nativeRenderManager;
}
@end
@@ -38,14 +46,15 @@
HippySetLogFunction(^(HippyLogLevel level, HippyLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message) {
NSLog(@"hippy says:%@ in file %@ at line %@", message, fileName, lineNumber);
});
[self runCommonDemo];
// [self runDemoWithoutRuntime];
}
- (void)runCommonDemo {
BOOL isSimulator = NO;
#if TARGET_IPHONE_SIMULATOR
isSimulator = YES;
#endif
//release macro below if use debug mode
//#define HIPPYDEBUG
@@ -71,17 +80,85 @@
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];
#endif
// 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];
}
- (void)runDemoWithoutRuntime {
//step1: create HippyBridge and HippyRootView
HippyBridge *bridge = [[HippyBridge alloc] initWithmoduleProviderWithoutRuntime:nil];
HippyRootView *rootView = [[HippyRootView alloc] initWithBridgeButNoRuntime:bridge];
rootView.backgroundColor = [UIColor whiteColor];
rootView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
rootView.frame = self.view.bounds;
[self.view addSubview:rootView];
//step2: get HippyUIManager, then set DomManager for it.
//and set RenderManager for DomNanager
HippyUIManager *uiManager = [bridge moduleForName:@"UIManager"];
int32_t rootTag = [rootView.hippyTag intValue];
_domManager = std::make_shared<hippy::DomManager>(rootTag);
[uiManager setDomManager:_domManager];
_domManager->SetRootSize(CGRectGetWidth(rootView.bounds), CGRectGetHeight(rootView.bounds));
_nativeRenderManager = std::make_shared<NativeRenderManager>(uiManager);
_domManager->SetRenderManager(_nativeRenderManager);
//step3:create your nodes data into dommanager
auto nodesData = [self mockNodesData];
_domManager->CreateDomNodes(std::move(nodesData));
_domManager->BeginBatch();
_domManager->EndBatch();
}
- (std::vector<std::shared_ptr<hippy::DomNode>>) mockNodesData {
std::vector<std::shared_ptr<hippy::DomNode>> dom_node_vector;
using StyleMap = std::unordered_map<std::string, std::shared_ptr<tdf::base::DomValue>>;
NSString *mockDataPath = [[NSBundle mainBundle] pathForResource:@"create_node" ofType:@"json"];
NSData *mockData = [NSData dataWithContentsOfFile:mockDataPath];
NSArray<NSDictionary<NSString *, id> *> *mockJson = [NSJSONSerialization JSONObjectWithData:mockData options:(NSJSONReadingOptions)0 error:nil];
for (NSDictionary<NSString *, id> *mockNode in mockJson) {
uint32_t tag = 0;
uint32_t pid = 0;
uint32_t index = 0;
StyleMap style_map;
StyleMap ext_map;
std::string name;
for (NSString *key in mockNode) {
if ([key isEqualToString:@"id"]) {
tag = [mockNode[@"id"] unsignedIntValue];
}
else if ([key isEqualToString:@"pId"]) {
pid = [mockNode[@"pId"] unsignedIntValue];
}
else if ([key isEqualToString:@"index"]) {
index = [mockNode[@"index"] unsignedIntValue];
}
else if ([key isEqualToString:@"props"]) {
id props = mockNode[key];
auto all_props = dictionaryToUnorderedMapDomValue(props);
auto style_props = all_props["style"];
if (style_props) {
if (tdf::base::DomValue::Type::kObject == style_props->GetType()) {
auto style_object = style_props->ToObject();
for (auto iter = style_object.begin(); iter != style_object.end(); iter++) {
const std::string &iter_key = iter->first;
auto &iter_value = iter->second;
style_map[iter_key] = std::make_shared<tdf::base::DomValue>(std::move(iter_value));
}
}
all_props.erase("style");
}
ext_map.swap(all_props);
}
else if ([key isEqualToString:@"name"]) {
name = [mockNode[key] UTF8String];
}
}
std::shared_ptr<hippy::DomNode> dom_node = std::make_shared<hippy::DomNode>(tag, pid, index, name, name, std::move(style_map), std::move(ext_map), _domManager);
dom_node_vector.push_back(dom_node);
}
return dom_node_vector;
}
- (NSDictionary *)objectsBeforeExecuteCode {
+17 -15
View File
@@ -143,22 +143,24 @@ HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithDelegate
dispatch_group_enter(initModulesAndLoadSource);
__weak HippyBatchedBridge *weakSelf = self;
__block NSData *sourceCode;
[self loadSource:^(NSError *error, NSData *source, __unused int64_t sourceLength) {
if (error) {
HippyLogWarn(@"Failed to load source: %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf stopLoadingWithError:error];
});
}
if (self.bundleURL) {
[self loadSource:^(NSError *error, NSData *source, __unused int64_t sourceLength) {
if (error) {
HippyLogWarn(@"Failed to load source: %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf stopLoadingWithError:error];
});
}
sourceCode = source;
dispatch_group_leave(initModulesAndLoadSource);
} onProgress:^(HippyLoadingProgress *progressData) {
#ifdef HIPPY_DEV
HippyDevLoadingView *loadingView = [weakSelf moduleForClass:[HippyDevLoadingView class]];
[loadingView updateProgress:progressData];
#endif
}];
sourceCode = source;
dispatch_group_leave(initModulesAndLoadSource);
} onProgress:^(HippyLoadingProgress *progressData) {
#ifdef HIPPY_DEV
HippyDevLoadingView *loadingView = [weakSelf moduleForClass:[HippyDevLoadingView class]];
[loadingView updateProgress:progressData];
#endif
}];
}
// Synchronously initialize all native modules that cannot be loaded lazily
[self initModulesWithDispatchGroup:initModulesAndLoadSource];
+2
View File
@@ -90,6 +90,8 @@ HIPPY_EXTERN NSString *HippyBridgeModuleNameForClass(Class bridgeModuleClass);
*/
@interface HippyBridge : NSObject <HippyInvalidating>
- (instancetype)initWithmoduleProviderWithoutRuntime:(HippyBridgeModuleProviderBlock)block;
- (instancetype)initWithDelegate:(id<HippyBridgeDelegate>)delegate
bundleURL:(NSURL *)bundleURL
moduleProvider:(HippyBridgeModuleProviderBlock)block
+12
View File
@@ -207,6 +207,18 @@ static HippyBridge *HippyCurrentBridgeInstance = nil;
return self;
}
- (instancetype)initWithmoduleProviderWithoutRuntime:(HippyBridgeModuleProviderBlock)block {
self = [super init];
if (self) {
_moduleProvider = block;
[self setUp];
}
HippyExecuteOnMainQueue(^{
[self bindKeys];
});
return self;
}
HIPPY_NOT_IMPLEMENTED(-(instancetype)init)
- (void)dealloc {
+2
View File
@@ -85,6 +85,8 @@ extern NSString *const HippyContentDidAppearNotification;
debugMode:(BOOL)mode
delegate:(id<HippyRootViewDelegate>)delegate;
- (instancetype)initWithBridgeButNoRuntime:(HippyBridge *)bridge;
/**
* The name of the JavaScript module to execute within the
* specified scriptURL (required). Setting this will not have
+15 -1
View File
@@ -192,7 +192,17 @@ NSString *const HippyContentDidAppearNotification = @"HippyContentDidAppearNotif
}
}
HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
- (instancetype)initWithBridgeButNoRuntime:(HippyBridge *)bridge {
self = [super init];
if (self) {
self.bridge = bridge;
bridge.batchedBridge.useCommonBridge = YES;
[self bundleFinishedLoading:bridge.batchedBridge];
}
return self;
}
//HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
- (void)setBackgroundColor:(UIColor *)backgroundColor {
@@ -200,6 +210,10 @@ HIPPY_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
_contentView.backgroundColor = backgroundColor;
}
- (void)setBridge:(HippyBridge *)bridge {
_bridge = bridge;
}
- (UIViewController *)hippyViewController {
return _hippyViewController ?: [super hippyViewController];
}
+3
View File
@@ -135,6 +135,9 @@ HIPPY_EXPORT_METHOD(hide) {
UIColor *color;
UIColor *backgroundColor;
NSString *source;
if (!URL) {
return;
}
if (URL.fileURL) {
color = [UIColor grayColor];
backgroundColor = [UIColor blackColor];
+2
View File
@@ -32,6 +32,8 @@ HIPPY_EXTERN id domValueToOCType(const tdf::base::DomValue *const pDomValue);
HIPPY_EXTERN NSDictionary *unorderedMapDomValueToDictionary(const std::unordered_map<std::string, std::shared_ptr<tdf::base::DomValue>> &domValuesObject);
extern std::unordered_map<std::string, std::shared_ptr<tdf::base::DomValue>> dictionaryToUnorderedMapDomValue(NSDictionary *dictionary);
HIPPY_EXTERN CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result);
HIPPY_EXTERN UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result);
+61
View File
@@ -70,6 +70,56 @@ id domValueToOCType(const DomValue *const pDomValue) {
return value;
}
DomValue OCTypeToDomValue(id value) {
if ([value isKindOfClass:[NSString class]]) {
return DomValue([value UTF8String]);
}
else if ([value isKindOfClass:[NSNumber class]]) {
CFNumberRef numberRef = (__bridge CFNumberRef)value;
CFNumberType numberType = CFNumberGetType(numberRef);
if (kCFNumberSInt32Type == numberType ||
kCFNumberSInt64Type == numberType ||
kCFNumberShortType == numberType ||
kCFNumberIntType == numberType ||
kCFNumberLongType == numberType ||
kCFNumberLongLongType == numberType) {
return DomValue([value unsignedIntValue]);
}
else if (kCFNumberFloatType == numberType ||
kCFNumberDoubleType == numberType) {
return DomValue([value doubleValue]);
}
else {
BOOL flag = [value boolValue];
return DomValue(flag);;
}
}
else if (value == [NSNull null]) {
return DomValue::Null();
}
else if ([value isKindOfClass:[NSDictionary class]]) {
DomValue::DomValueObjectType object;
for (NSString *key in value) {
std::string objKey = [key UTF8String];
id objValue = [value objectForKey:key];
auto dom_obj = OCTypeToDomValue(objValue);
object[objKey] = std::move(dom_obj);
}
return DomValue(std::move(object));
}
else if ([value isKindOfClass:[NSArray class]]) {
DomValue::DomValueArrayType array;
for (id obj in value) {
auto dom_obj = OCTypeToDomValue(obj);
array.push_back(std::move(dom_obj));
}
return DomValue(std::move(array));
}
else {
return DomValue::Undefined();
}
}
NSDictionary *unorderedMapDomValueToDictionary(const std::unordered_map<std::string, std::shared_ptr<DomValue>> &domValuesObject) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:domValuesObject.size()];
for (auto it = domValuesObject.begin(); it != domValuesObject.end(); it++) {
@@ -81,6 +131,17 @@ NSDictionary *unorderedMapDomValueToDictionary(const std::unordered_map<std::str
return [dic copy];
}
std::unordered_map<std::string, std::shared_ptr<DomValue>> dictionaryToUnorderedMapDomValue(NSDictionary *dictionary) {
std::unordered_map<std::string, std::shared_ptr<tdf::base::DomValue>> style;
for (NSString *key in dictionary) {
id value = dictionary[key];
std::string style_key = [key UTF8String];
tdf::base::DomValue dom_value = OCTypeToDomValue(value);
style[style_key] = std::make_shared<tdf::base::DomValue>(std::move(dom_value));
}
return style;
}
CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result) {
return CGRectMake(result.left, result.top, result.width, result.height);
}