|
| 1 | +type iosIdiom = |
| 2 | + | Phone |
| 3 | + | Pad |
| 4 | + | TV; |
| 5 | + |
| 6 | +[@bs.module "react-native"] [@bs.scope "Platform"] |
| 7 | +external _ios_isPad : bool = "isPad"; |
| 8 | + |
| 9 | +[@bs.module "react-native"] [@bs.scope "Platform"] |
| 10 | +external _ios_isTVOS : bool = "isTVOS"; |
| 11 | + |
1 | 12 | type os = |
2 | | - | IOS |
| 13 | + | IOS(iosIdiom) |
3 | 14 | | Android; |
4 | 15 |
|
5 | | -[@bs.scope "Platform"] [@bs.module "react-native"] external _os : string = "OS"; |
| 16 | +[@bs.module "react-native"] [@bs.scope "Platform"] |
| 17 | +external _os : string = "OS"; |
6 | 18 |
|
7 | | -let os = |
| 19 | +exception UnknownPlatform(string); |
| 20 | + |
| 21 | +let os = () => |
8 | 22 | switch _os { |
9 | | - | "ios" => IOS |
10 | | - | _ => Android |
| 23 | + | "ios" => |
| 24 | + switch _ios_isPad { |
| 25 | + | true => IOS(Pad) |
| 26 | + | _ => |
| 27 | + switch _ios_isTVOS { |
| 28 | + | true => IOS(TV) |
| 29 | + | _ => IOS(Phone) |
| 30 | + } |
| 31 | + } |
| 32 | + | "android" => Android |
| 33 | + | x => raise(UnknownPlatform(x)) |
11 | 34 | }; |
12 | 35 |
|
13 | | -let equals = (targetOs) => |
14 | | - switch (os, targetOs) { |
15 | | - | (IOS, IOS) => true |
| 36 | +let equals = targetOs => |
| 37 | + switch (os(), targetOs) { |
| 38 | + | (IOS(_), IOS(_)) => true |
16 | 39 | | (Android, Android) => true |
17 | | - | (IOS, Android) => false |
18 | | - | (Android, IOS) => false |
| 40 | + | exception (UnknownPlatform(_)) => false |
| 41 | + | _ => false |
19 | 42 | }; |
20 | 43 |
|
21 | | -[@bs.scope "Platform"] [@bs.module "react-native"] external version : int = "Version"; |
| 44 | +[@bs.module "react-native"] [@bs.scope "Platform"] |
| 45 | +external _version : Js.undefined(int) = "Version"; |
22 | 46 |
|
23 | | -[@bs.scope "Platform"] [@bs.module "react-native"] |
24 | | -external _select : {. "ios": 'a, "android": 'a} => 'a = |
25 | | - "select"; |
| 47 | +exception UnknownVersion; |
26 | 48 |
|
27 | | -let select = (~ios, ~android) => _select({"ios": ios, "android": android}); |
| 49 | +let version = () => |
| 50 | + switch (Js.Undefined.to_opt(_version)) { |
| 51 | + | Some(v) => v |
| 52 | + | None => raise(UnknownVersion) |
| 53 | + }; |
0 commit comments