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 all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ScrollView;
import android.view.WindowManager;

import com.facebook.react.ReactActivity;
Expand Down Expand Up @@ -60,15 +61,15 @@ public boolean dispatchKeyEvent(KeyEvent event) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
// volup
VolumeButtonListener.up();
VolumeButtonListener.up(VolumeButtonListener.getScrollAmount());
if (!VolumeButtonListener.prevent)
return super.dispatchKeyEvent(event);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
// voldown
VolumeButtonListener.down();
VolumeButtonListener.down(VolumeButtonListener.getScrollAmount());
if (!VolumeButtonListener.prevent)
return super.dispatchKeyEvent(event);
}
Expand All @@ -79,7 +80,6 @@ public boolean dispatchKeyEvent(KeyEvent event) {
}
return super.dispatchKeyEvent(event);
}

/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
Expand All @@ -99,4 +99,4 @@ protected ReactRootView createRootView() {
};
}

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.rajarsheechatterjee.VolumeButtonListener;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

Expand All @@ -27,22 +27,33 @@ public static boolean isActive() {

public static boolean prevent = false;

public static void sendEvent(boolean up) {
public static void sendEvent(boolean up, int amount) {
if (active) {
WritableMap args = Arguments.createMap();
args.putInt("scrollAmount", amount); // add scroll amount to event data
if (up)
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("VolumeUp", args);
else
appContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("VolumeDown", args);
}
}

public static void down() {
sendEvent(false);
public static void down(int amount) {
sendEvent(false, amount);
}

public static void up(int amount) {
sendEvent(true, amount);
}

private static int scrollAmount = 100;

public static void setScrollAmount(int amount) {
scrollAmount = amount;
}

public static void up() {
sendEvent(true);
public static int getScrollAmount() {
return scrollAmount;
r1di marked this conversation as resolved.
Show resolved Hide resolved
}

@ReactMethod
Expand Down Expand Up @@ -90,4 +101,14 @@ public void addListener(String eventName) {
public void removeListeners(Integer count) {
// Keep: Required for RN built in Event Emitter Calls.
}
}

@ReactMethod
public void setVolumeButtonScrollAmount(int amount) {
setScrollAmount(amount);
}

@ReactMethod
public void getVolumeButtonScrollAmount(Promise promise) {
promise.resolve(getScrollAmount());
}
}
1 change: 1 addition & 0 deletions src/redux/settings/settings.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const initialState = {
swipeGestures: false,
showScrollPercentage: true,
useVolumeButtons: false,
scrollAmount: 200,
showBatteryAndTime: false,
autoScroll: false,
autoScrollInterval: 10,
Expand Down
14 changes: 6 additions & 8 deletions src/screens/reader/ReaderScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const ChapterContent = ({ route, navigation }) => {
useVolumeButtons = false,
autoScroll = false,
autoScrollInterval = 10,
scrollAmount = 200,
autoScrollOffset = null,
verticalSeekbar = true,
removeExtraParagraphSpacing = false,
Expand Down Expand Up @@ -117,16 +118,13 @@ const ChapterContent = ({ route, navigation }) => {
VolumeButtonListener.preventDefault();
emmiter.current.addListener('VolumeUp', e => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${-Dimensions.get('window')
.height},behavior:'smooth',})
})()`);
window.scrollBy({top:${-scrollAmount},behavior:'smooth',})
})()`);
});
emmiter.current.addListener('VolumeDown', e => {
webViewRef.current?.injectJavaScript(`(()=>{
window.scrollBy({top:${
Dimensions.get('window').height
},behavior:'smooth',})
})()`);
window.scrollBy({top:${scrollAmount},behavior:'smooth',})
})()`);
});
};

Expand All @@ -144,7 +142,7 @@ const ChapterContent = ({ route, navigation }) => {
emmiter.current.removeAllListeners('VolumeUp');
emmiter.current.removeAllListeners('VolumeDown');
};
}, [useVolumeButtons, chapter]);
}, [useVolumeButtons, scrollAmount]);

const onLayout = useCallback(e => {
setTimeout(() => connectVolumeButton());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { StyleSheet, Text, useWindowDimensions, View } from 'react-native';
import {
StyleSheet,
Text,
useWindowDimensions,
View,
TextInput,
} from 'react-native';
import React, { Ref, useMemo, useState } from 'react';
import color from 'color';

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 @@ -43,6 +51,7 @@ const GeneralTab: React.FC = () => {
showBatteryAndTime,
showScrollPercentage,
useVolumeButtons = false,
scrollAmount = 200,
swipeGestures = false,
removeExtraParagraphSpacing = false,
} = useSettingsV1();
Expand Down Expand Up @@ -118,10 +127,26 @@ const GeneralTab: React.FC = () => {
value={useVolumeButtons}
theme={theme}
/>
{useVolumeButtons ? (
<View style={styles.textFieldContainer}>
<Text style={[styles.paddingRightM]} numberOfLines={2}>
{getString('readerScreen.bottomSheet.scrollAmount')}
</Text>
<TextInput
style={styles.textField}
defaultValue={defaultTo(scrollAmount, 200).toString()}
keyboardType="numeric"
onChangeText={text => {
if (text) {
dispatch(setAppSettings('scrollAmount', Number(text)));
}
}}
/>
</View>
) : null}
</View>
);
};

interface ReaderBottomSheetV2Props {
bottomSheetRef: Ref<BottomSheetType> | null;
}
Expand Down Expand Up @@ -218,4 +243,23 @@ const styles = StyleSheet.create({
readerTab: {
paddingVertical: 8,
},
textFieldContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 16,
paddingVertical: 8,
},
textField: {
borderWidth: 1,
borderRadius: 4,
paddingHorizontal: 8,
paddingVertical: 4,
width: 100,
textAlign: 'right',
},
paddingRightM: {
flex: 1,
paddingRight: 16,
},
});
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 @@ -69,6 +68,7 @@ const SettingsReaderScreen = () => {
const readerSettings = useReaderSettings();
const {
useVolumeButtons = false,
scrollAmount = 200,
verticalSeekbar = true,
swipeGestures = false,
autoScroll = false,
Expand Down Expand Up @@ -195,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 Down Expand Up @@ -510,4 +532,11 @@ const styles = StyleSheet.create({
flex: 1,
paddingRight: 16,
},
scrollAmount: {
paddingHorizontal: 16,
paddingBottom: 8,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
});
1 change: 1 addition & 0 deletions strings/languages/en/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"useChapterDrawerSwipeNavigation": "Swipe right to open drawer",
"removeExtraSpacing": "Remove extra paragraph spacing",
"volumeButtonsScroll": "Volume buttons scroll",
"scrollAmount": "Scroll amount:",
"showSwipeMargins": "Show swipe margins"
},
"drawer": {
Expand Down
1 change: 1 addition & 0 deletions strings/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface StringMap {
'readerScreen.bottomSheet.useChapterDrawerSwipeNavigation': 'string';
'readerScreen.bottomSheet.removeExtraSpacing': 'string';
'readerScreen.bottomSheet.volumeButtonsScroll': 'string';
'readerScreen.bottomSheet.scrollAmount': 'string';
'readerScreen.bottomSheet.showSwipeMargins': 'string';
'readerScreen.drawer.scrollToCurrentChapter': 'string';
'readerScreen.drawer.scrollToTop': 'string';
Expand Down