*/ private array $playlists = []; private bool $asked = false; private bool $loading = false; /** what the last import said (kept over the state's own line until something else happens) */ private string $message = ''; protected function loaded(array $params = []): void { $this->format->selected = ($this->wz->options['ytFormat'] ?? 'mp3') === 'mp4' ? 1 : 0; $this->playlists = []; $this->asked = false; $this->loading = false; $this->message = ''; $this->lists->items = []; $this->showState(); $this->setFocus($this->options->visible ? $this->options : ($this->connect->enabled ? $this->connect : $this->lists)); } /** No Client ID yet: the Options, where it is typed (this window reads it again when they close). */ public function onOptionsClick(Button $sender): void { $this->scenes()->push(OptionsForm::class); } protected function tick(float $dt): void { parent::tick($dt); $account = $this->wz->spotify(); if ($account !== null && $account->connected() && !$this->asked) { $this->load(); } $this->showState(); } private function load(): void { $account = $this->wz->spotify(); if ($account === null) { return; } $this->asked = true; $this->loading = true; $account->playlists()->then(function (mixed $lists): void { $this->loading = false; $this->playlists = is_array($lists) ? array_values($lists) : []; $this->lists->items = array_map(fn (array $p) => $p['name'] . ($p['count'] > 0 ? ' (' . $p['count'] . ')' : ''), $this->playlists); $this->lists->selected = $this->playlists !== [] ? 0 : -1; }); } private function showState(): void { $account = $this->wz->spotify(); $this->busy->visible = $this->loading; $this->busy->value = (int)(fmod(microtime(true), 1.0) * 100); $this->options->visible = $account === null; $this->connect->visible = $this->disconnect->visible = $this->reload->visible = $account !== null; if ($account === null) { $this->status->text = 'Il faut d\'abord votre Client ID Spotify : ouvrez les Options, puis « Obtenir mes identifiants… ».'; $this->connect->enabled = $this->import->enabled = $this->reload->enabled = $this->disconnect->enabled = false; $this->options->props['default'] = true; return; } $this->status->text = match ($account->state) { 'waiting' => 'Acceptez dans le navigateur (page de Spotify)…' . ($account->error !== '' ? ' ' . $account->error : ''), 'connected' => $this->message !== '' && !$this->loading ? $this->message : ($this->loading ? 'Lecture de vos playlists…' : 'Connecté à votre compte Spotify : choisissez une playlist.') . ($account->error !== '' ? ' (' . $account->error . ')' : ''), 'error' => 'Spotify refuse la connexion (' . $account->error . ') : vérifiez votre Client ID dans les Options, puis « Se connecter à Spotify ».', default => 'Reliez WinZik à votre compte Spotify : le navigateur s\'ouvre sur la page de Spotify.', }; $this->connect->enabled = !$account->connected() && $account->state !== 'waiting'; $this->disconnect->enabled = $account->connected(); $this->reload->enabled = $account->connected() && !$this->loading; $this->import->enabled = $account->connected() && $this->lists->selected >= 0 && $this->playlists !== [] && !$this->loading; // Entrée: connect first, then import the playlist chosen $this->options->props['default'] = false; $this->connect->props['default'] = !$account->connected(); $this->import->props['default'] = $account->connected(); } public function onConnectClick(Button $sender): void { $this->wz->spotify()?->login(); } public function onDisconnectClick(Button $sender): void { $this->wz->spotify()?->logout(); $this->playlists = []; $this->lists->items = []; $this->asked = false; $this->message = ''; } public function onReloadClick(Button $sender): void { $this->message = ''; $this->load(); } public function onListsActivate(ListBox $sender, int $index, string $item): void { $this->onImportClick($this->import); } public function onImportClick(Button $sender): void { $account = $this->wz->spotify(); $yt = $this->wz->youtube; $list = $this->playlists[$this->lists->selected] ?? null; if ($account === null || $list === null) { return; } if ($yt === null) { $this->message = "Les téléchargements marchent seulement dans WinZik installé sur l'ordinateur (Linux, Windows)."; return; } $format = (int)$this->format->selected === 1 ? 'mp4' : 'mp3'; $this->loading = true; $account->songs($list['id'])->then(function (mixed $songs) use ($yt, $format, $list): void { $this->loading = false; $songs = is_array($songs) ? $songs : []; // the songs already here (imported before, in the folders or the play list, waiting in the queue): not again $yt->forgetFiles(); $playlist = array_map(fn (string $f) => $this->wz->path($f), $this->wz->playlist->items); $added = $here = 0; foreach ($songs as $s) { if ($yt->alreadyHave($s['title'], $s['url'], $playlist) !== null) { $here++; continue; } $yt->download(['id' => $s['url'], 'title' => $s['title'], 'url' => $s['url']], $format, $s['title']); $added++; } $this->wz->saveQueue(); $this->wz->notice($added . ' morceau(x) de « ' . $list['name'] . ' » en téléchargement' . ($here > 0 ? ', ' . $here . ' déjà là' : ''), 6); $this->message = $songs === [] ? 'Cette playlist est vide, ou Spotify ne l\'a pas donnée : réessayez avec « Actualiser ».' : $added . ' morceau(x) ajouté(s) à la file' . ($here > 0 ? ' ; ' . $here . ' déjà là (pas de doublon)' : '') . '.'; }); } public function onDoneClick(Button $sender): void { $this->close(); } }