From ba2a90a6c16f6a495ce7e7c868b66c26c98ff6b0 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Tue, 13 Aug 2019 18:13:54 +0800 Subject: [PATCH] add some api for doric context --- .../penfeizhou/doricdemo/MainActivity.java | 1 - iOS/Example/Example/ViewController.m | 3 +- iOS/Pod/Classes/DoricContext.h | 11 ++++++++ iOS/Pod/Classes/DoricContext.m | 28 +++++++++++++++++++ iOS/Pod/Classes/Util/DoricConstant.h | 10 +++++++ iOS/Pod/Classes/Util/DoricConstant.m | 10 +++++++ 6 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java index 0b930451..d95c6481 100644 --- a/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java +++ b/Android/app/src/main/java/com/github/penfeizhou/doricdemo/MainActivity.java @@ -9,7 +9,6 @@ import com.github.penfeizhou.doric.Doric; import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.dev.LocalServer; import com.github.penfeizhou.doric.utils.DoricUtils; -import com.github.pengfeizhou.jscore.JSONBuilder; import java.io.IOException; diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 8d1a878f..53eaaadd 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -26,8 +26,7 @@ - (void)viewDidLoad { NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; self.doricContext = [[DoricContext alloc] initWithScript:jsContent source:@"Snake"]; self.doricContext.rootNode.view = self.view; - [self.doricContext callEntity:@"__init__",@{@"width": [NSNumber numberWithFloat:self.view.width], - @"height":[NSNumber numberWithFloat:self.view.height]},nil]; + [self.doricContext initContextWithWidth:self.view.width height:self.view.height]; } diff --git a/iOS/Pod/Classes/DoricContext.h b/iOS/Pod/Classes/DoricContext.h index 9bdb10ba..9c747761 100644 --- a/iOS/Pod/Classes/DoricContext.h +++ b/iOS/Pod/Classes/DoricContext.h @@ -18,6 +18,9 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic,strong) DoricDriver *driver; @property (nonatomic,strong) NSMutableDictionary *pluginInstanceMap; @property (nonatomic,strong) DoricRootNode *rootNode; +@property (nonatomic,strong) NSString *source; +@property (nonatomic,strong) NSString *script;; +@property (nonatomic,strong) NSDictionary *initialParams; - (instancetype)initWithScript:(NSString *)script source:(NSString *)source; @@ -27,6 +30,14 @@ NS_ASSUME_NONNULL_BEGIN - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args; +- (void)initContextWithWidth:(CGFloat)width height:(CGFloat)height; + +- (void)reload:(NSString *)script; + +- (void)onShow; + +- (void)onHidden; + @end NS_ASSUME_NONNULL_END diff --git a/iOS/Pod/Classes/DoricContext.m b/iOS/Pod/Classes/DoricContext.m index 395867c8..05c42e8e 100644 --- a/iOS/Pod/Classes/DoricContext.m +++ b/iOS/Pod/Classes/DoricContext.m @@ -8,6 +8,7 @@ #import "DoricContext.h" #import "DoricContextManager.h" #import "DoricRootNode.h" +#import "DoricConstant.h" @implementation DoricContext @@ -17,11 +18,16 @@ - (instancetype)initWithScript:(NSString *)script source:(NSString *)source { _pluginInstanceMap = [[NSMutableDictionary alloc] init]; [[DoricContextManager instance] createContext:self script:script source:source]; _rootNode = [[DoricRootNode alloc] initWithContext:self]; + _script = script; + _source = source; + _initialParams =[@{@"width":@(LAYOUT_MATCH_PARENT) ,@"height":@(LAYOUT_MATCH_PARENT)} mutableCopy]; + [self callEntity:DORIC_ENTITY_CREATE,nil]; } return self; } - (void)dealloc { + [self callEntity:DORIC_ENTITY_DESTROY,nil]; [[DoricContextManager instance] destroyContext:self]; } @@ -36,7 +42,29 @@ - (DoricAsyncResult *)callEntity:(NSString *)method, ... { - (DoricAsyncResult *)callEntity:(NSString *)method withArguments:(va_list)args { return [self.driver invokeContextEntity:self.contextId method:method arguments:args]; } + - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray *)args { return [self.driver invokeContextEntity:self.contextId method:method argumentsArray:args]; } + +- (void)initContextWithWidth:(CGFloat)width height:(CGFloat)height { + [self.initialParams setValue:@(width) forKey:@"width"]; + [self.initialParams setValue:@(height) forKey:@"height"]; + [self callEntity:DORIC_ENTITY_INIT,self.initialParams,nil]; +} + +- (void)reload:(NSString *)script { + self.script = script; + [self.driver createContext:self.contextId script:script source:self.source]; + [self callEntity:DORIC_ENTITY_INIT,self.initialParams,nil]; +} + +- (void)onShow { + [self callEntity:DORIC_ENTITY_SHOW,nil]; +} + +- (void)onHidden { + [self callEntity:DORIC_ENTITY_HIDDEN,nil]; +} + @end diff --git a/iOS/Pod/Classes/Util/DoricConstant.h b/iOS/Pod/Classes/Util/DoricConstant.h index c60f38e9..cec978ec 100644 --- a/iOS/Pod/Classes/Util/DoricConstant.h +++ b/iOS/Pod/Classes/Util/DoricConstant.h @@ -37,3 +37,13 @@ extern NSString * const DORIC_BRIDGE_RESOLVE; extern NSString * const DORIC_BRIDGE_REJECT; extern NSString * const DORIC_ENTITY_RESPONSE; + +extern NSString * const DORIC_ENTITY_INIT; + +extern NSString * const DORIC_ENTITY_CREATE; + +extern NSString * const DORIC_ENTITY_DESTROY; + +extern NSString * const DORIC_ENTITY_SHOW; + +extern NSString * const DORIC_ENTITY_HIDDEN; diff --git a/iOS/Pod/Classes/Util/DoricConstant.m b/iOS/Pod/Classes/Util/DoricConstant.m index b1bc7a5d..6eacfe64 100644 --- a/iOS/Pod/Classes/Util/DoricConstant.m +++ b/iOS/Pod/Classes/Util/DoricConstant.m @@ -54,3 +54,13 @@ NSString * const DORIC_BRIDGE_REJECT = @"jsCallReject"; NSString * const DORIC_ENTITY_RESPONSE = @"__response__"; + +NSString * const DORIC_ENTITY_INIT = @"__init__"; + +NSString * const DORIC_ENTITY_CREATE = @"__onCreate__"; + +NSString * const DORIC_ENTITY_DESTROY = @"__onDestroy__"; + +NSString * const DORIC_ENTITY_SHOW = @"__onShow__"; + +NSString * const DORIC_ENTITY_HIDDEN = @"__onHidden__";