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 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<RecyclerView> {
@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<RecyclerView> {
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<RecyclerView> {
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);

View File

@ -34,11 +34,14 @@ @implementation DoricSliderViewCell
@interface DoricSliderNode () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSString *> *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,44 +82,54 @@ - (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];
self.propLoop = [prop boolValue];
} else if ([@"bounces" isEqualToString:name]) {
self.view.bounces = [prop boolValue];
} else {
[super blendView:view forPropName:name propValue:prop];
}
}
- (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 (self.loop) {
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];
});
} 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]) {
self.view.bounces = [prop boolValue];
} else {
[super blendView:view forPropName:name propValue:prop];
}
}