aboutsummaryrefslogtreecommitdiff
path: root/src/content/usecases/AddonEnabledUseCase.ts
blob: f49fde5a152046c454673bf10a70091c38f4d760 (plain) (blame)
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
import { injectable, inject } from "tsyringe";
import AddonIndicatorClient from "../client/AddonIndicatorClient";
import AddonEnabledRepository from "../repositories/AddonEnabledRepository";
import ConsoleFramePresenter from "../presenters/ConsoleFramePresenter";

@injectable()
export default class AddonEnabledUseCase {
  constructor(
    @inject("AddonIndicatorClient")
    private readonly indicator: AddonIndicatorClient,
    @inject("AddonEnabledRepository")
    private readonly repository: AddonEnabledRepository,
    @inject("ConsoleFramePresenter")
    private readonly consoleFramePresenter: ConsoleFramePresenter
  ) {}

  async enable(): Promise<void> {
    await this.setEnabled(true);
  }

  async disable(): Promise<void> {
    await this.setEnabled(false);
  }

  async toggle(): Promise<void> {
    const current = this.repository.get();
    await this.setEnabled(!current);
  }

  getEnabled(): boolean {
    return this.repository.get();
  }

  private async setEnabled(on: boolean): Promise<void> {
    this.repository.set(on);

    if (this.consoleFramePresenter.isTopWindow()) {
      if (on) {
        this.consoleFramePresenter.attach();
      } else {
        this.consoleFramePresenter.detach();
      }
    }
    await this.indicator.setEnabled(on);
  }
}