feat:iOS implement coordinator plugin

This commit is contained in:
pengfei.zhou 2020-02-13 22:18:13 +08:00 committed by osborn
parent 630ec51504
commit 6bf61b3769
15 changed files with 226 additions and 24 deletions

View File

@ -180,6 +180,10 @@ public class CoordinatorPlugin extends DoricJavaPlugin {
viewNode.setWidth(value);
} else if ("height".equals(name)) {
viewNode.setHeight(value);
} else if ("x".equals(name)) {
viewNode.setX(value);
} else if ("y".equals(name)) {
viewNode.setY(value);
}
}

View File

@ -1,4 +1,4 @@
import { Group, Panel, List, text, gravity, Color, Stack, LayoutSpec, list, NativeCall, listItem, log, vlayout, Gravity, hlayout, Text, scroller, layoutConfig, image, IView, IVLayout, ScaleType, Image } from "doric";
import { Group, Panel, coordinator, text, gravity, Color, Stack, LayoutSpec, list, NativeCall, listItem, log, vlayout, Gravity, hlayout, Text, scroller, layoutConfig, image, IView, IVLayout, ScaleType, Image } from "doric";
import { colors, label } from "./utils";
import { img_base64 } from "./image_base64";
const imageUrl = 'https://img.zcool.cn/community/01e75b5da933daa801209e1ffa4649.jpg@1280w_1l_2o_100sh.jpg'
@ -43,12 +43,15 @@ class ImageDemo extends Panel {
label('WebP'),
imageView = image({
imageUrl: "https://p.upyun.com/demo/webp/webp/jpg-0.webp",
loadCallback: (ret) => {
if (ret) {
imageView.width = ret.width
imageView.height = ret.height
}
}
layoutConfig: layoutConfig().just(),
width: 200,
height: 200,
// loadCallback: (ret) => {
// if (ret) {
// imageView.width = ret.width
// imageView.height = ret.height
// }
// }
}),
label('ScaleToFill'),
image({
@ -110,6 +113,20 @@ class ImageDemo extends Panel {
{
layoutConfig: layoutConfig().most(),
}
).in(rootView)
).also(it => {
coordinator(context).verticalScrolling({
scrollable: it,
scrollRange: {
start: 0,
end: 100,
},
target: "NavBar",
changing: {
name: "backgroundColor",
start: Color.WHITE,
end: Color.RED,
}
})
}).in(rootView)
}
}

View File

@ -28,5 +28,4 @@
#import "DoricPromise.h"
#import "DoricLibrary.h"
#import "DoricNativePlugin.h"
#import "DoricMonitorProtocol.h"
#import "DoricScrollViewDelegate.h"
#import "DoricMonitorProtocol.h"

View File

