Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move useIonicRequiresUpdate state #1969

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/1969.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Android update screen
2 changes: 1 addition & 1 deletion src/app/components/Ionic/components/UpdateGate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const UpdateGate: FC<PropsWithChildren> = ({ children }) => {
const { t } = useTranslation()
const isMobile = useContext(ResponsiveContext) === 'small'
const {
state: { updateAvailability },
requiresUpdateState: { updateAvailability },
checkForUpdateAvailability,
skipUpdate,
} = useContext(IonicContext)
Expand Down
18 changes: 11 additions & 7 deletions src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Dispatch, SetStateAction, useEffect } from 'react'
import { IonicProviderState, UpdateAvailability } from '../providers/IonicContext'
import { useEffect, useState } from 'react'
import { IonicRequiresUpdateState, UpdateAvailability } from '../providers/IonicContext'
import { updateAvailable } from '../utils/capacitor-app-update'

export const useIonicRequiresUpdate = (
state: IonicProviderState,
setState: Dispatch<SetStateAction<IonicProviderState>>,
) => {
const ionicRequiresUpdateInitialState: IonicRequiresUpdateState = {
updateAvailability: UpdateAvailability.NOT_INITIALIZED,
error: null,
}

export const useIonicRequiresUpdate = () => {
const [state, setState] = useState<IonicRequiresUpdateState>({ ...ionicRequiresUpdateInitialState })

const checkForUpdateAvailability = async () => {
if (state.updateAvailability === UpdateAvailability.LOADING) {
return
Expand Down Expand Up @@ -35,5 +39,5 @@ export const useIonicRequiresUpdate = (
setState(prevState => ({ ...prevState, updateAvailability: UpdateAvailability.UPDATE_NOT_AVAILABLE }))
}

return { checkForUpdateAvailability, skipUpdate }
return { requiresUpdateState: state, checkForUpdateAvailability, skipUpdate }
}
4 changes: 2 additions & 2 deletions src/app/components/Ionic/providers/IonicContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export enum UpdateAvailability {
UNKNOWN,
}

export interface IonicProviderState {
export interface IonicRequiresUpdateState {
updateAvailability: UpdateAvailability
error: Error | null
}

export interface IonicProviderContext {
readonly state: IonicProviderState
readonly requiresUpdateState: IonicRequiresUpdateState
checkForUpdateAvailability: () => void
skipUpdate: () => void
}
Expand Down
15 changes: 4 additions & 11 deletions src/app/components/Ionic/providers/IonicProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { FC, PropsWithChildren, useState } from 'react'
import { FC, PropsWithChildren } from 'react'
import { useIonicBackButtonListener } from '../hooks/useIonicBackButtonListener'
import { useIonicAppStateChangeListener } from '../hooks/useIonicAppStateChangeListener'
import { IonicContext, IonicProviderContext, IonicProviderState, UpdateAvailability } from './IonicContext'
import { IonicContext, IonicProviderContext } from './IonicContext'
import { useIonicRequiresUpdate } from '../hooks/useIonicRequiresUpdate'

const ionicProviderInitialState: IonicProviderState = {
updateAvailability: UpdateAvailability.NOT_INITIALIZED,
error: null,
}

export const IonicContextProvider: FC<PropsWithChildren> = ({ children }) => {
const [state, setState] = useState<IonicProviderState>({ ...ionicProviderInitialState })

const { checkForUpdateAvailability, skipUpdate } = useIonicRequiresUpdate(state, setState)
const { requiresUpdateState, checkForUpdateAvailability, skipUpdate } = useIonicRequiresUpdate()
useIonicAppStateChangeListener()
useIonicBackButtonListener()

const providerState: IonicProviderContext = {
state,
requiresUpdateState,
checkForUpdateAvailability,
skipUpdate,
}
Expand Down
Loading