From ae6a50cd8031431646886a16effdf310c89a6e37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Poizat?= Date: Fri, 13 Dec 2024 17:24:29 +0100 Subject: [PATCH] feat: Remove cordova methods from cozy-device-helper --- packages/cozy-device-helper/package.json | 3 - packages/cozy-device-helper/src/apps.spec.ts | 28 ---- packages/cozy-device-helper/src/apps.ts | 136 ------------------ packages/cozy-device-helper/src/cordova.ts | 3 - packages/cozy-device-helper/src/device.ts | 37 ----- packages/cozy-device-helper/src/index.spec.ts | 67 --------- packages/cozy-device-helper/src/index.ts | 21 +-- packages/cozy-device-helper/src/link.ts | 30 ---- .../cozy-device-helper/src/platform.spec.ts | 40 ------ packages/cozy-device-helper/src/platform.ts | 19 --- packages/cozy-device-helper/src/plugins.ts | 27 ---- .../src/withHasSafariPlugin.tsx | 29 ---- yarn.lock | 11 -- 13 files changed, 1 insertion(+), 450 deletions(-) delete mode 100644 packages/cozy-device-helper/src/apps.spec.ts delete mode 100644 packages/cozy-device-helper/src/apps.ts delete mode 100644 packages/cozy-device-helper/src/cordova.ts delete mode 100644 packages/cozy-device-helper/src/device.ts delete mode 100644 packages/cozy-device-helper/src/index.spec.ts delete mode 100644 packages/cozy-device-helper/src/link.ts delete mode 100644 packages/cozy-device-helper/src/platform.spec.ts delete mode 100644 packages/cozy-device-helper/src/plugins.ts delete mode 100644 packages/cozy-device-helper/src/withHasSafariPlugin.tsx diff --git a/packages/cozy-device-helper/package.json b/packages/cozy-device-helper/package.json index 7778579fac..b2df51c9c0 100644 --- a/packages/cozy-device-helper/package.json +++ b/packages/cozy-device-helper/package.json @@ -6,9 +6,6 @@ "bugs": { "url": "https://github.com/cozy/cozy-libs/issues" }, - "dependencies": { - "lodash": "^4.17.19" - }, "devDependencies": { "@babel/cli": "7.16.8", "@babel/core": "7.16.12", diff --git a/packages/cozy-device-helper/src/apps.spec.ts b/packages/cozy-device-helper/src/apps.spec.ts deleted file mode 100644 index cc39d979d2..0000000000 --- a/packages/cozy-device-helper/src/apps.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import appHelpers from './apps' - -describe('apps helpers', () => { - it('should be able to start app if it exists', async () => { - const ok = { ok: true } - const mockStart = jest - .fn() - .mockImplementation((successCb: ({ ok: boolean }) => void) => { - successCb(ok) - }) - window.startApp = { - // @ts-expect-error ignore startApp mock - set: (): unknown => window.startApp, - start: mockStart - } - appHelpers.checkApp = jest.fn().mockImplementation(() => { - return true - }) - - const res = await appHelpers.startApp({ - appId: 'io.cozy.drive.mobile', - uri: 'cozydrive://' - }) - - expect(appHelpers.checkApp).toHaveBeenCalled() - expect(res).toBe(ok) - }) -}) diff --git a/packages/cozy-device-helper/src/apps.ts b/packages/cozy-device-helper/src/apps.ts deleted file mode 100644 index 6afd9b13c7..0000000000 --- a/packages/cozy-device-helper/src/apps.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { isAndroidApp } from './platform' - -const cordovaPluginIsInstalled = (): Window['startApp'] => window.startApp - -type AppInfo = { - appId: string - uri: string -} - -/** - * Normalize startApp params for Android and iOS - */ -const getParams = ({ appId, uri }: AppInfo): { package: string } | string => { - if (isAndroidApp()) { - return { - package: appId - } - } else { - return uri - } -} - -const exported: { - startApp?: { - (arg0: { appId: string; uri: string }): unknown - (appInfo: AppInfo): Promise - } - checkApp?: (appInfo: AppInfo) => Promise< - | boolean - | string - | { - versionName: string - packageName: string - versionCode: number - applicationInfo: string - } - > -} = {} - -/** - * Start an application if it is installed on the phone - * @returns Promise - False if the application was not able to be started - */ -const startApp = (exported.startApp = async function ( - appInfo: AppInfo -): Promise { - const startAppPlugin = window.startApp - const isAppInstalled = await exported.checkApp(appInfo) - if (isAppInstalled) { - const params = getParams(appInfo) - return new Promise((resolve, reject) => { - if (!cordovaPluginIsInstalled()) { - reject( - new Error( - `Cordova plugin 'com.lampa.startapp' is not installed. This plugin is needed to start a native app. Required by cozy-bar` - ) - ) - return - } - - startAppPlugin.set(params).start(resolve, reject) - }) - } else { - return false - } -}) - -/** - * Check that an application is installed on the phone - * @returns Promise - Promise containing information on the application - * - * @example - * > checkApp({ appId: 'io.cozy.drive.mobile', uri: 'cozydrive://' }) - * Promise.resolve({ - * versionName: "0.9.2", - * packageName: "io.cozy.drive.mobile", - * versionCode: 902, - * applicationInfo: "ApplicationInfo{70aa0ef io.cozy.drive.mobile}" - * }) - */ -const checkApp = (exported.checkApp = async function (appInfo): Promise< - | boolean - | string - | { - versionName: string - packageName: string - versionCode: number - applicationInfo: string - } -> { - const startAppPlugin = window.startApp - const params = getParams(appInfo) - return new Promise((resolve, reject) => { - if (!cordovaPluginIsInstalled()) { - reject(new Error(`Cordova plugin 'com.lampa.startapp' is not installed.`)) - return - } - - startAppPlugin.set(params).check( - ( - infos: - | string - | { - versionName: string - packageName: string - versionCode: number - applicationInfo: string - } - | PromiseLike<{ - versionName: string - packageName: string - versionCode: number - applicationInfo: string - }> - ) => { - return resolve(infos === 'OK' ? true : infos) - }, - (error: boolean | string) => { - if ( - error === false || - (error as string).indexOf('NameNotFoundException') === 0 - ) { - // Plugin returns an error 'NameNotFoundException' on Android and - // false on iOS when an application is not found. - // We prefer to always return false - resolve(false) - } else { - reject(error) - } - } - ) - }) -}) - -export { checkApp, startApp } -export default exported diff --git a/packages/cozy-device-helper/src/cordova.ts b/packages/cozy-device-helper/src/cordova.ts deleted file mode 100644 index a63099a201..0000000000 --- a/packages/cozy-device-helper/src/cordova.ts +++ /dev/null @@ -1,3 +0,0 @@ -// cordova -export const isCordova = (): boolean => - typeof window !== 'undefined' && window.cordova !== undefined diff --git a/packages/cozy-device-helper/src/device.ts b/packages/cozy-device-helper/src/device.ts deleted file mode 100644 index 73e0b6ccbd..0000000000 --- a/packages/cozy-device-helper/src/device.ts +++ /dev/null @@ -1,37 +0,0 @@ -import capitalize from 'lodash/capitalize' - -import { isCordova } from './cordova' -import { hasDevicePlugin } from './plugins' -import { isIOSApp } from './platform' - -const DEFAULT_DEVICE = 'Device' - -type device = 'iPhone' | 'iPad' | 'Watch' | 'AppleTV' | typeof DEFAULT_DEVICE - -// device -const getAppleModel = (identifier: string): device => { - const devices: device[] = ['iPhone', 'iPad', 'Watch', 'AppleTV'] - - for (const device of devices) { - if (identifier.match(new RegExp(device))) { - return device - } - } - - return DEFAULT_DEVICE -} - -export const getDeviceName = (): string => { - if (!hasDevicePlugin()) { - if (isCordova()) { - console.warn('You should install `cordova-plugin-device`.') // eslint-disable-line no-console - } - return DEFAULT_DEVICE - } - - const { manufacturer, model: originalModel } = window.device - - const model = isIOSApp() ? getAppleModel(originalModel) : originalModel - - return `${capitalize(manufacturer)} ${model}` -} diff --git a/packages/cozy-device-helper/src/index.spec.ts b/packages/cozy-device-helper/src/index.spec.ts deleted file mode 100644 index 240065020d..0000000000 --- a/packages/cozy-device-helper/src/index.spec.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { - hasDevicePlugin, - hasInAppBrowserPlugin, - hasSafariPlugin, - hasNetworkInformationPlugin -} from './index' - -describe('cordova plugins', () => { - it('should identify has device plugin', () => { - // @ts-expect-error replace mock with boolean - window.cordova = true - // @ts-expect-error replace mock with boolean - window.device = true - expect(hasDevicePlugin()).toBeTruthy() - window.cordova = undefined - // @ts-expect-error replace mock with boolean - window.device = true - expect(hasDevicePlugin()).toBeFalsy() - // @ts-expect-error replace mock with boolean - window.cordova = true - window.device = undefined - expect(hasDevicePlugin()).toBeFalsy() - }) - it('should identify has InAppBrowser plugin', () => { - // @ts-expect-error replace InAppBrowser mock with boolean - window.cordova = { InAppBrowser: true } - expect(hasInAppBrowserPlugin()).toBeTruthy() - // @ts-expect-error replace mock with empty object - window.cordova = {} - expect(hasInAppBrowserPlugin()).toBeFalsy() - }) - it('should identify has Safari plugin', async () => { - let hasSafari - // @ts-expect-error replace mock with boolean - window.cordova = true - // @ts-expect-error partially mock SafariViewController - window.SafariViewController = { isAvailable: (f): void => f(true) } - hasSafari = await hasSafariPlugin() - expect(hasSafari).toBeTruthy() - window.cordova = undefined - // @ts-expect-error partially mock SafariViewController - window.SafariViewController = { isAvailable: (f): void => f(true) } - hasSafari = await hasSafariPlugin() - expect(hasSafari).toBeFalsy() - // @ts-expect-error replace mock with boolean - window.cordova = true - window.SafariViewController = undefined - hasSafari = await hasSafariPlugin() - expect(hasSafari).toBeFalsy() - }) - it('should identify has Network Information plugin', () => { - // @ts-expect-error replace mock with boolean - window.cordova = true - // @ts-expect-error assign to read only property navigator.connection - window.navigator.connection = { type: 3 } - expect(hasNetworkInformationPlugin()).toBeTruthy() - window.cordova = undefined - // @ts-expect-error assign to read only property navigator.connection - window.navigator.connection = { type: 3 } - expect(hasNetworkInformationPlugin()).toBeFalsy() - // @ts-expect-error replace mock with boolean - window.cordova = true - // @ts-expect-error assign to read only property navigator.connection - window.navigator.connection = undefined - expect(hasNetworkInformationPlugin()).toBeFalsy() - }) -}) diff --git a/packages/cozy-device-helper/src/index.ts b/packages/cozy-device-helper/src/index.ts index ca921f2fb8..a9e8b4bd93 100644 --- a/packages/cozy-device-helper/src/index.ts +++ b/packages/cozy-device-helper/src/index.ts @@ -1,24 +1,5 @@ -export { - getPlatform, - isIOSApp, - isAndroidApp, - isWebApp, - isMobileApp, - isAndroid, - isIOS, - isMobile -} from './platform' -export { getDeviceName } from './device' -export { checkApp, startApp } from './apps' -export { - hasDevicePlugin, - hasInAppBrowserPlugin, - hasSafariPlugin, - hasNetworkInformationPlugin -} from './plugins' -export { isCordova } from './cordova' +export { isAndroid, isIOS, isMobile } from './platform' -export { nativeLinkOpen } from './link' export { openDeeplinkOrRedirect } from './deeplink' export { diff --git a/packages/cozy-device-helper/src/link.ts b/packages/cozy-device-helper/src/link.ts deleted file mode 100644 index 905b174fd4..0000000000 --- a/packages/cozy-device-helper/src/link.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { hasSafariPlugin, hasInAppBrowserPlugin } from './plugins' - -export const nativeLinkOpen = async ({ - url -}: { - url: Location -}): Promise => { - if ((await hasSafariPlugin()) && window.SafariViewController) { - window.SafariViewController.show( - { - url: url, - transition: 'curl' - }, - result => { - if (result.event === 'closed') { - window.SafariViewController.hide() - } - }, - () => { - window.SafariViewController.hide() - } - ) - } else if (hasInAppBrowserPlugin()) { - const target = '_blank' - const options = 'clearcache=yes,zoom=no' - window.cordova.InAppBrowser.open(url, target, options) - } else { - window.location = url - } -} diff --git a/packages/cozy-device-helper/src/platform.spec.ts b/packages/cozy-device-helper/src/platform.spec.ts deleted file mode 100644 index 7176847bba..0000000000 --- a/packages/cozy-device-helper/src/platform.spec.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { - isWebApp, - isMobileApp, - isIOSApp, - isAndroidApp, - getPlatform -} from './platform' - -describe('platforms', () => { - it('should identify is a web application', () => { - expect(isWebApp()).toBeTruthy() - }) - it('should identify is a mobile application', () => { - // @ts-expect-error replace mock with boolean - window.cordova = true - expect(isMobileApp()).toBeTruthy() - window.cordova = undefined - expect(isMobileApp()).toBeFalsy() - }) - it('should identify is an iOS or Android application', () => { - // @ts-expect-error do not mock InAppBrowser - window.cordova = { platformId: 'ios' } - expect(isIOSApp()).toBeTruthy() - expect(isAndroidApp()).toBeFalsy() - // @ts-expect-error do not mock InAppBrowser - window.cordova = { platformId: 'android' } - expect(isIOSApp()).toBeFalsy() - expect(isAndroidApp()).toBeTruthy() - }) - it('should return platform', () => { - window.cordova = undefined - expect(getPlatform()).toEqual('web') - // @ts-expect-error do not mock InAppBrowser - window.cordova = { platformId: 'ios' } - expect(getPlatform()).toEqual('ios') - // @ts-expect-error do not mock InAppBrowser - window.cordova = { platformId: 'android' } - expect(getPlatform()).toEqual('android') - }) -}) diff --git a/packages/cozy-device-helper/src/platform.ts b/packages/cozy-device-helper/src/platform.ts index 5395a89bad..5ce99f88a7 100644 --- a/packages/cozy-device-helper/src/platform.ts +++ b/packages/cozy-device-helper/src/platform.ts @@ -1,22 +1,3 @@ -import { isCordova } from './cordova' - -const ANDROID_PLATFORM = 'android' -const IOS_PLATFORM = 'ios' -const WEB_PLATFORM = 'web' - -type PLATFORM = - | typeof ANDROID_PLATFORM - | typeof IOS_PLATFORM - | typeof WEB_PLATFORM - -export const getPlatform = (): PLATFORM => - isCordova() ? window.cordova.platformId : WEB_PLATFORM -const isPlatform = (platform: PLATFORM): boolean => getPlatform() === platform -export const isIOSApp = (): boolean => isPlatform(IOS_PLATFORM) -export const isAndroidApp = (): boolean => isPlatform(ANDROID_PLATFORM) -export const isWebApp = (): boolean => isPlatform(WEB_PLATFORM) -export const isMobileApp = (): boolean => isCordova() - // return if is on an Android Device (native or browser) export const isAndroid = (): boolean => window.navigator.userAgent && diff --git a/packages/cozy-device-helper/src/plugins.ts b/packages/cozy-device-helper/src/plugins.ts deleted file mode 100644 index e569408b5f..0000000000 --- a/packages/cozy-device-helper/src/plugins.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { isCordova } from './cordova' - -export const hasDevicePlugin = (): boolean => { - return isCordova() && window.device !== undefined -} -export const hasInAppBrowserPlugin = (): boolean => { - return isCordova() && window.cordova.InAppBrowser !== undefined -} -export const hasSafariPlugin = (): Promise => { - return new Promise(resolve => { - if (!isCordova() || window.SafariViewController === undefined) { - resolve(false) - return - } - - window.SafariViewController.isAvailable(available => resolve(available)) - }) -} - -/** - * Check if the Cordova's cordova-plugin-network-information plugin is installed - * @see https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/ - * @returns {boolean} - */ -export const hasNetworkInformationPlugin = (): boolean => { - return isCordova() && window.navigator.connection !== undefined -} diff --git a/packages/cozy-device-helper/src/withHasSafariPlugin.tsx b/packages/cozy-device-helper/src/withHasSafariPlugin.tsx deleted file mode 100644 index 401a052b79..0000000000 --- a/packages/cozy-device-helper/src/withHasSafariPlugin.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import React, { PureComponent } from 'react' -import { isIOSApp, hasSafariPlugin } from '.' - -export const withHasSafariPlugin = () => { - return (WrappedComponent: React.ElementType): typeof PureComponent => { - return class withHasSafariPluginComponent extends PureComponent { - state = { - hasSafariPlugin: false - } - - checkSafariPlugin = async (): Promise => { - if (isIOSApp) { - const checked = await hasSafariPlugin() - this.setState({ hasSafariPlugin: checked }) - } - } - - componentDidMount(): void { - void this.checkSafariPlugin() - } - - render(): JSX.Element { - return - } - } - } -} - -export default withHasSafariPlugin diff --git a/yarn.lock b/yarn.lock index 9116b4d6dc..0704ae1b17 100644 --- a/yarn.lock +++ b/yarn.lock @@ -20189,17 +20189,6 @@ msgpack5@^4.0.2: readable-stream "^2.3.6" safe-buffer "^5.1.2" -"mui-bottom-sheet@git+https://github.com/cozy/mui-bottom-sheet.git#v1.0.9": - version "1.0.8" - uid "3dc4c2a245ab39079bc2f73546bccf80847be14c" - resolved "git+https://github.com/cozy/mui-bottom-sheet.git#3dc4c2a245ab39079bc2f73546bccf80847be14c" - dependencies: - "@juggle/resize-observer" "^3.1.3" - jest-environment-jsdom-sixteen "^1.0.3" - react-spring "9.0.0-rc.3" - react-use-gesture "^7.0.8" - react-use-measure "^2.0.0" - "mui-bottom-sheet@https://github.com/cozy/mui-bottom-sheet.git#v1.0.9": version "1.0.8" resolved "https://github.com/cozy/mui-bottom-sheet.git#3dc4c2a245ab39079bc2f73546bccf80847be14c"