All files / app-play/src/services silent-mode.ts

68.42% Statements 13/19
50% Branches 1/2
42.85% Functions 3/7
66.66% Lines 12/18

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                                                                          2x 2x 2x                                   3x 3x 3x 3x 3x   3x 3x               1x           1x                
// "Play in silent mode": the platform audio session asks for `'playback'`, so sound plays with the iPhone / iPad silent
// switch on (`silent-mode-playback.ts`). The setting is kept in the Options dialog's state and remembered, shows on the
// Options dialog and the start card where the browser offers it, and a hint points at it the first time sound can start.
 
import { applySilentModePlayback, supportsSystemAudioSession } from '@be-music/player-browser/audio';
import {
  SILENT_MODE_STRINGS,
  installSilentModePlayback,
  mountSilentModeHint,
  readSilentModeHintSeen,
  resolveSilentModeLanguage,
  shouldShowSilentModeHint,
  storePlayInSilentMode,
  storeSilentModeHintSeen,
  type StartSilentModeOption,
} from '../silent-mode-playback.ts';
 
export interface SilentModeSettingOptions {
  /** The state the setting is kept in: the Options dialog's, whose checkbox writes it. */
  readonly settings: { playInSilentMode: boolean };
  /** Whether the device is a phone or a tablet, where the hint shows. */
  readonly touchFirst: boolean;
  /** Called after the setting is remembered, so the Options dialog document stays with it. */
  readonly onRemember?: () => void;
}
 
/** The "Play in silent mode" setting, and the places it shows. */
export class SilentModeSetting {
  /** The Options dialog's "Play in silent mode" checkbox, when the browser offers the setting. */
  public controller: { refresh(): unknown } | undefined;
  /** The start card's "Play in silent mode" checkbox, when the browser offers the setting. */
  public startOption: Pick<StartSilentModeOption, 'setChecked'> | undefined;
  private readonly settings: { playInSilentMode: boolean };
  private readonly touchFirst: boolean;
  private readonly onRemember: (() => void) | undefined;
 
  public constructor(options: SilentModeSettingOptions) {
    this.settings = options.settings;
    this.touchFirst = options.touchFirst;
    this.onRemember = options.onRemember;
  }
 
  /**
   * Sets the platform audio session before any scene creates an AudioContext, and again on the first gesture (which is
   * when the scenes resume their contexts) — see `silent-mode-playback.ts`.
   */
  public install(): void {
    installSilentModePlayback({
      enabled: () => this.settings.playInSilentMode,
      target: window,
      visibility: document,
      onFirstGesture: () => this.maybeShowHint(),
    });
  }
 
  /** Applies and remembers the "Play in silent mode" setting (Options dialog checkbox or the hint's button). */
  public set(value: boolean): void {
    this.settings.playInSilentMode = value;
    storePlayInSilentMode(value);
    applySilentModePlayback(value);
    this.controller?.refresh();
    this.startOption?.setChecked(value);
    // Once the user has found the setting, the hint has nothing left to say.
    storeSilentModeHintSeen();
    this.onRemember?.();
  }
 
  /**
   * The first time sound can start on an iPhone / iPad, point at the setting: the silent switch mutes web audio there
   * and nothing on screen says why. Shown once per browser, only while the setting is off.
   */
  public maybeShowHint(): void {
    const show = shouldShowSilentModeHint({
      supported: supportsSystemAudioSession(),
      enabled: this.settings.playInSilentMode,
      seen: readSilentModeHintSeen(),
      touchFirst: this.touchFirst,
    });
    Eif (!show) return;
    storeSilentModeHintSeen();
    mountSilentModeHint({
      strings: SILENT_MODE_STRINGS[resolveSilentModeLanguage(navigator.language)],
      onEnable: () => this.set(true),
    });
  }
}