From 560990a39eea48c02f16d27babf10aedafef87f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 8 Jul 2021 20:02:37 +0800 Subject: [PATCH] add reset logic when item count or render page changed --- .../pub/doric/shader/slider/SliderNode.java | 44 +++++++----- .../Pod/Classes/Shader/DoricSliderNode.m | 69 +++++++++++-------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java index ec663647..b3b42c6d 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/slider/SliderNode.java @@ -45,6 +45,8 @@ public class SliderNode extends SuperNode { private String onPageSlidedFuncId; private int lastPosition = 0; private int itemCount = 0; + private boolean loop = false; + private String renderPageFuncId; private boolean scrollable = true; public SliderNode(DoricContext doricContext) { @@ -147,12 +149,35 @@ public class SliderNode extends SuperNode { @Override public void blend(JSObject jsObject) { super.blend(jsObject); + + final boolean needToScroll = (loop && !slideAdapter.loop) + || (!renderPageFuncId.equals(slideAdapter.renderPageFuncId)) + || (slideAdapter.itemCount == 0 && itemCount > 0); + + // If reset renderItem,should reset native cache. + if (!renderPageFuncId.equals(slideAdapter.renderPageFuncId)) { + slideAdapter.itemValues.clear(); + clearSubModel(); + slideAdapter.renderPageFuncId = renderPageFuncId; + } + + slideAdapter.loop = loop; + if (mView != null) { mView.post(new Runnable() { @Override public void run() { slideAdapter.itemCount = itemCount; slideAdapter.notifyDataSetChanged(); + + if (needToScroll) { + mView.post(new Runnable() { + @Override + public void run() { + mView.scrollToPosition(1); + } + }); + } } }); } @@ -171,13 +196,7 @@ public class SliderNode extends SuperNode { this.itemCount = prop.asNumber().toInt(); break; case "renderPage": - // If reset renderItem,should reset native cache. - String funcId = prop.asString().value(); - if (!funcId.equals(this.slideAdapter.renderPageFuncId)) { - this.slideAdapter.itemValues.clear(); - clearSubModel(); - this.slideAdapter.renderPageFuncId = funcId; - } + this.renderPageFuncId = prop.asString().value(); break; case "batchCount": this.slideAdapter.batchCount = prop.asNumber().toInt(); @@ -186,16 +205,7 @@ public class SliderNode extends SuperNode { this.onPageSlidedFuncId = prop.asString().toString(); break; case "loop": - boolean loop = prop.asBoolean().value(); - slideAdapter.loop = loop; - if (loop) { - mView.post(new Runnable() { - @Override - public void run() { - mView.scrollToPosition(1); - } - }); - } + this.loop = prop.asBoolean().value(); break; default: super.blend(view, name, prop); diff --git a/doric-iOS/Pod/Classes/Shader/DoricSliderNode.m b/doric-iOS/Pod/Classes/Shader/DoricSliderNode.m index 6f8d3867..b9c1f8f1 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricSliderNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricSliderNode.m @@ -34,11 +34,14 @@ @implementation DoricSliderViewCell @interface DoricSliderNode () @property(nonatomic, strong) NSMutableDictionary *itemViewIds; @property(nonatomic, assign) NSUInteger itemCount; +@property(nonatomic, assign) NSUInteger propItemCount; @property(nonatomic, assign) NSUInteger batchCount; @property(nonatomic, copy) NSString *onPageSelectedFuncId; @property(nonatomic) BOOL loop; +@property(nonatomic) BOOL propLoop; @property(nonatomic, assign) NSUInteger lastPosition; @property(nonatomic, copy) NSString *renderPageFuncId; +@property(nonatomic, copy) NSString *propRenderPageFuncId; @end @interface DoricSliderView : UICollectionView @@ -79,40 +82,15 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu if ([@"scrollable" isEqualToString:name]) { self.view.scrollEnabled = [prop boolValue]; } else if ([@"itemCount" isEqualToString:name]) { - self.itemCount = [prop unsignedIntegerValue]; - [self.view reloadData]; + self.propItemCount = [prop unsignedIntegerValue]; } else if ([@"renderPage" isEqualToString:name]) { - if ([self.renderPageFuncId isEqualToString:prop]) { - - } else { - [self.itemViewIds removeAllObjects]; - [self clearSubModel]; - [self.view reloadData]; - self.renderPageFuncId = prop; - } + self.propRenderPageFuncId = prop; } else if ([@"batchCount" isEqualToString:name]) { self.batchCount = [prop unsignedIntegerValue]; } else if ([@"onPageSlided" isEqualToString:name]) { self.onPageSelectedFuncId = prop; } else if ([@"loop" isEqualToString:name]) { - self.loop = [prop boolValue]; - - __weak typeof(self) _self = self; - if (self.loop) { - dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(_self) self = _self; - - [self.view reloadData]; - [self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false]; - }); - } else { - dispatch_async(dispatch_get_main_queue(), ^{ - __strong typeof(_self) self = _self; - - [self.view reloadData]; - [self.view setContentOffset:CGPointMake(0, self.view.contentOffset.y) animated:false]; - }); - } + self.propLoop = [prop boolValue]; } else if ([@"bounces" isEqualToString:name]) { self.view.bounces = [prop boolValue]; } else { @@ -120,6 +98,41 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu } } +- (void)afterBlended:(NSDictionary *)props { + bool needToScroll = (self.propLoop && !self.loop) + || (![self.renderPageFuncId isEqualToString: self.propRenderPageFuncId]) + || (self.itemCount == 0 && self.propItemCount > 0); + + // handle item count + if (self.itemCount != self.propItemCount) { + self.itemCount = self.propItemCount; + [self.view reloadData]; + } + + // handle render page + if ([self.renderPageFuncId isEqualToString:self.propRenderPageFuncId]) { + + } else { + [self.itemViewIds removeAllObjects]; + [self clearSubModel]; + [self.view reloadData]; + self.renderPageFuncId = self.propRenderPageFuncId; + } + + // handle loop + self.loop = self.propLoop; + + __weak typeof(self) _self = self; + if (needToScroll) { + dispatch_async(dispatch_get_main_queue(), ^{ + __strong typeof(_self) self = _self; + + [self.view reloadData]; + [self.view setContentOffset:CGPointMake(1 * self.view.width, self.view.contentOffset.y) animated:false]; + }); + } +} + - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (self.loop && self.itemCount > 0) { return self.itemCount + 2;