]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/misc/screen.service.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / misc / screen.service.ts
index 5b17a914a5d76dd984d9a9ae56e1b47232baf859..220d41d59b732736644afb5efff6c41de7dbb0cd 100644 (file)
@@ -1,23 +1,57 @@
-import { Injectable, NgZone } from '@angular/core'
+import { Injectable } from '@angular/core'
 
 @Injectable()
 export class ScreenService {
   private windowInnerWidth: number
+  private lastFunctionCallTime: number
+  private cacheForMs = 500
 
-  constructor (private zone: NgZone) {
-    this.windowInnerWidth = window.innerWidth
-
-    // Try to cache a little bit window.innerWidth
-    this.zone.runOutsideAngular(() => {
-      setInterval(() => this.windowInnerWidth = window.innerWidth, 500)
-    })
+  constructor () {
+    this.refreshWindowInnerWidth()
   }
 
   isInSmallView () {
-    return this.windowInnerWidth < 600
+    return this.getWindowInnerWidth() < 800
   }
 
   isInMobileView () {
-    return this.windowInnerWidth < 500
+    return this.getWindowInnerWidth() < 500
+  }
+
+  isInTouchScreen () {
+    return 'ontouchstart' in window || navigator.msMaxTouchPoints
+  }
+
+  getNumberOfAvailableMiniatures () {
+    const screenWidth = this.getWindowInnerWidth()
+
+    let numberOfVideos = 1
+
+    if (screenWidth > 1850) numberOfVideos = 6
+    else if (screenWidth > 1600) numberOfVideos = 5
+    else if (screenWidth > 1370) numberOfVideos = 4
+    else if (screenWidth > 1100) numberOfVideos = 3
+    else if (screenWidth > 850) numberOfVideos = 2
+
+    return numberOfVideos
+  }
+
+  // Cache window inner width, because it's an expensive call
+  getWindowInnerWidth () {
+    if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
+
+    return this.windowInnerWidth
+  }
+
+  private refreshWindowInnerWidth () {
+    this.lastFunctionCallTime = new Date().getTime()
+
+    this.windowInnerWidth = window.innerWidth
+  }
+
+  private cacheWindowInnerWidthExpired () {
+    if (!this.lastFunctionCallTime) return true
+
+    return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)
   }
 }