Skip to content

Commit

Permalink
fix(angular): populated the platforms array (#14466)
Browse files Browse the repository at this point in the history
The `_platforms` array wasn't being populated on provider instantiation. Added the appropriate code to populate the array so that when we use other functions from the provider, we get the correct data in return.
  • Loading branch information
AhsanAyaz authored and manucorporat committed May 14, 2018
1 parent d93b1d5 commit d177087
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion angular/src/providers/platform.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@

import { EventEmitter, Injectable } from '@angular/core';
import { proxyEvent } from '../util/util';
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet } from '@ionic/core';

export interface PlatformConfig {
name: string;
isMatch: (win: Window) => boolean;
}

export const PLATFORM_CONFIGS: PlatformConfig[] = [
{
name: 'ipad',
isMatch: isIpad
},
{
name: 'iphone',
isMatch: isIphone
},
{
name: 'ios',
isMatch: isIOS
},
{
name: 'android',
isMatch: isAndroid
},
{
name: 'phablet',
isMatch: isPhablet
},
{
name: 'tablet',
isMatch: isTablet
},
{
name: 'cordova',
isMatch: isCordova
},
{
name: 'electron',
isMatch: isElectron
}

];

@Injectable()
export class Platform {

private _platforms: PlatformConfig[] = [];
private _platforms: PlatformConfig[] = PLATFORM_CONFIGS;
private _readyPromise: Promise<string>;

/**
Expand Down Expand Up @@ -51,9 +88,11 @@ export class Platform {
this._readyPromise = new Promise(res => { readyResolve = res; } );
if ((window as any)['cordova']) {
window.addEventListener('deviceready', () => {
this._platforms = this.detectPlatforms(window, this._platforms);
readyResolve('cordova');
}, {once: true});
} else {
this._platforms = this.detectPlatforms(window, this._platforms);
readyResolve('dom');
}
}
Expand Down Expand Up @@ -104,6 +143,19 @@ export class Platform {
return this._platforms.some(p => p.name === platformName);
}

/**
* @param {Window} win the window object
* @param {PlatformConfig[]} platforms an array of platforms (platform configs)
* to get the appropriate platforms according to the configs provided.
* @description
* Detects the platforms using window and the platforms config provided.
* Populates the platforms array so they can be used later on for platform detection.
*/
detectPlatforms(win: Window, platforms: PlatformConfig[]) {
// bracket notation to ensure they're not property renamed
return platforms.filter(p => p.isMatch(win));
}

/**
* @returns {array} the array of platforms
* @description
Expand Down

0 comments on commit d177087

Please sign in to comment.