Authored by lixia.zhang

数据曝光及scrollview的下拉刷新

... ... @@ -8,7 +8,7 @@
*/
#import "RCTAssert.h"
#import "RCTLog.h"
#import <React/RCTLog.h>
NSString *const RCTErrorDomain = @"RCTErrorDomain";
NSString *const RCTJSStackTraceKey = @"RCTJSStackTraceKey";
... ... @@ -131,6 +131,15 @@ void RCTFatal(NSError *error)
#endif
NSString *name = [NSString stringWithFormat:@"%@: %@", RCTFatalExceptionName, error.localizedDescription];
NSString *message = RCTFormatError(error.localizedDescription, error.userInfo[RCTJSStackTraceKey], 75);
#ifdef APPYOHO
NSArray *callStack = [error.userInfo objectForKey:RCTJSStackTraceKey];
NSDictionary *parameters = @{
JsonKeyCrashCallstack : callStack ? : @[],
JsonKeyCrashExceptionName : name ? : @"",
JsonKeyCrashExceptionReason : message ? : @"",
};
[[YH_Analytics sharedInstance] logError:kErrorTypeRNException parameters:@{JsonKeyErrorST : parameters}];
#endif
@throw [[NSException alloc] initWithName:name reason:message userInfo:nil];
#if DEBUG
} @catch (NSException *e) {}
... ...
... ... @@ -13,6 +13,8 @@
#import <React/RCTEventDispatcher.h>
#import <React/RCTScrollableProtocol.h>
#import <React/RCTView.h>
#import "MJRefresh.h"
#import "UIScrollView+MJRefresh.h"
@protocol UIScrollViewDelegate;
... ... @@ -58,6 +60,25 @@
@property (nonatomic, copy) RCTDirectEventBlock onMomentumScrollBegin;
@property (nonatomic, copy) RCTDirectEventBlock onMomentumScrollEnd;
// ********************
// add pull to refresh
// ********************
@property (assign, nonatomic) BOOL currentRefreshingState;
@property(nonatomic, copy) RCTDirectEventBlock onRefreshData;
@property(nonatomic, assign) BOOL isOnPullToRefresh;
@property(nonatomic, assign) BOOL enablePullToRefresh;
- (void)startPullToRefresh;
- (void)stopPullToRefresh;
// ********************
// add pull to refresh
// ********************
// ********************
// add 下拉刷新结束
// ********************
@property(nonatomic, copy) RCTDirectEventBlock onFinishRefreshData;
@end
@interface RCTScrollView (Internal)
... ...
... ... @@ -11,19 +11,16 @@
#import <UIKit/UIKit.h>
#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTUIManager.h"
#import "RCTUIManagerObserverCoordinator.h"
#import "RCTUIManagerUtils.h"
#import "RCTUtils.h"
#import "UIView+Private.h"
#import "UIView+React.h"
#if !TARGET_OS_TV
#import "RCTRefreshControl.h"
#endif
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTRefreshControl.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
#import <React/UIView+Private.h>
#import <React/UIView+React.h>
#import <React/RCTUIManagerObserverCoordinator.h>
#import <React/RCTUIManagerUtils.h>
@interface RCTScrollEvent : NSObject <RCTEvent>
... ... @@ -156,15 +153,16 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
@interface RCTCustomScrollView : UIScrollView<UIGestureRecognizerDelegate>
@property (nonatomic, assign) BOOL centerContent;
#if !TARGET_OS_TV
@property (nonatomic, strong) RCTRefreshControl *rctRefreshControl;
@property (nonatomic, assign) BOOL pinchGestureEnabled;
#endif
@end
@implementation RCTCustomScrollView
{
__weak UIView *_dockedHeaderView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
... ... @@ -330,7 +328,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
}
}
#if !TARGET_OS_TV
- (void)setRctRefreshControl:(RCTRefreshControl *)refreshControl
{
if (_rctRefreshControl) {
... ... @@ -353,7 +350,6 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
// in the setter gets overriden when the view loads.
self.pinchGestureRecognizer.enabled = _pinchGestureEnabled;
}
#endif //TARGET_OS_TV
@end
... ... @@ -412,6 +408,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
_scrollListeners = [NSHashTable weakObjectsHashTable];
[self addSubview:_scrollView];
// ********************
// add pull to refresh
// ********************
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeSwitchNotificationHandle:) name:YHBThemeDidChangeNotification object:nil];
}
return self;
}
... ... @@ -442,11 +443,9 @@ static inline void RCTApplyTransformationAccordingLayoutDirection(UIView *view,
- (void)insertReactSubview:(UIView *)view atIndex:(NSInteger)atIndex
{
[super insertReactSubview:view atIndex:atIndex];
#if !TARGET_OS_TV
if ([view isKindOfClass:[RCTRefreshControl class]]) {
[_scrollView setRctRefreshControl:(RCTRefreshControl *)view];
} else
#endif
{
RCTAssert(_contentView == nil, @"RCTScrollView may only contain a single subview");
_contentView = view;
... ... @@ -458,11 +457,9 @@ static inline void RCTApplyTransformationAccordingLayoutDirection(UIView *view,
- (void)removeReactSubview:(UIView *)subview
{
[super removeReactSubview:subview];
#if !TARGET_OS_TV
if ([subview isKindOfClass:[RCTRefreshControl class]]) {
[_scrollView setRctRefreshControl:nil];
} else
#endif
{
RCTAssert(_contentView == subview, @"Attempted to remove non-existent subview");
_contentView = nil;
... ... @@ -501,6 +498,10 @@ static inline void RCTApplyTransformationAccordingLayoutDirection(UIView *view,
{
_scrollView.delegate = nil;
[_eventDispatcher.bridge.uiManager.observerCoordinator removeObserver:self];
// ********************
// add pull to refresh
// ********************
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)layoutSubviews
... ... @@ -509,13 +510,11 @@ static inline void RCTApplyTransformationAccordingLayoutDirection(UIView *view,
RCTAssert(self.subviews.count == 1, @"we should only have exactly one subview");
RCTAssert([self.subviews lastObject] == _scrollView, @"our only subview should be a scrollview");
#if !TARGET_OS_TV
// Adjust the refresh control frame if the scrollview layout changes.
RCTRefreshControl *refreshControl = _scrollView.rctRefreshControl;
if (refreshControl && refreshControl.refreshing) {
refreshControl.frame = (CGRect){_scrollView.contentOffset, {_scrollView.frame.size.width, refreshControl.frame.size.height}};
}
#endif
[self updateClippedSubviews];
}
... ... @@ -1013,10 +1012,8 @@ RCT_SET_AND_PRESERVE_OFFSET(setKeyboardDismissMode, keyboardDismissMode, UIScrol
RCT_SET_AND_PRESERVE_OFFSET(setMaximumZoomScale, maximumZoomScale, CGFloat)
RCT_SET_AND_PRESERVE_OFFSET(setMinimumZoomScale, minimumZoomScale, CGFloat)
RCT_SET_AND_PRESERVE_OFFSET(setScrollEnabled, isScrollEnabled, BOOL)
#if !TARGET_OS_TV
RCT_SET_AND_PRESERVE_OFFSET(setPagingEnabled, isPagingEnabled, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setScrollsToTop, scrollsToTop, BOOL)
#endif
RCT_SET_AND_PRESERVE_OFFSET(setShowsHorizontalScrollIndicator, showsHorizontalScrollIndicator, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setShowsVerticalScrollIndicator, showsVerticalScrollIndicator, BOOL)
RCT_SET_AND_PRESERVE_OFFSET(setZoomScale, zoomScale, CGFloat);
... ... @@ -1054,6 +1051,124 @@ RCT_SET_AND_PRESERVE_OFFSET(setScrollIndicatorInsets, scrollIndicatorInsets, UIE
[_eventDispatcher sendEvent:scrollEvent];
}
// ********************
// add pull to refresh
// ********************
- (void)startPullToRefresh
{
self.isOnPullToRefresh = YES;
[_scrollView.header beginRefreshing];
}
// ********************
// add pull to refresh
// ********************
- (void)stopPullToRefresh
{
self.isOnPullToRefresh = NO;
[_scrollView.header endRefreshing];
if (_onFinishRefreshData) {
_onFinishRefreshData(nil);
}
}
// ********************
// add pull to refresh
// ********************
- (void)setEnablePullToRefresh:(BOOL)enablePullToRefresh
{
_enablePullToRefresh = enablePullToRefresh;
if (enablePullToRefresh) {
if (_scrollView.header == nil) {
[_scrollView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
[self customRefreshHeaderView];
self.currentRefreshingState = NO;
}
}
}
- (void)setIsOnPullToRefresh:(BOOL)isOnPullToRefresh
{
if (_currentRefreshingState != isOnPullToRefresh) {
_currentRefreshingState = isOnPullToRefresh;
if (isOnPullToRefresh) {
[_scrollView.header beginRefreshing];
} else {
[_scrollView.header endRefreshing];
if (_onFinishRefreshData) {
_onFinishRefreshData(nil);
}
}
}
}
// ********************
// add pull to refresh
// ********************
-(void)refreshData
{
_currentRefreshingState = _scrollView.header.isRefreshing;
if (_onRefreshData) {
_onRefreshData(nil);
}
}
// ********************
// add pull to refresh
// ********************
- (void)setRefreshHeaderImage
{
// 设置普通状态的动画图片
NSString *idleImageName = [NSString stringWithFormat:@"refresh_000"];
UIImage *idleImage = [[YH_ThemeManager shareInstance] getThemeImage:idleImageName];
[_scrollView.gifHeader setImages:@[idleImage] forState:MJRefreshHeaderStateIdle];
// 设置正在刷新状态的动画图片
NSMutableArray *refreshingImages = [NSMutableArray array];
for (NSUInteger i = 1; i <= 12; i++) {
NSString *imageName = [NSString stringWithFormat:@"refresh_%03zd",i];
UIImage *image = [[YH_ThemeManager shareInstance] getThemeImage:imageName];
[refreshingImages addObject:image];
}
[_scrollView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
}
// ********************
// add pull to refresh
// ********************
- (void)customRefreshHeaderView
{
// 隐藏时间
_scrollView.header.updatedTimeHidden = YES;
// 隐藏状态
_scrollView.header.stateHidden = YES;
[self setRefreshHeaderImage];
}
// ********************
// add pull to refresh
// ********************
- (void)themeSwitchNotificationHandle:(NSNotification *)notification
{
if (_scrollView.header.isRefreshing) {
[_scrollView.header endRefreshing];
if (_onFinishRefreshData) {
_onFinishRefreshData(nil);
}
}
[self setRefreshHeaderImage];
}
// ********************
// add pull to refresh
// ********************
@end
@implementation RCTEventDispatcher (RCTScrollView)
... ...
... ... @@ -9,10 +9,10 @@
#import "RCTScrollViewManager.h"
#import "RCTBridge.h"
#import <React/RCTBridge.h>
#import "RCTScrollView.h"
#import "RCTShadowView.h"
#import "RCTUIManager.h"
#import <React/RCTShadowView.h>
#import <React/RCTUIManager.h>
@interface RCTScrollView (Private)
... ... @@ -71,11 +71,9 @@ RCT_EXPORT_VIEW_PROPERTY(keyboardDismissMode, UIScrollViewKeyboardDismissMode)
RCT_EXPORT_VIEW_PROPERTY(maximumZoomScale, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(minimumZoomScale, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL)
#if !TARGET_OS_TV
RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
RCT_REMAP_VIEW_PROPERTY(pinchGestureEnabled, scrollView.pinchGestureEnabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(scrollsToTop, BOOL)
#endif
RCT_EXPORT_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL)
RCT_EXPORT_VIEW_PROPERTY(scrollEventThrottle, NSTimeInterval)
... ... @@ -207,4 +205,46 @@ RCT_EXPORT_METHOD(flashScrollIndicators:(nonnull NSNumber *)reactTag)
}];
}
// ********************
// add pull to refresh
// ********************
RCT_EXPORT_VIEW_PROPERTY(onRefreshData, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(isOnPullToRefresh, BOOL)
RCT_EXPORT_VIEW_PROPERTY(enablePullToRefresh, BOOL)
RCT_EXPORT_METHOD(stopPullToRefresh:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTScrollView *> *viewRegistry) {
RCTScrollView *view = viewRegistry[reactTag];
if (!view || ![view isKindOfClass:[RCTScrollView class]]) {
RCTLogError(@"Cannot find RCTScrollView with tag #%@", reactTag);
return;
}
[view stopPullToRefresh];
}];
}
RCT_EXPORT_METHOD(startPullToRefresh:(nonnull NSNumber *)reactTag)
{
[self.bridge.uiManager addUIBlock:
^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTScrollView *> *viewRegistry) {
RCTScrollView *view = viewRegistry[reactTag];
if (!view || ![view isKindOfClass:[RCTScrollView class]]) {
RCTLogError(@"Cannot find RCTScrollView with tag #%@", reactTag);
return;
}
[view startPullToRefresh];
}];
}
// ********************
// add pull to refresh
// ********************
@end
... ...