aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/lib
diff options
context:
space:
mode:
authorJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-15 13:54:53 +0200
committerJohn Molakvoæ <skjnldsv@protonmail.com>2022-04-21 09:31:07 +0200
commit69d1d1a84e5e8937046d30714f11036b680cc04a (patch)
tree76666f2b2aef59e110d963fdeae04107e6ead828 /apps/theming/lib
parentfa18a77fa24ad01944d79232fb637076282dedc2 (diff)
downloadnextcloud-server-69d1d1a84e5e8937046d30714f11036b680cc04a.tar.gz
nextcloud-server-69d1d1a84e5e8937046d30714f11036b680cc04a.zip
Write body theme selector straight in the template
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Diffstat (limited to 'apps/theming/lib')
-rw-r--r--apps/theming/lib/Service/JSDataService.php16
-rw-r--r--apps/theming/lib/Service/ThemeInjectionService.php1
-rw-r--r--apps/theming/lib/Service/ThemesService.php55
3 files changed, 61 insertions, 11 deletions
diff --git a/apps/theming/lib/Service/JSDataService.php b/apps/theming/lib/Service/JSDataService.php
index 1c4d138a764..fdc85ea445a 100644
--- a/apps/theming/lib/Service/JSDataService.php
+++ b/apps/theming/lib/Service/JSDataService.php
@@ -32,22 +32,21 @@ use OCA\Theming\Util;
use OCP\IConfig;
class JSDataService implements \JsonSerializable {
-
- /** @var ThemingDefaults */
- private $themingDefaults;
- /** @var Util */
- private $util;
- /** @var IConfig */
- private $appConfig;
+ private ThemingDefaults $themingDefaults;
+ private Util $util;
+ private IConfig $appConfig;
+ private ThemesService $themesService;
public function __construct(
ThemingDefaults $themingDefaults,
Util $util,
- IConfig $appConfig
+ IConfig $appConfig,
+ ThemesService $themesService
) {
$this->themingDefaults = $themingDefaults;
$this->util = $util;
$this->appConfig = $appConfig;
+ $this->themesService = $themesService;
}
public function jsonSerialize(): array {
@@ -60,6 +59,7 @@ class JSDataService implements \JsonSerializable {
'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
+ 'enabledThemes' => $this->themesService->getEnabledThemes(),
];
}
}
diff --git a/apps/theming/lib/Service/ThemeInjectionService.php b/apps/theming/lib/Service/ThemeInjectionService.php
index 0b4890cd08b..e00839c21c5 100644
--- a/apps/theming/lib/Service/ThemeInjectionService.php
+++ b/apps/theming/lib/Service/ThemeInjectionService.php
@@ -22,6 +22,7 @@
*/
namespace OCA\Theming\Service;
+use OCA\Theming\AppInfo\Application;
use OCA\Theming\Themes\DefaultTheme;
use OCP\IURLGenerator;
use OCP\Util;
diff --git a/apps/theming/lib/Service/ThemesService.php b/apps/theming/lib/Service/ThemesService.php
index 3092b3bcbb5..832c443a2e1 100644
--- a/apps/theming/lib/Service/ThemesService.php
+++ b/apps/theming/lib/Service/ThemesService.php
@@ -22,21 +22,33 @@
*/
namespace OCA\Theming\Service;
+use OCA\Theming\AppInfo\Application;
use OCA\Theming\Themes\DefaultTheme;
use OCA\Theming\Themes\DarkTheme;
use OCA\Theming\Themes\DarkHighContrastTheme;
use OCA\Theming\Themes\HighContrastTheme;
use OCA\Theming\ITheme;
+use OCP\IAppConfig;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserSession;
class ThemesService {
+ private IUserSession $session;
+ private IConfig $config;
/** @var ITheme[] */
private array $themesProviders;
- public function __construct(DefaultTheme $defaultTheme,
+ public function __construct(IUserSession $userSession,
+ IConfig $config,
+ DefaultTheme $defaultTheme,
DarkTheme $darkTheme,
DarkHighContrastTheme $darkHighContrastTheme,
HighContrastTheme $highContrastTheme) {
+ $this->userSession = $userSession;
+ $this->config = $config;
+
// Register themes
$this->themesProviders = [
$defaultTheme->getId() => $defaultTheme,
@@ -46,11 +58,48 @@ class ThemesService {
];
}
- public function getThemes() {
+ public function getThemes(): array {
return $this->themesProviders;
}
- public function getThemeVariables(string $id) {
+ public function getThemeVariables(string $id): array {
return $this->themesProviders[$id]->getCSSVariables();
}
+
+ public function enableTheme(ITheme $theme): void {
+ $themes = $this->getEnabledThemes();
+ array_push($themes, $theme->getId());
+ $this->setEnabledThemes($themes);
+ }
+
+ public function disableTheme(ITheme $theme): void {
+ // Using keys as it's faster
+ $themes = $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();
+ }));
+ }
+ }
+
+ public function isEnabled(ITheme $theme): bool {
+ $user = $this->userSession->getUser();
+ if ($user instanceof IUser) {
+ // Using keys as it's faster
+ $themes = $this->getEnabledThemes();
+ return in_array($theme->getId(), $themes);
+ }
+ }
+
+ public function getEnabledThemes(): array {
+ $user = $this->userSession->getUser();
+ $enabledThemes = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]');
+ return json_decode($enabledThemes);
+ }
+
+ 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)));
+ }
}