From 9d5c50e4342c420a07f789bcf86cb01ff0cb2911 Mon Sep 17 00:00:00 2001 From: vishtree Date: Fri, 26 Feb 2021 21:55:47 +0100 Subject: [PATCH] Fixing prop change check --- .../MvcpScrollViewManagerModule.java | 12 +++++--- src/FlatList.tsx | 28 ++++++------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java b/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java index c9da42d..af26711 100644 --- a/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java +++ b/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java @@ -109,10 +109,14 @@ public void onLayoutUpdated(ReactShadowNode root) { @ReactMethod public void disableMaintainVisibleContentPosition(int key, Promise promise) { - if (key >= 0) { - final UIManagerModule uiManagerModule = this.reactContext.getNativeModule(UIManagerModule.class); - uiManagerModule.removeUIManagerListener(uiManagerModuleListeners.remove(key)); + try { + if (key >= 0) { + final UIManagerModule uiManagerModule = this.reactContext.getNativeModule(UIManagerModule.class); + uiManagerModule.removeUIManagerListener(uiManagerModuleListeners.remove(key)); + } + promise.resolve(null); + } catch (IllegalViewOperationException e) { + promise.resolve(-1); } - promise.resolve(null); } } diff --git a/src/FlatList.tsx b/src/FlatList.tsx index dd9fe8e..f9b6b01 100644 --- a/src/FlatList.tsx +++ b/src/FlatList.tsx @@ -1,11 +1,5 @@ import React, { MutableRefObject, useRef } from 'react'; -import { - FlatList, - FlatListProps, - findNodeHandle, - NativeModules, - Platform, -} from 'react-native'; +import { FlatList, FlatListProps, NativeModules, Platform } from 'react-native'; export const MvcpScrollViewManager = NativeModules.MvcpScrollViewManager; @@ -24,19 +18,13 @@ export default (React.forwardRef( const minIndexForVisible = useRef(); const cleanupPromiseRef = useRef>(); - const getAutoscrollToTopThresholdFromProp = () => - mvcp?.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; - - const getMinIndexForVisibleFromProp = () => mvcp?.minIndexForVisible || 1; - const resetMvcpIfNeeded = (): void => { if (!mvcp || Platform.OS !== 'android' || !flRef.current) { return; } if ( - autoscrollToTopThreshold.current === - getAutoscrollToTopThresholdFromProp() && - minIndexForVisible.current === getMinIndexForVisibleFromProp() + autoscrollToTopThreshold.current === mvcp?.autoscrollToTopThreshold && + minIndexForVisible.current === mvcp?.minIndexForVisible ) { // Don't do anythinig if the values haven't changed return; @@ -47,14 +35,14 @@ export default (React.forwardRef( MvcpScrollViewManager.disableMaintainVisibleContentPosition(handle); }); - autoscrollToTopThreshold.current = getAutoscrollToTopThresholdFromProp(); - minIndexForVisible.current = getMinIndexForVisibleFromProp(); + autoscrollToTopThreshold.current = mvcp?.autoscrollToTopThreshold; + minIndexForVisible.current = mvcp?.minIndexForVisible; - const viewTag = findNodeHandle(flRef.current); + const viewTag = flRef.current.getScrollableNode(); cleanupPromiseRef.current = MvcpScrollViewManager.enableMaintainVisibleContentPosition( viewTag, - autoscrollToTopThreshold.current, - minIndexForVisible.current + autoscrollToTopThreshold.current || -Number.MAX_SAFE_INTEGER, + minIndexForVisible.current || 1 ); };