From 49d3426d8509ca6c5e8f65dee6e65dafd4cf3570 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 14 Jul 2021 15:23:33 +0800 Subject: [PATCH] iOS:snapshot view add control logic --- .../Devkit/Classes/DoricDevViewController.m | 2 +- doric-iOS/Devkit/Classes/DoricSnapshotView.h | 2 + doric-iOS/Devkit/Classes/DoricSnapshotView.m | 142 +++++++++++++++++- doric-iOS/Example/Example/ViewController.m | 1 + doric-iOS/Pod/Classes/DoricContext.m | 3 + 5 files changed, 143 insertions(+), 7 deletions(-) diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index 98de6c8c..645a0ca0 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -121,7 +121,7 @@ - (void)onClick { } if ([DoricRegistry isEnableRenderSnapshot]) { UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_) { - DoricSnapshotView *doricSnapshotView = [DoricSnapshotView new]; + DoricSnapshotView *doricSnapshotView = [[DoricSnapshotView alloc] initWithDoricContext:self.doricContext]; doricSnapshotView.top = 50; [self.doricContext.vc.view addSubview:doricSnapshotView]; [self.vc.navigationController popViewControllerAnimated:NO]; diff --git a/doric-iOS/Devkit/Classes/DoricSnapshotView.h b/doric-iOS/Devkit/Classes/DoricSnapshotView.h index 280c1981..431dff72 100644 --- a/doric-iOS/Devkit/Classes/DoricSnapshotView.h +++ b/doric-iOS/Devkit/Classes/DoricSnapshotView.h @@ -20,6 +20,8 @@ #import #import "DoricFloatView.h" +#import @interface DoricSnapshotView : DoricFloatView +- (instancetype)initWithDoricContext:(DoricContext *)context; @end \ No newline at end of file diff --git a/doric-iOS/Devkit/Classes/DoricSnapshotView.m b/doric-iOS/Devkit/Classes/DoricSnapshotView.m index e5eabef7..518ccb3d 100644 --- a/doric-iOS/Devkit/Classes/DoricSnapshotView.m +++ b/doric-iOS/Devkit/Classes/DoricSnapshotView.m @@ -18,19 +18,149 @@ // #import "DoricSnapshotView.h" -#import +#import + +@interface DoricSnapshotView () +@property(nonatomic, weak) DoricContext *doricContext; +@property(nonatomic, strong) UIImageView *prevBtn; +@property(nonatomic, strong) UIImageView *nextBtn; +@property(nonatomic, strong) UIImageView *closeBtn; +@property(nonatomic, strong) UILabel *indexLabel; +@property(nonatomic, assign) NSUInteger snapSize; +@property(nonatomic, assign) NSUInteger snapIndex; +@end @implementation DoricSnapshotView -- (instancetype)init { +- (instancetype)initWithDoricContext:(DoricContext *)context { if (self = [super init]) { + _doricContext = context; [self setupUI]; } return self; } - (void)setupUI { - self.width = 20; - self.height = 20; - self.backgroundColor = [UIColor redColor]; + self.width = 240; + self.height = 70; + self.backgroundColor = DoricColor(@(0xffecf0f1)); + self.alpha = 0.8f; + UIImageView *moveBtn = [[UIImageView new] also:^(UIImageView *it) { + UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_move"]; + it.image = image; + it.width = 40; + it.height = 40; + it.contentMode = UIViewContentModeScaleToFill; + [self addSubview:it]; + }]; + self.prevBtn = [[UIImageView new] also:^(UIImageView *it) { + UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_prev"]; + it.image = image; + it.width = 40; + it.height = 40; + it.contentMode = UIViewContentModeScaleToFill; + [self addSubview:it]; + it.userInteractionEnabled = YES; + UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onPrev)]; + [it addGestureRecognizer:tapGestureRecognizer]; + }]; + self.nextBtn = [[UIImageView new] also:^(UIImageView *it) { + UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_next"]; + it.image = image; + it.width = 40; + it.height = 40; + it.contentMode = UIViewContentModeScaleToFill; + [self addSubview:it]; + it.userInteractionEnabled = YES; + UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onNext)]; + [it addGestureRecognizer:tapGestureRecognizer]; + }]; + self.closeBtn = [[UIImageView new] also:^(UIImageView *it) { + UIImage *image = [UIImage imageNamed:@"DoricDevkit.bundle/icon_doricdev_close"]; + it.image = image; + it.width = 40; + it.height = 40; + it.contentMode = UIViewContentModeScaleToFill; + [self addSubview:it]; + it.userInteractionEnabled = YES; + UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClose)]; + [it addGestureRecognizer:tapGestureRecognizer]; + }]; + self.indexLabel = [[UILabel new] also:^(UILabel *it) { + it.width = 40; + it.height = 40; + it.textAlignment = NSTextAlignmentCenter; + it.font = [UIFont systemFontOfSize:30]; + [self addSubview:it]; + }]; + moveBtn.left = 0; + moveBtn.centerY = self.centerY; + + self.prevBtn.left = moveBtn.right + 5; + self.prevBtn.centerY = self.centerY; + + self.indexLabel.left = self.prevBtn.right + 10; + self.indexLabel.centerY = self.centerY; + + self.nextBtn.left = self.indexLabel.right + 10; + self.nextBtn.centerY = self.centerY; + + self.closeBtn.left = self.nextBtn.right + 10; + self.closeBtn.centerY = self.centerY; + + [[self.doricContext callEntity:@"__renderSnapshotDepth__" withArgumentsArray:@[]] setResultCallback:^(JSValue *result) { + self.snapSize = [[result toNumber] unsignedIntegerValue]; + dispatch_async(dispatch_get_main_queue(), ^{ + self.snapIndex = self.snapSize; + [self updateUI]; + }); + }]; } -@end \ No newline at end of file + +- (void)onPrev { + if (self.snapIndex <= 0) { + return; + } + self.snapIndex--; + [self updateUI]; +} + +- (void)onNext { + if (self.snapIndex >= self.snapSize) { + return; + } + self.snapIndex++; + [self updateUI]; +} + +- (void)onClose { + self.snapIndex = self.snapSize; + [self updateUI]; + [self removeFromSuperview]; +} + +- (void)updateUI { + self.prevBtn.alpha = self.snapIndex <= 0 ? 0.5f : 1; + self.nextBtn.alpha = self.snapIndex >= self.snapSize ? 0.5f : 1; + self.indexLabel.text = [NSString stringWithFormat:@"%@", @(self.snapIndex)]; + [[self.doricContext callEntity:@"__restoreRenderSnapshot__" withArgumentsArray:@[@(self.snapIndex)]] setResultCallback:^(JSValue *result) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.doricContext.rootNode.view.subviews forEach:^(__kindof UIView *obj) { + [obj removeFromSuperview]; + }]; + [self.doricContext.rootNode clearSubModel]; + [[result toArray] forEach:^(NSDictionary *obj) { + NSString *viewId = obj[@"id"]; + if (self.doricContext.rootNode.viewId == nil && [@"Root" isEqualToString:[obj optString:@"type"]]) { + self.doricContext.rootNode.viewId = viewId; + [self.doricContext.rootNode blend:[obj optObject:@"props"]]; + [self.doricContext.rootNode requestLayout]; + } else { + DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId]; + [viewNode blend:[obj optObject:@"props"]]; + [viewNode requestLayout]; + } + }]; + }); + }]; +} +@end diff --git a/doric-iOS/Example/Example/ViewController.m b/doric-iOS/Example/Example/ViewController.m index 01825e0a..70630233 100644 --- a/doric-iOS/Example/Example/ViewController.m +++ b/doric-iOS/Example/Example/ViewController.m @@ -47,6 +47,7 @@ - (void)viewDidLoad { }]]; [DoricRegistry register:[DemoLibrary new]]; [DoricRegistry enablePerformance:YES]; + [DoricRegistry enableRenderSnapshot:YES]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { diff --git a/doric-iOS/Pod/Classes/DoricContext.m b/doric-iOS/Pod/Classes/DoricContext.m index 3f2be3e7..5d22738e 100644 --- a/doric-iOS/Pod/Classes/DoricContext.m +++ b/doric-iOS/Pod/Classes/DoricContext.m @@ -84,6 +84,9 @@ - (DoricAsyncResult *)callEntity:(NSString *)method withArgumentsArray:(NSArray } - (void)init:(NSString *)initData { + if ([DoricRegistry isEnableRenderSnapshot]) { + [self callEntity:@"__enableSnapshot__" withArgumentsArray:@[]]; + } self.extra = initData; if (initData) { [self callEntity:DORIC_ENTITY_INIT withArgumentsArray:@[initData]];