Skip to content
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

extending Volume button feature, by scroll amount #587

Merged
merged 2 commits into from
Apr 6, 2023
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
2 changes: 1 addition & 1 deletion src/redux/settings/settings.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const initialState = {
swipeGestures: false,
showScrollPercentage: true,
useVolumeButtons: false,
volumeButtonScrollAmount: 100,
scrollAmount: 200,
showBatteryAndTime: false,
autoScroll: false,
autoScrollInterval: 10,
Expand Down
9 changes: 5 additions & 4 deletions src/screens/reader/ReaderScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ const ChapterContent = ({ route, navigation }) => {
const {
swipeGestures = false,
useVolumeButtons = false,
volumeButtonScrollAmount = 100,
autoScroll = false,
autoScrollInterval = 10,
scrollAmount = 200,
autoScrollOffset = null,
verticalSeekbar = true,
removeExtraParagraphSpacing = false,
Expand Down Expand Up @@ -118,12 +118,12 @@ const ChapterContent = ({ route, navigation }) => {
VolumeButtonListener.preventDefault();
emmiter.current.addListener('VolumeUp', e => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${-volumeButtonScrollAmount},behavior:'smooth',})
window.scrollBy({top:${-scrollAmount},behavior:'smooth',})
})()`);
});
emmiter.current.addListener('VolumeDown', e => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${volumeButtonScrollAmount},behavior:'smooth',})
window.scrollBy({top:${scrollAmount},behavior:'smooth',})
})()`);
});
};
Expand All @@ -135,13 +135,14 @@ const ChapterContent = ({ route, navigation }) => {
VolumeButtonListener.disconnect();
emmiter.current.removeAllListeners('VolumeUp');
emmiter.current.removeAllListeners('VolumeDown');
// this is just for sure, without it app still works properly
}
return () => {
VolumeButtonListener.disconnect();
emmiter.current.removeAllListeners('VolumeUp');
emmiter.current.removeAllListeners('VolumeDown');
};
}, [useVolumeButtons, volumeButtonScrollAmount]);
}, [useVolumeButtons, scrollAmount]);

const onLayout = useCallback(e => {
setTimeout(() => connectVolumeButton());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
default as BottomSheetType,
BottomSheetView,
} from '@gorhom/bottom-sheet';

import { defaultTo } from 'lodash-es';
import BottomSheet from '@components/BottomSheet/BottomSheet';
import { useAppDispatch, useSettingsV1 } from '../../../../redux/hooks';
import { useTheme } from '@hooks/useTheme';
Expand Down Expand Up @@ -49,23 +51,11 @@ const GeneralTab: React.FC = () => {
showBatteryAndTime,
showScrollPercentage,
useVolumeButtons = false,
volumeButtonScrollAmount: initialVolumeButtonScrollAmount,
scrollAmount = 200,
swipeGestures = false,
removeExtraParagraphSpacing = false,
} = useSettingsV1();

const [volumeButtonScrollAmount, setVolumeButtonScrollAmount] = useState(
initialVolumeButtonScrollAmount || 0,
);

const handleVolumeButtonScrollAmountChange = (value: string) => {
const amount = parseInt(value, 10);
if (!isNaN(amount)) {
setVolumeButtonScrollAmount(amount);
dispatch(setAppSettings('volumeButtonScrollAmount', amount));
}
};

return (
<View>
<ReaderSheetPreferenceItem
Expand Down Expand Up @@ -130,33 +120,33 @@ const GeneralTab: React.FC = () => {
theme={theme}
/>
<ReaderSheetPreferenceItem
label={getString('readerScreen.bottomSheet.scrollAmount')}
label={getString('readerScreen.bottomSheet.volumeButtonsScroll')}
onPress={() =>
dispatch(setAppSettings('useVolumeButtons', !useVolumeButtons))
}
value={useVolumeButtons}
theme={theme}
/>
{useVolumeButtons && (
{useVolumeButtons ? (
<View style={styles.textFieldContainer}>
<Text style={{ color: theme.onSurfaceVariant }}>
<Text style={[styles.paddingRightM]} numberOfLines={2}>
{getString('readerScreen.bottomSheet.scrollAmount')}
</Text>
<TextInput
style={[
styles.textField,
{ borderColor: theme.outline, color: theme.onSurface },
]}
style={styles.textField}
defaultValue={defaultTo(scrollAmount, 200).toString()}
keyboardType="numeric"
value={volumeButtonScrollAmount.toString()}
onChangeText={handleVolumeButtonScrollAmountChange}
onChangeText={text => {
if (text) {
dispatch(setAppSettings('scrollAmount', Number(text)));
}
}}
/>
</View>
)}
) : null}
</View>
);
};

interface ReaderBottomSheetV2Props {
bottomSheetRef: Ref<BottomSheetType> | null;
}
Expand Down Expand Up @@ -268,4 +258,8 @@ const styles = StyleSheet.create({
width: 100,
textAlign: 'right',
},
paddingRightM: {
flex: 1,
paddingRight: 16,
},
});
54 changes: 25 additions & 29 deletions src/screens/settings/SettingsReaderScreen/SettingsReaderScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
useWindowDimensions,
} from 'react-native';
import React, { useState } from 'react';

