From b0f4500c3419b10317c5e976f6bd357efdf8b049 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 7 Feb 2021 23:12:05 +0000 Subject: [PATCH] Added env option for setting dark mode default Also allowed config-centralised default user settings for this change and bought existing user-level view options into that default settings system to be cleaner in code usage. For #2081 --- .env.example.complete | 8 ++++++-- app/Config/app.php | 7 ------- app/Config/setting-defaults.php | 7 +++++++ app/Http/Controllers/BookController.php | 2 +- app/Http/Controllers/BookshelfController.php | 4 ++-- app/Http/Controllers/HomeController.php | 2 +- app/Settings/SettingService.php | 12 ++++++++---- phpunit.xml | 1 + resources/sass/_html.scss | 1 - tests/Entity/BookShelfTest.php | 2 +- tests/User/UserPreferencesTest.php | 13 +++++++++++++ 11 files changed, 40 insertions(+), 19 deletions(-) diff --git a/.env.example.complete b/.env.example.complete index 9547b0fcd..9b8c8298a 100644 --- a/.env.example.complete +++ b/.env.example.complete @@ -246,11 +246,15 @@ AVATAR_URL= DRAWIO=true # Default item listing view -# Used for public visitors and user's without a preference -# Can be 'list' or 'grid' +# Used for public visitors and user's without a preference. +# Can be 'list' or 'grid'. APP_VIEWS_BOOKS=list APP_VIEWS_BOOKSHELVES=grid +# Use dark mode by default +# Will be overriden by any user/session preference. +APP_DEFAULT_DARK_MODE=false + # Page revision limit # Number of page revisions to keep in the system before deleting old revisions. # If set to 'false' a limit will not be enforced. diff --git a/app/Config/app.php b/app/Config/app.php index 762845e9f..c97e36e62 100755 --- a/app/Config/app.php +++ b/app/Config/app.php @@ -19,13 +19,6 @@ return [ // private configuration variables so should remain disabled in public. 'debug' => env('APP_DEBUG', false), - // Set the default view type for various lists. Can be overridden by user preferences. - // These will be used for public viewers and users that have not set a preference. - 'views' => [ - 'books' => env('APP_VIEWS_BOOKS', 'list'), - 'bookshelves' => env('APP_VIEWS_BOOKSHELVES', 'grid'), - ], - // The number of revisions to keep in the database. // Once this limit is reached older revisions will be deleted. // If set to false then a limit will not be enforced. diff --git a/app/Config/setting-defaults.php b/app/Config/setting-defaults.php index d84c0c264..08646091a 100644 --- a/app/Config/setting-defaults.php +++ b/app/Config/setting-defaults.php @@ -24,4 +24,11 @@ return [ 'app-custom-head' => false, 'registration-enabled' => false, + // User-level default settings + 'user' => [ + 'dark-mode-enabled' => env('APP_DEFAULT_DARK_MODE', false), + 'bookshelves_view_type' => env('APP_VIEWS_BOOKSHELVES', 'grid'), + 'books_view_type' => env('APP_VIEWS_BOOKS', 'grid'), + ], + ]; diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 3d695ba85..59c205d0a 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -30,7 +30,7 @@ class BookController extends Controller */ public function index() { - $view = setting()->getForCurrentUser('books_view_type', config('app.views.books')); + $view = setting()->getForCurrentUser('books_view_type'); $sort = setting()->getForCurrentUser('books_sort', 'name'); $order = setting()->getForCurrentUser('books_sort_order', 'asc'); diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index 32c22e185..8574c1b48 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -32,7 +32,7 @@ class BookshelfController extends Controller */ public function index() { - $view = setting()->getForCurrentUser('bookshelves_view_type', config('app.views.bookshelves', 'grid')); + $view = setting()->getForCurrentUser('bookshelves_view_type'); $sort = setting()->getForCurrentUser('bookshelves_sort', 'name'); $order = setting()->getForCurrentUser('bookshelves_sort_order', 'asc'); $sortOptions = [ @@ -103,7 +103,7 @@ class BookshelfController extends Controller Views::add($shelf); $this->entityContextManager->setShelfContext($shelf->id); - $view = setting()->getForCurrentUser('bookshelf_view_type', config('app.views.books')); + $view = setting()->getForCurrentUser('bookshelf_view_type'); $this->setPageTitle($shelf->getShortName()); return view('shelves.show', [ diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 3258f4369..31736e1b0 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -56,7 +56,7 @@ class HomeController extends Controller // Add required list ordering & sorting for books & shelves views. if ($homepageOption === 'bookshelves' || $homepageOption === 'books') { $key = $homepageOption; - $view = setting()->getForCurrentUser($key . '_view_type', config('app.views.' . $key)); + $view = setting()->getForCurrentUser($key . '_view_type'); $sort = setting()->getForCurrentUser($key . '_sort', 'name'); $order = setting()->getForCurrentUser($key . '_sort_order', 'asc'); diff --git a/app/Settings/SettingService.php b/app/Settings/SettingService.php index 042ae7aa4..feb54c30a 100644 --- a/app/Settings/SettingService.php +++ b/app/Settings/SettingService.php @@ -29,9 +29,9 @@ class SettingService * Gets a setting from the database, * If not found, Returns default, Which is false by default. */ - public function get(string $key, $default = false) + public function get(string $key, $default = null) { - if ($default === false) { + if (is_null($default)) { $default = config('setting-defaults.' . $key, false); } @@ -57,8 +57,12 @@ class SettingService /** * Get a user-specific setting from the database or cache. */ - public function getUser(User $user, string $key, $default = false) + public function getUser(User $user, string $key, $default = null) { + if (is_null($default)) { + $default = config('setting-defaults.user.' . $key, false); + } + if ($user->isDefault()) { return $this->getFromSession($key, $default); } @@ -68,7 +72,7 @@ class SettingService /** * Get a value for the current logged-in user. */ - public function getForCurrentUser(string $key, $default = false) + public function getForCurrentUser(string $key, $default = null) { return $this->getUser(user(), $key, $default); } diff --git a/phpunit.xml b/phpunit.xml index fc1a591a9..dd3e53c08 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -57,5 +57,6 @@ + diff --git a/resources/sass/_html.scss b/resources/sass/_html.scss index 026295669..1d5defa97 100644 --- a/resources/sass/_html.scss +++ b/resources/sass/_html.scss @@ -25,7 +25,6 @@ body { line-height: 1.6; @include lightDark(color, #444, #AAA); -webkit-font-smoothing: antialiased; - background-color: #F2F2F2; height: 100%; display: flex; flex-direction: column; diff --git a/tests/Entity/BookShelfTest.php b/tests/Entity/BookShelfTest.php index 9b3290370..29fac5ec5 100644 --- a/tests/Entity/BookShelfTest.php +++ b/tests/Entity/BookShelfTest.php @@ -59,7 +59,7 @@ class BookShelfTest extends TestCase public function test_book_not_visible_in_shelf_list_view_if_user_cant_view_shelf() { config()->set([ - 'app.views.bookshelves' => 'list', + 'setting-defaults.user.bookshelves_view_type' => 'list', ]); $shelf = Bookshelf::query()->first(); $book = $shelf->books()->first(); diff --git a/tests/User/UserPreferencesTest.php b/tests/User/UserPreferencesTest.php index 7ffc8f9db..49c49188b 100644 --- a/tests/User/UserPreferencesTest.php +++ b/tests/User/UserPreferencesTest.php @@ -92,4 +92,17 @@ class UserPreferencesTest extends TestCase $home->assertDontSee('Dark Mode'); $home->assertSee('Light Mode'); } + + public function test_dark_mode_defaults_to_config_option() + { + config()->set('setting-defaults.user.dark-mode-enabled', false); + $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled')); + $home = $this->get('/login'); + $home->assertElementNotExists('.dark-mode'); + + config()->set('setting-defaults.user.dark-mode-enabled', true); + $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled')); + $home = $this->get('/login'); + $home->assertElementExists('.dark-mode'); + } } \ No newline at end of file