Cleanup
This commit is contained in:
+3
-3
@@ -33,6 +33,7 @@ import {
|
|||||||
ensureGeolocationResolved,
|
ensureGeolocationResolved,
|
||||||
Provider as GeolocationProvider,
|
Provider as GeolocationProvider,
|
||||||
} from '#/state/geolocation'
|
} from '#/state/geolocation'
|
||||||
|
import {GlobalGestureEventsProvider} from '#/state/global-gesture-events'
|
||||||
import {Provider as HomeBadgeProvider} from '#/state/home-badge'
|
import {Provider as HomeBadgeProvider} from '#/state/home-badge'
|
||||||
import {Provider as InvitesStateProvider} from '#/state/invites'
|
import {Provider as InvitesStateProvider} from '#/state/invites'
|
||||||
import {Provider as LightboxStateProvider} from '#/state/lightbox'
|
import {Provider as LightboxStateProvider} from '#/state/lightbox'
|
||||||
@@ -73,7 +74,6 @@ import {Splash} from '#/Splash'
|
|||||||
import {BottomSheetProvider} from '../modules/bottom-sheet'
|
import {BottomSheetProvider} from '../modules/bottom-sheet'
|
||||||
import {BackgroundNotificationPreferencesProvider} from '../modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider'
|
import {BackgroundNotificationPreferencesProvider} from '../modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider'
|
||||||
import {Provider as HideBottomBarBorderProvider} from './lib/hooks/useHideBottomBarBorder'
|
import {Provider as HideBottomBarBorderProvider} from './lib/hooks/useHideBottomBarBorder'
|
||||||
import {GlobalGestureEvents} from '#/state/shell/GlobalGestureEvents'
|
|
||||||
|
|
||||||
SplashScreen.preventAutoHideAsync()
|
SplashScreen.preventAutoHideAsync()
|
||||||
if (isIOS) {
|
if (isIOS) {
|
||||||
@@ -155,13 +155,13 @@ function InnerApp() {
|
|||||||
<HideBottomBarBorderProvider>
|
<HideBottomBarBorderProvider>
|
||||||
<GestureHandlerRootView
|
<GestureHandlerRootView
|
||||||
style={s.h100pct}>
|
style={s.h100pct}>
|
||||||
<GlobalGestureEvents>
|
<GlobalGestureEventsProvider>
|
||||||
<IntentDialogProvider>
|
<IntentDialogProvider>
|
||||||
<TestCtrls />
|
<TestCtrls />
|
||||||
<Shell />
|
<Shell />
|
||||||
<NuxDialogs />
|
<NuxDialogs />
|
||||||
</IntentDialogProvider>
|
</IntentDialogProvider>
|
||||||
</GlobalGestureEvents>
|
</GlobalGestureEventsProvider>
|
||||||
</GestureHandlerRootView>
|
</GestureHandlerRootView>
|
||||||
</HideBottomBarBorderProvider>
|
</HideBottomBarBorderProvider>
|
||||||
</ServiceAccountManager>
|
</ServiceAccountManager>
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import {
|
import {
|
||||||
|
createContext,
|
||||||
useCallback,
|
useCallback,
|
||||||
|
useContext,
|
||||||
useMemo,
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
createContext,
|
|
||||||
useContext,
|
|
||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import {View, Dimensions} from 'react-native'
|
import {Dimensions, View} from 'react-native'
|
||||||
import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
|
||||||
|
|
||||||
import {Portal} from '#/components/Portal'
|
|
||||||
import {atoms as a, useTheme} from '#/alf'
|
import {atoms as a, useTheme} from '#/alf'
|
||||||
import {useOnInteract} from '#/state/shell/GlobalGestureEvents'
|
import {useOnGesture} from '#/components/hooks/useOnGesture'
|
||||||
|
// import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
||||||
|
import {Portal} from '#/components/Portal'
|
||||||
import {TIP_SIZE} from '#/components/Tooltip/const'
|
import {TIP_SIZE} from '#/components/Tooltip/const'
|
||||||
|
|
||||||
type TooltipContextType = {
|
type TooltipContextType = {
|
||||||
@@ -160,7 +160,7 @@ function Bubble({
|
|||||||
>
|
>
|
||||||
}) {
|
}) {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const insets = useSafeAreaInsets()
|
// const insets = useSafeAreaInsets()
|
||||||
const [bubbleMeasurements, setBubbleMeasurements] = useState<
|
const [bubbleMeasurements, setBubbleMeasurements] = useState<
|
||||||
| {
|
| {
|
||||||
width: number
|
width: number
|
||||||
@@ -180,9 +180,9 @@ function Bubble({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const win = Dimensions.get('window')
|
const win = Dimensions.get('window')
|
||||||
const {width: ww, height: wh} = win
|
const {width: ww, height: _wh} = win
|
||||||
const maxTop = insets.top
|
// const maxTop = insets.top
|
||||||
const maxBottom = wh - insets.bottom
|
// const maxBottom = wh - insets.bottom
|
||||||
const {width: cw, height: ch} = bubbleMeasurements
|
const {width: cw, height: ch} = bubbleMeasurements
|
||||||
const minLeft = a.px_xl.paddingLeft
|
const minLeft = a.px_xl.paddingLeft
|
||||||
const maxLeft = ww - minLeft
|
const maxLeft = ww - minLeft
|
||||||
@@ -211,14 +211,14 @@ function Bubble({
|
|||||||
tipTop,
|
tipTop,
|
||||||
tipLeft,
|
tipLeft,
|
||||||
}
|
}
|
||||||
}, [targetMeasurements, bubbleMeasurements, insets])
|
}, [targetMeasurements, bubbleMeasurements])
|
||||||
|
|
||||||
const requestCloseWrapped = useCallback(() => {
|
const requestCloseWrapped = useCallback(() => {
|
||||||
setBubbleMeasurements(undefined)
|
setBubbleMeasurements(undefined)
|
||||||
requestClose()
|
requestClose()
|
||||||
}, [requestClose])
|
}, [requestClose])
|
||||||
|
|
||||||
useOnInteract(
|
useOnGesture(
|
||||||
useCallback(
|
useCallback(
|
||||||
e => {
|
e => {
|
||||||
const {x, y} = e
|
const {x, y} = e
|
||||||
@@ -243,7 +243,7 @@ function Bubble({
|
|||||||
a.align_start,
|
a.align_start,
|
||||||
{
|
{
|
||||||
width: 200,
|
width: 200,
|
||||||
opacity: !!bubbleMeasurements ? 1 : 0,
|
opacity: bubbleMeasurements ? 1 : 0,
|
||||||
top: coords.top,
|
top: coords.top,
|
||||||
left: coords.left,
|
left: coords.left,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import {useEffect} from 'react'
|
||||||
|
|
||||||
|
import {
|
||||||
|
type GlobalGestureEvents,
|
||||||
|
useGlobalGestureEvents,
|
||||||
|
} from '#/state/global-gesture-events'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for global gesture events. Callback should be wrapped with
|
||||||
|
* `useCallback` or otherwise memoized to avoid unnecessary re-renders.
|
||||||
|
*/
|
||||||
|
export function useOnGesture(
|
||||||
|
onGestureCallback: (e: GlobalGestureEvents['begin']) => void,
|
||||||
|
) {
|
||||||
|
const ctx = useGlobalGestureEvents()
|
||||||
|
useEffect(() => {
|
||||||
|
ctx.register()
|
||||||
|
ctx.events.on('begin', onGestureCallback)
|
||||||
|
return () => {
|
||||||
|
ctx.unregister()
|
||||||
|
ctx.events.off('begin', onGestureCallback)
|
||||||
|
}
|
||||||
|
}, [ctx, onGestureCallback])
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export function useOnGesture() {}
|
||||||
+21
-27
@@ -1,36 +1,41 @@
|
|||||||
import {createContext, useContext, useMemo, useEffect, useRef, useState} from 'react'
|
import {createContext, useContext, useMemo, useRef, useState} from 'react'
|
||||||
import EventEmitter from 'eventemitter3'
|
|
||||||
import {
|
import {
|
||||||
GestureDetector,
|
|
||||||
Gesture,
|
Gesture,
|
||||||
GestureStateChangeEvent,
|
GestureDetector,
|
||||||
GestureUpdateEvent,
|
type GestureStateChangeEvent,
|
||||||
PanGestureHandlerEventPayload,
|
type GestureUpdateEvent,
|
||||||
|
type PanGestureHandlerEventPayload,
|
||||||
} from 'react-native-gesture-handler'
|
} from 'react-native-gesture-handler'
|
||||||
|
import EventEmitter from 'eventemitter3'
|
||||||
|
|
||||||
const events = new EventEmitter<{
|
export type GlobalGestureEvents = {
|
||||||
begin: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
begin: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
||||||
update: GestureUpdateEvent<PanGestureHandlerEventPayload>
|
update: GestureUpdateEvent<PanGestureHandlerEventPayload>
|
||||||
end: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
end: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
||||||
finalize: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
finalize: GestureStateChangeEvent<PanGestureHandlerEventPayload>
|
||||||
}>()
|
}
|
||||||
|
|
||||||
const Context = createContext<{
|
const Context = createContext<{
|
||||||
|
events: EventEmitter<GlobalGestureEvents>
|
||||||
register: () => void
|
register: () => void
|
||||||
unregister: () => void
|
unregister: () => void
|
||||||
}>({
|
}>({
|
||||||
|
events: new EventEmitter<GlobalGestureEvents>(),
|
||||||
register: () => {},
|
register: () => {},
|
||||||
unregister: () => {},
|
unregister: () => {},
|
||||||
})
|
})
|
||||||
|
|
||||||
export function GlobalGestureEvents({
|
export function GlobalGestureEventsProvider({
|
||||||
children,
|
children,
|
||||||
}: {
|
}: {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}): React.ReactElement {
|
}) {
|
||||||
const refCount = useRef(0)
|
const refCount = useRef(0)
|
||||||
|
const events = useMemo(() => new EventEmitter<GlobalGestureEvents>(), [])
|
||||||
const [enabled, setEnabled] = useState(false)
|
const [enabled, setEnabled] = useState(false)
|
||||||
const ctx = useMemo(() => ({
|
const ctx = useMemo(
|
||||||
|
() => ({
|
||||||
|
events,
|
||||||
register() {
|
register() {
|
||||||
refCount.current += 1
|
refCount.current += 1
|
||||||
if (refCount.current === 1) {
|
if (refCount.current === 1) {
|
||||||
@@ -43,7 +48,9 @@ export function GlobalGestureEvents({
|
|||||||
setEnabled(false)
|
setEnabled(false)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}), [setEnabled])
|
}),
|
||||||
|
[events, setEnabled],
|
||||||
|
)
|
||||||
const gesture = Gesture.Pan()
|
const gesture = Gesture.Pan()
|
||||||
.runOnJS(true)
|
.runOnJS(true)
|
||||||
.enabled(enabled)
|
.enabled(enabled)
|
||||||
@@ -68,19 +75,6 @@ export function GlobalGestureEvents({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useOnInteract(
|
export function useGlobalGestureEvents() {
|
||||||
onInteract: (
|
return useContext(Context)
|
||||||
e: GestureStateChangeEvent<PanGestureHandlerEventPayload>,
|
|
||||||
) => void,
|
|
||||||
) {
|
|
||||||
const ctx = useContext(Context)
|
|
||||||
useEffect(() => {
|
|
||||||
ctx.register()
|
|
||||||
events.on('begin', onInteract)
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
ctx.unregister()
|
|
||||||
events.off('begin', onInteract)
|
|
||||||
}
|
|
||||||
}, [ctx, onInteract])
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
export function GlobalGestureEventsProvider(_props: {
|
||||||
|
children: React.ReactNode
|
||||||
|
}) {
|
||||||
|
throw new Error('GlobalGestureEventsProvider is not supported on web.')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useGlobalGestureEvents() {
|
||||||
|
throw new Error('useGlobalGestureEvents is not supported on web.')
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user