import { useNavigation } from '@react-navigation/native';
import { defaultTo } from 'lodash-es';
import WebView from 'react-native-webview';
Expand Down Expand Up @@ -65,11 +64,11 @@ const SettingsReaderScreen = () => {
const theme = useTheme();
const navigation = useNavigation();
const dispatch = useAppDispatch();
const { useVolumeButtons = false, volumeButtonScrollAmount } =
useSettingsV1();

const readerSettings = useReaderSettings();
const {
useVolumeButtons = false,
scrollAmount = 200,
verticalSeekbar = true,
swipeGestures = false,
autoScroll = false,
Expand Down Expand Up @@ -196,6 +195,28 @@ const SettingsReaderScreen = () => {
}
theme={theme}
/>
{useVolumeButtons ? (
<View style={styles.scrollAmount}>
<Text style={[labelStyle, styles.paddingRightM]} numberOfLines={2}>
{getString('readerScreen.bottomSheet.scrollAmount')}
</Text>
<TextInput
style={{
paddingHorizontal: 16,
r1di marked this conversation as resolved.
Show resolved Hide resolved
paddingBottom: 8,
flexDirection: 'row',
}}
defaultValue={defaultTo(scrollAmount, 100).toString()}
keyboardType="numeric"
onChangeText={text => {
if (text) {
dispatch(setAppSettings('scrollAmount', Number(text)));
}
}}
/>
</View>
) : null}

<SwitchItem
label={getString('readerScreen.bottomSheet.swipeGestures')}
value={swipeGestures}
Expand All @@ -210,7 +231,6 @@ const SettingsReaderScreen = () => {
onPress={() => dispatch(setAppSettings('autoScroll', !autoScroll))}
theme={theme}
/>

{autoScroll ? (
<>
<List.Divider theme={theme} />
Expand Down Expand Up @@ -276,30 +296,6 @@ const SettingsReaderScreen = () => {
) : null}
</>
) : null}

{useVolumeButtons && (
<View style={styles.volumeButtonScrollAmount}>
<Text style={[labelStyle, styles.paddingRightM]} numberOfLines={2}>
{getString('readerScreen.bottomSheet.scrollAmount')}
</Text>
<TextInput
style={{
paddingHorizontal: 16,
paddingBottom: 8,
flexDirection: 'row',
}}
defaultValue={defaultTo(volumeButtonScrollAmount, 100).toString()}
keyboardType="numeric"
onChangeText={text => {
if (text) {
dispatch(
setAppSettings('volumeButtonScrollAmount', Number(text)),
);
}
}}
/>
</View>
)}
<>
<List.Divider theme={theme} />
<List.SubHeader theme={theme}>
Expand Down Expand Up @@ -536,7 +532,7 @@ const styles = StyleSheet.create({
flex: 1,
paddingRight: 16,
},
volumeButtonScrollAmount: {
scrollAmount: {
paddingHorizontal: 16,
paddingBottom: 8,
flexDirection: 'row',
Expand Down
2 changes: 1 addition & 1 deletion strings/languages/en/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"useChapterDrawerSwipeNavigation": "Swipe right to open drawer",
"removeExtraSpacing": "Remove extra paragraph spacing",
"volumeButtonsScroll": "Volume buttons scroll",
"scrollAmount": "The amount you want to scroll",
"scrollAmount": "Scroll amount:",
"showSwipeMargins": "Show swipe margins"
},
"drawer": {
Expand Down
2 changes: 1 addition & 1 deletion strings/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface StringMap {
'readerScreen.bottomSheet.useChapterDrawerSwipeNavigation': 'string';
'readerScreen.bottomSheet.removeExtraSpacing': 'string';
'readerScreen.bottomSheet.volumeButtonsScroll': 'string';
'readerScreen.bottomSheet.scrollAmount': 'number';
'readerScreen.bottomSheet.scrollAmount': 'string';
'readerScreen.bottomSheet.showSwipeMargins': 'string';
'readerScreen.drawer.scrollToCurrentChapter': 'string';
'readerScreen.drawer.scrollToTop': 'string';
Expand Down