Check for screen focus in app state listener
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, {useContext, useState, useSyncExternalStore} from 'react'
|
import React, {useContext, useState, useSyncExternalStore} from 'react'
|
||||||
import {AppState} from 'react-native'
|
import {AppState} from 'react-native'
|
||||||
import {BskyAgent} from '@atproto-labs/api'
|
import {BskyAgent} from '@atproto-labs/api'
|
||||||
import {useFocusEffect} from '@react-navigation/native'
|
import {useFocusEffect, useIsFocused} from '@react-navigation/native'
|
||||||
|
|
||||||
import {Convo, ConvoParams, ConvoState} from '#/state/messages/convo'
|
import {Convo, ConvoParams, ConvoState} from '#/state/messages/convo'
|
||||||
import {useAgent} from '#/state/session'
|
import {useAgent} from '#/state/session'
|
||||||
@@ -21,6 +21,7 @@ export function ChatProvider({
|
|||||||
children,
|
children,
|
||||||
convoId,
|
convoId,
|
||||||
}: Pick<ConvoParams, 'convoId'> & {children: React.ReactNode}) {
|
}: Pick<ConvoParams, 'convoId'> & {children: React.ReactNode}) {
|
||||||
|
const isScreenFocused = useIsFocused()
|
||||||
const {serviceUrl} = useDmServiceUrlStorage()
|
const {serviceUrl} = useDmServiceUrlStorage()
|
||||||
const {getAgent} = useAgent()
|
const {getAgent} = useAgent()
|
||||||
const [convo] = useState(
|
const [convo] = useState(
|
||||||
@@ -47,10 +48,12 @@ export function ChatProvider({
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const handleAppStateChange = (nextAppState: string) => {
|
const handleAppStateChange = (nextAppState: string) => {
|
||||||
if (nextAppState === 'active') {
|
if (isScreenFocused) {
|
||||||
convo.resume()
|
if (nextAppState === 'active') {
|
||||||
} else {
|
convo.resume()
|
||||||
convo.background()
|
} else {
|
||||||
|
convo.background()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +62,7 @@ export function ChatProvider({
|
|||||||
return () => {
|
return () => {
|
||||||
sub.remove()
|
sub.remove()
|
||||||
}
|
}
|
||||||
}, [convo])
|
}, [convo, isScreenFocused])
|
||||||
|
|
||||||
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user