From 39655a629e4b1b11a62b8fa64205c8440762e704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-=C3=89ric?= Date: Fri, 21 Apr 2017 15:38:56 -0400 Subject: [PATCH] feat(media): media service (#18) --- src/lib/core/media/index.ts | 2 ++ src/lib/core/media/media.service.spec.ts | 17 +++++++++ src/lib/core/media/media.service.ts | 43 +++++++++++++++++++++++ src/lib/core/media/media.type.ts | 1 + src/lib/core/message/message.interface.ts | 7 ---- src/lib/core/module.ts | 7 ++-- 6 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 src/lib/core/media/index.ts create mode 100644 src/lib/core/media/media.service.spec.ts create mode 100644 src/lib/core/media/media.service.ts create mode 100644 src/lib/core/media/media.type.ts diff --git a/src/lib/core/media/index.ts b/src/lib/core/media/index.ts new file mode 100644 index 0000000000..d662caabaf --- /dev/null +++ b/src/lib/core/media/index.ts @@ -0,0 +1,2 @@ +export * from './media.type'; +export * from './media.service'; diff --git a/src/lib/core/media/media.service.spec.ts b/src/lib/core/media/media.service.spec.ts new file mode 100644 index 0000000000..09eefe7d04 --- /dev/null +++ b/src/lib/core/media/media.service.spec.ts @@ -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(); + })); +}); diff --git a/src/lib/core/media/media.service.ts b/src/lib/core/media/media.service.ts new file mode 100644 index 0000000000..a98ddac2a1 --- /dev/null +++ b/src/lib/core/media/media.service.ts @@ -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(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); + } + } + +} diff --git a/src/lib/core/media/media.type.ts b/src/lib/core/media/media.type.ts new file mode 100644 index 0000000000..38d58ea528 --- /dev/null +++ b/src/lib/core/media/media.type.ts @@ -0,0 +1 @@ +export type Media = 'mobile' | 'tablet' | 'desktop'; diff --git a/src/lib/core/message/message.interface.ts b/src/lib/core/message/message.interface.ts index 110b5dc0ee..4af490f122 100644 --- a/src/lib/core/message/message.interface.ts +++ b/src/lib/core/message/message.interface.ts @@ -8,10 +8,3 @@ export interface Message { export interface MessageOptions { timeOut: number; } - -export enum MessageType { - ERROR = 'error', - WARN = 'warn', - INFO = 'info', - SUCCESS = 'success' -} diff --git a/src/lib/core/module.ts b/src/lib/core/module.ts index 4de85739a6..bdcc219865 100644 --- a/src/lib/core/module.ts +++ b/src/lib/core/module.ts @@ -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({ @@ -13,6 +14,7 @@ export class IgoCoreModule { return { ngModule: IgoCoreModule, providers: [ + MediaService, MessageService, RequestService ] @@ -20,5 +22,6 @@ export class IgoCoreModule { } } -export * from './request'; +export * from './media'; export * from './message'; +export * from './request';