-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/app/components/Ionic/components/IonicNativePlatformProvider/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { FC, PropsWithChildren } from 'react' | ||
import { Capacitor } from '@capacitor/core' | ||
import { IonicContextProvider } from '../../providers/IonicProvider' | ||
import { RequiresUpdate } from '../RequiresUpdate' | ||
|
||
export const IonicNativePlatformProvider: FC<PropsWithChildren> = ({ children }) => { | ||
if (Capacitor.isNativePlatform()) { | ||
return <IonicContextProvider> | ||
<RequiresUpdate> | ||
{children} | ||
</RequiresUpdate> | ||
</IonicContextProvider> | ||
} | ||
|
||
return children | ||
} |
29 changes: 29 additions & 0 deletions
29
src/app/components/Ionic/components/RequiresUpdate/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { FC, PropsWithChildren, useContext } from 'react' | ||
import { IonicContext } from '../../providers/IonicContext' | ||
import { Box } from 'grommet/es6/components/Box' | ||
import { Button } from 'grommet/es6/components/Button' | ||
import { navigateToAppStore } from '../../utils/capacitor-app-update' | ||
|
||
export const RequiresUpdate: FC<PropsWithChildren> = ({ children }) => { | ||
const { state: { requiresUpdate } } = useContext(IonicContext) | ||
|
||
if (requiresUpdate === false) return children | ||
|
||
const handleNavigateToAppStore = () => { | ||
navigateToAppStore() | ||
} | ||
|
||
return ( | ||
<Box direction="row-responsive" background="background-back" fill style={{ minHeight: '100dvh' }}> | ||
{requiresUpdate === undefined && <>Loading...</>} | ||
{requiresUpdate === true && <> | ||
Check warning on line 19 in src/app/components/Ionic/components/RequiresUpdate/index.tsx GitHub Actions / lint
|
||
This app requires an update to the latest version in order to operate correctly. | ||
<Button | ||
Check warning on line 21 in src/app/components/Ionic/components/RequiresUpdate/index.tsx GitHub Actions / lint
|
||
fill="horizontal" | ||
label="Update application" | ||
onClick={handleNavigateToAppStore} | ||
/> | ||
</>} | ||
</Box> | ||
) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useEffect } from 'react' | ||
import { IonicProviderState } from '../providers/IonicContext' | ||
import { updateAvailable } from '../utils/capacitor-app-update' | ||
|
||
export const useIonicRequiresUpdate = (setState: (state: IonicProviderState) => void) => { | ||
useEffect(() => { | ||
let inProgress = true | ||
|
||
const init = async () => { | ||
const requiresUpdate = await updateAvailable() | ||
|
||
if (inProgress) { | ||
setState({ requiresUpdate }) | ||
} | ||
} | ||
|
||
init() | ||
|
||
return () => { | ||
inProgress = false | ||
} | ||
}, []) | ||
Check warning on line 22 in src/app/components/Ionic/hooks/useIonicRequiresUpdate.ts GitHub Actions / lint
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createContext } from 'react' | ||
|
||
export interface IonicProviderState { | ||
/** | ||
* Indicates whether an update is required. | ||
* In an undefined state, it indicates that the application is currently in loading phase. | ||
* | ||
* @type {boolean | undefined} | ||
*/ | ||
requiresUpdate: boolean | undefined | ||
} | ||
|
||
export interface IonicProviderContext { | ||
readonly state: IonicProviderState | ||
} | ||
|
||
export const IonicContext = createContext<IonicProviderContext>({} as IonicProviderContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { FC, PropsWithChildren, useState } from 'react' | ||
import { useIonicBackButtonListener } from '../hooks/useIonicBackButtonListener' | ||
import { useIonicAppStateChangeListener } from '../hooks/useIonicAppStateChangeListener' | ||
import { IonicContext, IonicProviderContext, IonicProviderState } from './IonicContext' | ||
import { useIonicRequiresUpdate } from '../hooks/useIonicRequiresUpdate' | ||
|
||
const ionicProviderInitialState: IonicProviderState = { | ||
requiresUpdate: undefined, | ||
} | ||
|
||
export const IonicContextProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [state, setState] = useState<IonicProviderState>({ ...ionicProviderInitialState }) | ||
|
||
useIonicRequiresUpdate(setState) | ||
useIonicAppStateChangeListener() | ||
useIonicBackButtonListener() | ||
|
||
const providerState: IonicProviderContext = { | ||
state, | ||
} | ||
|
||
return <IonicContext.Provider value={providerState}>{children}</IonicContext.Provider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { AppUpdate, AppUpdateAvailability } from '@capawesome/capacitor-app-update' | ||
import { Capacitor } from '@capacitor/core' | ||
|
||
export const updateAvailable = async () => { | ||
const result = await AppUpdate.getAppUpdateInfo() | ||
const { updateAvailability, currentVersionCode, availableVersionCode } = result | ||
|
||
if (updateAvailability === AppUpdateAvailability.UPDATE_IN_PROGRESS) { | ||
return undefined | ||
} | ||
|
||
// TODO: Skips in case there is no internet connection | ||
if (updateAvailability !== AppUpdateAvailability.UPDATE_AVAILABLE) { | ||
return false | ||
} | ||
|
||
if (Capacitor.getPlatform() === 'android') { | ||
// Example of version code -> "1", "2", ... | ||
return parseInt(availableVersionCode ?? `${Number.MAX_SAFE_INTEGER}`, 10) > parseInt(currentVersionCode ?? '0', 10) | ||
} | ||
|
||
// TODO: Add for iOS | ||
// Compare semVer between currentVersionName and availableVersionName | ||
throw new Error('Unknown Capacitor platform!') | ||
} | ||
|
||
export const navigateToAppStore = async () => { | ||
await AppUpdate.openAppStore() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters