iOS:optimize devkit header ui

This commit is contained in:
pengfeizhou 2021-02-24 16:51:23 +08:00 committed by osborn
parent efdabd8309
commit dfcbec696e
4 changed files with 178 additions and 30 deletions

View File

@ -26,27 +26,131 @@
#import "DoricDevViewController.h" #import "DoricDevViewController.h"
#import "QRScanViewController.h" #import "QRScanViewController.h"
@interface DoricDevViewController () <UITableViewDelegate, UITableViewDataSource> @interface DoricDevViewController () <UITableViewDelegate, UITableViewDataSource, DoricDevStatusCallback>
@property(nonatomic, strong) UIView *headerView;
@property(nonatomic, strong) UILabel *tvLabel;
@property(nonatomic, strong) UILabel *tvConnection;
@property(nonatomic, strong) UILabel *tvInput;
@property(nonatomic, strong) UILabel *tvScan;
@property(nonatomic, strong) UILabel *tvDisconnect;
@property(nonatomic, strong) UITableView *listView;
@end @end
@implementation DoricDevViewController @implementation DoricDevViewController
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.title = @"Doric Devkit"; self.title = @"Doric Devkit";
[self.view addSubview:[[UITableView new] also:^(UITableView *it) { self.view.backgroundColor = UIColor.whiteColor;
self.headerView = [[UIView new] also:^(UIView *it) {
it.width = self.view.width; it.width = self.view.width;
it.height = self.view.height; it.height = 60;
it.left = it.top = 0; it.backgroundColor = DoricColor(@(0xff70a1ff));
it.dataSource = self; [self.view addSubview:it];
it.delegate = self; }];
}]]; self.tvLabel = [[UILabel new] also:^(UILabel *it) {
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"Disconnect" style:UIBarButtonItemStylePlain target:self action:@selector(onClose)]; it.text = @"Devkit";
self.navigationItem.rightBarButtonItem = rightBarItem; it.textColor = [UIColor whiteColor];
if ([[DoricDev instance] isInDevMode]) { it.font = [UIFont systemFontOfSize:30];
return; it.textAlignment = NSTextAlignmentCenter;
[it sizeToFit];
it.centerY = self.headerView.centerY;
it.left = 15;
[self.headerView addSubview:it];
}];
self.tvConnection = [[UILabel new] also:^(UILabel *it) {
it.text = @"Disconnected";
it.textColor = [UIColor whiteColor];
it.font = [UIFont systemFontOfSize:12];
it.textAlignment = NSTextAlignmentCenter;
[it sizeToFit];
[self.headerView addSubview:it];
}];
self.tvInput = [[UILabel new] also:^(UILabel *it) {
it.text = @"Input";
it.textColor = [UIColor whiteColor];
it.font = [UIFont systemFontOfSize:15];
it.textAlignment = NSTextAlignmentCenter;
[it sizeToFit];
it.width += 30;
it.height += 10;
it.backgroundColor = DoricColor(@(0xff1abc9c));
[self.headerView addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(input)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
self.tvScan = [[UILabel new] also:^(UILabel *it) {
it.text = @"Scan";
it.textColor = [UIColor whiteColor];
it.font = [UIFont systemFontOfSize:15];
it.textAlignment = NSTextAlignmentCenter;
[it sizeToFit];
it.width += 30;
it.height += 10;
it.backgroundColor = DoricColor(@(0xff1abc9c));
[self.headerView addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scan)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
self.tvDisconnect = [[UILabel new] also:^(UILabel *it) {
it.text = @"Disconnect";
it.textColor = [UIColor whiteColor];
it.font = [UIFont systemFontOfSize:15];
it.textAlignment = NSTextAlignmentCenter;
[it sizeToFit];
it.width += 30;
it.height += 10;
it.backgroundColor = DoricColor(@(0xff95a5a6));
[self.headerView addSubview:it];
it.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disconnect)];
[it addGestureRecognizer:tapGestureRecognizer];
}];
[self initHeaders];
[DoricDev.instance addStatusCallback:self];
}
- (void)disconnect {
[DoricDev.instance closeDevMode];
}
- (void)input {
if (self.isSimulator) {
NSString *result = @"127.0.0.1";
[[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]];
ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricGravityBottom);
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please input devkit ip"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder = @"192.168.1.1";
}];
__weak typeof(alert) _alert = alert;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:cancelAction];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
__strong typeof(_alert) alert = _alert;
NSString *ip = alert.textFields.lastObject.text;
[[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", ip]];
}];
[alert addAction:okAction];
[self presentViewController:alert animated:YES completion:nil];
} }
}
- (void)scan {
if (self.isSimulator) { if (self.isSimulator) {
NSString *result = @"127.0.0.1"; NSString *result = @"127.0.0.1";
[[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]];
@ -56,9 +160,40 @@ - (void)viewDidLoad {
} }
} }
- (void)onClose { - (void)initHeaders {
[[DoricDev instance] closeDevMode]; if (DoricDev.instance.isInDevMode) {
[self.navigationController popViewControllerAnimated:YES]; self.tvConnection.text = DoricDev.instance.ip;
self.tvInput.hidden = YES;
self.tvScan.hidden = YES;
self.tvDisconnect.hidden = NO;
self.tvConnection.backgroundColor = DoricColor(@(0xff2ed573));
[self.tvConnection sizeToFit];
self.tvConnection.width += 20;
self.tvConnection.height += 6;
self.tvConnection.left = self.tvLabel.right + 20;
self.tvDisconnect.right = self.view.width - 15;
self.tvConnection.centerY = self.headerView.centerY;
self.tvDisconnect.centerY = self.headerView.centerY;
} else {
self.tvConnection.text = @"Disconnected";
self.tvInput.hidden = NO;
self.tvScan.hidden = NO;
self.tvDisconnect.hidden = YES;
self.tvConnection.backgroundColor = DoricColor(@(0xffa4b0be));
[self.tvConnection sizeToFit];
self.tvConnection.width += 20;
self.tvConnection.height += 6;
self.tvConnection.left = self.tvLabel.right + 20;
self.tvScan.right = self.view.width - 15;
self.tvInput.right = self.tvScan.left - 10;
self.tvConnection.centerY = self.headerView.centerY;
self.tvScan.centerY = self.headerView.centerY;
self.tvInput.centerY = self.headerView.centerY;
}
} }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@ -93,4 +228,29 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}; };
} }
- (void)onOpen:(NSString *)url {
[self initHeaders];
}
- (void)onClose:(NSString *)url {
[self initHeaders];
}
- (void)onFailure:(NSError *)error {
[self initHeaders];
}
- (void)onReload:(DoricContext *)context script:(NSString *)script {
}
- (void)onStartDebugging:(DoricContext *)context {
}
- (void)onStopDebugging {
}
@end @end

