Skip to content

Commit 33efe01

Browse files
committed
improve cordova integration
1 parent 0d3d25d commit 33efe01

File tree

10 files changed

+57
-10
lines changed

10 files changed

+57
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ You can also add more environments with ```--env.target <env-name>``` but there
175175
run throw so if your like to set the evnirnment for ```npm start``` you have to do this like so:
176176

177177
```shell
178-
npm start -- -- --env.target <json-file-name-without-extension>
178+
npm start -- -- -- --env.target <json-file-name-without-extension>
179179
```
180180

181181
This because ```npm start``` runs ```npm run server:dev``` and then the target command, so we have to to pass the ```--env.target``` by providing two times ```--```.

src/app/app.vm.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import { autoinject } from 'aurelia-framework';
1+
import { Lazy, inject } from 'aurelia-framework';
22
import { Router, RouterConfiguration } from 'aurelia-router';
33
import { I18N } from 'aurelia-i18n';
44
import { AppConfigService } from './services/app-config.service';
5+
import { CordovaService } from './services/cordova.service';
56

6-
@autoinject
7+
@inject(I18N, AppConfigService, Lazy.of(CordovaService))
78
export class AppViewModel {
89
public router: Router;
910

1011
constructor(
1112
private i18n: I18N,
12-
private appConfigService: AppConfigService
13+
private appConfigService: AppConfigService,
14+
private cordovaServiceFn: () => CordovaService
1315
) { }
1416

1517
public configureRouter(config: RouterConfiguration, router: Router): void {
1618
config.title = this.i18n.tr('TITLE');
17-
if (this.appConfigService.getPlatform() === 'web') {
19+
if (this.appConfigService.platformIsBrowser()) {
1820
config.options.pushState = true;
1921
}
22+
if (this.appConfigService.platformIsMobile()) {
23+
this.cordovaServiceFn();
24+
}
2025
config.map([
2126
{
2227
route: ['', 'welcome'],

src/app/models/platforms.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const platforms = {
2+
BROWSER: 'browser',
3+
MOBILE: 'mobile'
4+
};

src/app/modules/welcome/welcome.vm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { autoinject } from 'aurelia-framework';
2-
import { LogManager, Logger} from './../../services/logging.service';
2+
import { LogManager, Logger} from './../../services/logger.service';
33
import { AppConfigService } from './../../services/app-config.service';
44

55
@autoinject

src/app/services/app-config.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { platforms } from './../models/platforms';
2+
13
/**
24
* Gets and defines the environment configruation
35
*/
@@ -32,4 +34,6 @@ export class AppConfigService {
3234
public getEnv(): string { return this.env; }
3335
public getPlatform(): string { return this.platform; }
3436
public getConfig(): IAppConfigEnv { return this.config; }
37+
public platformIsBrowser(): boolean { return this.platform === platforms.BROWSER; }
38+
public platformIsMobile(): boolean { return this.platform === platforms.MOBILE; }
3539
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { LogManager, Logger} from './logger.service';
2+
3+
export class CordovaService {
4+
5+
private logger: Logger;
6+
private cordovaReady: boolean = false;
7+
8+
constructor() {
9+
this.logger = LogManager.getLogger('CordovaService');
10+
this.logger.debug('Cordova service initialized');
11+
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
12+
}
13+
14+
public isCordovaReady(): boolean {
15+
return this.cordovaReady;
16+
}
17+
18+
private onDeviceReady(): void {
19+
this.cordovaReady = true;
20+
21+
document.addEventListener('pause', this.onPause.bind(this), false);
22+
document.addEventListener('resume', this.onResume.bind(this), false);
23+
24+
this.logger.debug('Cordova platform:', window.cordova.platformId);
25+
};
26+
27+
private onPause(): void {
28+
this.logger.debug('Cordova onPause');
29+
};
30+
31+
private onResume(): void {
32+
this.logger.debug('Cordova onResume');
33+
};
34+
}

test/unit/app.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('the App module', () => {
4444

4545
appConfigSub = new AppConfigService();
4646

47-
sut = new AppViewModel(sut, appConfigSub);
47+
sut = new AppViewModel(sut, appConfigSub, (<any>undefined));
4848
sut.configureRouter(mockedRouter, mockedRouter);
4949
});
5050

typings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "aurelia-skeleton-navigation-webpack",
33
"dependencies": {},
44
"globalDependencies": {
5+
"cordova": "registry:dt/cordova#0.0.0+20160819210047",
56
"jasmine": "registry:dt/jasmine#2.5.0+20161003201800",
67
"jquery": "registry:dt/jquery#1.10.0+20160704162008",
78
"lodash": "registry:dt/lodash#4.14.0+20160817155621",

webpack.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (envArguments) {
1313
let config;
1414
const ENV = process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() || 'development';
1515
const pkg = require(path.join(process.cwd(), 'package.json'));
16-
let PLATFORM = 'web';
16+
let PLATFORM = 'browser';
1717
let TARGET = ENV;
1818
let DOCS = false;
1919
if (envArguments) {
@@ -98,8 +98,7 @@ module.exports = function (envArguments) {
9898
},
9999
output: {
100100
path: outDir,
101-
},
102-
externals: [ /cordova(\.js)?$/ ]
101+
}
103102
};
104103

105104
const configAurelia = {

0 commit comments

Comments
 (0)