All files / src/app/components/countdown countdown.component.ts

59.01% Statements 36/61
33.33% Branches 10/30
66.66% Functions 8/12
57.62% Lines 34/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                          97x 1x 1x 1x   1x 1x 1x 1x   1x 1x         1x     1x     1x                                                               1x 1x 56x 1x       3x 3x                 60x 60x 60x   60x 60x       60x 60x 60x 60x                     1576x     1576x       4x 4x   4x 4x              
import { NgIf } from '@angular/common';
import { Component, Input, OnChanges, OnDestroy, OnInit } from '@angular/core';
 
import { interval, Subscription } from 'rxjs';
 
import { TwinpadApiService } from '../../services/twinpad-api.service';
 
@Component({
  selector: 'app-countdown',
  imports: [NgIf],
  templateUrl: './countdown.component.html',
  styleUrl: './countdown.component.scss'
})
export class CountdownComponent implements OnInit, OnChanges, OnDestroy {
  @Input() targetTimestamp: number | null = null;
  @Input() targetSignalId: string | null = null;
  @Input() compactMode: boolean = false;
 
  days: number | null = null;
  hours: number | null = null;
  minutes: number | null = null;
  seconds: number | null = null;
 
  isPositiveTime = false;
  targetDate: Date | null = null;
  localTimeZone: string;
  localMonth: string;
  UTCMonth: string;
 
  private subscription: Subscription | null = null;
  signalSubscription: Subscription;
 
  constructor(private twinpadApiService: TwinpadApiService) {}
 
  ngOnInit() {
    Iif (this.targetSignalId !== null && this.targetSignalId !== "") {
      this.twinpadApiService.getSignal(this.targetSignalId).subscribe({
        next: targetSignal => {
          if (targetSignal.data_type !== "epoch") {
            this.targetSignalId = null;
            return;
          }
          this.signalSubscription = this.twinpadApiService.callPeriodically(() => {
            return this.twinpadApiService.getSignalValue(this.targetSignalId as string);
          }, 5000).subscribe({
            next: countDownValue => {
              let valueChanged: boolean = false;
              if (typeof countDownValue.value === "string") {
                if (this.targetTimestamp !== null) {
                  valueChanged = true;
                }
                this.targetTimestamp = null;
              }
              else {
                if (this.targetTimestamp !== (countDownValue.value ?? null)) {
                  valueChanged = true;
                }
                this.targetTimestamp = countDownValue.value ?? null;
              }
              if (valueChanged) {
                this.updateTarget();
              }
            }
          });
        }
      });
    }
    this.updateTarget();
    this.updateTime();
    this.subscription = interval(100).subscribe(() => this.updateTime());
    this.localTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
  }
 
  ngOnChanges() {
    this.updateTarget();
    this.updateTime();
  }
 
  ngOnDestroy() {
    this.subscription?.unsubscribe();
    this.signalSubscription.unsubscribe();
  }
 
  private updateTime() {
    const now = Math.floor(Date.now() / 1000);
    Eif (this.targetTimestamp !== null) {
      let diff = this.targetTimestamp - now;
 
      this.isPositiveTime = diff < 0;
      Iif (this.isPositiveTime) {
        diff = -diff;
      }
 
      this.days = Math.floor(diff / 86400);
      this.hours = Math.floor((diff % 86400) / 3600);
      this.minutes = Math.floor((diff % 3600) / 60);
      this.seconds = Math.floor(diff % 60);
    }
    else {
      this.days = null;
      this.hours = null;
      this.minutes = null;
      this.seconds = null;
    }
  }
 
  formatNumber(value: number | null): string {
    Iif (value === null) {
      return '--';
    }
    return value < 10 ? '0' + value : value.toString();
  }
 
  updateTarget() {
    Eif (this.targetTimestamp !== null) {
      this.targetDate = new Date(this.targetTimestamp * 1000);
 
      this.localMonth = this.targetDate.toLocaleString('en-US', { month: 'long' });
      this.UTCMonth = this.targetDate.toLocaleString('en-US', { month: 'long', timeZone: 'UTC' });
    }
    else {
      this.targetDate = null;
    }
  }
}