1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

Fix sorting to be stored also for searches. fixes #539

This commit is contained in:
Patrik J. Braun 2023-08-05 16:32:54 +02:00
parent de30430e63
commit b1a26ef90c
2 changed files with 28 additions and 15 deletions

View File

@ -10,6 +10,7 @@ import {SearchQueryDTO, SearchQueryTypes,} from '../../../../common/entities/Sea
import {ContentWrapper} from '../../../../common/entities/ConentWrapper';
import {ContentWrapperWithError} from './content.service';
import {ThemeModes} from '../../../../common/config/public/ClientConfig';
import {SearchResultDTO} from '../../../../common/entities/SearchResultDTO';
interface CacheItem<T> {
timestamp: number;
@ -96,8 +97,13 @@ export class GalleryCacheService {
}
}
public getSorting(dir: DirectoryPathDTO): SortingMethods {
const key = GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
public getSorting(cw: ContentWrapper): SortingMethods {
let key = GalleryCacheService.SORTING_PREFIX;
if (cw?.searchResult?.searchQuery) {
key += JSON.stringify(cw.searchResult.searchQuery);
} else {
key += cw?.directory?.path + '/' + cw?.directory?.name;
}
const tmp = localStorage.getItem(key);
if (tmp != null) {
return parseInt(tmp, 10);
@ -105,10 +111,14 @@ export class GalleryCacheService {
return null;
}
public removeSorting(dir: DirectoryPathDTO): void {
public removeSorting(cw: ContentWrapper): void {
try {
const key =
GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
let key = GalleryCacheService.SORTING_PREFIX;
if (cw?.searchResult?.searchQuery) {
key += JSON.stringify(cw.searchResult.searchQuery);
} else {
key += cw?.directory?.path + '/' + cw?.directory?.name;
}
localStorage.removeItem(key);
} catch (e) {
this.reset();
@ -117,12 +127,16 @@ export class GalleryCacheService {
}
public setSorting(
dir: DirectoryPathDTO,
cw: ContentWrapper,
sorting: SortingMethods
): SortingMethods {
try {
const key =
GalleryCacheService.SORTING_PREFIX + dir.path + '/' + dir.name;
let key = GalleryCacheService.SORTING_PREFIX;
if (cw?.searchResult?.searchQuery) {
key += JSON.stringify(cw.searchResult.searchQuery);
} else {
key += cw?.directory?.path + '/' + cw?.directory?.name;
}
localStorage.setItem(key, sorting.toString());
} catch (e) {
this.reset();

View File

@ -1,12 +1,11 @@
import {Injectable} from '@angular/core';
import {NetworkService} from '../../../model/network/network.service';
import {ParentDirectoryDTO} from '../../../../../common/entities/DirectoryDTO';
import {GalleryCacheService} from '../cache.gallery.service';
import {BehaviorSubject, Observable} from 'rxjs';
import {Config} from '../../../../../common/config/public/Config';
import {SortingMethods} from '../../../../../common/entities/SortingMethods';
import {PG2ConfMap} from '../../../../../common/PG2ConfMap';
import {ContentService, ContentWrapperWithError, DirectoryContent} from '../content.service';
import {ContentService, DirectoryContent} from '../content.service';
import {PhotoDTO} from '../../../../../common/entities/PhotoDTO';
import {map, switchMap} from 'rxjs/operators';
import {SeededRandomService} from '../../../model/seededRandom.service';
@ -27,8 +26,8 @@ export class GallerySortingService {
Config.Gallery.defaultPhotoSortingMethod
);
this.galleryService.content.subscribe((c) => {
if (c.directory) {
const sort = this.galleryCacheService.getSorting(c.directory);
if (c) {
const sort = this.galleryCacheService.getSorting(c);
if (sort !== null) {
this.sorting.next(sort);
return;
@ -54,18 +53,18 @@ export class GallerySortingService {
setSorting(sorting: SortingMethods): void {
this.sorting.next(sorting);
if (this.galleryService.content.value.directory) {
if (this.galleryService.content.value) {
if (
sorting !==
this.getDefaultSorting(this.galleryService.content.value)
) {
this.galleryCacheService.setSorting(
this.galleryService.content.value.directory,
this.galleryService.content.value,
sorting
);
} else {
this.galleryCacheService.removeSorting(
this.galleryService.content.value.directory
this.galleryService.content.value
);
}
}