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 {
ShowToast(@"dev kit connected", DoricBottom);
ShowToast(@"dev kit connected", DoricGravityBottom);
}
- (void)onEOFEvent {
ShowToast(@"dev kit eof exception", DoricBottom);
ShowToast(@"dev kit eof exception", DoricGravityBottom);
}
- (void)onConnectExceptionEvent {
ShowToast(@"dev kit connection exception", DoricBottom);
ShowToast(@"dev kit connection exception", DoricGravityBottom);
}
- (void)onStartDebugEvent:(NSNotification *)notification {
NSString *contextId = notification.object;
ShowToast(contextId, DoricBottom);
ShowToast(contextId, DoricGravityBottom);
for (NSValue *value in [[DoricContextManager instance] aliveContexts]) {
DoricContext *context = value.nonretainedObjectValue;
BOOL result = [context.contextId compare:contextId] == NSOrderedSame;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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