From b49f814faa08be51d4008f4b367a797cf95002a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Julius=20H=C3=A4rtl?= Date: Fri, 14 Aug 2020 16:22:22 +0200 Subject: [PATCH] Implement background reset and proper shipped setting MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../lib/Controller/DashboardController.php | 19 ++++-- .../lib/Service/BackgroundService.php | 29 +++++++-- apps/dashboard/src/App.vue | 19 ++++-- .../src/components/BackgroundSettings.vue | 64 ++++++++++++++----- 4 files changed, 97 insertions(+), 34 deletions(-) diff --git a/apps/dashboard/lib/Controller/DashboardController.php b/apps/dashboard/lib/Controller/DashboardController.php index f6fea2aaf45..1c11ca24518 100644 --- a/apps/dashboard/lib/Controller/DashboardController.php +++ b/apps/dashboard/lib/Controller/DashboardController.php @@ -33,11 +33,13 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\Dashboard\IManager; use OCP\Dashboard\IWidget; use OCP\Dashboard\RegisterWidgetEvent; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IInitialStateService; use OCP\IRequest; @@ -105,6 +107,7 @@ class DashboardController extends Controller { $this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout); $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); $this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); + $this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); return new TemplateResponse('dashboard', 'index'); @@ -123,16 +126,20 @@ class DashboardController extends Controller { /** * @NoAdminRequired */ - public function setBackground($path = null, $url = null): JSONResponse { + public function setBackground($path = null, $url = null, $shipped = null): JSONResponse { // FIXME: store current version of the background and return the result // FIXME: handle shipped backgrounds avoid file duplication // FIXME: allow to reset to default ones + if ($shipped !== null) { + $this->backgroundService->setShippedBackground($shipped); + } if ($path !== null) { $this->backgroundService->setFileBackground($path); } if ($url !== null) { $this->backgroundService->setUrlBackground($url); } + $this->backgroundService->setDefaultBackground(); return new JSONResponse([]); } @@ -140,9 +147,13 @@ class DashboardController extends Controller { * @NoAdminRequired * @NoCSRFRequired */ - public function getBackground(): FileDisplayResponse { + public function getBackground() { $file = $this->backgroundService->getBackground(); - $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); - return $response; + if ($file !== null) { + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); + $response->cacheFor(24 * 60 * 60); + return $response; + } + return new NotFoundResponse(); } } diff --git a/apps/dashboard/lib/Service/BackgroundService.php b/apps/dashboard/lib/Service/BackgroundService.php index 2046513851f..06e0a451b88 100644 --- a/apps/dashboard/lib/Service/BackgroundService.php +++ b/apps/dashboard/lib/Service/BackgroundService.php @@ -32,6 +32,7 @@ use OCP\Files\IAppData; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; +use OCP\IConfig; class BackgroundService { @@ -64,7 +65,7 @@ class BackgroundService { 'yana-sichikova-sergey-ovachev-stone-flower-2k.jpg', ]; - public function __construct(IRootFolder $rootFolder, IAppData $appData, $userId) { + public function __construct(IRootFolder $rootFolder, IAppData $appData, IConfig $config, $userId) { if ($userId === null) { return; } @@ -74,14 +75,26 @@ class BackgroundService { } catch (NotFoundException $e) { $this->dashboardUserFolder = $appData->newFolder($userId); } + $this->config = $config; + $this->userId = $userId; + } + + public function setDefaultBackground() { + $this->config->deleteUserValue($this->userId, 'dashboard', 'background'); } public function setFileBackground($path) { + $this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom'); $file = $this->userFolder->get($path); $newFile = $this->dashboardUserFolder->newFile('background.jpg', $file->fopen('r')); } + public function setShippedBackground($fileName) { + $this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName); + } + public function setUrlBackground($url) { + $this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom'); if (substr($url, 0, 1) === '/') { $url = \OC::$server->getURLGenerator()->getAbsoluteURL($url); } @@ -92,11 +105,15 @@ class BackgroundService { $newFile = $this->dashboardUserFolder->newFile('background.jpg', $content); } - /** - * @throws NotFoundException - */ - public function getBackground(): ISimpleFile { - return $this->dashboardUserFolder->getFile('background.jpg'); + public function getBackground() { + $background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'); + if ($background === 'custom') { + try { + return $this->dashboardUserFolder->getFile('background.jpg'); + } catch (NotFoundException $e) { + } + } + return null; } } diff --git a/apps/dashboard/src/App.vue b/apps/dashboard/src/App.vue index 807a0e6f61e..db2f20382b1 100644 --- a/apps/dashboard/src/App.vue +++ b/apps/dashboard/src/App.vue @@ -76,6 +76,7 @@ import BackgroundSettings from './components/BackgroundSettings' const panels = loadState('dashboard', 'panels') const firstRun = loadState('dashboard', 'firstRun') +const background = loadState('dashboard', 'background') const prefixWithBaseUrl = (url) => generateFilePath('dashboard', '', 'img/') + url @@ -103,18 +104,22 @@ export default { modal: false, appStoreUrl: generateUrl('/settings/apps/dashboard'), statuses: {}, + background, backgroundTime: Date.now(), defaultBackground: window.OCA.Accessibility.theme === 'dark' ? prefixWithBaseUrl('flickr-148302424@N05-36591009215.jpg?v=1') : prefixWithBaseUrl('flickr-paszczak000-8715851521.jpg?v=1'), } }, computed: { backgroundImage() { - // FIXME: make this dependent if the default is set or not - return generateUrl('/apps/dashboard/background') + '?v=' + this.backgroundTime - if (window.OCA.Accessibility.theme === 'dark') { - return !isMobile ? prefixWithBaseUrl('flickr-148302424@N05-36591009215.jpg?v=1') : prefixWithBaseUrl('flickr-148302424@N05-36591009215-mobile.jpg?v=1') + if (this.background === 'default') { + if (window.OCA.Accessibility.theme === 'dark') { + return !isMobile ? prefixWithBaseUrl('flickr-148302424@N05-36591009215.jpg?v=1') : prefixWithBaseUrl('flickr-148302424@N05-36591009215-mobile.jpg?v=1') + } + return !isMobile ? prefixWithBaseUrl('flickr-paszczak000-8715851521.jpg?v=1') : prefixWithBaseUrl('flickr-paszczak000-8715851521-mobile.jpg?v=1') + } else if (this.background === 'custom') { + return generateUrl('/apps/dashboard/background') + '?v=' + this.backgroundTime } - return !isMobile ? prefixWithBaseUrl('flickr-paszczak000-8715851521.jpg?v=1') : prefixWithBaseUrl('flickr-paszczak000-8715851521-mobile.jpg?v=1') + return prefixWithBaseUrl(this.background) }, tooltip() { if (!this.firstRun) { @@ -254,8 +259,8 @@ export default { this.firstRun = false }, 1000) }, - updateBackground(date) { - this.backgroundTime = date + updateBackground(backgroundType) { + this.background = backgroundType }, }, } diff --git a/apps/dashboard/src/components/BackgroundSettings.vue b/apps/dashboard/src/components/BackgroundSettings.vue index 7645d8fb832..ad7898f8abe 100644 --- a/apps/dashboard/src/components/BackgroundSettings.vue +++ b/apps/dashboard/src/components/BackgroundSettings.vue @@ -22,18 +22,25 @@ @@ -41,10 +48,23 @@ import axios from '@nextcloud/axios' import { generateUrl, generateFilePath } from '@nextcloud/router' import { loadState } from '@nextcloud/initial-state' +import isMobile from '../mixins/isMobile' const prefixWithBaseUrl = (url) => generateFilePath('dashboard', '', 'img/') + url const shippedBackgroundList = loadState('dashboard', 'shippedBackgrounds') +const getBackgroundUrl = (background, time = 0) => { + if (background === 'default') { + if (window.OCA.Accessibility.theme === 'dark') { + return !isMobile ? prefixWithBaseUrl('flickr-148302424@N05-36591009215.jpg?v=1') : prefixWithBaseUrl('flickr-148302424@N05-36591009215-mobile.jpg?v=1') + } + return !isMobile ? prefixWithBaseUrl('flickr-paszczak000-8715851521.jpg?v=1') : prefixWithBaseUrl('flickr-paszczak000-8715851521-mobile.jpg?v=1') + } else if (background === 'custom') { + return generateUrl('/apps/dashboard/background') + '?v=' + time + } + return prefixWithBaseUrl(background) +} + export default { name: 'BackgroundSettings', data() { @@ -56,36 +76,43 @@ export default { computed: { shippedBackgrounds() { return shippedBackgroundList.map((item) => { - return prefixWithBaseUrl(item) + return { + name: item, + url: prefixWithBaseUrl(item), + } }) }, }, methods: { - async update() { + async update(state) { const date = Date.now() - this.backgroundImage = generateUrl('/apps/dashboard/background') + '?v=' + date + this.backgroundImage = getBackgroundUrl(state, date) const image = new Image() image.onload = () => { - this.$emit('updateBackground', date) + this.$emit('updateBackground', state) this.loading = false } image.src = this.backgroundImage }, - setDefault() { + async setDefault() { console.debug('SetDefault') - this.update() + await axios.post(generateUrl('/apps/dashboard/background')) + this.update('default') + }, + async setShipped(shipped) { + this.loading = shipped + await axios.post(generateUrl('/apps/dashboard/background'), { shipped }) + this.update(shipped) }, async setUrl(url) { this.loading = true - console.debug('SetUrl ' + url) await axios.post(generateUrl('/apps/dashboard/background'), { url }) - this.update() + this.update('custom') }, async setFile(path) { this.loading = true - console.debug('SetFile ' + path) await axios.post(generateUrl('/apps/dashboard/background'), { path }) - this.update() + this.update('custom') }, pickFile() { window.OC.dialogs.filepicker(t('dashboard', 'Insert from {productName}', { productName: OC.theme.name }), (path, type) => { @@ -114,8 +141,11 @@ export default { background-image: var(--color-background-dark); } - & img { + &--preview { width: 140px; + height: 90px; + background-size: cover; + background-position: center center; } &:hover { -- 2.39.5