Skip to content

Commit af5db2f

Browse files
committed
fix(angula): platform logic belongs to core
1 parent d177087 commit af5db2f

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

angular/src/providers/platform.ts

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,12 @@
11

22
import { EventEmitter, Injectable } from '@angular/core';
33
import { proxyEvent } from '../util/util';
4-
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet } from '@ionic/core';
5-
6-
export interface PlatformConfig {
7-
name: string;
8-
isMatch: (win: Window) => boolean;
9-
}
10-
11-
export const PLATFORM_CONFIGS: PlatformConfig[] = [
12-
{
13-
name: 'ipad',
14-
isMatch: isIpad
15-
},
16-
{
17-
name: 'iphone',
18-
isMatch: isIphone
19-
},
20-
{
21-
name: 'ios',
22-
isMatch: isIOS
23-
},
24-
{
25-
name: 'android',
26-
isMatch: isAndroid
27-
},
28-
{
29-
name: 'phablet',
30-
isMatch: isPhablet
31-
},
32-
{
33-
name: 'tablet',
34-
isMatch: isTablet
35-
},
36-
{
37-
name: 'cordova',
38-
isMatch: isCordova
39-
},
40-
{
41-
name: 'electron',
42-
isMatch: isElectron
43-
}
44-
45-
];
4+
import { PlatformConfig, detectPlatforms } from '@ionic/core';
465

476
@Injectable()
487
export class Platform {
498

50-
private _platforms: PlatformConfig[] = PLATFORM_CONFIGS;
9+
private _platforms = detectPlatforms(window);
5110
private _readyPromise: Promise<string>;
5211

5312
/**
@@ -88,11 +47,9 @@ export class Platform {
8847
this._readyPromise = new Promise(res => { readyResolve = res; } );
8948
if ((window as any)['cordova']) {
9049
window.addEventListener('deviceready', () => {
91-
this._platforms = this.detectPlatforms(window, this._platforms);
9250
readyResolve('cordova');
9351
}, {once: true});
9452
} else {
95-
this._platforms = this.detectPlatforms(window, this._platforms);
9653
readyResolve('dom');
9754
}
9855
}
@@ -151,9 +108,8 @@ export class Platform {
151108
* Detects the platforms using window and the platforms config provided.
152109
* Populates the platforms array so they can be used later on for platform detection.
153110
*/
154-
detectPlatforms(win: Window, platforms: PlatformConfig[]) {
155-
// bracket notation to ensure they're not property renamed
156-
return platforms.filter(p => p.isMatch(win));
111+
detectPlatforms(platforms: PlatformConfig[]) {
112+
return detectPlatforms(window, platforms);
157113
}
158114

159115
/**

core/src/utils/platform.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,48 @@ export function matchMedia(win: Window, query: string, fallback = false): boolea
7070
? win.matchMedia(query).matches
7171
: fallback;
7272
}
73+
74+
export interface PlatformConfig {
75+
name: string;
76+
isMatch: (win: Window) => boolean;
77+
}
78+
79+
export const PLATFORM_CONFIGS: PlatformConfig[] = [
80+
{
81+
name: 'ipad',
82+
isMatch: isIpad
83+
},
84+
{
85+
name: 'iphone',
86+
isMatch: isIphone
87+
},
88+
{
89+
name: 'ios',
90+
isMatch: isIOS
91+
},
92+
{
93+
name: 'android',
94+
isMatch: isAndroid
95+
},
96+
{
97+
name: 'phablet',
98+
isMatch: isPhablet
99+
},
100+
{
101+
name: 'tablet',
102+
isMatch: isTablet
103+
},
104+
{
105+
name: 'cordova',
106+
isMatch: isCordova
107+
},
108+
{
109+
name: 'electron',
110+
isMatch: isElectron
111+
}
112+
];
113+
114+
export function detectPlatforms(win: Window, platforms: PlatformConfig[] = PLATFORM_CONFIGS) {
115+
// bracket notation to ensure they're not property renamed
116+
return platforms.filter(p => p.isMatch(win));
117+
}

0 commit comments

Comments
 (0)