Skip to content

Commit

Permalink
feat: support setVerticalScrollEnabled (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
huhuanming authored Nov 29, 2024
1 parent 5565421 commit 3e158b0
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ data class TabProps(

class HomePageManager : ViewGroupManager<HomePageView>() {
companion object {
const val COMMAND_SET_PAGE_INDEX = "setPageIndex"
const val COMMAND_SET_REFRESHING = "setRefreshing"
const val COMMAND_SET_PAGE_INDEX = 1
const val COMMAND_SET_REFRESHING = 2
const val COMMAND_SET_VERTICAL_SCROLL_ENABLED = 3
}

private val REACT_CLASS = "NestedTabView"
Expand Down Expand Up @@ -137,8 +138,15 @@ class HomePageManager : ViewGroupManager<HomePageView>() {
)
}

override fun getCommandsMap(): Map<String, Int> {
return mapOf(
"setPageIndex" to COMMAND_SET_PAGE_INDEX,
"setRefreshing" to COMMAND_SET_REFRESHING,
"setVerticalScrollEnabled" to COMMAND_SET_VERTICAL_SCROLL_ENABLED
)
}

override fun receiveCommand(view: HomePageView, commandId: String?, args: ReadableArray?) {
override fun receiveCommand(view: HomePageView, commandId: Int, args: ReadableArray?) {
super.receiveCommand(view, commandId, args)
when (commandId) {
COMMAND_SET_PAGE_INDEX -> if (args != null) {
Expand All @@ -150,6 +158,11 @@ class HomePageManager : ViewGroupManager<HomePageView>() {
val refresh = args.getBoolean(0)
view.setRefresh(refresh)
}

COMMAND_SET_VERTICAL_SCROLL_ENABLED -> if (args != null) {
val enabled = args.getBoolean(0)
view.setVerticalScrollEnabled(enabled)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ open class HomePageLayout @JvmOverloads constructor(
private var mAppBarExtended = true
private var mHeaderHeight = 56

private var mVerticalScrollEnabled = true

val eventDispatcher =
(context as ReactContext).getNativeModule(UIManagerModule::class.java)?.eventDispatcher

Expand Down Expand Up @@ -138,6 +140,21 @@ open class HomePageLayout @JvmOverloads constructor(
}
}

fun setVerticalScrollEnabled(enabled: Boolean) {
mVerticalScrollEnabled = enabled
content.findViewById<AppBarLayout>(R.id.appbar)?.let {
it.setExpanded(true, false)
val params = it.layoutParams as androidx.coordinatorlayout.widget.CoordinatorLayout.LayoutParams
val behavior = AppBarLayout.Behavior()
behavior.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
override fun canDrag(appBarLayout: AppBarLayout): Boolean {
return mVerticalScrollEnabled
}
})
params.behavior = behavior
}
}

fun initRefreshListener() {
content.findViewById<SwipeRefreshLayout>(R.id.layout_refresh)?.let {
it.setOnRefreshListener {
Expand Down
2 changes: 2 additions & 0 deletions ios/PagingView/PagingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ typedef NSString* _Nonnull (^RenderItemBlock)(NSDictionary *body);

- (void)setRefresh:(BOOL)refresh;

- (void)setVerticalScrollEnabled:(BOOL)verticalScrollEnabled;

@end

NS_ASSUME_NONNULL_END
14 changes: 13 additions & 1 deletion ios/PagingView/PagingView.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ @interface PagingView ()<JXPagerViewDelegate,JXCategoryViewDelegate>

@property (nonatomic, assign) BOOL refresh;
@property (nonatomic, assign) BOOL disableRefresh;
@property (nonatomic, assign) BOOL verticalScrollEnabled;
@property (nonatomic, copy) NSString *spinnerColor;

@property (nonatomic, weak) UIWindow *window;
Expand All @@ -52,6 +53,7 @@ -(instancetype)init {
if (self){
NSLog(@"PagingView init");
[self startObservingViewPosition];
_verticalScrollEnabled = YES;
_isScrolling = NO;
}
return self;
Expand Down Expand Up @@ -92,7 +94,6 @@ - (void)checkViewPosition {
}
}


- (void)setPageIndex:(NSInteger)index{
[self.tabView.categoryView selectItemAtIndex:index];
}
Expand Down Expand Up @@ -220,6 +221,13 @@ -(void)setDisableRefresh:(BOOL)disableRefresh {
}


- (void) setVerticalScrollEnabled: (BOOL) verticalScrollEnabled {
if (_verticalScrollEnabled != verticalScrollEnabled) {
_verticalScrollEnabled = verticalScrollEnabled;
self.pagingView.mainTableView.scrollEnabled = verticalScrollEnabled;
}
}

-(JXPagerView *)pagingView {
if (!_pagingView) {
_pagingView = [[JXPagerView alloc] initWithDelegate:self];
Expand All @@ -238,6 +246,10 @@ -(JXPagerView *)pagingView {
[_pagingView.listContainerView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[self addSubview:_pagingView];
}

if (_pagingView != nil && _pagingView.mainTableView.scrollEnabled != self.verticalScrollEnabled) {
_pagingView.mainTableView.scrollEnabled = self.verticalScrollEnabled;
}
return _pagingView;
}

Expand Down
9 changes: 9 additions & 0 deletions ios/PagingView/PagingViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ @implementation PagingViewManager
}];
}

RCT_EXPORT_METHOD(setVerticalScrollEnabled:(nonnull NSNumber *)reactTag verticalScrollEnabled:(BOOL)verticalScrollEnabled) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
PagingView *view = (PagingView *)viewRegistry[reactTag];
if (!!view && [view isKindOfClass:[PagingView class]]) {
[view setVerticalScrollEnabled:verticalScrollEnabled];
}
}];
}

- (UIView *)view
{
return [[PagingView alloc] init];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onekeyfe/react-native-tab-page-view",
"version": "1.0.16",
"version": "1.0.17",
"description": "React Native Tab Page View",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
32 changes: 28 additions & 4 deletions src/NestedTabView/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import { useRef } from 'react';
import { forwardRef, useImperativeHandle, useRef } from 'react';

import { requireNativeComponent } from 'react-native';
import {
UIManager,
findNodeHandle,
requireNativeComponent,
} from 'react-native';

const BaseNestedTabView = requireNativeComponent('NestedTabView');

function NestedTabViewContainer(props: any) {
function NestedTabViewContainer(props: any, forwardRef: any) {
const point = useRef({ x: 0, y: 0 });
const ref = useRef<{ _nativeTag?: number }>();

useImperativeHandle(
forwardRef,
() => ({
setVerticalScrollEnabled: (scrollEnabled: boolean) => {
if (ref?.current?._nativeTag) {
UIManager.dispatchViewManagerCommand(
findNodeHandle(ref.current._nativeTag),
UIManager.getViewManagerConfig('NestedTabView').Commands
.setVerticalScrollEnabled as number,
[scrollEnabled]
);
}
},
}),
[]
);

return (
// @ts-ignore
<BaseNestedTabView
ref={ref}
onStartShouldSetResponderCapture={(e: any) => {
const { locationX: x, locationY: y } = e.nativeEvent;
point.current = { x, y };
Expand All @@ -25,4 +49,4 @@ function NestedTabViewContainer(props: any) {
);
}

export default NestedTabViewContainer;
export default forwardRef<any, any>(NestedTabViewContainer);

0 comments on commit 3e158b0

Please sign in to comment.