Skip to content

Commit

Permalink
feat(media): media service (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbourget authored and mbarbeau committed Apr 21, 2017
1 parent 7c97cee commit 39655a6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/lib/core/media/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './media.type';
export * from './media.service';
17 changes: 17 additions & 0 deletions src/lib/core/media/media.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TestBed, inject } from '@angular/core/testing';

import { MediaService } from './media.service';

describe('MediaService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
MediaService
]
});
});

it('should ...', inject([MediaService], (service: MediaService) => {
expect(service).toBeTruthy();
}));
});
43 changes: 43 additions & 0 deletions src/lib/core/media/media.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

import { Media } from './media.type';


@Injectable()
export class MediaService {

public media$ = new BehaviorSubject<Media>(undefined);

private media: Media;

constructor() {
this.setMedia();

window.addEventListener('resize', (event) => {
this.setMedia();
});
}

getMedia(): Media {
const width = window.innerWidth;

let media = 'desktop';
if (width <= 500) {
media = 'mobile';
} else if (width <= 800) {
media = 'tablet';
}

return media as Media;
}

private setMedia() {
const media = this.getMedia();
if (media !== this.media) {
this.media = media;
this.media$.next(media);
}
}

}
1 change: 1 addition & 0 deletions src/lib/core/media/media.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Media = 'mobile' | 'tablet' | 'desktop';
7 changes: 0 additions & 7 deletions src/lib/core/message/message.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ export interface Message {
export interface MessageOptions {
timeOut: number;
}

export enum MessageType {
ERROR = <any> 'error',
WARN = <any> 'warn',
INFO = <any> 'info',
SUCCESS = <any> 'success'
}
7 changes: 5 additions & 2 deletions src/lib/core/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NgModule, ModuleWithProviders } from '@angular/core';

import { RequestService } from './request';
import { MediaService } from './media';
import { MessageService } from './message';
import { RequestService } from './request';


@NgModule({
Expand All @@ -13,12 +14,14 @@ export class IgoCoreModule {
return {
ngModule: IgoCoreModule,
providers: [
MediaService,
MessageService,
RequestService
]
};
}
}

export * from './request';
export * from './media';
export * from './message';
export * from './request';

0 comments on commit 39655a6

Please sign in to comment.