Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/Devnull/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down
24 changes: 24 additions & 0 deletions app/Devnull/Theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Devnull;

/**
* The theme options for the app. Each value is a mode that System::setAppearance() accepts.
* System follows the phone's theme and is the default.
* The cases are in the order the Account screen shows them.
*/
enum Theme: string
{
case System = 'system';
case Light = 'light';
case Dark = 'dark';

public function label(): string
{
return match ($this) {
self::System => 'System',
self::Light => 'Light',
self::Dark => 'Dark',
};
}
}
27 changes: 27 additions & 0 deletions app/NativeComponents/ThisDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
16 changes: 16 additions & 0 deletions resources/views/native/this-device.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@use('App\Devnull\Lane')
@use('App\Devnull\Theme')
@use('App\Icons\Ios')
@use('App\Icons\Lucide')

Expand Down Expand Up @@ -112,6 +113,21 @@
</native:column>
</native:column>

{{--
The Theme setting. System is first and the default.
A choice changes the app straight away and stays on this phone.
--}}
<native:column ref="theme" class="w-full pt-10">
@include('native.partials.settings-heading', [
'title' => 'Theme',
'description' => 'System follows your operating system, choosing another option locks the app to that theme.',
])

<native:column class="w-full px-4 pt-4">
<native:button-group ref="theme-choice" :options="array_map(fn (Theme $theme): string => $theme->label(), Theme::cases())" :value="$themeIndex" @change="chooseTheme" a11y-label="Theme" class="w-full" />
</native:column>
</native:column>

{{--
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
Expand Down
87 changes: 87 additions & 0 deletions tests/Feature/Screens/ThisDeviceThemeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use App\Devnull\Account;
use App\Devnull\Theme;
use Illuminate\Support\Facades\Cache;
use Native\Mobile\Testing\Native;

beforeEach(function () {
config(['devnull.url' => '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);
});