Feature/fix npe exception (#566)

* android: add exception catch for list flowlayout scroller  type

* iOS: add exception catch for list flowlayout scroller  type
This commit is contained in:
osborn 2023-01-03 16:42:52 +08:00 committed by GitHub
parent 37d2df9eea
commit 4eeebf6118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 220 additions and 186 deletions

View File

@ -20,16 +20,15 @@ import android.util.SparseArray;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull; import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -62,6 +61,7 @@ class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.DoricViewHolder> {
@Override @Override
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) { public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
try {
JSValue jsValue = getItemModel(position); JSValue jsValue = getItemModel(position);
boolean fullSpan = this.loadMore && position >= this.itemCount; boolean fullSpan = this.loadMore && position >= this.itemCount;
if (jsValue != null && jsValue.isObject()) { if (jsValue != null && jsValue.isObject()) {
@ -90,6 +90,9 @@ class FlowAdapter extends RecyclerView.Adapter<FlowAdapter.DoricViewHolder> {
&& !TextUtils.isEmpty(this.flowLayoutNode.onLoadMoreFuncId)) { && !TextUtils.isEmpty(this.flowLayoutNode.onLoadMoreFuncId)) {
callLoadMore(); callLoadMore();
} }
} catch (Exception e) {
flowLayoutNode.getDoricContext().getDriver().getRegistry().onException(flowLayoutNode.getDoricContext(), e);
}
} }
@Override @Override

View File

@ -21,15 +21,14 @@ import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull; import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -49,6 +48,7 @@ class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.D
int itemCount = 0; int itemCount = 0;
int loadAnchor = -1; int loadAnchor = -1;
boolean loadMore = false; boolean loadMore = false;
@NonNull @NonNull
@Override @Override
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@ -59,6 +59,7 @@ class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.D
@Override @Override
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) { public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
try {
JSValue jsValue = getItemModel(position); JSValue jsValue = getItemModel(position);
if (jsValue != null && jsValue.isObject()) { if (jsValue != null && jsValue.isObject()) {
JSObject jsObject = jsValue.asObject(); JSObject jsObject = jsValue.asObject();
@ -71,6 +72,9 @@ class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.D
&& !TextUtils.isEmpty(this.horizontalListNode.onLoadMoreFuncId)) { && !TextUtils.isEmpty(this.horizontalListNode.onLoadMoreFuncId)) {
callLoadMore(); callLoadMore();
} }
} catch (Exception e) {
horizontalListNode.getDoricContext().getDriver().getRegistry().onException(horizontalListNode.getDoricContext(), e);
}
} }
@Override @Override

View File

@ -21,15 +21,14 @@ import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull; import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -49,6 +48,7 @@ class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolder> {
int itemCount = 0; int itemCount = 0;
int loadAnchor = -1; int loadAnchor = -1;
boolean loadMore = false; boolean loadMore = false;
@NonNull @NonNull
@Override @Override
public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public DoricViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@ -59,6 +59,7 @@ class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolder> {
@Override @Override
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) { public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
try {
JSValue jsValue = getItemModel(position); JSValue jsValue = getItemModel(position);
if (jsValue != null && jsValue.isObject()) { if (jsValue != null && jsValue.isObject()) {
JSObject jsObject = jsValue.asObject(); JSObject jsObject = jsValue.asObject();
@ -71,6 +72,9 @@ class ListAdapter extends RecyclerView.Adapter<ListAdapter.DoricViewHolder> {
&& !TextUtils.isEmpty(this.listNode.onLoadMoreFuncId)) { && !TextUtils.isEmpty(this.listNode.onLoadMoreFuncId)) {
callLoadMore(); callLoadMore();
} }
} catch (Exception e) {
listNode.getDoricContext().getDriver().getRegistry().onException(listNode.getDoricContext(), e);
}
} }
@Override @Override

View File

@ -20,15 +20,14 @@ import android.util.SparseArray;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSNull; import com.github.pengfeizhou.jscore.JSNull;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.shader.ViewNode; import pub.doric.shader.ViewNode;
@ -61,6 +60,7 @@ class SlideAdapter extends RecyclerView.Adapter<SlideAdapter.DoricViewHolder> {
@Override @Override
public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) { public void onBindViewHolder(@NonNull DoricViewHolder holder, int position) {
try {
JSValue jsValue = getItemModel(position); JSValue jsValue = getItemModel(position);
if (jsValue != null && jsValue.isObject()) { if (jsValue != null && jsValue.isObject()) {
JSObject jsObject = jsValue.asObject(); JSObject jsObject = jsValue.asObject();
@ -68,6 +68,9 @@ class SlideAdapter extends RecyclerView.Adapter<SlideAdapter.DoricViewHolder> {
holder.slideItemNode.reset(); holder.slideItemNode.reset();
holder.slideItemNode.blend(jsObject.getProperty("props").asObject()); holder.slideItemNode.blend(jsObject.getProperty("props").asObject());
} }
} catch (Exception e) {
sliderNode.getDoricContext().getDriver().getRegistry().onException(sliderNode.getDoricContext(), e);
}
} }
@Override @Override

View File

@ -459,6 +459,7 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe
} }
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
@try {
NSUInteger position = (NSUInteger) indexPath.row; NSUInteger position = (NSUInteger) indexPath.row;
NSDictionary *model = [self itemModelAt:position]; NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"]; NSDictionary *props = model[@"props"];
@ -494,6 +495,10 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
[node requestLayout]; [node requestLayout];
[self callItem:position size:node.view.frame.size]; [self callItem:position size:node.view.frame.size];
return cell; return cell;
} @catch (NSException *exception) {
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
return nil;
}
} }
- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath { - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)currentIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath {

View File

@ -240,6 +240,7 @@ - (void)callLoadMore {
} }
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
@try {
NSUInteger position = (NSUInteger) indexPath.row; NSUInteger position = (NSUInteger) indexPath.row;
NSDictionary *model = [self itemModelAt:position]; NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"]; NSDictionary *props = model[@"props"];
@ -271,6 +272,10 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
[node requestLayout]; [node requestLayout];
[self callItem:position width:node.view.width]; [self callItem:position width:node.view.width];
return cell; return cell;
} @catch (NSException *exception) {
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
return nil;
}
} }
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

View File

@ -242,6 +242,7 @@ - (void)callLoadMore {
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
@try {
NSUInteger position = (NSUInteger) indexPath.row; NSUInteger position = (NSUInteger) indexPath.row;
NSDictionary *model = [self itemModelAt:position]; NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"]; NSDictionary *props = model[@"props"];
@ -273,6 +274,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
[node requestLayout]; [node requestLayout];
[self callItem:position height:node.view.height]; [self callItem:position height:node.view.height];
return cell; return cell;
} @catch (NSException *exception) {
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
return nil;
}
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

View File

@ -177,6 +177,7 @@ - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectio
} }
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
@try {
NSUInteger position = (NSUInteger) indexPath.row; NSUInteger position = (NSUInteger) indexPath.row;
NSDictionary *model = [self itemModelAt:position]; NSDictionary *model = [self itemModelAt:position];
NSDictionary *props = model[@"props"]; NSDictionary *props = model[@"props"];
@ -195,6 +196,10 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
[node.view.doricLayout apply:CGSizeMake(collectionView.width, collectionView.height)]; [node.view.doricLayout apply:CGSizeMake(collectionView.width, collectionView.height)];
[node requestLayout]; [node requestLayout];
return cell; return cell;
} @catch (NSException *exception) {
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
return nil;
}
} }
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {