diff --git a/doric-iOS/Devkit/Classes/DoricDev.h b/doric-iOS/Devkit/Classes/DoricDev.h index b8d1684e..c62b6e6b 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.h +++ b/doric-iOS/Devkit/Classes/DoricDev.h @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)connectDevKit:(NSString *)url; +- (void)sendDevCommand:(NSString *)command; + - (void)disconnectDevKit; @end diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index 970f9613..9f0d5888 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -51,6 +51,10 @@ - (void)connectDevKit:(NSString *)url { self.wsclient = [[DoricWSClient alloc] initWithUrl:url]; } +- (void)sendDevCommand:(NSString *)command { + [self.wsclient send:command]; +} + - (void)disconnectDevKit { if (self.wsclient) { [self.wsclient close]; diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.h b/doric-iOS/Devkit/Classes/DoricDevViewController.h new file mode 100644 index 00000000..81c3547c --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.h @@ -0,0 +1,29 @@ +/* + * 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. + */ +// +// DoricDevViewController.h +// Doric +// +// Created by jingpeng.wang on 2020/2/26. +// + +#import + +@interface DoricDevViewController : UIViewController + + +@end + diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m new file mode 100644 index 00000000..74960206 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -0,0 +1,81 @@ +/* + * 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. + */ +// +// DoricDevViewController.m +// Doric +// +// Created by jingpeng.wang on 2020/2/26. +// +#import +#import + +#import "DoricDev.h" +#import "DoricDevViewController.h" +#import "DoricJSRemoteExecutor.h" +#import "QRScanViewController.h" + +@interface DoricDevViewController () +@end + +@implementation DoricDevViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + self.title = @"Doric Devkit"; + [self.view addSubview:[[UITableView new] also:^(UITableView *it) { + it.width = self.view.width; + it.height = self.view.height; + it.left = it.top = 0; + it.dataSource = self; + it.delegate = self; + }]]; + + if (self.isSimulator) { + NSString *result = @"127.0.0.1"; + [DoricJSRemoteExecutor configIp:result]; + [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; + ShowToast([NSString stringWithFormat:@"Connected to %@", result], BOTTOM); + } else { + [self.navigationController pushViewController:[QRScanViewController new] animated:NO]; + } +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [DoricContextManager.instance aliveContexts].count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + NSValue *value = [DoricContextManager.instance aliveContexts][(NSUInteger) indexPath.row]; + DoricContext *context = value.nonretainedObjectValue; + NSString *path = context.source; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; + if (cell == nil) { + cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"]; + cell.selectionStyle = UITableViewCellSelectionStyleNone; + } + cell.textLabel.text = path; + return cell; +} + +- (BOOL)isSimulator { + return TARGET_OS_SIMULATOR == 1; +} + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { +} + +@end diff --git a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h index 65a34727..a4d5a630 100644 --- a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h +++ b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h @@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, strong) dispatch_semaphore_t semaphore; ++ (void)configIp:(NSString *)ip; + - (void)close; @end diff --git a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m index 8b35d198..83379e3c 100644 --- a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m +++ b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m @@ -25,7 +25,7 @@ #import "DoricJSRemoteArgType.h" #import "NSString+JsonString.h" -static NSString * const kUrlStr = @"ws://192.168.24.240:2080"; +static NSString *debuggerAddress = @""; typedef id (^Block0)(void); typedef id (^Block1)(id arg0); @@ -182,6 +182,10 @@ - (NSDictionary *)dicForArg:(id)arg { return dic; } ++ (void)configIp:(NSString *)ip { + debuggerAddress = [[@"ws://" stringByAppendingString:ip] stringByAppendingString:@":2080"]; +} + - (void)close { [self.srWebSocket close]; } @@ -189,7 +193,7 @@ - (void)close { #pragma mark - Properties - (SRWebSocket *)srWebSocket { if (!_srWebSocket) { - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kUrlStr] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; + NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:debuggerAddress] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; _srWebSocket = [[SRWebSocket alloc] initWithURLRequest:request]; _srWebSocket.delegate = self; [_srWebSocket open]; diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.h b/doric-iOS/Devkit/Classes/DoricWSClient.h index 4fb5afcd..63626027 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.h +++ b/doric-iOS/Devkit/Classes/DoricWSClient.h @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN @interface DoricWSClient : NSObject - (instancetype)initWithUrl:(NSString *)url; +- (void)send:(NSString *)command; + - (void)close; @end diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.m b/doric-iOS/Devkit/Classes/DoricWSClient.m index f927ef4e..28bf8206 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.m +++ b/doric-iOS/Devkit/Classes/DoricWSClient.m @@ -75,6 +75,10 @@ - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reas DoricLog(@"webSocketdidCloseWithCode"); } +- (void)send:(NSString *)command { + [_websocket send:command]; +} + - (void)close { [self.websocket close]; } diff --git a/doric-iOS/Devkit/Classes/QRScanViewController.m b/doric-iOS/Devkit/Classes/QRScanViewController.m index e3a406b2..a5ec5536 100644 --- a/doric-iOS/Devkit/Classes/QRScanViewController.m +++ b/doric-iOS/Devkit/Classes/QRScanViewController.m @@ -24,6 +24,7 @@ #import #import #import +#import "DoricJSRemoteExecutor.h" @interface QRScanViewController () @property(strong, nonatomic) AVCaptureDevice *device; @@ -104,6 +105,7 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects: NSString *result = qrObject.stringValue; NSLog(@"Scan result is %@", result); [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; + [DoricJSRemoteExecutor configIp:result]; ShowToast([NSString stringWithFormat:@"Connected to %@", result], BOTTOM); [self.navigationController popViewControllerAnimated:NO]; } diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index f0a18b3c..4050da8b 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -7,8 +7,8 @@ // #import -#import -#import + +#import #import "ViewController.h" @@ -68,13 +68,7 @@ - (BOOL)isSimulator { - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { - if (self.isSimulator) { - NSString *result = @"127.0.0.1"; - [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; - ShowToast([NSString stringWithFormat:@"Connected to %@", result], BOTTOM); - } else { - [self.navigationController pushViewController:[QRScanViewController new] animated:NO]; - } + [self.navigationController pushViewController:[DoricDevViewController new] animated:NO]; return; } NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row];