From 9d8d480ec32091e09803b502eb148807fedd32c9 Mon Sep 17 00:00:00 2001 From: Shane Smith Date: Mon, 30 Nov 2026 12:21:59 +1030 Subject: [PATCH] Let people choose a light or dark theme, or follow the system Account has a new Theme section between Color and Timezone, with System, Light and Dark. System is the default, so nothing changes for anyone who never touches it. Choosing Light or Dark switches the whole app straight away, bars, sheets and keyboard included, and the app stays in that theme after it restarts. Choosing System makes it follow the phone again. It uses System::setAppearance() from NativePHP mobile 4.9.0. The choice belongs to the phone, not the account. It doesn't sync to the web, and signing in to another account keeps it. --- app/Devnull/Account.php | 22 +++++ app/Devnull/Theme.php | 24 +++++ app/NativeComponents/ThisDevice.php | 27 ++++++ resources/views/native/this-device.blade.php | 16 ++++ tests/Feature/Screens/ThisDeviceThemeTest.php | 87 +++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 app/Devnull/Theme.php create mode 100644 tests/Feature/Screens/ThisDeviceThemeTest.php diff --git a/app/Devnull/Account.php b/app/Devnull/Account.php index 34a2ee73..8ee3da1c 100644 --- a/app/Devnull/Account.php +++ b/app/Devnull/Account.php @@ -27,6 +27,12 @@ final class Account private const string PinWarningKey = 'devnull.pin-warning-dismissed'; + /** + * This theme is tied to the phone, so changing it doesn't sync to the web. + * Logging into another devnull account on the phone also doesn't change it. + */ + private const string ThemeKey = 'devnull.theme'; + /** * What's kept while devnull has said you have Remotes off, as against not having said yet. */ @@ -217,6 +223,22 @@ public function rememberSkipsPinWarning(): void Cache::forever(self::PinWarningKey, true); } + /** + * The theme you chose for this device. By default it is 'System' so it follows the phone's theme. + * If you manually selected something else ('Light' or 'Dark'), it will use that theme instead. + */ + public function theme(): Theme + { + $kept = Cache::get(self::ThemeKey); + + return (is_string($kept) ? Theme::tryFrom($kept) : null) ?? Theme::System; + } + + public function rememberTheme(Theme $theme): void + { + Cache::forever(self::ThemeKey, $theme->value); + } + /** * The timezone you chose on devnull, as it last said, or null while you haven't chosen one. */ diff --git a/app/Devnull/Theme.php b/app/Devnull/Theme.php new file mode 100644 index 00000000..58349891 --- /dev/null +++ b/app/Devnull/Theme.php @@ -0,0 +1,24 @@ + 'System', + self::Light => 'Light', + self::Dark => 'Dark', + }; + } +} diff --git a/app/NativeComponents/ThisDevice.php b/app/NativeComponents/ThisDevice.php index dd17af64..cf8ebfca 100644 --- a/app/NativeComponents/ThisDevice.php +++ b/app/NativeComponents/ThisDevice.php @@ -14,6 +14,7 @@ use App\Devnull\Lane; use App\Devnull\PushPrompt; use App\Devnull\ResponseCache; +use App\Devnull\Theme; use App\Devnull\Timezone; use App\Devnull\WhoToFollow; use App\NativeComponents\Concerns\LeavesDevnull; @@ -129,6 +130,12 @@ class ThisDevice extends NativeComponent public string $timezoneQuery = ''; + /** + * Which theme option is selected, as its position in Theme::cases(). + * 0 is System, 1 is Light and 2 is Dark. + */ + public int $themeIndex = 0; + /** * The timezone devnull has for you, and whether your profile shows your local time, as `me/settings` last said. * Until it has, the timezone is the one this phone remembers. @@ -219,6 +226,7 @@ public function mount(): void } $this->savedTimezone = $this->chosenTimezone = app(Account::class)->timezone(); + $this->themeIndex = array_search(app(Account::class)->theme(), Theme::cases(), true); $this->pushPermission = PushNotifications::checkPermission(); $this->pushOn = PushPrompt::allowed($this->pushPermission) && PushNotifications::getToken() !== null; @@ -387,6 +395,25 @@ public function setShowsWhoToFollow(bool $shows): void app(WhoToFollow::class)->show($this->me()?->id, $shows); } + /** + * Switches the whole app to the theme at this position in Theme::cases(). Light or Dark locks + * the app to that theme. System makes it follow the phone's theme again. The phone keeps the + * choice after the app restarts. We also remember it, so the Theme section shows it next time. + * Any other position is ignored. + */ + public function chooseTheme(int $index): void + { + $theme = Theme::cases()[$index] ?? null; + + if ($theme === null) { + return; + } + + $this->themeIndex = $index; + app(Account::class)->rememberTheme($theme); + System::setAppearance($theme->value); + } + /** * Opens the sheet that lists every timezone, as the web's picker opens its list. */ diff --git a/resources/views/native/this-device.blade.php b/resources/views/native/this-device.blade.php index f71a111b..219a66f9 100644 --- a/resources/views/native/this-device.blade.php +++ b/resources/views/native/this-device.blade.php @@ -1,4 +1,5 @@ @use('App\Devnull\Lane') +@use('App\Devnull\Theme') @use('App\Icons\Ios') @use('App\Icons\Lucide') @@ -112,6 +113,21 @@ + {{-- + The Theme setting. System is first and the default. + A choice changes the app straight away and stays on this phone. + --}} + + @include('native.partials.settings-heading', [ + 'title' => 'Theme', + 'description' => 'System follows your operating system, choosing another option locks the app to that theme.', + ]) + + + + + + {{-- The web's Timezone section: the timezone picked, or null while it's unset and the phone's is used, the web's "Show my local time on my profile" once there is one, then Choose timezone, which opens diff --git a/tests/Feature/Screens/ThisDeviceThemeTest.php b/tests/Feature/Screens/ThisDeviceThemeTest.php new file mode 100644 index 00000000..3811da75 --- /dev/null +++ b/tests/Feature/Screens/ThisDeviceThemeTest.php @@ -0,0 +1,87 @@ + 'http://devnull.test']); + serveApi(['GET /api/v1/me' => [currentUserJson()]]); +}); + +it('shows Theme after Color with System picked until you choose', function () { + signIn(); + + $screen = Native::visit('/profile/device') + ->assertSee('Theme') + ->assertSee('System follows your operating system, choosing another option locks the app to that theme.') + ->assertNativeNotCalled('System.SetAppearance'); + + $sections = findNodes($screen->tree(), fn (array $node): bool => in_array($node['ref'] ?? null, ['color', 'theme', 'timezone'], true)); + + expect(array_column($sections, 'ref'))->toBe(['color', 'theme', 'timezone']) + ->and(nodeByRef($screen, 'theme-choice')['props']['options'] ?? null)->toBe(['System', 'Light', 'Dark']) + ->and(nodeByRef($screen, 'theme-choice')['props']['value'] ?? null)->toBe(0); +}); + +it('pins the app to the theme you choose', function (int $index, string $mode) { + signIn(); + + Native::visit('/profile/device') + ->changeTab('theme-choice', $index) + ->assertNativeCalled('System.SetAppearance', fn (array $params): bool => $params['mode'] === $mode); + + expect(app(Account::class)->theme()->value)->toBe($mode) + ->and(nodeByRef(Native::visit('/profile/device'), 'theme-choice')['props']['value'] ?? null)->toBe($index); +})->with([ + 'light' => [1, 'light'], + 'dark' => [2, 'dark'], +]); + +it('hands the app back to the phone with System', function () { + signIn(); + app(Account::class)->rememberTheme(Theme::Dark); + + $screen = Native::visit('/profile/device'); + + expect(nodeByRef($screen, 'theme-choice')['props']['value'] ?? null)->toBe(2); + + $screen->changeTab('theme-choice', 0) + ->assertNativeCalled('System.SetAppearance', fn (array $params): bool => $params['mode'] === 'system'); + + expect(app(Account::class)->theme())->toBe(Theme::System); +}); + +it('ignores a choice that is not one of the three', function () { + signIn(); + + Native::visit('/profile/device') + ->changeTab('theme-choice', 5) + ->assertNativeNotCalled('System.SetAppearance'); + + expect(app(Account::class)->theme())->toBe(Theme::System); +}); + +it('remembers the theme you chose, and is on System until you choose', function () { + $account = app(Account::class); + + expect($account->theme())->toBe(Theme::System); + + $account->rememberTheme(Theme::Dark); + + expect($account->theme())->toBe(Theme::Dark); + + Cache::forever('devnull.theme', 'sepia'); + + expect($account->theme())->toBe(Theme::System); +}); + +it('keeps your theme when you sign out, as the phone keeps the app in it', function () { + $account = app(Account::class); + $account->rememberTheme(Theme::Light); + + $account->forget(); + + expect($account->theme())->toBe(Theme::Light); +});