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

fixed webview #977

Merged
merged 10 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
10 changes: 9 additions & 1 deletion src/screens/WebviewScreen/WebviewScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useRef, useState } from 'react';
import WebView, { WebViewNavigation } from 'react-native-webview';
import { ProgressBar } from 'react-native-paper';

import { useBackHandler } from '@hooks';
import { useTheme } from '@hooks/persisted';
import { WebviewScreenProps } from '@navigators/types';
import { getUserAgent } from '@hooks/persisted/useUserAgent';
Expand All @@ -27,6 +28,14 @@ const WebviewScreen = ({ route, navigation }: WebviewScreenProps) => {
setCanGoForward(e.canGoForward);
};

useBackHandler(() => {
if (canGoBack) {
webViewRef.current?.goBack();
return true;
}
return false;
});

return (
<>
<Appbar
Expand All @@ -44,7 +53,6 @@ const WebviewScreen = ({ route, navigation }: WebviewScreenProps) => {
visible={progress !== 1}
/>
<WebView
startInLoadingState
userAgent={getUserAgent()}
ref={webViewRef}
source={{ uri }}
Expand Down
155 changes: 100 additions & 55 deletions src/screens/WebviewScreen/components/Appbar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react';
import { Share } from 'react-native';
import { Share, View, Text } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { IconButton, Menu } from 'react-native-paper';
import WebView from 'react-native-webview';
import { Appbar as PaperAppbar, Menu } from 'react-native-paper';
import * as WebBrowser from 'expo-web-browser';
import * as Linking from 'expo-linking';

import { WebviewScreenProps } from '@navigators/types';
import { getString } from '@strings/translations';
Expand All @@ -13,8 +14,8 @@ interface AppbarProps {
title: string;
theme: ThemeColors;
currentUrl: string;
canGoBack: Boolean;
canGoForward: Boolean;
canGoBack: boolean;
canGoForward: boolean;
webView: WebView;
Rider21 marked this conversation as resolved.
Show resolved Hide resolved
navigation: WebviewScreenProps['navigation'];
}
Expand All @@ -28,64 +29,108 @@ const Appbar: React.FC<AppbarProps> = ({
webView,
navigation,
}) => {
const [visible, setVisible] = useState(false);
const { top } = useSafeAreaInsets();
const [menuVisible, setMenuVisible] = useState(false);

return (
<PaperAppbar.Header style={{ backgroundColor: theme.surface }}>
<PaperAppbar.BackAction
<View
style={{
paddingTop: top,
backgroundColor: theme.surface,
flexDirection: 'row',
}}
>
<IconButton
icon="close"
iconColor={theme.onSurface}
onPress={() => navigation.goBack()}
theme={{ colors: { ...theme } }}
/>
<PaperAppbar.Content title={title} />
<PaperAppbar.Action
icon="arrow-left"
iconColor={theme.onSurface}
disabled={!canGoBack}
onPress={() => webView.current?.goBack()}
/>
<PaperAppbar.Action
icon="arrow-right"
iconColor={theme.onSurface}
disabled={!canGoForward}
onPress={() => webView.current?.goForward()}
/>
<Menu
visible={visible}
onDismiss={() => setVisible(false)}
anchor={
<PaperAppbar.Action
icon="dots-vertical"
iconColor={theme.onSurface}
onPress={() => setVisible(true)}
/>
}
>
<Menu.Item
title={getString('webview.refresh')}
titleStyle={{ color: theme.onSurface }}
onPress={() => webView.current?.reload()}
/>
<Menu.Item
title={getString('webview.share')}
titleStyle={{ color: theme.onSurface }}
onPress={() => Share.share({ message: currentUrl })}
/>
<Menu.Item
title={getString('webview.openInBrowser')}
titleStyle={{ color: theme.onSurface }}
onPress={() => WebBrowser.openBrowserAsync(currentUrl)}
/>
<Menu.Item
title={getString('webview.clearData')}
titleStyle={{ color: theme.onSurface }}
onPress={() => {
webView.current?.clearCache(true);
showToast(getString('webview.dataDeleted'));

<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text
style={{
color: theme.onSurface,
textAlign: 'center',
}}
numberOfLines={1}
ellipsizeMode="tail"
>
{title}
</Text>
</View>
<View style={{ flexDirection: 'row', alignItems: 'flex-end' }}>
<IconButton
icon="arrow-left"
iconColor={theme.onSurface}
disabled={!canGoBack}
onPress={() => webView.current?.goBack()}
theme={{ colors: { ...theme } }}
/>

<IconButton
icon="arrow-right"
iconColor={theme.onSurface}
disabled={!canGoForward}
onPress={() => webView.current?.goForward()}
theme={{ colors: { ...theme } }}
/>
</Menu>
</PaperAppbar.Header>

<Menu
visible={menuVisible}
onDismiss={() => setMenuVisible(false)}
anchor={
<IconButton
icon="dots-vertical"
iconColor={theme.onSurface}
onPress={() => setMenuVisible(true)}
theme={{ colors: { ...theme } }}
/>
}
style={{ backgroundColor: theme.surface2 }}
contentStyle={{ backgroundColor: theme.surface2 }}
>
<Menu.Item
title={getString('webview.refresh')}
style={{ backgroundColor: theme.surface2 }}
titleStyle={{ color: theme.onSurface }}
onPress={() => {
setMenuVisible(false);
webView.current?.reload();
}}
/>
<Menu.Item
title={getString('webview.share')}
style={{ backgroundColor: theme.surface2 }}
titleStyle={{ color: theme.onSurface }}
onPress={() => {
setMenuVisible(false);
Share.share({ message: currentUrl });
}}
/>
<Menu.Item
title={getString('webview.openInBrowser')}
style={{ backgroundColor: theme.surface2 }}
titleStyle={{ color: theme.onSurface }}
onPress={() => {
setMenuVisible(false);
Linking.openURL(currentUrl);
}}
/>
<Menu.Item
title={getString('webview.clearData')}
style={{ backgroundColor: theme.surface2 }}
titleStyle={{ color: theme.onSurface }}
onPress={() => {
setMenuVisible(false);
webView.current?.clearCache(true);
Rider21 marked this conversation as resolved.
Show resolved Hide resolved
webView.current?.reload();
showToast(getString('webview.dataDeleted'));
}}
/>
</Menu>
</View>
</View>
);
};

Expand Down
Loading