Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions src/ReaderScreen/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

const elementIdToSave = currentElementIdRef.current || currentElementId;
const elementIdToSave = currentElementIdRef.current;
if (elementIdToSave) {
dispatch(actions.setPosition(elementIdToSave, id));
}
}, [dispatch, id, currentElementId]);
}, [dispatch, id, currentElementIdRef.current]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 currentElementIdRef changes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


useEffect(() => {
dispatch(actions.setCurrentBani({ id, title, titleUni }));
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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));
Expand All @@ -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]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.
It won't trigger re-render, so this callback won't trigger when currentElementIdRef changes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


const handleError = useCallback((syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
Expand Down
26 changes: 18 additions & 8 deletions src/ReaderScreen/utils/gutkaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let isScrolling;
let isManuallyScrolling = false;
let lastHighlightedElement = null;
let highlightTimeout = null;
let hasReachedEnd = false;

const clearScrollTimeout=()=> {
if (autoScrollTimeout != null) {
Expand All @@ -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;
Expand All @@ -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) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix the code indentation here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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;
}
Expand Down
Loading