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

Lock profile when user leaves app on Ionic platform #1933

Merged
merged 4 commits into from
May 17, 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/1933.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lock profile when user leaves app on Ionic platform
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"extract-messages": "rm src/locales/en/translation.json && i18next-scanner --config=internals/extractMessages/i18next-scanner.config.js && node ./internals/scripts/normalize-translations.js ./src/locales/*/translation.json",
"fix-grommet-icons-types": "node ./internals/scripts/fix-grommet-icons-types.js",
"print-extension-dev-csp": "node ./internals/scripts/print-extension-dev-csp.js",
"android": "yarn build && yarn cap run android -l --external",
"ios": "yarn build && yarn cap run ios -l --external"
"android": "yarn build && yarn cap sync android && yarn cap run android -l --external",
"ios": "yarn build && yarn cap sync ios && yarn cap run ios -l --external"
},
"browserslist": {
"production": [
Expand Down
27 changes: 5 additions & 22 deletions src/app/components/Ionic/IonicProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
import { createContext, FC, PropsWithChildren, useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import { createContext, FC, PropsWithChildren } from 'react'
import { Capacitor } from '@capacitor/core'
import { App } from '@capacitor/app'
import { useIonicBackButtonListener } from './hooks/useIonicBackButtonListener'
import { useIonicAppStateChangeListener } from './hooks/useIonicAppStateChangeListener'

const IonicContext = createContext<undefined>(undefined)

const IonicContextProvider: FC<PropsWithChildren> = ({ children }) => {
const navigate = useNavigate()

useEffect(() => {
/**
* The back button refers to the physical back button on an Android device and should not be confused
* with either the browser back button or ion-back-button.
*/
const backButtonListenerHandle = App.addListener('backButton', ({ canGoBack }) => {
if (!canGoBack) {
App.exitApp()
} else {
navigate(-1)
}
})

return () => {
backButtonListenerHandle.remove()
}
}, [navigate])
useIonicBackButtonListener()
useIonicAppStateChangeListener()

return <IonicContext.Provider value={undefined}>{children}</IonicContext.Provider>
}
Expand Down
29 changes: 29 additions & 0 deletions src/app/components/Ionic/hooks/useIonicAppStateChangeListener.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useEffect, useRef } from 'react'
import { App } from '@capacitor/app'
import { deltaMsToLockProfile } from '../../../../ionicConfig'
import { persistActions } from '../../../state/persist'
import { useDispatch } from 'react-redux'

export const useIonicAppStateChangeListener = () => {
const dispatch = useDispatch()
const lockTimestamp = useRef<number>()

useEffect(() => {
const appStateChangeListenerHandle = App.addListener('appStateChange', ({ isActive }) => {
const shouldLock = lockTimestamp.current && Date.now() - lockTimestamp.current > deltaMsToLockProfile
if (isActive && shouldLock) {
dispatch(persistActions.lockAsync())
} else if (isActive && !shouldLock) {
lockTimestamp.current = undefined
}

if (!isActive) {
lockTimestamp.current = Date.now()
}
})

return () => {
appStateChangeListenerHandle.then(pluginListenerHandle => pluginListenerHandle.remove())
}
}, [dispatch])
}
25 changes: 25 additions & 0 deletions src/app/components/Ionic/hooks/useIonicBackButtonListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect } from 'react'
import { App } from '@capacitor/app'
import { useNavigate } from 'react-router-dom'

export const useIonicBackButtonListener = () => {
const navigate = useNavigate()

useEffect(() => {
/**
* The back button refers to the physical back button on an Android device and should not be confused
* with either the browser back button or ion-back-button.
*/
const backButtonListenerHandle = App.addListener('backButton', ({ canGoBack }) => {
if (!canGoBack) {
App.exitApp()
} else {
navigate(-1)
}
})

return () => {
backButtonListenerHandle.then(pluginListenerHandle => pluginListenerHandle.remove())
}
}, [navigate])
}
6 changes: 6 additions & 0 deletions src/ionicConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Represents the duration in milliseconds to lock a profile.
*
* @type {number}
*/
export const deltaMsToLockProfile = 30 * 1000
Loading