<commit_before>import { Component, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core';

import { AssetsService } from 'src/app/assets.service';
import { ManagerService } from 'src/app/manager.service';
import { dpi, DpiValue } from 'src/app/model/dpi';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-adjust-dpi',
  templateUrl: './adjust-dpi.component.html',
  styleUrls: ['./adjust-dpi.component.scss'],
})
export class AdjustDpiComponent implements OnInit, OnDestroy {
  currentDpi?: dpi;

  stages!: number[];

  defaultDpiValues!: number[];

  minDpi!: number;

  maxDpi!: number;

  step!: number;

  mouseBottomImg = this.assetsService.getDeviceBottomImgUri();

  selectedDpi!: number;

  selectedStage!: number;

  invertedLevels!: Record<number, number>;

  deviceSubscription!: Subscription;

  // eslint-disable-next-line no-useless-constructor
  constructor(
    private changeDetectorRef: ChangeDetectorRef,
    private assetsService: AssetsService,
    private manager: ManagerService,
  ) {}

  ngOnInit(): void {
    if (!this.manager.dpiCapabilities) return;
    const { dpiCapabilities } = this.manager;

    this.manager.requestDpiLevels().then(currentDpi => {
      this.currentDpi = currentDpi;

      this.stages = Array.from(
        { length: currentDpi.count },
        (_, i) => dpiCapabilities.levels[currentDpi.levels[i]],
      );

      // MOCK
      this.defaultDpiValues = Array.from(
        { length: currentDpi.count },
        () => 1500,
      );

      const levelsValues = Object.values(dpiCapabilities.levels);
      this.maxDpi = Math.max(...levelsValues);
      this.minDpi = Math.min(...levelsValues);

      this.step = (this.maxDpi - this.minDpi) / (levelsValues.length - 1);

      this.selectedStage = currentDpi.current;
      this.selectedDpi =
        dpiCapabilities.levels[currentDpi.levels[this.selectedStage]];

      this.invertedLevels = Object.entries(dpiCapabilities.levels).reduce(
        (acc, [key, value]) => ({ ...acc, [value]: parseInt(key, 10) }),
        {},
      );

      this.deviceSubscription = this.manager.currentDpiStageSubject.subscribe(
        stage => {
          if (stage === undefined) return;
          this.selectedStage = stage;
          this.selectedDpi = this.stages[stage];
        },
      );
    });
  }

  ngOnDestroy(): void {
    this.deviceSubscription.unsubscribe();
  }

  setDpiInput(stage: number, dpiValue: string): void {
    const oldValue = this.stages[stage];

    // Temporarily change value to force re-render
    this.stages[stage] = 0;
    this.changeDetectorRef.detectChanges();

    const parsedValue = dpiValue.match(/\d+/g)?.join(''); // Get only numbers
    const value = parsedValue ? Number(parsedValue) : oldValue;
    const filteredValue = Math.round(value / this.step) * this.step;
    if (filteredValue > this.maxDpi) {
      this.stages[stage] = this.maxDpi;
    } else if (filteredValue < this.minDpi) {
      this.stages[stage] = this.minDpi;
    } else {
      this.stages[stage] = filteredValue;
    }
    this.selectedDpi = this.stages[stage];
    this.manager
      .setDpiLevel(stage, this.getKeyFromDpiValue(this.stages[stage]))
      .then((setDpiValue: DpiValue) => {
        // TODO: Add proper component for user feedback<commit_msg>fix: setDpiInput dont change dpi stage<commit_after>import { Component, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core';

import { AssetsService } from 'src/app/assets.service';
import { ManagerService } from 'src/app/manager.service';
import { dpi, DpiValue } from 'src/app/model/dpi';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-adjust-dpi',
  templateUrl: './adjust-dpi.component.html',
  styleUrls: ['./adjust-dpi.component.scss'],
})
export class AdjustDpiComponent implements OnInit, OnDestroy {
  currentDpi?: dpi;

  stages!: number[];

  defaultDpiValues!: number[];

  minDpi!: number;

  maxDpi!: number;

  step!: number;

  mouseBottomImg = this.assetsService.getDeviceBottomImgUri();

  selectedDpi!: number;

  selectedStage!: number;

  invertedLevels!: Record<number, number>;

  deviceSubscription!: Subscription;

  // eslint-disable-next-line no-useless-constructor
  constructor(
    private changeDetectorRef: ChangeDetectorRef,
    private assetsService: AssetsService,
    private manager: ManagerService,
  ) {}

  ngOnInit(): void {
    if (!this.manager.dpiCapabilities) return;
    const { dpiCapabilities } = this.manager;

    this.manager.requestDpiLevels().then(currentDpi => {
      this.currentDpi = currentDpi;

      this.stages = Array.from(
        { length: currentDpi.count },
        (_, i) => dpiCapabilities.levels[currentDpi.levels[i]],
      );

      // MOCK
      this.defaultDpiValues = Array.from(
        { length: currentDpi.count },
        () => 1500,
      );

      const levelsValues = Object.values(dpiCapabilities.levels);
      this.maxDpi = Math.max(...levelsValues);
      this.minDpi = Math.min(...levelsValues);

      this.step = (this.maxDpi - this.minDpi) / (levelsValues.length - 1);

      this.selectedStage = currentDpi.current;
      this.selectedDpi =
        dpiCapabilities.levels[currentDpi.levels[this.selectedStage]];

      this.invertedLevels = Object.entries(dpiCapabilities.levels).reduce(
        (acc, [key, value]) => ({ ...acc, [value]: parseInt(key, 10) }),
        {},
      );

      this.deviceSubscription = this.manager.currentDpiStageSubject.subscribe(
        stage => {
          if (stage === undefined) return;
          this.selectedStage = stage;
          this.selectedDpi = this.stages[stage];
        },
      );
    });
  }

  ngOnDestroy(): void {
    this.deviceSubscription.unsubscribe();
  }

  setDpiInput(stage: number, dpiValue: string): void {
    const oldValue = this.stages[stage];

    // Temporarily change value to force re-render
    this.stages[stage] = 0;
    this.changeDetectorRef.detectChanges();

    const parsedValue = dpiValue.match(/\d+/g)?.join(''); // Get only numbers
    const value = parsedValue ? Number(parsedValue) : oldValue;
    const filteredValue = Math.round(value / this.step) * this.step;
    if (filteredValue > this.maxDpi) {
      this.stages[stage] = this.maxDpi;
    } else if (filteredValue < this.minDpi) {
      this.stages[stage] = this.minDpi;
    } else {
      this.stages[stage] = filteredValue;
    }
    if (stage !== this.selectedStage) this.changeStage(stage);
    this.manager
      .setDpiLevel(stage, this.getKeyFromDpiValue(this.stages[stage]))
      .then((setDpiValue: DpiValue) => {
        // TODO: Add proper component for user feedback
{'fraction_non_alphanumeric': 0.13609645817633761, 'fraction_numerical': 0.0024114544084400903, 'mean_word_length': 3.142322097378277, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1880368', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2089, 'n_words': 396}