From c050a18546fff2d975143c20f7f2e992994b0d8a Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 13 Jul 2022 16:38:20 +0800 Subject: [PATCH] Add embedded example for iOS --- doric-demo/src/CellModule1Demo.tsx | 16 +- doric-demo/src/CellModule2Demo.tsx | 16 +- .../Example/Example.xcodeproj/project.pbxproj | 6 + .../Example/Example/DoricEmbeddedExampleVC.h | 14 ++ .../Example/Example/DoricEmbeddedExampleVC.m | 168 ++++++++++++++++++ doric-iOS/Example/Example/ViewController.m | 17 +- 6 files changed, 220 insertions(+), 17 deletions(-) create mode 100644 doric-iOS/Example/Example/DoricEmbeddedExampleVC.h create mode 100644 doric-iOS/Example/Example/DoricEmbeddedExampleVC.m diff --git a/doric-demo/src/CellModule1Demo.tsx b/doric-demo/src/CellModule1Demo.tsx index 64e59807..473b51cb 100644 --- a/doric-demo/src/CellModule1Demo.tsx +++ b/doric-demo/src/CellModule1Demo.tsx @@ -10,16 +10,20 @@ import { Gravity, Color, createRef, + Ref, } from "doric"; @Entry export class Cell1 extends Panel { - imageRef = createRef(); - titleRef = createRef(); - contentRef = createRef(); + imageRef?: Ref; + titleRef?: Ref; + contentRef?: Ref; data?: { imageUrl: string; title: string; content: string }; build(root: Group) { + this.imageRef = createRef(); + this.titleRef = createRef(); + this.contentRef = createRef(); (); - titleRef = createRef(); - contentRef = createRef(); + imageRef?: Ref; + titleRef?: Ref; + contentRef?: Ref; data?: { imageUrl: string; title: string; content: string }; build(root: Group) { + this.imageRef = createRef(); + this.titleRef = createRef(); + this.contentRef = createRef(); +#import + + +@interface DoricEmbeddedExampleVC : UIViewController +@end diff --git a/doric-iOS/Example/Example/DoricEmbeddedExampleVC.m b/doric-iOS/Example/Example/DoricEmbeddedExampleVC.m new file mode 100644 index 00000000..6dd761de --- /dev/null +++ b/doric-iOS/Example/Example/DoricEmbeddedExampleVC.m @@ -0,0 +1,168 @@ +// +// DoricEmbeddedExampleVC.m +// Example +// +// Created by pengfei.zhou on 2022/7/13. +// Copyright © 2022 pengfei.zhou. All rights reserved. +// + +#import +#import "DoricEmbeddedExampleVC.h" +#import +#import + + +@interface MyFlowLayout : UICollectionViewLayout +@property(nonatomic, readonly) NSInteger columnCount; +@property(nonatomic, strong) NSMutableDictionary *columnHeightInfo; +@end + +@implementation MyFlowLayout +- (instancetype)init { + if (self = [super init]) { + _columnHeightInfo = [NSMutableDictionary new]; + } + return self; +} + +- (NSInteger)columnCount { + return 2; +} + +- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { + return YES; +} + +- (void)prepareLayout { + [super prepareLayout]; + for (int i = 0; i < self.columnCount; i++) { + self.columnHeightInfo[@(i)] = @(0); + } +} + +- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { + for (int i = 0; i < self.columnCount; i++) { + self.columnHeightInfo[@(i)] = @(0); + } + NSMutableArray *array = [NSMutableArray array]; + NSInteger count = [self.collectionView numberOfItemsInSection:0]; + for (int i = 0; i < count; i++) { + UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; + [array addObject:attrs]; + } + return array; +} + +- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { + NSNumber *minYOfColumn = @(0); + NSArray *keys = self.columnHeightInfo.allKeys; + NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) { + return ([obj1 intValue] <= [obj2 intValue] ? NSOrderedAscending : NSOrderedDescending); + }]; + + for (NSNumber *key in sortedKeys) { + if ([self.columnHeightInfo[key] floatValue] < [self.columnHeightInfo[minYOfColumn] floatValue]) { + minYOfColumn = key; + } + } + CGFloat columnWidth = self.collectionView.width / self.columnCount; + CGFloat width = columnWidth; + CGFloat height = 200; + CGFloat x = 0; + CGFloat y = [self.columnHeightInfo[minYOfColumn] floatValue]; + x = columnWidth * [minYOfColumn integerValue]; + self.columnHeightInfo[minYOfColumn] = @(y + height); + UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; + attrs.frame = CGRectMake(x, y, width, height); + return attrs; +} + +- (CGSize)collectionViewContentSize { + CGFloat width = self.collectionView.width; + CGFloat height = 0; + for (NSNumber *column in self.columnHeightInfo.allValues) { + height = MAX(height, [column floatValue]); + } + return CGSizeMake(width, height); +} +@end + + +@interface MyCollectionViewCell : UICollectionViewCell +@property(nonatomic, strong) DoricPanel *panel; +@end + +@implementation MyCollectionViewCell +@end + + +@interface DoricEmbeddedExampleVC () +@end + +@implementation DoricEmbeddedExampleVC + +- (instancetype)init { + if (self = [super init]) { + } + return self; +} + + +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; + [self.view addSubview:[[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) collectionViewLayout:[MyFlowLayout new]] also:^(UICollectionView *it) { + it.dataSource = self; + it.delegate = self; + }]]; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + self.title = @"Doric Embedded Demo"; + [self.navigationController.navigationBar setBackgroundImage:UIImageWithColor(UIColor.whiteColor) forBarMetrics:UIBarMetricsDefault]; +} + +- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { + return 1000; +} + +- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { + NSString *cellId = (indexPath.row % 2 == 0) ? @"cell0" : @"cell1"; + [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellId]; + MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; + NSDictionary *data = @{ + @"imageUrl": (indexPath.row % 2 == 0) + ? @"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202107%2F09%2F20210709142454_dc8dc.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660279617&t=8caf9c88dbeb00c6436f76e90c54eecc" + : @"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202005%2F02%2F20200502185802_FuFU2.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1660279617&t=77131edf568efeda32c3a6513412f324", + @"title": [NSString stringWithFormat:@"%@", @(indexPath.row + 1)], + @"content": [NSString stringWithFormat:@"第%@项\n++++++++可填充内容++++++++%@", @(indexPath.row + 1), (indexPath.row % 2 == 0) ? @"\n+++再加一行+++" : @""], + }; + if (cell.panel == nil) { + cell.panel = [[DoricPanel new] also:^(DoricPanel *it) { + it.view.width = self.view.width / 2; + it.view.height = 200; + NSString *scheme = (indexPath.row % 2 == 0) ? @"assets://src/CellModule1Demo.js" : @"assets://src/CellModule2Demo.js"; + NSString *alias = (indexPath.row % 2 == 0) ? @"CellModule1Demo.js" : @"CellModule2Demo.js"; + [[DoricSingleton.instance.jsLoaderManager request:scheme] + setResultCallback:^(NSString *script) { + [it config:script alias:alias extra:nil]; + }]; + [cell.panel.doricContext callEntity:@"setData", data, nil]; + }]; + } + if (cell.panel.doricContext != nil) { + [cell.panel.doricContext callEntity:@"setData", data, nil]; + } + __weak typeof(cell) _cell = cell; + cell.panel.frameChangedBlock = ^(CGSize frameSize) { + __strong typeof(_cell) c = _cell; + c.width = frameSize.width; + c.height = frameSize.height; + c.contentView.width = c.width; + c.contentView.height = c.height; + }; + [cell.contentView addSubview:cell.panel.view]; + return cell; +} + +@end diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index d95bf904..87513fea 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -11,7 +11,8 @@ #import "ViewController.h" #import "DemoLibrary.h" -#import "DoricPanelListViewController.h"" +#import "DoricPanelListViewController.h" +#import "DoricEmbeddedExampleVC.h" @interface ViewController () @property(nonatomic, copy) NSArray *demoFilePaths; @@ -38,6 +39,8 @@ - (void)viewDidLoad { }]; [tmp insertObject:@"Dev Kit" atIndex:0]; [tmp insertObject:@"Doric Panel List" atIndex:1]; + [tmp insertObject:@"Doric Embedded Example" atIndex:2]; + self.demoFilePaths = tmp; [Doric registerLibrary:[DemoLibrary new]]; @@ -96,6 +99,18 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath } [navigationController pushViewController:panelListViewController animated:NO]; return; + } else if (indexPath.row == 2) { + DoricEmbeddedExampleVC *vc = [DoricEmbeddedExampleVC new]; + + UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController; + UINavigationController *navigationController; + if ([viewController isKindOfClass:[UINavigationController class]]) { + navigationController = (UINavigationController *) viewController; + } else { + navigationController = viewController.navigationController; + } + [navigationController pushViewController:vc animated:NO]; + return; } NSString *file = self.demoFilePaths[(NSUInteger) indexPath.row]; DoricViewController *doricViewController = [[DoricViewController alloc]