refact some code

This commit is contained in:
pengfei.zhou 2020-04-03 10:19:13 +08:00 committed by osborn
parent 00f5fb3918
commit ca6a3284f2
9 changed files with 55 additions and 60 deletions

View File

@ -95,20 +95,20 @@ - (void)sendDevCommand:(NSString *)command {
} }
- (void)onOpenEvent { - (void)onOpenEvent {
ShowToast(@"dev kit connected", DoricBottom); ShowToast(@"dev kit connected", DoricGravityBottom);
} }
- (void)onEOFEvent { - (void)onEOFEvent {
ShowToast(@"dev kit eof exception", DoricBottom); ShowToast(@"dev kit eof exception", DoricGravityBottom);
} }
- (void)onConnectExceptionEvent { - (void)onConnectExceptionEvent {
ShowToast(@"dev kit connection exception", DoricBottom); ShowToast(@"dev kit connection exception", DoricGravityBottom);
} }
- (void)onStartDebugEvent:(NSNotification *)notification { - (void)onStartDebugEvent:(NSNotification *)notification {
NSString *contextId = notification.object; NSString *contextId = notification.object;
ShowToast(contextId, DoricBottom); ShowToast(contextId, DoricGravityBottom);
for (NSValue *value in [[DoricContextManager instance] aliveContexts]) { for (NSValue *value in [[DoricContextManager instance] aliveContexts]) {
DoricContext *context = value.nonretainedObjectValue; DoricContext *context = value.nonretainedObjectValue;
BOOL result = [context.contextId compare:contextId] == NSOrderedSame; BOOL result = [context.contextId compare:contextId] == NSOrderedSame;

View File

@ -53,7 +53,7 @@ - (void)viewDidLoad {
NSString *result = @"127.0.0.1"; NSString *result = @"127.0.0.1";
[DoricJSRemoteExecutor configIp:result]; [DoricJSRemoteExecutor configIp:result];
[[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]];
ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricBottom); ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricGravityBottom);
} else { } else {
[self.navigationController pushViewController:[QRScanViewController new] animated:NO]; [self.navigationController pushViewController:[QRScanViewController new] animated:NO];
} }

View File

@ -106,7 +106,7 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:
NSLog(@"Scan result is %@", result); NSLog(@"Scan result is %@", result);
[[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]];
[DoricJSRemoteExecutor configIp:result]; [DoricJSRemoteExecutor configIp:result];
ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricBottom); ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricGravityBottom);
[self.navigationController popViewControllerAnimated:NO]; [self.navigationController popViewControllerAnimated:NO];
} }
} }

View File

@ -12,7 +12,7 @@ @interface DoricDemoPlugin : DoricNativePlugin
@implementation DoricDemoPlugin @implementation DoricDemoPlugin
- (void)test { - (void)test {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
ShowToast(@"Test called", DoricCenter); ShowToast(@"Test called", DoricGravityCenter);
}); });
} }
@end @end

View File

