2e01839b2c28c9dd6646c5d1cedaf2fd16fab642
[oweals/peertube.git] / client / src / app / shared / misc / screen.service.ts
1 import { Injectable } from '@angular/core'
2
3 @Injectable()
4 export class ScreenService {
5   private windowInnerWidth: number
6   private lastFunctionCallTime: number
7   private cacheForMs = 500
8
9   constructor () {
10     this.refreshWindowInnerWidth()
11   }
12
13   isInSmallView () {
14     return this.getWindowInnerWidth() < 600
15   }
16
17   isInMobileView () {
18     return this.getWindowInnerWidth() < 500
19   }
20
21   // Cache window inner width, because it's an expensive call
22   private getWindowInnerWidth () {
23     if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
24
25     return this.windowInnerWidth
26   }
27
28   private refreshWindowInnerWidth () {
29     this.lastFunctionCallTime = new Date().getTime()
30
31     this.windowInnerWidth = window.innerWidth
32   }
33
34   private cacheWindowInnerWidthExpired () {
35     return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)
36   }
37 }