-
Notifications
You must be signed in to change notification settings - Fork 16
Updated “Reached End” experience #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,11 +44,10 @@ const Reader = ({ navigation, route }) => { | |
| const { title, id, titleUni } = route.params.params || {}; | ||
| const [isHeader, toggleHeader] = useState(false); | ||
| const [viewLoaded, toggleViewLoaded] = useState(false); | ||
| const [currentElementId, setCurrentElementId] = useState(savePosition[id] || null); | ||
| const [shouldNavigateBack, setShouldNavigateBack] = useState(false); | ||
| const [dateKey, setDateKey] = useState(Date.now().toString()); | ||
| const [titleText, setTitleText] = useState(null); | ||
| const currentElementIdRef = useRef(null); | ||
| const currentElementIdRef = useRef(savePosition[id] || null); | ||
|
|
||
| const dispatch = useDispatch(); | ||
| const { shabad, isLoading } = useFetchShabad(id); | ||
|
|
@@ -59,11 +58,11 @@ const Reader = ({ navigation, route }) => { | |
| // Save element ID when leaving screen or app goes to background | ||
| const saveScrollPosition = useCallback(() => { | ||
| // Prefer element ID if available, otherwise fall back to currentElementId state | ||
| const elementIdToSave = currentElementIdRef.current || currentElementId; | ||
| const elementIdToSave = currentElementIdRef.current; | ||
| if (elementIdToSave) { | ||
| dispatch(actions.setPosition(elementIdToSave, id)); | ||
| } | ||
| }, [dispatch, id, currentElementId]); | ||
| }, [dispatch, id, currentElementIdRef.current]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refs don't trigger re-render, so this callback won't trigger when
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| useEffect(() => { | ||
| dispatch(actions.setCurrentBani({ id, title, titleUni })); | ||
|
|
@@ -144,11 +143,9 @@ const Reader = ({ navigation, route }) => { | |
| const savedElementId = savePosition[id]; | ||
| // Check if it's a number (old position format) or string (element ID) | ||
| if (typeof savedElementId === "string") { | ||
| setCurrentElementId(savedElementId); | ||
| currentElementIdRef.current = savedElementId; | ||
| } else if (typeof savedElementId === "number" && savedElementId > 0.9) { | ||
| // Old position format - reset to null if at end | ||
| setCurrentElementId(null); | ||
| currentElementIdRef.current = null; | ||
| } | ||
| } | ||
|
|
@@ -181,19 +178,13 @@ const Reader = ({ navigation, route }) => { | |
| } else if (data.includes("scroll-elementId-")) { | ||
| // Capture element ID from WebView scroll events | ||
| const elementId = data.split("scroll-elementId-")[1]; | ||
| setCurrentElementId(elementId); | ||
| currentElementIdRef.current = elementId; | ||
| // Save immediately when element ID changes | ||
| dispatch(actions.setPosition(elementId, id)); | ||
| if (shouldNavigateBack) { | ||
| navigation.goBack(); | ||
| setShouldNavigateBack(false); | ||
| } | ||
| } else if (data.includes("reached-end")) { | ||
| // User reached the end - reset saved position | ||
| setCurrentElementId(null); | ||
| currentElementIdRef.current = null; | ||
| dispatch(actions.setPosition(0, id)); | ||
| } else if (data.includes("sequenceString-")) { | ||
| const sequenceStringData = data.split("-")[1]; | ||
| dispatch(actions.setBookmarkSequenceString(sequenceStringData)); | ||
|
|
@@ -210,14 +201,14 @@ const Reader = ({ navigation, route }) => { | |
|
|
||
| const handleLoadEnd = useCallback(() => { | ||
| // Scroll to saved element ID after WebView is fully loaded | ||
| if (webViewRef.current && currentElementId) { | ||
| if (webViewRef.current && currentElementIdRef.current) { | ||
| const scrollMessage = { | ||
| action: "scrollToPosition", | ||
| elementId: currentElementId, | ||
| elementId: currentElementIdRef.current, | ||
| }; | ||
| webViewRef.current.postMessage(JSON.stringify(scrollMessage)); | ||
| } | ||
| }, [currentElementId]); | ||
| }, [currentElementIdRef.current]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| const handleError = useCallback((syntheticEvent) => { | ||
| const { nativeEvent } = syntheticEvent; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ let isScrolling; | |
| let isManuallyScrolling = false; | ||
| let lastHighlightedElement = null; | ||
| let highlightTimeout = null; | ||
| let hasReachedEnd = false; | ||
|
|
||
| const clearScrollTimeout=()=> { | ||
| if (autoScrollTimeout != null) { | ||
|
|
@@ -22,12 +23,6 @@ const clearScrollTimeout=()=> { | |
|
|
||
| const scrollFunc=(e)=> { | ||
| const elementId = getTopmostElementId(); | ||
| if (elementId) { | ||
| window.ReactNativeWebView.postMessage("scroll-elementId-" + elementId); | ||
| } | ||
| if (window.scrollY == 0) { | ||
| window.ReactNativeWebView.postMessage("show"); | ||
| } | ||
|
|
||
| // Check if user has reached the end of the document | ||
| const scrollHeight = document.documentElement.scrollHeight; | ||
|
|
@@ -36,10 +31,25 @@ const scrollFunc=(e)=> { | |
| const threshold = 50; // pixels from bottom to consider "at end" | ||
| const isAtEnd = scrollTop + clientHeight >= scrollHeight - threshold; | ||
|
|
||
| if (isAtEnd) { | ||
| window.ReactNativeWebView.postMessage("reached-end"); | ||
| if (isAtEnd && !hasReachedEnd) { | ||
| hasReachedEnd = true; | ||
| } else if (!isAtEnd && hasReachedEnd) { | ||
| // Reset flag when user scrolls away from the end | ||
| hasReachedEnd = false; | ||
| } | ||
| if (elementId && !hasReachedEnd) { | ||
| window.ReactNativeWebView.postMessage("scroll-elementId-" + elementId); | ||
| } | ||
| else if (hasReachedEnd) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you fix the code indentation here.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| window.ReactNativeWebView.postMessage("scroll-elementId-null"); | ||
| } | ||
|
|
||
|
|
||
| if (window.scrollY == 0) { | ||
| window.ReactNativeWebView.postMessage("show"); | ||
| } | ||
|
|
||
|
|
||
| if (typeof scrollFunc.y == "undefined") { | ||
| scrollFunc.y = window.pageYOffset; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This state doesn't exist anymore, so the comment can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed