2021-06-04 16:19:34 +00:00
|
|
|
<?php
|
|
|
|
|
2023-08-20 15:24:56 +00:00
|
|
|
namespace App\Values;
|
2021-06-04 16:19:34 +00:00
|
|
|
|
|
|
|
use Illuminate\Contracts\Support\Arrayable;
|
|
|
|
use JsonSerializable;
|
2024-01-23 22:50:50 +00:00
|
|
|
use Webmozart\Assert\Assert;
|
2021-06-04 16:19:34 +00:00
|
|
|
|
|
|
|
final class UserPreferences implements Arrayable, JsonSerializable
|
|
|
|
{
|
2024-01-23 22:50:50 +00:00
|
|
|
private const CASTS = [
|
|
|
|
'volume' => 'float',
|
|
|
|
'show_now_playing_notification' => 'boolean',
|
|
|
|
'confirm_before_closing' => 'boolean',
|
|
|
|
'transcode_on_mobile' => 'boolean',
|
2024-09-03 14:51:23 +00:00
|
|
|
'transcode_quality' => 'integer',
|
2024-01-23 22:50:50 +00:00
|
|
|
'show_album_art_overlay' => 'boolean',
|
|
|
|
'lyrics_zoom_level' => 'integer',
|
|
|
|
'make_uploads_public' => 'boolean',
|
2024-03-25 22:59:38 +00:00
|
|
|
'continuous_playback' => 'boolean',
|
2024-01-23 22:50:50 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
private const CUSTOMIZABLE_KEYS = [
|
|
|
|
'volume',
|
|
|
|
'repeat_mode',
|
|
|
|
'equalizer',
|
|
|
|
'artists_view_mode',
|
|
|
|
'albums_view_mode',
|
|
|
|
'theme',
|
|
|
|
'show_now_playing_notification',
|
|
|
|
'confirm_before_closing',
|
|
|
|
'transcode_on_mobile',
|
2024-09-03 14:51:23 +00:00
|
|
|
'transcode_quality',
|
2024-01-23 22:50:50 +00:00
|
|
|
'show_album_art_overlay',
|
|
|
|
'make_uploads_public',
|
|
|
|
'support_bar_no_bugging',
|
|
|
|
'lyrics_zoom_level',
|
|
|
|
'visualizer',
|
|
|
|
'active_extra_panel_tab',
|
2024-03-25 22:59:38 +00:00
|
|
|
'continuous_playback',
|
2024-01-23 22:50:50 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
private const ALL_KEYS = self::CUSTOMIZABLE_KEYS + ['lastfm_session_key'];
|
|
|
|
|
|
|
|
private function __construct(
|
|
|
|
public float $volume,
|
|
|
|
public string $repeatMode,
|
|
|
|
public Equalizer $equalizer,
|
|
|
|
public string $artistsViewMode,
|
|
|
|
public string $albumsViewMode,
|
|
|
|
public string $theme,
|
|
|
|
public bool $showNowPlayingNotification,
|
|
|
|
public bool $confirmBeforeClosing,
|
|
|
|
public bool $transcodeOnMobile,
|
2024-09-03 14:51:23 +00:00
|
|
|
public int $transcodeQuality,
|
2024-01-23 22:50:50 +00:00
|
|
|
public bool $showAlbumArtOverlay,
|
|
|
|
public bool $makeUploadsPublic,
|
|
|
|
public bool $supportBarNoBugging,
|
2024-03-25 22:59:38 +00:00
|
|
|
public bool $continuousPlayback,
|
2024-01-23 22:50:50 +00:00
|
|
|
public int $lyricsZoomLevel,
|
|
|
|
public string $visualizer,
|
|
|
|
public ?string $activeExtraPanelTab,
|
|
|
|
public ?string $lastFmSessionKey
|
|
|
|
) {
|
|
|
|
Assert::oneOf($this->repeatMode, ['NO_REPEAT', 'REPEAT_ALL', 'REPEAT_ONE']);
|
|
|
|
Assert::oneOf($this->artistsViewMode, ['list', 'thumbnails']);
|
|
|
|
Assert::oneOf($this->albumsViewMode, ['list', 'thumbnails']);
|
|
|
|
Assert::oneOf($this->activeExtraPanelTab, [null, 'Lyrics', 'Artist', 'Album', 'YouTube']);
|
2024-09-03 14:51:23 +00:00
|
|
|
|
|
|
|
if (!in_array($this->transcodeQuality, [64, 96, 128, 192, 256, 320], true)) {
|
|
|
|
$this->transcodeQuality = 128;
|
|
|
|
}
|
2024-01-23 22:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromArray(array $data): self
|
|
|
|
{
|
|
|
|
return new self(
|
|
|
|
volume: $data['volume'] ?? 7.0,
|
|
|
|
repeatMode: $data['repeat_mode'] ?? 'NO_REPEAT',
|
|
|
|
equalizer: isset($data['equalizer']) ? Equalizer::tryMake($data['equalizer']) : Equalizer::default(),
|
|
|
|
artistsViewMode: $data['artists_view_mode'] ?? 'thumbnails',
|
|
|
|
albumsViewMode: $data['albums_view_mode'] ?? 'thumbnails',
|
|
|
|
theme: $data['theme'] ?? 'classic',
|
|
|
|
showNowPlayingNotification: $data['show_now_playing_notification'] ?? true,
|
|
|
|
confirmBeforeClosing: $data['confirm_before_closing'] ?? false,
|
|
|
|
transcodeOnMobile: $data['transcode_on_mobile'] ?? true,
|
2024-09-03 14:51:23 +00:00
|
|
|
transcodeQuality: $data['transcode_quality'] ?? 128,
|
2024-01-23 22:50:50 +00:00
|
|
|
showAlbumArtOverlay: $data['show_album_art_overlay'] ?? true,
|
|
|
|
makeUploadsPublic: $data['make_uploads_public'] ?? false,
|
|
|
|
supportBarNoBugging: $data['support_bar_no_bugging'] ?? false,
|
2024-03-25 22:59:38 +00:00
|
|
|
continuousPlayback: $data['continuous_playback'] ?? false,
|
2024-01-23 22:50:50 +00:00
|
|
|
lyricsZoomLevel: $data['lyrics_zoom_level'] ?? 1,
|
|
|
|
visualizer: $data['visualizer'] ?? 'default',
|
|
|
|
activeExtraPanelTab: $data['active_extra_panel_tab'] ?? null,
|
|
|
|
lastFmSessionKey: $data['lastfm_session_key'] ?? null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function customizable(string $key): bool
|
2021-06-04 16:19:34 +00:00
|
|
|
{
|
2024-01-23 22:50:50 +00:00
|
|
|
return in_array($key, self::CUSTOMIZABLE_KEYS, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set(string $key, mixed $value): self
|
|
|
|
{
|
|
|
|
self::assertValidKey($key);
|
|
|
|
|
|
|
|
$cast = self::CASTS[$key] ?? null;
|
|
|
|
|
|
|
|
$value = match ($cast) {
|
|
|
|
'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN),
|
|
|
|
'integer' => (int) $value,
|
|
|
|
'float' => (float) $value,
|
|
|
|
default => $value,
|
|
|
|
};
|
|
|
|
|
|
|
|
$arr = $this->toArray();
|
|
|
|
$arr[$key] = $value;
|
|
|
|
|
|
|
|
return self::fromArray($arr);
|
2021-06-04 16:19:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 22:50:50 +00:00
|
|
|
public static function assertValidKey(string $key): void
|
2021-06-04 16:19:34 +00:00
|
|
|
{
|
2024-01-23 22:50:50 +00:00
|
|
|
Assert::inArray($key, self::ALL_KEYS);
|
2021-06-04 16:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return array<mixed> */
|
|
|
|
public function toArray(): array
|
|
|
|
{
|
2024-01-23 22:50:50 +00:00
|
|
|
return [
|
|
|
|
'theme' => $this->theme,
|
|
|
|
'show_now_playing_notification' => $this->showNowPlayingNotification,
|
|
|
|
'confirm_before_closing' => $this->confirmBeforeClosing,
|
|
|
|
'show_album_art_overlay' => $this->showAlbumArtOverlay,
|
|
|
|
'transcode_on_mobile' => $this->transcodeOnMobile,
|
2024-09-03 14:51:23 +00:00
|
|
|
'transcode_quality' => $this->transcodeQuality,
|
2024-01-23 22:50:50 +00:00
|
|
|
'make_uploads_public' => $this->makeUploadsPublic,
|
|
|
|
'lastfm_session_key' => $this->lastFmSessionKey,
|
|
|
|
'support_bar_no_bugging' => $this->supportBarNoBugging,
|
|
|
|
'artists_view_mode' => $this->artistsViewMode,
|
|
|
|
'albums_view_mode' => $this->albumsViewMode,
|
|
|
|
'repeat_mode' => $this->repeatMode,
|
|
|
|
'volume' => $this->volume,
|
|
|
|
'equalizer' => $this->equalizer->toArray(),
|
|
|
|
'lyrics_zoom_level' => $this->lyricsZoomLevel,
|
|
|
|
'visualizer' => $this->visualizer,
|
|
|
|
'active_extra_panel_tab' => $this->activeExtraPanelTab,
|
2024-03-25 22:59:38 +00:00
|
|
|
'continuous_playback' => $this->continuousPlayback,
|
2024-01-23 22:50:50 +00:00
|
|
|
];
|
2021-06-04 16:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return array<mixed> */
|
|
|
|
public function jsonSerialize(): array
|
|
|
|
{
|
|
|
|
return $this->toArray();
|
|
|
|
}
|
|
|
|
}
|