@ -28,7 +28,7 @@ @implementation DoricModalPlugin
- (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise { - (void)toast:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
__block DoricGravity gravity = DoricBottom; __block DoricGravity gravity = DoricGravityBottom;
[dic[@"gravity"] also:^(NSNumber *it) { [dic[@"gravity"] also:^(NSNumber *it) {
gravity = (DoricGravity) [it integerValue]; gravity = (DoricGravity) [it integerValue];
}]; }];

View File

@ -36,27 +36,27 @@ - (CGSize)measureSize:(CGSize)targetSize {
if (!config) { if (!config) {
config = [DoricLayoutConfig new]; config = [DoricLayoutConfig new];
} }
if (config.widthSpec == DoricLayoutAtMost if (config.widthSpec == DoricLayoutMost
|| config.widthSpec == DoricLayoutWrapContent) { || config.widthSpec == DoricLayoutFit) {
width = targetSize.width - config.margin.left - config.margin.right; width = targetSize.width - config.margin.left - config.margin.right;
} }
if (config.heightSpec == DoricLayoutAtMost if (config.heightSpec == DoricLayoutMost
|| config.heightSpec == DoricLayoutWrapContent) { || config.heightSpec == DoricLayoutFit) {
height = targetSize.height - config.margin.top - config.margin.bottom; height = targetSize.height - config.margin.top - config.margin.bottom;
} }
DoricPadding padding = self.padding; DoricPadding padding = self.padding;
CGSize contentSize = [self sizeThatFits:CGSizeMake( CGSize contentSize = [self sizeThatFits:CGSizeMake(
width - padding.left - padding.right, width - padding.left - padding.right,
height - padding.top - padding.bottom)]; height - padding.top - padding.bottom)];
if (config.widthSpec == DoricLayoutWrapContent) { if (config.widthSpec == DoricLayoutFit) {
width = contentSize.width + padding.left + padding.right; width = contentSize.width + padding.left + padding.right;
if (config.heightSpec != DoricLayoutWrapContent && contentSize.width != 0 && contentSize.height != 0) { if (config.heightSpec != DoricLayoutFit && contentSize.width != 0 && contentSize.height != 0) {
width = contentSize.width / contentSize.height * height + padding.left + padding.right; width = contentSize.width / contentSize.height * height + padding.left + padding.right;
} }
} }
if (config.heightSpec == DoricLayoutWrapContent) { if (config.heightSpec == DoricLayoutFit) {
height = contentSize.height + padding.top + padding.bottom; height = contentSize.height + padding.top + padding.bottom;
if (config.widthSpec != DoricLayoutWrapContent && contentSize.width != 0 && contentSize.height != 0) { if (config.widthSpec != DoricLayoutFit && contentSize.width != 0 && contentSize.height != 0) {
height = contentSize.height / contentSize.width * width + padding.top + padding.bottom; height = contentSize.height / contentSize.width * width + padding.top + padding.bottom;
} }
} }

View File

@ -27,9 +27,9 @@ typedef UIEdgeInsets DoricPadding;
DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom); DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom);
typedef NS_ENUM(NSInteger, DoricLayoutSpec) { typedef NS_ENUM(NSInteger, DoricLayoutSpec) {
DoricLayoutExact = 0, DoricLayoutJust = 0,
DoricLayoutWrapContent = 1, DoricLayoutFit = 1,
DoricLayoutAtMost = 2, DoricLayoutMost = 2,
}; };
typedef NS_ENUM(NSInteger, DoricGravity) { typedef NS_ENUM(NSInteger, DoricGravity) {
@ -40,11 +40,11 @@ typedef NS_ENUM(NSInteger, DoricGravity) {
DoricGravityShiftY = 4, DoricGravityShiftY = 4,
DoricGravityLeft = (DoricGravityStart | DoricGravitySpecified) << DoricGravityShiftX, DoricGravityLeft = (DoricGravityStart | DoricGravitySpecified) << DoricGravityShiftX,
DoricGravityRight = (DoricGravityEnd | DoricGravitySpecified) << DoricGravityShiftX, DoricGravityRight = (DoricGravityEnd | DoricGravitySpecified) << DoricGravityShiftX,
DoricTOP = (DoricGravityStart | DoricGravitySpecified) << DoricGravityShiftY, DoricGravityTOP = (DoricGravityStart | DoricGravitySpecified) << DoricGravityShiftY,
DoricBottom = (DoricGravityEnd | DoricGravitySpecified) << DoricGravityShiftY, DoricGravityBottom = (DoricGravityEnd | DoricGravitySpecified) << DoricGravityShiftY,
DoricCenterX = DoricGravitySpecified << DoricGravityShiftX, DoricGravityCenterX = DoricGravitySpecified << DoricGravityShiftX,
DoricCenterY = DoricGravitySpecified << DoricGravityShiftY, DoricGravityCenterY = DoricGravitySpecified << DoricGravityShiftY,
DoricCenter = DoricCenterX | DoricCenterY, DoricGravityCenter = DoricGravityCenterX | DoricGravityCenterY,
}; };
@interface DoricLayoutConfig : NSObject @interface DoricLayoutConfig : NSObject

View File

@ -88,22 +88,22 @@ - (CGSize)measureSize:(CGSize)targetSize {
if (!config) { if (!config) {
config = [DoricLayoutConfig new]; config = [DoricLayoutConfig new];
} }
if (config.widthSpec == DoricLayoutAtMost if (config.widthSpec == DoricLayoutMost
|| config.widthSpec == DoricLayoutWrapContent) { || config.widthSpec == DoricLayoutFit) {
width = targetSize.width - config.margin.left - config.margin.right; width = targetSize.width - config.margin.left - config.margin.right;
} }
if (config.heightSpec == DoricLayoutAtMost if (config.heightSpec == DoricLayoutMost
|| config.heightSpec == DoricLayoutWrapContent) { || config.heightSpec == DoricLayoutFit) {
height = targetSize.height - config.margin.top - config.margin.bottom; height = targetSize.height - config.margin.top - config.margin.bottom;
} }
DoricPadding padding = self.padding; DoricPadding padding = self.padding;
CGSize contentSize = [self sizeThatFits:CGSizeMake( CGSize contentSize = [self sizeThatFits:CGSizeMake(
width - padding.left - padding.right, width - padding.left - padding.right,
height - padding.top - padding.bottom)]; height - padding.top - padding.bottom)];
if (config.widthSpec == DoricLayoutWrapContent) { if (config.widthSpec == DoricLayoutFit) {
width = contentSize.width + padding.left + padding.right; width = contentSize.width + padding.left + padding.right;
} }
if (config.heightSpec == DoricLayoutWrapContent) { if (config.heightSpec == DoricLayoutFit) {
height = contentSize.height + padding.top + padding.bottom; height = contentSize.height + padding.top + padding.bottom;
} }
if (config.weight) { if (config.weight) {
@ -139,8 +139,8 @@ - (void)doricLayoutSubviews {
- (BOOL)requestFromSubview:(UIView *)subview { - (BOOL)requestFromSubview:(UIView *)subview {
if (self.layoutConfig if (self.layoutConfig
&& self.layoutConfig.widthSpec != DoricLayoutExact && self.layoutConfig.widthSpec != DoricLayoutJust
&& self.layoutConfig.heightSpec != DoricLayoutExact) { && self.layoutConfig.heightSpec != DoricLayoutJust) {
return YES; return YES;
} }
return NO; return NO;
@ -148,19 +148,14 @@ - (BOOL)requestFromSubview:(UIView *)subview {
@end @end
DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) { DoricMargin DoricMarginMake(CGFloat left, CGFloat top, CGFloat right, CGFloat bottom) {
DoricMargin margin; return UIEdgeInsetsMake(top, left, bottom, right);
margin.left = left;
margin.top = top;
margin.right = right;
margin.bottom = bottom;
return margin;
} }
@implementation DoricLayoutConfig @implementation DoricLayoutConfig
- (instancetype)init { - (instancetype)init {
if (self = [super init]) { if (self = [super init]) {
_widthSpec = DoricLayoutExact; _widthSpec = DoricLayoutJust;
_heightSpec = DoricLayoutExact; _heightSpec = DoricLayoutJust;
} }
return self; return self;
} }
@ -259,18 +254,18 @@ - (void)layoutSelf:(CGSize)targetSize {
point.x = padding.left; point.x = padding.left;
} else if ((gravity & DoricGravityRight) == DoricGravityRight) { } else if ((gravity & DoricGravityRight) == DoricGravityRight) {
point.x = targetSize.width - padding.right - child.width; point.x = targetSize.width - padding.right - child.width;
} else if ((gravity & DoricCenterX) == DoricCenterX) { } else if ((gravity & DoricGravityCenterX) == DoricGravityCenterX) {
point.x = targetSize.width / 2 - child.width / 2; point.x = targetSize.width / 2 - child.width / 2;
} else { } else {
if (childConfig.margin.left || childConfig.margin.right) { if (childConfig.margin.left || childConfig.margin.right) {
point.x = padding.left; point.x = padding.left;
} }
} }
if ((gravity & DoricTOP) == DoricTOP) { if ((gravity & DoricGravityTOP) == DoricGravityTOP) {
point.y = padding.top; point.y = padding.top;
} else if ((gravity & DoricBottom) == DoricBottom) { } else if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
point.y = targetSize.height - padding.bottom - child.height; point.y = targetSize.height - padding.bottom - child.height;
} else if ((gravity & DoricCenterY) == DoricCenterY) { } else if ((gravity & DoricGravityCenterY) == DoricGravityCenterY) {
point.y = targetSize.height / 2 - child.height / 2; point.y = targetSize.height / 2 - child.height / 2;
} else { } else {
if (childConfig.margin.top || childConfig.margin.bottom) { if (childConfig.margin.top || childConfig.margin.bottom) {
@ -279,7 +274,7 @@ - (void)layoutSelf:(CGSize)targetSize {
} }
if (!gravity) { if (!gravity) {
gravity = DoricGravityLeft | DoricTOP; gravity = DoricGravityLeft | DoricGravityTOP;
} }
if (childConfig.margin.left && !((gravity & DoricGravityRight) == DoricGravityRight)) { if (childConfig.margin.left && !((gravity & DoricGravityRight) == DoricGravityRight)) {
point.x += childConfig.margin.left; point.x += childConfig.margin.left;
@ -287,10 +282,10 @@ - (void)layoutSelf:(CGSize)targetSize {
if (childConfig.margin.right && !((gravity & DoricGravityLeft) == DoricGravityLeft)) { if (childConfig.margin.right && !((gravity & DoricGravityLeft) == DoricGravityLeft)) {
point.x -= childConfig.margin.right; point.x -= childConfig.margin.right;
} }
if (childConfig.margin.top && !((gravity & DoricBottom) == DoricBottom)) { if (childConfig.margin.top && !((gravity & DoricGravityBottom) == DoricGravityBottom)) {
point.y += childConfig.margin.top; point.y += childConfig.margin.top;
} }
if (childConfig.margin.bottom && !((gravity & DoricTOP) == DoricTOP)) { if (childConfig.margin.bottom && !((gravity & DoricGravityTOP) == DoricGravityTOP)) {
point.y -= childConfig.margin.bottom; point.y -= childConfig.margin.bottom;
} }
if (point.x != child.x) { if (point.x != child.x) {
@ -345,11 +340,11 @@ - (void)layoutSelf:(CGSize)targetSize {
self.height = targetSize.height; self.height = targetSize.height;
DoricPadding padding = self.padding; DoricPadding padding = self.padding;
CGFloat yStart = padding.top; CGFloat yStart = padding.top;
if ((self.gravity & DoricTOP) == DoricTOP) { if ((self.gravity & DoricGravityTOP) == DoricGravityTOP) {
yStart = padding.top; yStart = padding.top;
} else if ((self.gravity & DoricBottom) == DoricBottom) { } else if ((self.gravity & DoricGravityBottom) == DoricGravityBottom) {
yStart = targetSize.height - self.contentHeight - padding.bottom; yStart = targetSize.height - self.contentHeight - padding.bottom;
} else if ((self.gravity & DoricCenterY) == DoricCenterY) { } else if ((self.gravity & DoricGravityCenterY) == DoricGravityCenterY) {
yStart = (targetSize.height - self.contentHeight - padding.top - padding.bottom) / 2 + padding.top; yStart = (targetSize.height - self.contentHeight - padding.top - padding.bottom) / 2 + padding.top;
} }
CGFloat remain = targetSize.height - self.contentHeight - padding.top - padding.bottom; CGFloat remain = targetSize.height - self.contentHeight - padding.top - padding.bottom;
@ -378,7 +373,7 @@ - (void)layoutSelf:(CGSize)targetSize {
point.x = padding.left; point.x = padding.left;
} else if ((gravity & DoricGravityRight) == DoricGravityRight) { } else if ((gravity & DoricGravityRight) == DoricGravityRight) {
point.x = targetSize.width - padding.right - child.width; point.x = targetSize.width - padding.right - child.width;
} else if ((gravity & DoricCenterX) == DoricCenterX) { } else if ((gravity & DoricGravityCenterX) == DoricGravityCenterX) {
point.x = targetSize.width / 2 - child.width / 2; point.x = targetSize.width / 2 - child.width / 2;
} else { } else {
point.x = padding.left; point.x = padding.left;
@ -451,7 +446,7 @@ - (void)layoutSelf:(CGSize)targetSize {
xStart = padding.left; xStart = padding.left;
} else if ((self.gravity & DoricGravityRight) == DoricGravityRight) { } else if ((self.gravity & DoricGravityRight) == DoricGravityRight) {
xStart = targetSize.width - self.contentWidth - padding.right; xStart = targetSize.width - self.contentWidth - padding.right;
} else if ((self.gravity & DoricCenterX) == DoricCenterX) { } else if ((self.gravity & DoricGravityCenterX) == DoricGravityCenterX) {
xStart = (targetSize.width - self.contentWidth - padding.left - padding.right) / 2 + padding.left; xStart = (targetSize.width - self.contentWidth - padding.left - padding.right) / 2 + padding.left;
} }
CGFloat remain = targetSize.width - self.contentWidth - padding.left - padding.right; CGFloat remain = targetSize.width - self.contentWidth - padding.left - padding.right;
@ -479,22 +474,22 @@ - (void)layoutSelf:(CGSize)targetSize {
DoricGravity gravity = childConfig.alignment | self.gravity; DoricGravity gravity = childConfig.alignment | self.gravity;
CGPoint point = child.frame.origin; CGPoint point = child.frame.origin;
if ((gravity & DoricTOP) == DoricTOP) { if ((gravity & DoricGravityTOP) == DoricGravityTOP) {
point.y = padding.top; point.y = padding.top;
} else if ((gravity & DoricBottom) == DoricBottom) { } else if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
point.y = targetSize.height - padding.bottom - child.height; point.y = targetSize.height - padding.bottom - child.height;
} else if ((gravity & DoricCenterY) == DoricCenterY) { } else if ((gravity & DoricGravityCenterY) == DoricGravityCenterY) {
point.y = targetSize.height / 2 - child.height / 2; point.y = targetSize.height / 2 - child.height / 2;
} else { } else {
point.y = padding.top; point.y = padding.top;
} }
if (!gravity) { if (!gravity) {
gravity = DoricTOP; gravity = DoricGravityTOP;
} }
if (childConfig.margin.top && !((gravity & DoricBottom) == DoricBottom)) { if (childConfig.margin.top && !((gravity & DoricGravityBottom) == DoricGravityBottom)) {
point.y += childConfig.margin.top; point.y += childConfig.margin.top;
} }
if (childConfig.margin.bottom && !((gravity & DoricTOP) == DoricTOP)) { if (childConfig.margin.bottom && !((gravity & DoricGravityTOP) == DoricGravityTOP)) {
point.y -= childConfig.margin.bottom; point.y -= childConfig.margin.bottom;
} }
if (point.y != child.y) { if (point.y != child.y) {

View File

@ -71,9 +71,9 @@ void ShowToast(NSString *text, DoricGravity gravity) {
label.height += 10; label.height += 10;
label.layer.cornerRadius = label.height / 2; label.layer.cornerRadius = label.height / 2;
label.centerX = superView.width / 2; label.centerX = superView.width / 2;
if ((gravity & DoricBottom) == DoricBottom) { if ((gravity & DoricGravityBottom) == DoricGravityBottom) {
label.bottom = superView.height - 20; label.bottom = superView.height - 20;
} else if ((gravity & DoricTOP) == DoricTOP) { } else if ((gravity & DoricGravityTOP) == DoricGravityTOP) {
label.top = 108; label.top = 108;
} else { } else {
label.centerY = (superView.height - 88) / 2; label.centerY = (superView.height - 88) / 2;