iOS:optimize API

This commit is contained in:
pengfei.zhou 2020-02-28 17:31:59 +08:00 committed by osborn
parent 7f1b1256ce
commit a2f2e74380
6 changed files with 104 additions and 25 deletions

View File

@ -25,11 +25,16 @@ NS_ASSUME_NONNULL_BEGIN
@interface DoricDev : NSObject
+ (instancetype)instance;
- (void)openDevMode;
- (void)closeDevMode;
- (BOOL)isInDevMode;
- (void)connectDevKit:(NSString *)url;
- (void)sendDevCommand:(NSString *)command;
- (void)disconnectDevKit;
@end
NS_ASSUME_NONNULL_END

View File

@ -27,6 +27,7 @@
#import "DoricDev.h"
#import "DoricWSClient.h"
#import "DoricDebugDriver.h"
#import "DoricDevViewController.h"
@interface DoricDev ()
@property(nonatomic, strong) DoricWSClient *wsclient;
@ -58,6 +59,30 @@ + (instancetype)instance {
return _instance;
}
- (void)openDevMode {
DoricDevViewController *devViewController = [DoricDevViewController new];
UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
UINavigationController *navigationController;
if ([viewController isKindOfClass:[UINavigationController class]]) {
navigationController = (UINavigationController *) viewController;
} else {
navigationController = viewController.navigationController;
}
[navigationController pushViewController:devViewController animated:NO];
}
- (void)closeDevMode {
if (self.wsclient) {
[self.wsclient close];
self.wsclient = nil;
}
}
- (BOOL)isInDevMode {
return self.wsclient != nil;
}
- (void)connectDevKit:(NSString *)url {
if (self.wsclient) {
[self.wsclient close];
@ -69,13 +94,6 @@ - (void)sendDevCommand:(NSString *)command {
[self.wsclient send:command];
}
- (void)disconnectDevKit {
if (self.wsclient) {
[self.wsclient close];
self.wsclient = nil;
}
}
- (void)onOpenEvent {
ShowToast(@"dev kit connected", BOTTOM);
}

View File

@ -35,7 +35,7 @@ @implementation DoricDevViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Doric Devkit";
[self.view addSubview:[[UITableView new] also:^(UITableView *it) {
it.width = self.view.width;
@ -44,7 +44,9 @@ - (void)viewDidLoad {
it.dataSource = self;
it.delegate = self;
}]];
if ([[DoricDev instance] isInDevMode]) {
return;
}
if (self.isSimulator) {
NSString *result = @"127.0.0.1";
[DoricJSRemoteExecutor configIp:result];
@ -81,13 +83,13 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
DoricContext *context = value.nonretainedObjectValue;
[[NSNotificationCenter defaultCenter] postNotificationName:@"StartDebugEvent" object:context.contextId];
NSDictionary *jsonDic = @{
@"cmd": @"DEBUG",
@"data": @{
@"contextId": context.contextId,
@"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"]
}
@"cmd": @"DEBUG",
@"data": @{
@"contextId": context.contextId,
@"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"]
}
};
NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic];
[[DoricDev instance] sendDevCommand:jsonStr];
}

View File

@ -62,14 +62,14 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
if ([cmd compare:@"SWITCH_TO_DEBUG"] == NSOrderedSame) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"EnterDebugEvent" object:nil];
} else if ([cmd compare:@"RELOAD"] == NSOrderedSame) {
// NSString *source = [[dic valueForKey:@"source"] mutableCopy];
// NSString *script = [dic valueForKey:@"script"];
// for (NSValue *value in [[DoricContextManager instance] aliveContexts]) {
// DoricContext *context = value.nonretainedObjectValue;
// if ([source containsString:context.source]) {
// [context reload:script];
// }
// }
NSString *source = [[dic valueForKey:@"source"] mutableCopy];
NSString *script = [dic valueForKey:@"script"];
for (NSValue *value in [[DoricContextManager instance] aliveContexts]) {
DoricContext *context = value.nonretainedObjectValue;
if ([source containsString:context.source]) {
[context reload:script];
}
}
}
}

View File

@ -13,6 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Doric.m
// Doric
//
// Created by pengfei.zhou on 2020/2/28.
//
#import "DoricContext.h"
#import "DoricLayouts.h"
#import "DoricExtensions.h"
@ -28,4 +35,18 @@
#import "DoricPromise.h"
#import "DoricLibrary.h"
#import "DoricNativePlugin.h"
#import "DoricMonitorProtocol.h"
#import "DoricMonitorProtocol.h"
@interface Doric : NSObject
/**
* Register DoricLibrary For Extended ViewNode And Native Plugins
* */
+ (void)registerLibrary:(DoricLibrary *)library;
/**
* Add DoricJSLoader For Loading JS Bundles
* */
+ (void)addJSLoader:(id <DoricLoaderProtocol>)loader;
@end

View File

@ -0,0 +1,33 @@
/*
* Copyright [2019] [Doric.Pub]
*
* 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.
*/
//
// Doric.m
// Doric
//
// Created by pengfei.zhou on 2020/2/28.
//
#import "Doric.h"
@implementation Doric
+ (void)registerLibrary:(DoricLibrary *)library {
[DoricRegistry register:library];
}
+ (void)addJSLoader:(id <DoricLoaderProtocol>)loader {
[[DoricJSLoaderManager instance] addJSLoader:loader];
}
@end