From d17708722569c45081d82b283368e8be94a6c852 Mon Sep 17 00:00:00 2001 From: Ahsan Ayaz Date: Mon, 14 May 2018 16:06:23 +0500 Subject: [PATCH] fix(angular): populated the platforms array (#14466) 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. --- angular/src/providers/platform.ts | 54 ++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/angular/src/providers/platform.ts b/angular/src/providers/platform.ts index 27a04a7648..e840fa0d7e 100644 --- a/angular/src/providers/platform.ts +++ b/angular/src/providers/platform.ts @@ -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; /** @@ -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'); } } @@ -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