diff --git a/src/lib/functions/Platform/index.shared.ts b/src/lib/functions/Platform/index.shared.ts new file mode 100644 index 00000000..86b22373 --- /dev/null +++ b/src/lib/functions/Platform/index.shared.ts @@ -0,0 +1,14 @@ +export type PlatformOSType = 'android' | 'ios' | 'web' + +export type PlataformSelectSpecifics = { + [platform in PlatformOSType | 'default']?: T +} + +export interface PlatformSelectOptions { + fallbackToWeb?: boolean +} + +export type PlatformSelect = ( + specifics: PlataformSelectSpecifics, + options: PlatformSelectOptions, +) => T \ No newline at end of file diff --git a/src/lib/functions/Platform/index.ts b/src/lib/functions/Platform/index.ts new file mode 100644 index 00000000..7666d28e --- /dev/null +++ b/src/lib/functions/Platform/index.ts @@ -0,0 +1,18 @@ +// Credits: Bruno Lemos (https://github.com/devhubapp/devhub/blob/master/packages/components/src/libs/platform/index.ts) + +import { Platform as _Platform } from 'react-native' + +import { PlataformSelectSpecifics, PlatformSelectOptions } from './index.shared' + +export const Platform = { + ..._Platform, + isElectron: false, + isStandalone: true, + realOS: _Platform.OS, + selectUsingRealOS( + specifics: PlataformSelectSpecifics, + _options?: PlatformSelectOptions, + ) { + return _Platform.select(specifics) + }, +} \ No newline at end of file diff --git a/src/lib/functions/Platform/index.web.ts b/src/lib/functions/Platform/index.web.ts new file mode 100644 index 00000000..2d7aee26 --- /dev/null +++ b/src/lib/functions/Platform/index.web.ts @@ -0,0 +1,76 @@ +import { Platform as _Platform } from 'react-native' + +import { + PlataformSelectSpecifics, + PlatformOSType, + PlatformSelectOptions, +} from './index.shared' + +// From: https://github.com/cheton/is-electron +function isElectron() { + if ( + !(typeof window !== 'undefined' && (window as any).bara === true && !!(window as any).ipc) + ) { + return false + } + + if ( + typeof window !== 'undefined' && + typeof (window as any).process === 'object' && + (window as any).process.type === 'renderer' + ) { + return true + } + + if ( + typeof process !== 'undefined' && + typeof process.versions === 'object' && + !!(process.versions as any).electron + ) { + return true + } + + if ( + typeof navigator === 'object' && + typeof navigator.userAgent === 'string' && + navigator.userAgent.indexOf('Electron') >= 0 + ) { + return true + } + + return false +} + +function getOSName(): PlatformOSType { + const userAgent = + navigator.userAgent || navigator.vendor || (window as any).opera + + if (/android/i.test(userAgent)) return 'android' + + if (/iPad|iPhone|iPod/.test(userAgent) && !(window as any).MSStream) + return 'ios' + + return 'web' +} + +const realOS = getOSName() + +export const Platform = { + realOS, + ..._Platform, + isElectron: isElectron(), + isStandalone: (window.navigator as any).standalone, + selectUsingRealOS( + specifics: PlataformSelectSpecifics, + { fallbackToWeb = false }: PlatformSelectOptions = {}, + ) { + const result = + Platform.realOS in specifics + ? specifics[realOS] + : fallbackToWeb && 'web' in specifics + ? specifics.web + : specifics.default + + return result + }, +} \ No newline at end of file diff --git a/src/lib/functions/index.ts b/src/lib/functions/index.ts index e199d7aa..56a583d8 100644 --- a/src/lib/functions/index.ts +++ b/src/lib/functions/index.ts @@ -1,3 +1,5 @@ export * from './use-barn-state' export * from './barn-bridge' export * from './use-components-stream' + +export * from './Platform' \ No newline at end of file