Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker committed Jan 20, 2019
1 parent 80628ca commit 39d9e7d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/xplat/_lib_files/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './base';
export * from './environments/environment';
export * from './models';
export * from './services';
export {CoreModule} from './core.module';
1 change: 1 addition & 0 deletions src/xplat/_lib_files/core/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './xplat-window.interface';
24 changes: 24 additions & 0 deletions src/xplat/_lib_files/core/models/xplat-window.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export interface IXPlatWindow {
navigator: any;
location: any;
localStorage: any;
process?: any;
require?: any;
alert(msg: any): Promise<any>;
confirm(msg: any): Promise<any>;
setTimeout(handler: (...args: any[]) => void, timeout?: number): number;
clearTimeout(timeoutId: number): void;
setInterval(
handler: (...args: any[]) => void,
ms?: number,
...args: any[]
): number;
clearInterval(intervalId: number): void;

// ...expand support for more window methods as you needed here...
}

export type XPlatWindow = Partial<
Pick<Window, Exclude<keyof Window, keyof IXPlatWindow>>
> &
IXPlatWindow;
6 changes: 5 additions & 1 deletion src/xplat/_lib_files/core/services/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { InjectionToken, Type } from '@angular/core';
import { IXPlatWindow } from '../models';

/**
* Various InjectionTokens shared across all platforms
* Always suffix with 'Token' for clarity and consistency
*/

export const PlatformLanguageToken = new InjectionToken<string>('PlatformLanguage');
export const PlatformLanguageToken = new InjectionToken<string>('PlatformLanguageToken');
export const PlatformWindowToken = new InjectionToken<IXPlatWindow>(
'PlatformWindowToken'
);
29 changes: 6 additions & 23 deletions src/xplat/_lib_files/core/services/window.service.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
// angular
import { Injectable, ViewContainerRef } from '@angular/core';
import { Injectable, Inject, ViewContainerRef } from '@angular/core';

// app
import { isObject, isNativeScript } from '@<%= npmScope %>/utils';

@Injectable()
export class WindowPlatformService {
public navigator: any = {};
public location: any = {};
public localStorage: any;
public process: any;
public require: any;
public alert(msg: any) {}
public confirm(msg: any) {}
public setTimeout(handler: (...args: any[]) => void, timeout?: number) {
return 0;
}
public clearTimeout(timeoutId: number) {}
public setInterval(handler: (...args: any[]) => void, ms?: number, ...args: any[]) {
return 0;
}
public clearInterval(intervalId: number) {}

// ...You can expand support for more window methods as you need them here...
}
import { XPlatWindow } from '../models';
import { PlatformWindowToken } from './tokens';

@Injectable()
export class WindowService {

constructor(private _platformWindow: WindowPlatformService) {}
constructor(
@Inject(PlatformWindowToken) private _platformWindow: XPlatWindow,
) {}

public get navigator() {
return this._platformWindow.navigator;
Expand Down
8 changes: 4 additions & 4 deletions src/xplat/_nativescript_files/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { TNSFontIconModule } from 'nativescript-ngx-fonticon';

// libs
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { CoreModule, PlatformLanguageToken, WindowPlatformService } from '@<%= npmScope %>/core';<% if (sample) { %>
import { CoreModule, PlatformLanguageToken, PlatformWindowToken } from '@<%= npmScope %>/core';<% if (sample) { %>
import { ITEM_PROVIDERS } from '@<%= npmScope %>/features';<% } %>
import { throwIfAlreadyLoaded } from '@<%= npmScope %>/utils';

// app
import { PROVIDERS } from './services';
import { TNSWindowPlatformService } from './services/tns-window.service';
import { TNSWindowService } from './services/tns-window.service';
import { TNSTranslateLoader } from './services/tns-translate.loader';

// factories
Expand All @@ -39,8 +39,8 @@ export function createTranslateLoader() {
useFactory: platformLangFactory
},
{
provide: WindowPlatformService,
useClass: TNSWindowPlatformService
provide: PlatformWindowToken,
useClass: TNSWindowService
}
]),
TranslateModule.forRoot({
Expand Down
4 changes: 2 additions & 2 deletions src/xplat/_nativescript_files/core/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AppService } from './app.service';
import { TNSWindowPlatformService } from './tns-window.service';
import { TNSWindowService } from './tns-window.service';

export const PROVIDERS: any[] = [
AppService,
TNSWindowPlatformService
TNSWindowService
];

export * from './app.service';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as timer from 'tns-core-modules/timer';
import { isString } from '@<%= npmScope %>/utils';

@Injectable()
export class TNSWindowPlatformService {
export class TNSWindowService {
private _dialogOpened = false;

public get navigator(): any {
Expand Down
4 changes: 2 additions & 2 deletions src/xplat/_web_files/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { throwIfAlreadyLoaded } from '@<%= npmScope %>/utils';
import { CoreModule, PlatformLanguageToken, WindowPlatformService } from '@<%= npmScope %>/core';<% if (sample) { %>
import { CoreModule, PlatformLanguageToken, PlatformWindowToken } from '@<%= npmScope %>/core';<% if (sample) { %>
import { ITEM_PROVIDERS } from '@<%= npmScope %>/features';<% } %>

// bring in custom web services here...
Expand Down Expand Up @@ -36,7 +36,7 @@ export function createTranslateLoader(http: HttpClient) {
useFactory: platformLangFactory
},
{
provide: WindowPlatformService,
provide: PlatformWindowToken,
useFactory: winFactory
}
]),
Expand Down

0 comments on commit 39d9e7d

Please sign in to comment.