Skip to content

Commit

Permalink
feat: diable refetch when window on focus
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 committed Jan 3, 2023
1 parent 16c88c7 commit 9963d38
Show file tree
Hide file tree
Showing 30 changed files with 312 additions and 288 deletions.
9 changes: 4 additions & 5 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import '@/utils/dayjsPlugins'

import StyledImageViewer from './components/StyledImageViewer'
import StyledToast from './components/StyledToast'
import V2exWebview from './components/V2exWebview'
import { imageViewerAtom } from './jotai/imageViewerAtom'
import { profileAtom } from './jotai/profileAtom'
import { store } from './jotai/store'
Expand Down Expand Up @@ -78,7 +77,7 @@ function App() {

<StyledToast />

<V2exWebview />
{/* <V2exWebview /> */}
</PersistQueryClientProvider>
</Provider>
</ActionSheetProvider>
Expand Down Expand Up @@ -108,9 +107,9 @@ function GlobalImageViewer() {
const [imageViewer, setImageViewer] = useAtom(imageViewerAtom)
return (
<StyledImageViewer
{...imageViewer}
onClose={() =>
setImageViewer({ visible: false, index: 0, imageUrls: [] })
{...(imageViewer as any)}
onRequestClose={() =>
setImageViewer({ visible: false, imageIndex: 0, images: [] })
}
/>
)
Expand Down
10 changes: 7 additions & 3 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"expo": {
"name": "V2exHi",
"slug": "v2ex",
"version": "1.1.2",
"version": "1.1.3",
"orientation": "portrait",
"icon": "./assets/icon.png",
"userInterfaceStyle": "automatic",
Expand All @@ -12,13 +12,14 @@
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
"fallbackToCacheTimeout": 0,
"url": "https://u.expo.dev/dd398bbd-e8f2-4519-a933-ec68a355980f"
},
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": false,
"bundleIdentifier": "com.liaoliao666.v2ex",
"buildNumber": "1.1.2.2"
"buildNumber": "1.1.3.1"
},
"android": {
"adaptiveIcon": {
Expand All @@ -34,6 +35,9 @@
"eas": {
"projectId": "dd398bbd-e8f2-4519-a933-ec68a355980f"
}
},
"runtimeVersion": {
"policy": "sdkVersion"
}
}
}
14 changes: 7 additions & 7 deletions components/Html/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ function Html({

const setImageViewer = useSetAtom(imageViewerAtom)

const imageUrls = useMemo(() => {
const images = useMemo(() => {
if (!isString(html)) return []
const $ = load(html)
return $('img')
.map((i, img) => ({
url: $(img).attr('src')!,
uri: $(img).attr('src')!,
}))
.get()
.filter(item => !!item.url)
.filter(item => !!item.uri)
}, [html])

const navigation =
Expand All @@ -97,16 +97,16 @@ function Html({
<HtmlContext.Provider
value={useMemo(
() => ({
onPreview: url => {
onPreview: uri => {
setImageViewer({
index: findIndex(imageUrls, { url }),
imageIndex: findIndex(images, { uri }),
visible: true,
imageUrls,
images,
})
},
youtubePaddingX,
}),
[imageUrls, setImageViewer, youtubePaddingX]
[images, setImageViewer, youtubePaddingX]
)}
>
<RenderHtml
Expand Down
8 changes: 4 additions & 4 deletions components/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import { profileAtom } from '@/jotai/profileAtom'
import { colorSchemeAtom, themeAtom } from '@/jotai/themeAtom'
import { useSignout } from '@/servicies/authentication'
import { RootStackParamList } from '@/types'
import { clearCookie } from '@/utils/cookie'
import tw from '@/utils/tw'
import { openURL } from '@/utils/url'

import Badge from './Badge'
import { withQuerySuspense } from './QuerySuspense'
import RadioButtonGroup from './RadioButtonGroup'
import v2exMessage from './V2exWebview/v2exMessage'

export default withQuerySuspense(memo(Profile))

Expand Down Expand Up @@ -174,8 +174,7 @@ function Profile() {
<TouchableOpacity
style={tw`px-4 py-8 flex-row items-center`}
onPress={async () => {
v2exMessage.clearWebviewCache()
v2exMessage.reloadWebview()
clearCookie()
navigation.navigate('Login')
}}
>
Expand Down Expand Up @@ -306,7 +305,8 @@ function SignoutItem({ once }: { once: string }) {
// empty
} finally {
setProfileAtom(RESET)
v2exMessage.clearWebviewCache()
// v2exMessage.clearWebviewCache()
clearCookie()
}
}

Expand Down
230 changes: 108 additions & 122 deletions components/StyledImageViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,136 +1,122 @@
import { Ionicons } from '@expo/vector-icons'
import { ComponentProps } from 'react'
import {
Modal,
Pressable,
Text,
TouchableWithoutFeedback,
View,
useWindowDimensions,
} from 'react-native'
import ImageViewer from 'react-native-image-zoom-viewer'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { useCallback, useState } from 'react'
import { Pressable, Text, View } from 'react-native'
import ImageView from 'react-native-image-viewing'
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'
import Toast from 'react-native-toast-message'

import { savePicture } from '@/utils/savePicture'
import tw from '@/utils/tw'

import { NAV_BAR_HEIGHT } from './NavBar'
import StyledImage from './StyledImage'
import StyledToast from './StyledToast'
const StyledImageViewer: typeof ImageView = props => {
const safeAreaInsets = useSafeAreaInsets()

const [menuVisible, setMenuVisible] = useState(false)

const FooterComponent = useCallback(() => {
return (
<Pressable
onPress={() => {
setMenuVisible(false)
}}
style={tw`bg-mask h-screen`}
>
<View
style={tw.style(
`bg-body-1 absolute bottom-0 inset-x-0 rounded-t-[32px] overflow-hidden`,
{
paddingBottom: safeAreaInsets.bottom,
}
)}
>
{[
{
label: '保存',
value: 'saveToLocal',
onPress: async () => {
try {
const { images, imageIndex } = props

export interface StyledImageViewerProps
extends Omit<ComponentProps<typeof ImageViewer>, 'onCancel'> {
visible?: boolean
onClose?: () => void
await savePicture((images[imageIndex] as any).uri)
Toast.show({
type: 'success',
text1: '保存成功',
})
} catch (error) {
Toast.show({
type: 'error',
text1: '保存失败',
})
}
},
},
{
label: '取消',
value: 'cancel',
onPress: () => {
setMenuVisible(false)
},
},
].map(item => (
<Pressable
key={item.value}
onPress={item.onPress}
style={({ pressed }) =>
tw.style(
`h-[53px] justify-center items-center border-tint-border border-t border-solid`,
pressed && `bg-message-press`
)
}
>
<Text style={tw`text-body-5 text-tint-primary`}>
{item.label}
</Text>
</Pressable>
))}
</View>
</Pressable>
)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return (
<ImageView
{...props}
onLongPress={() => {
setMenuVisible(true)
}}
keyExtractor={image => (image as any).uri}
FooterComponent={
menuVisible
? FooterComponent
: ({ imageIndex }) => (
<ImageFooter
imageIndex={imageIndex}
imagesCount={props.images.length}
/>
)
}
/>
)
}

export default function StyledImageViewer({
visible,
onClose,
...props
}: StyledImageViewerProps) {
function ImageFooter({
imageIndex,
imagesCount,
}: {
imageIndex: number
imagesCount: number
}) {
const safeAreaInsets = useSafeAreaInsets()

const { width } = useWindowDimensions()

return (
<Modal
presentationStyle="fullScreen"
visible={visible}
onRequestClose={onClose}
<View
style={tw`px-4 flex-row justify-center pb-[${safeAreaInsets.bottom}px]`}
>
<ImageViewer
enableSwipeDown
onCancel={onClose}
menus={({ cancel, saveToLocal }) => (
<Pressable onPress={cancel} style={tw`bg-mask absolute inset-0`}>
<View
style={tw.style(
`bg-body-1 absolute bottom-0 inset-x-0 rounded-t-[32px] overflow-hidden`,
{
paddingBottom: safeAreaInsets.bottom,
}
)}
>
{[
{
label: '保存',
value: 'saveToLocal',
onPress: saveToLocal,
},
{
label: '取消',
value: 'cancel',
onPress: cancel,
},
].map(item => (
<Pressable
key={item.value}
onPress={item.onPress}
style={({ pressed }) =>
tw.style(
`h-[53px] justify-center items-center border-tint-border border-t border-solid`,
pressed && `bg-message-press`
)
}
>
<Text style={tw`text-body-5 text-tint-primary`}>
{item.label}
</Text>
</Pressable>
))}
</View>
</Pressable>
)}
onSave={async url => {
try {
await savePicture(url)
Toast.show({
type: 'success',
text1: '保存成功',
})
} catch (error) {
Toast.show({
type: 'error',
text1: '保存失败',
})
}
}}
renderHeader={() => (
<View
style={tw`pt-[${safeAreaInsets.top}px] px-4 z-20 absolute top-0 inset-x-0 flex-row justify-end`}
>
<TouchableWithoutFeedback
onPress={onClose}
style={tw`h-[${NAV_BAR_HEIGHT}px] justify-center items-center`}
>
<Ionicons name="close-sharp" size={24} color={'#fff'} />
</TouchableWithoutFeedback>
</View>
)}
renderIndicator={(currentIndex, allSize) => (
<View
style={tw`pt-[${safeAreaInsets.top}px] px-4 z-10 absolute top-0 inset-x-0 flex-row justify-center`}
>
<Text style={tw`text-white text-body-4`}>
{currentIndex + '/' + allSize}
</Text>
</View>
)}
renderImage={imageProps => (
<StyledImage
{...imageProps}
style={{
...props.style,
width,
}}
/>
)}
{...props}
/>

<StyledToast />
</Modal>
<Text style={tw`text-white text-body-4`}>
{imageIndex + 1 + '/' + imagesCount}
</Text>
</View>
)
}

export default StyledImageViewer
9 changes: 4 additions & 5 deletions jotai/imageViewerAtom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { atom } from 'jotai'
import ImageView from 'react-native-image-viewing'

import { StyledImageViewerProps } from '@/components/StyledImageViewer'

export const imageViewerAtom = atom<StyledImageViewerProps>({
index: 0,
export const imageViewerAtom = atom<Partial<Parameters<typeof ImageView>[0]>>({
imageIndex: 0,
visible: false,
imageUrls: [],
images: [],
})
Loading

0 comments on commit 9963d38

Please sign in to comment.