Add PTR
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {TextStyle, View} from 'react-native'
|
import {RefreshControl,TextStyle, View} from 'react-native'
|
||||||
import {msg, Trans} from '@lingui/macro'
|
import {msg, Trans} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {NativeStackScreenProps} from '@react-navigation/native-stack'
|
import {NativeStackScreenProps} from '@react-navigation/native-stack'
|
||||||
|
|
||||||
import {CommonNavigatorParams} from '#/lib/routes/types'
|
import {CommonNavigatorParams} from '#/lib/routes/types'
|
||||||
import {isAndroid, isIOS, isWeb} from '#/platform/detection'
|
import {isAndroid, isIOS, isWeb} from '#/platform/detection'
|
||||||
import {PurchasesState, usePurchases} from '#/state/purchases'
|
import {PurchasesState, usePurchases, usePurchasesApi} from '#/state/purchases'
|
||||||
import {useManageSubscription} from '#/state/purchases/hooks/useManageSubscription'
|
import {useManageSubscription} from '#/state/purchases/hooks/useManageSubscription'
|
||||||
import {useNativeUserState} from '#/state/purchases/hooks/useNativeUserState'
|
import {useNativeUserState} from '#/state/purchases/hooks/useNativeUserState'
|
||||||
import {
|
import {
|
||||||
@@ -48,13 +48,26 @@ export type ScreenProps = NativeStackScreenProps<
|
|||||||
export function Subscriptions(_props: ScreenProps) {
|
export function Subscriptions(_props: ScreenProps) {
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const purchases = usePurchases()
|
const purchases = usePurchases()
|
||||||
|
const {refetch} = usePurchasesApi()
|
||||||
const {loading, restricted} = useNativeUserState()
|
const {loading, restricted} = useNativeUserState()
|
||||||
|
const [refreshing, setRefreshing] = React.useState(false)
|
||||||
|
const onRefresh = async () => {
|
||||||
|
setRefreshing(true)
|
||||||
|
try {
|
||||||
|
await refetch()
|
||||||
|
} finally {
|
||||||
|
setRefreshing(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout.Screen>
|
<Layout.Screen>
|
||||||
<Layout.Header title={_(msg`Subscriptions`)} />
|
<Layout.Header title={_(msg`Subscriptions`)} />
|
||||||
|
|
||||||
<Layout.Content>
|
<Layout.Content
|
||||||
|
refreshControl={
|
||||||
|
<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
|
||||||
|
}>
|
||||||
<CenteredView style={[a.util_screen_outer]}>
|
<CenteredView style={[a.util_screen_outer]}>
|
||||||
<View style={[a.px_xl, a.py_xl]}>
|
<View style={[a.px_xl, a.py_xl]}>
|
||||||
{purchases.status === 'loading' || loading ? (
|
{purchases.status === 'loading' || loading ? (
|
||||||
|
|||||||
@@ -12,12 +12,21 @@ export type PurchasesState =
|
|||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
status: 'ready'
|
status: 'ready'
|
||||||
|
refetching: boolean
|
||||||
email?: string
|
email?: string
|
||||||
subscriptions: APISubscription[]
|
subscriptions: APISubscription[]
|
||||||
entitlements: APIEntitlement[]
|
entitlements: APIEntitlement[]
|
||||||
config: {}
|
config: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Context = React.createContext<PurchasesState>({
|
export type PurchasesApi = {
|
||||||
|
refetch: () => Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
export const StateContext = React.createContext<PurchasesState>({
|
||||||
status: 'loading',
|
status: 'loading',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const ApiContext = React.createContext<PurchasesApi>({
|
||||||
|
refetch: async () => {},
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
|
|
||||||
import {Context, PurchasesState} from '#/state/purchases/context'
|
import {
|
||||||
|
ApiContext,
|
||||||
|
PurchasesApi,
|
||||||
|
PurchasesState,
|
||||||
|
StateContext,
|
||||||
|
} from '#/state/purchases/context'
|
||||||
import {useNativeEventsListener} from '#/state/purchases/hooks/useNativeEventsListener'
|
import {useNativeEventsListener} from '#/state/purchases/hooks/useNativeEventsListener'
|
||||||
import {usePurchasesState} from '#/state/purchases/hooks/usePurchasesState'
|
import {usePurchasesState} from '#/state/purchases/hooks/usePurchasesState'
|
||||||
|
|
||||||
@@ -11,6 +16,7 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
data: purchases,
|
data: purchases,
|
||||||
error: purchasesStateError,
|
error: purchasesStateError,
|
||||||
refetch,
|
refetch,
|
||||||
|
isRefetching,
|
||||||
} = usePurchasesState()
|
} = usePurchasesState()
|
||||||
const ctx = React.useMemo<PurchasesState>(() => {
|
const ctx = React.useMemo<PurchasesState>(() => {
|
||||||
if (purchasesStateError) {
|
if (purchasesStateError) {
|
||||||
@@ -25,13 +31,22 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
status: 'ready',
|
status: 'ready',
|
||||||
|
refetching: isRefetching,
|
||||||
email: purchases?.email,
|
email: purchases?.email,
|
||||||
subscriptions: purchases?.subscriptions ?? [],
|
subscriptions: purchases?.subscriptions ?? [],
|
||||||
entitlements: purchases?.entitlements ?? [],
|
entitlements: purchases?.entitlements ?? [],
|
||||||
config: {},
|
config: {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [purchases, purchasesStateError])
|
}, [purchases, purchasesStateError, isRefetching])
|
||||||
|
|
||||||
|
const apiCtx = React.useMemo<PurchasesApi>(() => {
|
||||||
|
return {
|
||||||
|
refetch: async () => {
|
||||||
|
await refetch()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}, [refetch])
|
||||||
|
|
||||||
useNativeEventsListener({
|
useNativeEventsListener({
|
||||||
onCustomerInfoUpdated() {
|
onCustomerInfoUpdated() {
|
||||||
@@ -39,9 +54,17 @@ export function Provider({children}: {children: React.ReactNode}) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return <Context.Provider value={ctx}>{children}</Context.Provider>
|
return (
|
||||||
|
<ApiContext.Provider value={apiCtx}>
|
||||||
|
<StateContext.Provider value={ctx}>{children}</StateContext.Provider>
|
||||||
|
</ApiContext.Provider>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function usePurchases() {
|
export function usePurchases() {
|
||||||
return React.useContext(Context)
|
return React.useContext(StateContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function usePurchasesApi() {
|
||||||
|
return React.useContext(ApiContext)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user