From 054167f67303c1b3cdb18710d477211811c4e760 Mon Sep 17 00:00:00 2001 From: Alex Brazier Date: Sun, 13 Sep 2020 22:50:35 +0100 Subject: [PATCH] feat: Add option to use existing back button to navigate (#24) * feat: Add option to use existing back button to navigate - Add an option that allows you to set your own back button, e.g. in the nav header and use that to navigate around the network logger - Handle hardware back press when on details page * Remove padding when close button is hidden --- README.md | 22 +++++++++++++++- example/src/App.tsx | 42 ++++++++++++++++++++++++++++--- src/backHandler.ts | 41 ++++++++++++++++++++++++++++++ src/components/NetworkLogger.tsx | 36 +++++++++++++++++++++++--- src/components/RequestDetails.tsx | 9 ++++--- src/components/ResultItem.tsx | 3 ++- src/index.tsx | 2 ++ 7 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 src/backHandler.ts diff --git a/README.md b/README.md index 7a142ed..34346d9 100644 --- a/README.md +++ b/README.md @@ -91,20 +91,40 @@ const MyScreen = () => ; ### Logging options +#### Max Requests + You can configure the max number of requests stored on the device using by calling `startNetworkLogging` with the `maxRequests` option. The default is `500`. ```ts startNetworkLogging({ maxRequests: 500 }); ``` +#### Sorting + Set the sort order of requests. Options are `asc` or `desc`, default is `desc` (most recent at the top). -```ts +```tsx import NetworkLogger from 'react-native-network-logger'; const MyScreen = () => ; ``` +#### Integrate with existing navigation + +Use your existing back button (e.g. in your navigation header) to navigate within the network logger. + +```tsx +import NetworkLogger, { getBackHandler } from 'react-native-network-logger'; + +const onBack = getBackHandler(navigate.onBack); + +const MyScreen = () => ( + + + +); +``` + ## Example App To test the example app, after cloning the repo, install the required dependencies by running: diff --git a/example/src/App.tsx b/example/src/App.tsx index 0459ff0..0fb20ce 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -6,8 +6,13 @@ import { Platform, View, Text, + Alert, + TouchableOpacity, } from 'react-native'; -import NetworkLogger, { ThemeName } from 'react-native-network-logger'; +import NetworkLogger, { + ThemeName, + getBackHandler, +} from 'react-native-network-logger'; export default function App() { const formData = new FormData(); @@ -36,11 +41,27 @@ export default function App() { const styles = themedStyles(theme === 'dark'); + const goBack = () => { + Alert.alert('Going back'); + }; + + const backHandler = getBackHandler(goBack); + return ( - - react-native-network-logger - + + + {'‹'} + + + react-native-network-logger + + + - + {!backHandlerSet() && ( + + )} ); }; diff --git a/src/components/ResultItem.tsx b/src/components/ResultItem.tsx index fb4692d..3276f29 100644 --- a/src/components/ResultItem.tsx +++ b/src/components/ResultItem.tsx @@ -2,6 +2,7 @@ import React from 'react'; import { View, TouchableOpacity, Text, StyleSheet } from 'react-native'; import NetworkRequestInfo from '../NetworkRequestInfo'; import { Theme, useThemedStyles, useTheme } from '../theme'; +import { backHandlerSet } from '../backHandler'; interface Props { request: NetworkRequestInfo; @@ -79,7 +80,7 @@ const ResultItem: React.FC = ({ style, request, onPress }) => { styles.text, styles.content, getUrlTextColor(request.status), - onDetailsPage && styles.paddedUrl, + onDetailsPage && !backHandlerSet() && styles.paddedUrl, ]} > {request.url} diff --git a/src/index.tsx b/src/index.tsx index 385dd16..cbfab8d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -11,4 +11,6 @@ export const getRequests = () => logger.getRequests(); export const clearRequests = () => logger.clearRequests(); +export { getBackHandler } from './backHandler'; + export { ThemeName } from './theme';