Skip to content

Commit

Permalink
Improve platform detection, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed May 17, 2023
1 parent 566efc8 commit 9ab11d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/StringClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type Data from './Data'
import type RemoteObservableEvent from './RemoteObservableEvent'
import AsyncIterableSubject from './lib/AsyncIterableSubject'
import Deferred from './lib/Deferred'
import { detectPlatform } from './lib/Platform'
import assert from './lib/assert'
import generateId from './lib/generateId'
import isNonNull from './lib/isNonNull'
import platform from './lib/platform'

export type RemoteFunction<I, O> = (arg: I) => Promise<O>
export type RemoteObservable<T> = AsyncIterable<T>
Expand Down Expand Up @@ -147,6 +147,8 @@ export default class StringClient<T extends RemoteInterface> implements Client {

assert(isNonNull(this._context), ErrorMessages.NOT_CONNECTED)

const platform = detectPlatform(this._context)

if (platform === 'android') {
const { default: AndroidConnector } = await import('./AndroidConnector')
return new AndroidConnector({
Expand Down
22 changes: 22 additions & 0 deletions src/lib/Platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Platform = 'android' | 'ios'

export function detectPlatform(context: Window): Platform | null {
const {
navigator: { maxTouchPoints, platform, userAgent },
} = context

if (/android/i.test(userAgent)) {
return 'android'
}

if (
/iPad|iPhone|iPod/.test(userAgent) ||
(platform === 'MacIntel' && maxTouchPoints > 1)
) {
return 'ios'
}

return null
}

export default Platform
42 changes: 42 additions & 0 deletions src/lib/__tests__/Platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { detectPlatform } from '../Platform'

describe('detectPlatform', () => {
it('should detect iPhone', () => {
const context = {
navigator: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 16_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
platform: 'iPhone',
maxTouchPoints: 5,
},
} as Window

expect(detectPlatform(context)).toEqual('ios')
})

it('should detect newer iPads', () => {
const context = {
navigator: {
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)',
platform: 'MacIntel',
maxTouchPoints: 5,
},
} as Window

expect(detectPlatform(context)).toEqual('ios')
})

it('should detect older iPads', () => {
const context = {
navigator: {
userAgent:
'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10',
platform: 'MacIntel',
maxTouchPoints: 5,
},
} as Window

expect(detectPlatform(context)).toEqual('ios')
})
})
12 changes: 0 additions & 12 deletions src/lib/platform.ts

This file was deleted.

0 comments on commit 9ab11d1

Please sign in to comment.