From 741024e9622603f91b4aff3759c50bef5edc2f69 Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Wed, 19 Dec 2018 23:14:33 +0100 Subject: [PATCH] implementing folder sorting caching --- frontend/app/gallery/cache.gallery.service.ts | 44 ++++++++++++++++--- frontend/app/gallery/gallery.service.ts | 32 +++++++++++--- .../app/gallery/map/map.gallery.component.css | 1 + .../navigator.gallery.component.html | 4 +- .../navigator/navigator.gallery.component.ts | 3 +- package.json | 2 +- 6 files changed, 71 insertions(+), 15 deletions(-) diff --git a/frontend/app/gallery/cache.gallery.service.ts b/frontend/app/gallery/cache.gallery.service.ts index 3d550bc9..7e39f5e2 100644 --- a/frontend/app/gallery/cache.gallery.service.ts +++ b/frontend/app/gallery/cache.gallery.service.ts @@ -6,6 +6,7 @@ import {AutoCompleteItem, SearchTypes} from '../../../common/entities/AutoComple import {SearchResultDTO} from '../../../common/entities/SearchResultDTO'; import {MediaDTO} from '../../../common/entities/MediaDTO'; import {DataStructureVersion} from '../../../common/DataStructureVersion'; +import {SortingMethods} from '../../../common/entities/SortingMethods'; interface CacheItem { timestamp: number; @@ -15,12 +16,13 @@ interface CacheItem { @Injectable() export class GalleryCacheService { - private static CONTENT_PREFIX = 'content:'; - private static AUTO_COMPLETE_PREFIX = 'autocomplete:'; - private static INSTANT_SEARCH_PREFIX = 'instant_search:'; - private static SEARCH_PREFIX = 'search:'; - private static SEARCH_TYPE_PREFIX = ':type:'; - private static VERSION = 'version'; + private static readonly CONTENT_PREFIX = 'content:'; + private static readonly AUTO_COMPLETE_PREFIX = 'autocomplete:'; + private static readonly INSTANT_SEARCH_PREFIX = 'instant_search:'; + private static readonly SEARCH_PREFIX = 'search:'; + private static readonly SORTING_PREFIX = 'sorting:'; + private static readonly SEARCH_TYPE_PREFIX = ':type:'; + private static readonly VERSION = 'version'; constructor() { const version = parseInt(localStorage.getItem(GalleryCacheService.VERSION), 10) || 0; @@ -40,6 +42,36 @@ export class GalleryCacheService { } + public getSorting(dir: DirectoryDTO): SortingMethods { + const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name; + const tmp = localStorage.getItem(key); + if (tmp != null) { + return parseInt(tmp, 10); + } + return null; + } + + public removeSorting(dir: DirectoryDTO) { + try { + const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name; + localStorage.removeItem(key); + } catch (e) { + this.reset(); + console.error(e); + } + } + + public setSorting(dir: DirectoryDTO, sorting: SortingMethods): SortingMethods { + try { + const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name; + localStorage.setItem(key, sorting.toString()); + } catch (e) { + this.reset(); + console.error(e); + } + return null; + } + public getAutoComplete(text: string): AutoCompleteItem[] { const key = GalleryCacheService.AUTO_COMPLETE_PREFIX + text; const tmp = localStorage.getItem(key); diff --git a/frontend/app/gallery/gallery.service.ts b/frontend/app/gallery/gallery.service.ts index b9d5508f..d1bf9f3b 100644 --- a/frontend/app/gallery/gallery.service.ts +++ b/frontend/app/gallery/gallery.service.ts @@ -44,6 +44,26 @@ export class GalleryService { setSorting(sorting: SortingMethods): void { this.sorting.next(sorting); + if (this.content.value.directory) { + if (sorting !== Config.Client.Other.defaultPhotoSortingMethod) { + this.galleryCacheService.setSorting(this.content.value.directory, sorting); + } else { + this.galleryCacheService.removeSorting(this.content.value.directory); + } + } + } + + + setContent(content: ContentWrapper): void { + this.content.next(content); + if (content.directory) { + const sort = this.galleryCacheService.getSorting(content.directory); + if (sort !== null) { + this.sorting.next(sort); + } else { + this.sorting.next(Config.Client.Other.defaultPhotoSortingMethod); + } + } } @@ -54,7 +74,7 @@ export class GalleryService { content.searchResult = null; - this.content.next(content); + this.setContent(content); this.lastRequest.directory = directoryName; const params: { [key: string]: any } = {}; @@ -87,7 +107,7 @@ export class GalleryService { DirectoryDTO.addReferences(cw.directory); this.lastDirectory = cw.directory; - this.content.next(cw); + this.setContent(cw); } catch (e) { console.error(e); this.navigationService.toGallery().catch(console.error); @@ -105,7 +125,7 @@ export class GalleryService { this.ongoingSearch = {text: text, type: type}; - this.content.next(new ContentWrapper()); + this.setContent(new ContentWrapper()); const cw = new ContentWrapper(); cw.searchResult = this.galleryCacheService.getSearch(text, type); if (cw.searchResult == null) { @@ -124,13 +144,13 @@ export class GalleryService { } this.galleryCacheService.setSearch(text, type, cw.searchResult); } - this.content.next(cw); + this.setContent(cw); } public async instantSearch(text: string, type?: SearchTypes): Promise { if (text === null || text === '' || text.trim() === '.') { const content = new ContentWrapper(this.lastDirectory); - this.content.next(content); + this.setContent(content); if (this.searchId != null) { clearTimeout(this.searchId); } @@ -168,7 +188,7 @@ export class GalleryService { this.galleryCacheService.setInstantSearch(text, cw.searchResult); } } - this.content.next(cw); + this.setContent(cw); // if instant search do not have a result, do not do a search if (cw.searchResult.media.length === 0 && cw.searchResult.directories.length === 0) { diff --git a/frontend/app/gallery/map/map.gallery.component.css b/frontend/app/gallery/map/map.gallery.component.css index c98b616d..54870efb 100644 --- a/frontend/app/gallery/map/map.gallery.component.css +++ b/frontend/app/gallery/map/map.gallery.component.css @@ -1,4 +1,5 @@ .yaga-map{ + z-index: 0; width: 100%; height: 100%; } diff --git a/frontend/app/gallery/navigator/navigator.gallery.component.html b/frontend/app/gallery/navigator/navigator.gallery.component.html index 94335b9e..9a45dee6 100644 --- a/frontend/app/gallery/navigator/navigator.gallery.component.html +++ b/frontend/app/gallery/navigator/navigator.gallery.component.html @@ -28,7 +28,9 @@