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
This commit is contained in:
Dan Brown 2021-02-07 23:12:05 +00:00
parent af032f8993
commit b0f4500c34
11 changed files with 40 additions and 19 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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'),
],
];

View File

@ -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');

View File

@ -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', [

View File

@ -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');

View File

@ -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);
}

View File

@ -57,5 +57,6 @@
<server name="LOG_FAILED_LOGIN_MESSAGE" value=""/>
<server name="LOG_FAILED_LOGIN_CHANNEL" value="testing"/>
<server name="WKHTMLTOPDF" value="false"/>
<server name="APP_DEFAULT_DARK_MODE" value="false"/>
</php>
</phpunit>

View File

@ -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;

View File

@ -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();

View File

@ -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');
}
}