@ -49,6 +49,7 @@
#import "DoricNotificationPlugin.h"
#import "DoricStatusBarPlugin.h"
#import "DoricUtil.h"
#import "DoricCoordinatorPlugin.h"
@interface DoricLibraries : NSObject
@property(nonatomic, strong) NSMutableSet <DoricLibrary *> *libraries;
@ -130,6 +131,7 @@ - (void)innerRegister {
[self registerNativePlugin:DoricAnimatePlugin.class withName:@"animate"];
[self registerNativePlugin:DoricNotificationPlugin.class withName:@"notification"];
[self registerNativePlugin:DoricStatusBarPlugin.class withName:@"statusbar"];
[self registerNativePlugin:DoricCoordinatorPlugin.class withName:@"coordinator"];
[self registerViewNode:DoricStackNode.class withName:@"Stack"];
[self registerViewNode:DoricVLayoutNode.class withName:@"VLayout"];

View File

@ -0,0 +1,10 @@
//
// Created by pengfei.zhou on 2020/2/13.
//
#import <Foundation/Foundation.h>
#import "DoricScrollableProtocol.h"
@protocol DoricScrollableProtocol <NSObject>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end

View File

@ -1,3 +1,18 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2019/11/29.
//

View File

@ -1,3 +1,18 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2019/11/29.
//

View File

@ -18,7 +18,7 @@
//
#import <Foundation/Foundation.h>
#import "DoricNativePlugin.h"
@protocol DoricScrollViewDelegate <NSObject>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
@interface DoricCoordinatorPlugin : DoricNativePlugin
@end

View File

@ -0,0 +1,133 @@
/*
* 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.
*/
//
// Created by pengfei.zhou on 2020/2/13.
//
#import "DoricCoordinatorPlugin.h"
#import "DoricSuperNode.h"
#import "DoricScrollableProtocol.h"
#import "DoricUtil.h"
@implementation DoricCoordinatorPlugin
- (void)ready:(id)param withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[promise resolve:nil];
});
});
}
- (void)verticalScrolling:(NSDictionary *)params withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
NSArray <NSString *> *scrollableIds = params[@"scrollable"];
DoricViewNode *scrollNode = nil;
for (NSString *value in scrollableIds) {
if (!scrollNode) {
scrollNode = [self.doricContext targetViewNode:value];
} else {
if ([scrollNode isKindOfClass:[DoricSuperNode class]]) {
scrollNode = [((DoricSuperNode *) scrollNode) subNodeWithViewId:value];
}
}
}
if (!scrollNode) {
[promise reject:@"Cannot find scrollable view"];
return;
}
NSDictionary *scrollRange = params[@"scrollRange"];
CGFloat startAnchor = [scrollRange[@"start"] floatValue];
CGFloat endAnchor = [scrollRange[@"end"] floatValue];
id target = params[@"target"];
BOOL isNavBar = false;
DoricViewNode *targetNode = nil;
if ([target isKindOfClass:[NSString class]] && [target isEqualToString:@"NavBar"]) {
isNavBar = true;
} else {
for (NSString *value in target) {
if (!targetNode) {
targetNode = [self.doricContext targetViewNode:value];
} else {
if ([targetNode isKindOfClass:[DoricSuperNode class]]) {
targetNode = [((DoricSuperNode *) targetNode) subNodeWithViewId:value];
}
}
}
if (!targetNode) {
[promise reject:@"Cannot find target view"];
}
}
NSDictionary *changing = params[@"changing"];
NSString *name = changing[@"name"];
NSNumber *changingStart = changing[@"start"];
NSNumber *changingEnd = changing[@"end"];
if ([scrollNode conformsToProtocol:@protocol(DoricScrollableProtocol)]) {
((id <DoricScrollableProtocol>) scrollNode).didScrollListener = ^(UIScrollView *scrollView) {
CGFloat scrollY = scrollView.contentOffset.y;
if (scrollY <= startAnchor) {
[self setValue:targetNode isNavBar:isNavBar name:name value:changingStart];
} else if (scrollY >= endAnchor) {
[self setValue:targetNode isNavBar:isNavBar name:name value:changingEnd];
} else {
CGFloat range = MAX(1, endAnchor - startAnchor);
CGFloat offset = scrollY - startAnchor;
CGFloat rate = offset / range;
id value;
if ([@"backgroundColor" isEqualToString:name]) {
UIColor *startColor = DoricColor(changingStart);
UIColor *endColor = DoricColor(changingEnd);
CGFloat startR, startG, startB, startA;
[startColor getRed:&startR green:&startG blue:&startB alpha:&startA];
CGFloat endR, endG, endB, endA;
[endColor getRed:&endR green:&endG blue:&endB alpha:&endA];
value = [UIColor colorWithRed:startR + (endR - startR) * rate
green:startG + (endG - startG) * rate
blue:startB + (endB - startB) * rate
alpha:startA + (endA - startA) * rate];
} else {
value = @([changingStart floatValue] + ([changingEnd floatValue] - [changingStart floatValue]) * rate);
}
[self setValue:targetNode isNavBar:isNavBar name:name value:value];
}
};
} else {
[promise reject:@"Scroller type error"];
}
});
}
- (void)setValue:(DoricViewNode *)viewNode isNavBar:(BOOL)isNavBar name:(NSString *)name value:(id)value {
if ([@"backgroundColor" isEqualToString:name]) {
if ([value isKindOfClass:[NSNumber class]]) {
value = DoricColor(value);
}
if (isNavBar) {
[self.doricContext.navBar doric_navBar_setBackgroundColor:value];
} else {
viewNode.view.backgroundColor = value;
}
} else if ([@"width" isEqualToString:name]) {
viewNode.view.width = [value floatValue];
} else if ([@"height" isEqualToString:name]) {
viewNode.view.height = [value floatValue];
} else if ([@"x" isEqualToString:name]) {
viewNode.view.left = [value floatValue];
} else if ([@"y" isEqualToString:name]) {
viewNode.view.top = [value floatValue];
}
}
@end

View File

@ -19,8 +19,8 @@
#import <Foundation/Foundation.h>
#import "DoricSuperNode.h"
#import "DoricScrollViewDelegate.h"
#import "DoricScrollableProtocol.h"
@interface DoricFlowLayoutNode : DoricSuperNode<UICollectionView *>
@property(nonatomic, weak, nullable) id <DoricScrollViewDelegate> delegate;
@interface DoricFlowLayoutNode : DoricSuperNode<UICollectionView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end

View File

@ -370,6 +370,8 @@ - (NSInteger)doricFlowLayoutColumnCount {
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.delegate scrollViewDidScroll:scrollView];
if (self.didScrollListener) {
self.didScrollListener(scrollView);
}
}
@end

View File

@ -19,8 +19,8 @@
#import <Foundation/Foundation.h>
#import "DoricSuperNode.h"
#import "DoricScrollViewDelegate.h"
#import "DoricScrollableProtocol.h"
@interface DoricListNode : DoricSuperNode<UITableView *>
@property(nonatomic, weak, nullable) id <DoricScrollViewDelegate> delegate;
@interface DoricListNode : DoricSuperNode<UITableView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end

View File

@ -250,6 +250,8 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.delegate scrollViewDidScroll:scrollView];
if (self.didScrollListener) {
self.didScrollListener(scrollView);
}
}
@end

View File

@ -21,12 +21,12 @@
//
#import <Foundation/Foundation.h>
#import "DoricSuperNode.h"
#import "DoricScrollViewDelegate.h"
#import "DoricScrollableProtocol.h"
@interface DoricScrollView : UIScrollView
@property(nonatomic, strong) UIView *contentView;
@end
@interface DoricScrollerNode : DoricSuperNode<DoricScrollView *>
@property(nonatomic, weak, nullable) id <DoricScrollViewDelegate> delegate;
@interface DoricScrollerNode : DoricSuperNode<DoricScrollView *> <DoricScrollableProtocol>
@property(nonatomic, strong, nullable) void (^didScrollListener)(UIScrollView *__nonnull scrollView);
@end

View File

@ -124,7 +124,10 @@ - (DoricViewNode *)subNodeWithViewId:(NSString *)viewId {
}
return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.delegate scrollViewDidScroll:scrollView];
if (self.didScrollListener) {
self.didScrollListener(scrollView);
}
}
@end