View File

@ -45,7 +45,6 @@ - (instancetype)initWithUrl:(NSString *)url {
- (void)webSocketDidOpen:(SRWebSocket *)webSocket { - (void)webSocketDidOpen:(SRWebSocket *)webSocket {
DoricLog(@"webSocketDidOpen"); DoricLog(@"webSocketDidOpen");
[[NSNotificationCenter defaultCenter] postNotificationName:@"OpenEvent" object:nil];
[DoricDev.instance onOpen]; [DoricDev.instance onOpen];
} }
@ -83,18 +82,6 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
NSString *script = payload[@"script"]; NSString *script = payload[@"script"];
[DoricDev.instance reload:source script:script]; [DoricDev.instance reload:source script:script];
} }
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 (DoricContext *context in [[DoricContextManager instance] aliveContexts]) {
if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) {
[context reload:script];
}
}
}
} }
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
@ -138,5 +125,6 @@ - (void)sendToServer:(NSString *)cmd payload:(NSDictionary *)payload {
- (void)close { - (void)close {
[self.websocket close]; [self.websocket close];
[DoricDev.instance onClose];
} }
@end @end

View File

@ -39,7 +39,7 @@ @interface QRScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
@implementation QRScanViewController @implementation QRScanViewController
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.title = @"扫一扫"; self.title = @"Scan QRCode";
[self configBasicDevice]; [self configBasicDevice];
[self configPinchGes]; [self configPinchGes];
[self.session startRunning]; [self.session startRunning];

View File

@ -305,7 +305,7 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
} }
} }
- (void)onClick:(UIView *)view { - (void)onClick:(UITapGestureRecognizer *)recognizer {
[self callJSResponse:self.callbackIds[@"onClick"], nil]; [self callJSResponse:self.callbackIds[@"onClick"], nil];
} }