<?phpnamespace App\Form;use JumPHP\Gui\Button;use JumPHP\Gui\CheckBox;use JumPHP\Gui\Label;use JumPHP\Gui\ListBox;use JumPHP\Gui\TextBox;/** * The WinZik account (App\Cloud\Cloud): a pseudo and a password, no e-mail. Connexion, Créer un compte (the recovery * code shown once: "notez ce code"), Mot de passe oublié (the recovery code), Mes appareils (online or not, sign one * out), Déconnexion. Params: tab ('login' | 'signup' | 'devices'). * * @property Label $heading * @property Label $state * @property Button $tabLogin * @property Button $tabSignup * @property Button $tabDevices * @property \JumPHP\Gui\Container $enterPanel * @property Label $intro * @property TextBox $pseudo * @property \JumPHP\Gui\Container $recoveryRow * @property TextBox $recovery * @property Label $passwordLabel * @property TextBox $password * @property TextBox $deviceName * @property Button $enter * @property Button $forgot * @property \JumPHP\Gui\Container $codePanel * @property TextBox $code * @property Button $noted * @property \JumPHP\Gui\Container $devicesPanel * @property Label $account * @property ListBox $devices * @property Button $revoke * @property Button $refresh * @property Button $signOut * @property CheckBox $playSync * @property Label $playSyncInfo * @property Label $syncState * @property CheckBox $autoCopy * @property Label $status * @property Button $quit */final class CloudForm extends WinZikDialog{ /** login | signup | recover | devices | code */ public string $tab = 'login'; /** @var list<array<string, mixed>> the devices listed */ public array $deviceRows = []; private int $busy = 0; protected function loaded(array $params = []): void { $cloud = $this->wz->cloud(); $this->password->text = ''; $this->recovery->text = ''; $this->code->text = ''; $this->status->text = ''; if ($cloud === null || $cloud->state() === 'off') { $this->show('login'); $this->status->text = 'Pas de cloud ici : pas de réseau. Vérifiez la connexion, puis rouvrez cette fenêtre.'; foreach ([$this->pseudo, $this->password, $this->deviceName, $this->enter, $this->forgot, $this->tabSignup, $this->tabDevices] as $w) { $w->enabled = false; } return; } $this->pseudo->text = $cloud->login(); $this->deviceName->text = $cloud->deviceName(); $this->autoCopy->checked = $cloud->autoCopy; $this->playSync->checked = $cloud->playSynced(); $this->playSync->props['visible'] = $this->playSyncInfo->props['visible'] = $cloud->playSync !== null; $tab = (string)($params['tab'] ?? ''); $this->show($cloud->signedIn() ? ($tab === 'signup' || $tab === 'login' ? 'devices' : ($tab ?: 'devices')) : ($tab === 'signup' ? 'signup' : 'login')); } /** One of the tabs (the buttons of the top), what it shows. */ public function show(string $tab): void { $this->tab = $tab; $signed = $this->wz->cloud()?->signedIn() ?? false; $enter = in_array($tab, ['login', 'signup', 'recover'], true); $this->enterPanel->props['visible'] = $enter; $this->codePanel->props['visible'] = $tab === 'code'; $this->devicesPanel->props['visible'] = $tab === 'devices'; $this->recoveryRow->props['visible'] = $tab === 'recover'; $this->tabDevices->enabled = $signed; $this->tabLogin->enabled = $this->tabSignup->enabled = !$signed && $tab !== 'code'; foreach (['login' => $this->tabLogin, 'signup' => $this->tabSignup, 'devices' => $this->tabDevices] as $name => $button) { $button->props['style'] = $name === $tab || ($name === 'login' && $tab === 'recover') ? 'primary' : ''; } $this->passwordLabel->text = $tab === 'recover' ? 'Nouveau mot de passe :' : 'Mot de passe :'; $this->enter->text = match ($tab) { 'signup' => 'Créer le compte', 'recover' => 'Changer le mot de passe', default => 'Se connecter', }; $this->forgot->props['visible'] = $tab === 'login'; $this->intro->text = match ($tab) { 'signup' => "Un pseudo et un mot de passe, rien d'autre : pas d'e-mail, aucune donnée personnelle. Un code de secours vous sera donné une seule fois.", 'recover' => 'Sans le mot de passe : le code de secours donné à la création du compte. Tous vos appareils seront déconnectés.', default => 'Le même compte sur le PC et sur le téléphone : la playlist cloud, les paroles, les MP3 envoyés du PC.', }; if ($tab === 'devices') { $this->fillDevices(); $this->refreshDevices(); } $this->updateState(); if ($enter) { $this->setFocus($tab === 'recover' ? $this->recovery : ($this->pseudo->text === '' ? $this->pseudo : $this->password)); } } private function updateState(): void { $cloud = $this->wz->cloud(); $this->state->text = match ($cloud?->state()) { 'online' => 'En ligne : ' . $cloud->login(), 'connecting' => 'Connexion de ' . $cloud->login() . '…', 'offline' => 'Hors ligne (' . $cloud->login() . ')', 'signedOut' => 'Non connecté', default => '', }; } public function onTabLoginClick(Button $sender): void { $this->show('login'); } public function onTabSignupClick(Button $sender): void { $this->show('signup'); } public function onTabDevicesClick(Button $sender): void { $this->show('devices'); } public function onForgotClick(Button $sender): void { $this->show('recover'); } public function onEnterClick(Button $sender): void { $cloud = $this->wz->cloud(); if ($cloud === null || $this->busy > 0) { return; } $login = trim($this->pseudo->text); $password = $this->password->text; if ($login === '' || $password === '') { $this->status->text = $login === '' ? 'Tapez votre pseudo.' : 'Tapez le mot de passe.'; $this->setFocus($login === '' ? $this->pseudo : $this->password); return; } if ($this->tab !== 'login' && mb_strlen($password) < 8) { $this->status->text = 'Mot de passe trop court : 8 caractères au moins.'; return; } $name = trim($this->deviceName->text); $this->status->text = match ($this->tab) { 'signup' => 'Création du compte…', 'recover' => 'Changement du mot de passe…', default => 'Connexion…', }; $this->busy++; $task = match ($this->tab) { 'signup' => $cloud->signUp($login, $password, $name), 'recover' => $cloud->recover($login, trim($this->recovery->text), $password, $name), default => $cloud->signIn($login, $password, $name), }; $task->then(function (array $data): void { $this->busy--; $this->password->text = ''; $this->recovery->text = ''; $code = (string)($data['recovery'] ?? ''); if ($code !== '') { $this->code->text = $code; // shown once, never kept $this->show('code'); $this->status->text = 'Compte « ' . $data['login'] . ' » prêt.'; return; } $this->status->text = 'Connecté : ' . $data['login'] . '. La playlist cloud est dans le menu (clic droit).'; $this->show('devices'); }, function (\Throwable $e): void { $this->busy--; $this->status->text = $e->getMessage(); }); } public function onNotedClick(Button $sender): void { $this->code->text = ''; $this->show('devices'); $this->status->text = 'La playlist cloud est dans le menu de la playlist (clic droit).'; } private function refreshDevices(): void { $this->wz->cloud()?->refreshDevices()->then(fn () => $this->fillDevices(), fn (\Throwable $e) => $this->status->text = $e->getMessage()); } private function fillDevices(): void { $cloud = $this->wz->cloud(); if ($cloud === null) { return; } $this->account->text = 'Compte : ' . $cloud->login(); $this->deviceRows = $cloud->devices(); $this->devices->items = array_map(fn (array $d) => (!empty($d['online']) ? '● ' : '○ ') . $d['name'] . ' (' . match ($d['kind'] ?? '') { 'phone' => 'téléphone', 'web' => 'navigateur', default => 'ordinateur' } . ')' . (!empty($d['current']) ? ' - cet appareil' : (!empty($d['online']) ? ' - en ligne' : ' - hors ligne')), $this->deviceRows); if ($this->devices->selected >= count($this->deviceRows)) { $this->devices->selected = -1; } } public function onRefreshClick(Button $sender): void { $this->refreshDevices(); } public function onRevokeClick(Button $sender): void { $d = $this->deviceRows[(int)$this->devices->selected] ?? null; $cloud = $this->wz->cloud(); if ($d === null || $cloud === null) { $this->status->text = 'Choisissez d\'abord un appareil dans la liste.'; return; } if (!empty($d['current'])) { $this->onSignOutClick($this->signOut); return; } $cloud->revoke((string)$d['id'])->then(function () use ($d): void { $this->status->text = '« ' . $d['name'] . ' » est déconnecté du compte.'; $this->fillDevices(); }, fn (\Throwable $e) => $this->status->text = $e->getMessage()); } public function onSignOutClick(Button $sender): void { $cloud = $this->wz->cloud(); if ($cloud === null) { return; } $cloud->signOut(); $this->status->text = 'Cet appareil est déconnecté du compte.'; $this->show('login'); } /** « Lecture synchronisée avec mes appareils »: Synchronisé / Indépendant, kept on this device */ public function onPlaySyncChange(CheckBox $sender, bool $checked): void { $this->wz->cloud()?->setPlaySync($checked); $this->syncState->text = $this->wz->cloud()?->syncText() ?? ''; } public function onAutoCopyChange(CheckBox $sender, bool $checked): void { $cloud = $this->wz->cloud(); if ($cloud !== null) { $cloud->autoCopy = $checked; $cloud->saveSettings(); } } protected function tick(float $dt): void { parent::tick($dt); $this->updateState(); $this->syncState->text = $this->wz->cloud()?->syncText() ?? ''; $this->enter->enabled = $this->busy === 0 && $this->wz->cloud()?->state() !== 'off'; } public function onQuitClick(Button $sender): void { $cloud = $this->wz->cloud(); if ($cloud !== null && $cloud->signedIn() && trim($this->deviceName->text) !== '' && trim($this->deviceName->text) !== $cloud->deviceName()) { $cloud->setDeviceName($this->deviceName->text); } $this->close(); }}