From f78126e01a43250b0968321d959b8722d0d399ff Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 7 May 2024 17:54:52 -0500 Subject: [🐴] State transitions (#3880) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Handle init/resume/suspend/background and polling * Add debug and temp guard * Make state transitions sync * Make init sync also * Checkpoint: confusing but working state machine * Reducer-esque * Remove poll events * Guard fetchConvo (cherry picked from commit 8385579d31500bb4bfb60afeecdc1eb3ddd7e747) * Clean up polling, make sync (cherry picked from commit 7f75cd04c3bf81c94662785748698640a84bef51) * Update history handling (cherry picked from commit b82b552ba4040adf7ead2377541132a386964ff8) * Check for screen focus in app state listener * Get rid of ad-hoc status checks --- src/state/messages/index.tsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/state/messages/index.tsx') diff --git a/src/state/messages/index.tsx b/src/state/messages/index.tsx index 22c4242e2..95ebf0afd 100644 --- a/src/state/messages/index.tsx +++ b/src/state/messages/index.tsx @@ -1,6 +1,7 @@ import React, {useContext, useState, useSyncExternalStore} from 'react' +import {AppState} from 'react-native' 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 {useAgent} from '#/state/session' @@ -20,6 +21,7 @@ export function ChatProvider({ children, convoId, }: Pick & {children: React.ReactNode}) { + const isScreenFocused = useIsFocused() const {serviceUrl} = useDmServiceUrlStorage() const {getAgent} = useAgent() const [convo] = useState( @@ -44,5 +46,23 @@ export function ChatProvider({ }, [convo]), ) + React.useEffect(() => { + const handleAppStateChange = (nextAppState: string) => { + if (isScreenFocused) { + if (nextAppState === 'active') { + convo.resume() + } else { + convo.background() + } + } + } + + const sub = AppState.addEventListener('change', handleAppStateChange) + + return () => { + sub.remove() + } + }, [convo, isScreenFocused]) + return {children} } -- cgit 1.4.1