Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
youniaogu committed Sep 1, 2022
1 parent 57fca5d commit 974f8ac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/components/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const Controller = ({ onTap, children, horizontal = false }: ControllerProps) =>
<GestureDetector gesture={pinchGesture}>
<GestureDetector gesture={panGesture}>
{horizontal ? (
<Box w={windowWidth} h={windowHeight} bg="black" safeArea>
<Box w={windowWidth} h={windowHeight} safeArea>
<Animated.View style={animatedStyle}>{children}</Animated.View>
</Box>
) : (
Expand Down
4 changes: 2 additions & 2 deletions src/components/JMComicImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ const JMComicImage = ({ uri, headers = {}, horizontal = false, onSuccess }: JMCo
let prevDh: number = 0;
step.forEach(({ dx, dy, sx, sy, sWidth, sHeight, dWidth, dHeight }, index) => {
if (index <= 0) {
prevDy = Math.round(dy * container.scale * 1000) / 1000;
prevDy = dy * container.scale;
} else {
prevDy = prevDh + prevDy;
}
prevDh = Math.round(dHeight * container.scale * 1000) / 1000;
prevDh = dHeight * container.scale;

ctx.drawImage(
image,
Expand Down
60 changes: 14 additions & 46 deletions src/components/Reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Controller from '~/components/Controller';
import PageSlider, { PageSliderRef } from '~/components/PageSlider';

const windowWidth = Dimensions.get('window').width;
const windowHeight = Dimensions.get('window').height;
const lastPageToastId = 'LAST_PAGE_TOAST_ID';

interface ReaderProps {
Expand All @@ -35,13 +34,10 @@ const Reader = ({
const toast = useToast();
const [page, setPage] = useState(initPage);
const [showExtra, setShowExtra] = useState(false);
const layout = useRef(
data.map((_, index) => ({ length: windowHeight, offset: index * windowHeight, index }))
);
const timeout = useRef<NodeJS.Timeout | null>(null);
const flatListRef = useRef<FlatListRN>(null);
const pageSliderRef = useRef<PageSliderRef>(null);
const initialScrollIndex = Math.max(Math.min(initPage - 1, data.length - 1), 0);
const initialScrollIndex = Math.max(Math.min(page - 1, data.length - 1), 0);
const toastRef = useRef(toast);
const dataRef = useRef(data);
toastRef.current = toast;
Expand Down Expand Up @@ -75,31 +71,16 @@ const Reader = ({
}, 200);
}, []);
const renderVerticalItem = useCallback(
({ item, index }: ListRenderItemInfo<typeof data[0]>) => {
({ item }: ListRenderItemInfo<typeof data[0]>) => {
const { uri, needUnscramble } = item;
const handleSuccess = (image: { width: number; height: number }) => {
const prevIndex = layout.current[index].index;
const prevLength = layout.current[index].length;
const newLayout = layout.current.map((curr) => {
if (curr.index < prevIndex) {
return curr;
}
return {
...curr,
length: image.height,
offset: curr.offset - prevLength + image.height,
};
});
layout.current === newLayout;
};

return needUnscramble ? (
<JMComicImage uri={uri} headers={headers} onSuccess={handleSuccess} />
<JMComicImage uri={uri} headers={headers} />
) : (
<ImageWithRetry uri={uri} headers={headers} onSuccess={handleSuccess} />
<ImageWithRetry uri={uri} headers={headers} />
);
},
[headers, layout]
[headers]
);
const renderHorizontalItem = useCallback(
({ item }: ListRenderItemInfo<typeof data[0]>) => {
Expand All @@ -117,7 +98,7 @@ const Reader = ({
},
[headers, toggleExtra]
);
const handleSliderChangeEndHor = useCallback((newStep: number) => {
const handleSliderChangeEnd = useCallback((newStep: number) => {
const newPage = Math.floor(newStep);

setPage(newPage);
Expand All @@ -128,15 +109,6 @@ const Reader = ({
flatListRef.current?.scrollToOffset({ offset: 1, animated: false });
}
}, []);
const handleSliderChangeEndVer = useCallback((newStep: number) => {
const newPage = Math.floor(newStep);
const offset = layout.current[newPage].offset;

setPage(newPage);
flatListRef.current?.scrollToOffset({
offset: offset <= 0 ? 1 : offset,
});
}, []);

const handleVertical = () => {
onModeChange(false);
Expand Down Expand Up @@ -210,12 +182,6 @@ const Reader = ({
/>
<Text fontSize="md" w="3/5" numberOfLines={1} color="white" fontWeight="bold">
{title}
{title}
{title}
{title}
{title}
{title}
{title}
</Text>
<Box flex={1} />
<Text color="white" fontWeight="bold">
Expand All @@ -238,12 +204,14 @@ const Reader = ({
)}
</Flex>

<PageSlider
ref={pageSliderRef}
max={data.length}
defaultValue={page}
onSliderChangeEnd={horizontal ? handleSliderChangeEndHor : handleSliderChangeEndVer}
/>
{horizontal && (
<PageSlider
ref={pageSliderRef}
max={data.length}
defaultValue={page}
onSliderChangeEnd={handleSliderChangeEnd}
/>
)}
</Fragment>
)}
</Box>
Expand Down

0 comments on commit 974f8ac

Please sign in to comment.