Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-20 15:21:42 +0300
committerJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-21 20:00:28 +0300
commit3c75a9926716484020544046f03bfad1c6712cfe (patch)
tree7d92aff6a6b03aabb078ec30b4f9e37970c22c06 /apps/theming/lib
parent48381b8913f90989582a60d90a1dbd0ce96608ae (diff)
Phpunit
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/theming/lib')
-rw-r--r--apps/theming/lib/Service/ThemesService.php48
-rw-r--r--apps/theming/lib/Util.php2
2 files changed, 32 insertions, 18 deletions
diff --git a/apps/theming/lib/Service/ThemesService.php b/apps/theming/lib/Service/ThemesService.php
index 8b39da6bb5d..d8101c5b48a 100644
--- a/apps/theming/lib/Service/ThemesService.php
+++ b/apps/theming/lib/Service/ThemesService.php
@@ -44,8 +44,8 @@ class ThemesService {
IConfig $config,
DefaultTheme $defaultTheme,
DarkTheme $darkTheme,
- DarkHighContrastTheme $darkHighContrastTheme,
HighContrastTheme $highContrastTheme,
+ DarkHighContrastTheme $darkHighContrastTheme,
DyslexiaFont $dyslexiaFont) {
$this->userSession = $userSession;
$this->config = $config;
@@ -73,44 +73,54 @@ class ThemesService {
* Enable a theme for the logged-in user
*
* @param ITheme $theme the theme to enable
+ * @return string[] the enabled themes
*/
- public function enableTheme(ITheme $theme): void {
+ public function enableTheme(ITheme $theme): array {
$themesIds = $this->getEnabledThemes();
+ // If already enabled, ignore
+ if (in_array($theme->getId(), $themesIds)) {
+ return $themesIds;
+ }
+
/** @var ITheme[] */
$themes = array_map(function($themeId) {
return $this->getThemes()[$themeId];
}, $themesIds);
// Filtering all themes with the same type
- $filteredThemes = array_filter($themes, function($t) use ($theme) {
+ $filteredThemes = array_filter($themes, function(ITheme $t) use ($theme) {
return $theme->getType() === $t->getType();
});
- // Disable all the other themes of the same type
- // as there can only be one enabled at the same time
- foreach ($filteredThemes as $t) {
- $this->disableTheme($t);
- }
+ // Retrieve IDs only
+ $filteredThemesIds = array_map(function(ITheme $t) {
+ return $t->getId();
+ }, $filteredThemes);
- $this->setEnabledThemes([...$this->getEnabledThemes(), $theme->getId()]);
+ $enabledThemes = [...array_diff($themesIds, $filteredThemesIds), $theme->getId()];
+ $this->setEnabledThemes($enabledThemes);
+
+ return $enabledThemes;
}
/**
* Disable a theme for the logged-in user
*
* @param ITheme $theme the theme to disable
+ * @return string[] the enabled themes
*/
- public function disableTheme(ITheme $theme): void {
- // Using keys as it's faster
- $themes = $this->getEnabledThemes();
+ public function disableTheme(ITheme $theme): array {
+ $themesIds = $this->getEnabledThemes();
// If enabled, removing it
- if (in_array($theme->getId(), $themes)) {
- $this->setEnabledThemes(array_filter($themes, function($themeId) use ($theme) {
- return $themeId !== $theme->getId();
- }));
+ if (in_array($theme->getId(), $themesIds)) {
+ $enabledThemes = array_diff($themesIds, [$theme->getId()]);
+ $this->setEnabledThemes($enabledThemes);
+ return $enabledThemes;
}
+
+ return $themesIds;
}
/**
@@ -136,6 +146,10 @@ class ThemesService {
*/
public function getEnabledThemes(): array {
$user = $this->userSession->getUser();
+ if ($user === null) {
+ return [];
+ }
+
try {
return json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]'));
} catch (\Exception $e) {
@@ -151,6 +165,6 @@ class ThemesService {
*/
private function setEnabledThemes(array $themes): void {
$user = $this->userSession->getUser();
- $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($themes)));
+ $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique(array_values($themes))));
}
}
diff --git a/apps/theming/lib/Util.php b/apps/theming/lib/Util.php
index beaca679149..35c7703bd45 100644
--- a/apps/theming/lib/Util.php
+++ b/apps/theming/lib/Util.php
@@ -129,7 +129,7 @@ class Util {
public function calculateLuminance(string $color): float {
[$red, $green, $blue] = $this->hexToRGB($color);
$hsl = $this->toHSL($red, $green, $blue);
- return $hsl[2] / 100;
+ return $hsl[2];
}
/**