summaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/Service
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-20 14:21:42 +0200
committerJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-21 19:00:28 +0200
commit3c75a9926716484020544046f03bfad1c6712cfe (patch)
tree7d92aff6a6b03aabb078ec30b4f9e37970c22c06 /apps/theming/lib/Service
parent48381b8913f90989582a60d90a1dbd0ce96608ae (diff)
downloadnextcloud-server-3c75a9926716484020544046f03bfad1c6712cfe.tar.gz
nextcloud-server-3c75a9926716484020544046f03bfad1c6712cfe.zip
Phpunit
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/theming/lib/Service')
-rw-r--r--apps/theming/lib/Service/ThemesService.php48
1 files changed, 31 insertions, 17 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))));
}
}