Skip to content

Commit

Permalink
feat(functions): add Platform API for OS detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nampdn committed May 1, 2019
1 parent 9fc918a commit a2e7395
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/functions/Platform/index.shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type PlatformOSType = 'android' | 'ios' | 'web'

export type PlataformSelectSpecifics<T> = {
[platform in PlatformOSType | 'default']?: T
}

export interface PlatformSelectOptions {
fallbackToWeb?: boolean
}

export type PlatformSelect<T> = (
specifics: PlataformSelectSpecifics<T>,
options: PlatformSelectOptions,
) => T
18 changes: 18 additions & 0 deletions src/lib/functions/Platform/index.ts
Original file line number Diff line number Diff line change
@@ -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<T>(
specifics: PlataformSelectSpecifics<T>,
_options?: PlatformSelectOptions,
) {
return _Platform.select(specifics)
},
}
76 changes: 76 additions & 0 deletions src/lib/functions/Platform/index.web.ts
Original file line number Diff line number Diff line change
@@ -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<T>(
specifics: PlataformSelectSpecifics<T>,
{ fallbackToWeb = false }: PlatformSelectOptions = {},
) {
const result =
Platform.realOS in specifics
? specifics[realOS]
: fallbackToWeb && 'web' in specifics
? specifics.web
: specifics.default

return result
},
}
2 changes: 2 additions & 0 deletions src/lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './use-barn-state'
export * from './barn-bridge'
export * from './use-components-stream'

export * from './Platform'

0 comments on commit a2e7395

Please sign in to comment.