Selaa lähdekoodia

Reset battery status after 1 minute without connectivity

Danilo Bargen 7 vuotta sitten
vanhempi
commit
dfb2639b91
2 muutettua tiedostoa jossa 27 lisäystä ja 1 poistoa
  1. 8 1
      src/services/battery.ts
  2. 19 0
      src/services/webclient.ts

+ 8 - 1
src/services/battery.ts

@@ -44,7 +44,7 @@ export class BatteryStatusService {
     public setStatus(batteryStatus: threema.BatteryStatus): void {
         // Handle null percent value. This can happen if the battery status could not be determined.
         if (batteryStatus.percent === null) {
-            this.batteryStatus = null;
+            this.clearStatus();
             return;
         }
 
@@ -73,6 +73,13 @@ export class BatteryStatusService {
         }
     }
 
+    /**
+     * Clear the battery status information.
+     */
+    public clearStatus(): void {
+        this.batteryStatus = null;
+    }
+
     /**
      * Is battery status information available?
      */

+ 19 - 0
src/services/webclient.ts

@@ -181,6 +181,7 @@ export class WebClientService {
     private trustedKeyStore: TrustedKeyStoreService;
     public clientInfo: threema.ClientInfo;
     public version = null;
+    private batteryStatusTimeout: ng.IPromise<void> = null;
 
     private blobCache = new Map<string, threema.BlobInfo>();
     private loadingMessages = new Map<string, boolean>();
@@ -277,6 +278,7 @@ export class WebClientService {
                 }
             },
         );
+        this.stateService.evtGlobalConnectionStateChange.attach(this.handleGlobalConnectionStateChange.bind(this));
     }
 
     get me(): threema.MeReceiver {
@@ -3179,4 +3181,21 @@ export class WebClientService {
     public clearIsTypingFlags(): void {
         this.typing.clearAll();
     }
+
+    private handleGlobalConnectionStateChange(stateChange: threema.GlobalConnectionStateChange): void {
+        const isOk = stateChange.state === threema.GlobalConnectionState.Ok;
+        const wasOk = stateChange.prevState === threema.GlobalConnectionState.Ok;
+        if (!isOk && wasOk && this.batteryStatusService.dataAvailable) {
+            this.batteryStatusTimeout = this.$timeout(
+                () => {
+                    this.batteryStatusService.clearStatus();
+                    this.batteryStatusTimeout = null;
+                },
+                60000,
+            );
+        } else if (isOk && this.batteryStatusTimeout !== null) {
+            this.$timeout.cancel(this.batteryStatusTimeout);
+            this.batteryStatusTimeout = null;
+        }
+    }
 }