add reset logic when item count or render page changed

This commit is contained in:
王劲鹏 2021-07-08 20:02:37 +08:00 committed by osborn
parent 98c3bc6668
commit 560990a39e
2 changed files with 68 additions and 45 deletions

View File

@ -45,6 +45,8 @@ public class SliderNode extends SuperNode<RecyclerView> {
private String onPageSlidedFuncId; private String onPageSlidedFuncId;
private int lastPosition = 0; private int lastPosition = 0;
private int itemCount = 0; private int itemCount = 0;
private boolean loop = false;
private String renderPageFuncId;
private boolean scrollable = true; private boolean scrollable = true;
public SliderNode(DoricContext doricContext) { public SliderNode(DoricContext doricContext) {
@ -147,12 +149,35 @@ public class SliderNode extends SuperNode<RecyclerView> {
@Override @Override
public void blend(JSObject jsObject) { public void blend(JSObject jsObject) {
super.blend(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) { if (mView != null) {
mView.post(new Runnable() { mView.post(new Runnable() {
@Override @Override
public void run() { public void run() {
slideAdapter.itemCount = itemCount; slideAdapter.itemCount = itemCount;
slideAdapter.notifyDataSetChanged(); slideAdapter.notifyDataSetChanged();
if (needToScroll) {
mView.post(new Runnable() {
@Override
public void run() {
mView.scrollToPosition(1);
}
});
}
} }
}); });
} }
@ -171,13 +196,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
this.itemCount = prop.asNumber().toInt(); this.itemCount = prop.asNumber().toInt();
break; break;
case "renderPage": case "renderPage":
// If reset renderItem,should reset native cache. this.renderPageFuncId = prop.asString().value();
String funcId = prop.asString().value();
if (!funcId.equals(this.slideAdapter.renderPageFuncId)) {
this.slideAdapter.itemValues.clear();
clearSubModel();
this.slideAdapter.renderPageFuncId = funcId;
}
break; break;
case "batchCount": case "batchCount":
this.slideAdapter.batchCount = prop.asNumber().toInt(); this.slideAdapter.batchCount = prop.asNumber().toInt();
@ -186,16 +205,7 @@ public class SliderNode extends SuperNode<RecyclerView> {
this.onPageSlidedFuncId = prop.asString().toString(); this.onPageSlidedFuncId = prop.asString().toString();
break; break;
case "loop": case "loop":
boolean loop = prop.asBoolean().value(); this.loop = prop.asBoolean().value();
slideAdapter.loop = loop;
if (loop) {
mView.post(new Runnable() {
@Override
public void run() {
mView.scrollToPosition(1);
}
});
}
break; break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);

View File

@ -34,11 +34,14 @@ @implementation DoricSliderViewCell
@interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> @interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *itemViewIds; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *itemViewIds;
@property(nonatomic, assign) NSUInteger itemCount; @property(nonatomic, assign) NSUInteger itemCount;
@property(nonatomic, assign) NSUInteger propItemCount;
@property(nonatomic, assign) NSUInteger batchCount; @property(nonatomic, assign) NSUInteger batchCount;
@property(nonatomic, copy) NSString *onPageSelectedFuncId; @property(nonatomic, copy) NSString *onPageSelectedFuncId;
@property(nonatomic) BOOL loop; @property(nonatomic) BOOL loop;
@property(nonatomic) BOOL propLoop;
@property(nonatomic, assign) NSUInteger lastPosition; @property(nonatomic, assign) NSUInteger lastPosition;
@property(nonatomic, copy) NSString *renderPageFuncId; @property(nonatomic, copy) NSString *renderPageFuncId;
@property(nonatomic, copy) NSString *propRenderPageFuncId;
@end @end
@interface DoricSliderView : UICollectionView @interface DoricSliderView : UICollectionView
@ -79,40 +82,15 @@ - (void)blendView:(UICollectionView *)view forPropName:(NSString *)name propValu
if ([@"scrollable" isEqualToString:name]) { if ([@"scrollable" isEqualToString:name]) {
self.view.scrollEnabled = [prop boolValue]; self.view.scrollEnabled = [prop boolValue];
} else if ([@"itemCount" isEqualToString:name]) { } else if ([@"itemCount" isEqualToString:name]) {
self.itemCount = [prop unsignedIntegerValue]; self.propItemCount = [prop unsignedIntegerValue];
[self.view reloadData];
} else if ([@"renderPage" isEqualToString:name]) { } else if ([@"renderPage" isEqualToString:name]) {
if ([self.renderPageFuncId isEqualToString:prop]) { self.propRenderPageFuncId = prop;
} else {
[self.itemViewIds removeAllObjects];
[self clearSubModel];
[self.view reloadData];
self.renderPageFuncId = prop;
}
} else if ([@"batchCount" isEqualToString:name]) { } else if ([@"batchCount" isEqualToString:name]) {
self.batchCount = [prop unsignedIntegerValue]; self.batchCount = [prop unsignedIntegerValue];
} else if ([@"onPageSlided" isEqualToString:name]) { } else if ([@"onPageSlided" isEqualToString:name]) {
self.onPageSelectedFuncId = prop; self.onPageSelectedFuncId = prop;
} else if ([@"loop" isEqualToString:name]) { } else if ([@"loop" isEqualToString:name]) {
self.loop = [prop boolValue]; self.propLoop = [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];
});
}
} else if ([@"bounces" isEqualToString:name]) { } else if ([@"bounces" isEqualToString:name]) {
self.view.bounces = [prop boolValue]; self.view.bounces = [prop boolValue];
} else { } 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 { - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.loop && self.itemCount > 0) { if (self.loop && self.itemCount > 0) {
return self.itemCount + 2; return self.itemCount + 2;