1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/src/frontend/app/ui/gallery/thumbnailManager.service.ts

298 lines
7.6 KiB
TypeScript
Raw Normal View History

2022-04-04 19:37:31 +02:00
import { Injectable } from '@angular/core';
import {
ThumbnailLoaderService,
ThumbnailLoadingListener,
ThumbnailLoadingPriority,
ThumbnailTaskEntity,
} from './thumbnailLoader.service';
import { Media } from './Media';
import { MediaIcon } from './MediaIcon';
import { PersonDTO } from '../../../../common/entities/PersonDTO';
import { Person } from '../faces/Person';
2017-03-20 00:01:41 +01:00
@Injectable()
export class ThumbnailManagerService {
2022-04-04 19:37:31 +02:00
constructor(private thumbnailLoader: ThumbnailLoaderService) {}
2017-03-20 00:01:41 +01:00
2018-11-04 19:28:32 +01:00
public getThumbnail(photo: Media): Thumbnail {
return new Thumbnail(photo, this.thumbnailLoader);
}
2018-11-04 19:28:32 +01:00
public getLazyThumbnail(photo: Media): Thumbnail {
2017-07-23 22:36:53 +02:00
return new Thumbnail(photo, this.thumbnailLoader, false);
}
public getIcon(photo: MediaIcon): IconThumbnail {
return new IconThumbnail(photo, this.thumbnailLoader);
}
public getPersonThumbnail(person: PersonDTO): PersonThumbnail {
return new PersonThumbnail(person, this.thumbnailLoader);
}
}
export abstract class ThumbnailBase {
public loading = false;
protected available = false;
protected src: string = null;
protected error = false;
protected onLoad: () => void = null;
2017-07-23 22:36:53 +02:00
protected thumbnailTask: ThumbnailTaskEntity = null;
2022-04-04 19:37:31 +02:00
protected constructor(protected thumbnailService: ThumbnailLoaderService) {}
abstract set Visible(visible: boolean);
set OnLoad(onLoad: () => void) {
this.onLoad = onLoad;
}
2017-07-17 23:11:35 +02:00
get Available(): boolean {
return this.available;
}
2017-07-17 23:11:35 +02:00
get Src(): string {
return this.src;
}
2017-07-17 23:11:35 +02:00
get Loading(): boolean {
return this.loading;
}
2017-07-17 23:11:35 +02:00
get Error(): boolean {
return this.error;
}
destroy(): void {
if (this.thumbnailTask != null) {
this.thumbnailService.removeTask(this.thumbnailTask);
this.thumbnailTask = null;
}
}
2017-03-20 00:01:41 +01:00
}
export class PersonThumbnail extends ThumbnailBase {
2022-04-04 19:37:31 +02:00
constructor(
private person: PersonDTO,
thumbnailService: ThumbnailLoaderService
) {
super(thumbnailService);
this.src = '';
this.error = false;
if (!this.person.missingThumbnail) {
this.src = Person.getThumbnailUrl(person);
this.available = true;
if (this.onLoad) {
this.onLoad();
}
return;
}
setTimeout((): void => {
const listener: ThumbnailLoadingListener = {
2022-04-04 19:37:31 +02:00
onStartedLoading: (): void => {
// onLoadStarted
this.loading = true;
},
2022-04-04 19:37:31 +02:00
onLoad: (): void => {
// onLoaded
this.src = Person.getThumbnailUrl(person);
if (this.onLoad) {
this.onLoad();
}
this.available = true;
this.loading = false;
this.thumbnailTask = null;
},
2022-04-04 19:37:31 +02:00
onError: (): void => {
// onError
this.thumbnailTask = null;
this.loading = false;
this.error = true;
2022-04-04 19:37:31 +02:00
},
};
2022-04-04 19:37:31 +02:00
this.thumbnailTask = this.thumbnailService.loadPersonThumbnail(
person,
ThumbnailLoadingPriority.high,
listener
);
}, 0);
}
set Visible(visible: boolean) {
if (!this.thumbnailTask) {
return;
}
if (visible === true) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
export class IconThumbnail extends ThumbnailBase {
2022-04-04 19:37:31 +02:00
constructor(
private media: MediaIcon,
thumbnailService: ThumbnailLoaderService
) {
super(thumbnailService);
2018-03-30 15:30:30 -04:00
this.src = '';
2017-06-21 21:16:04 +02:00
this.error = false;
2018-11-04 19:28:32 +01:00
if (this.media.isIconAvailable()) {
this.src = this.media.getIconPath();
this.available = true;
if (this.onLoad) {
this.onLoad();
}
}
2018-11-04 19:28:32 +01:00
if (!this.media.isIconAvailable()) {
setTimeout((): void => {
const listener: ThumbnailLoadingListener = {
2022-04-04 19:37:31 +02:00
onStartedLoading: (): void => {
// onLoadStarted
this.loading = true;
},
2022-04-04 19:37:31 +02:00
onLoad: (): void => {
// onLoaded
2018-11-04 19:28:32 +01:00
this.src = this.media.getIconPath();
if (this.onLoad) {
this.onLoad();
}
this.available = true;
this.loading = false;
this.thumbnailTask = null;
},
2022-04-04 19:37:31 +02:00
onError: (): void => {
// onError
this.thumbnailTask = null;
2017-06-21 21:16:04 +02:00
this.loading = false;
this.error = true;
2022-04-04 19:37:31 +02:00
},
};
2022-04-04 19:37:31 +02:00
this.thumbnailTask = this.thumbnailService.loadIcon(
this.media,
ThumbnailLoadingPriority.high,
listener
);
2017-06-10 22:56:23 +02:00
}, 0);
}
}
set Visible(visible: boolean) {
if (!this.thumbnailTask) {
return;
}
if (visible === true) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
export class Thumbnail extends ThumbnailBase {
2022-04-04 19:37:31 +02:00
constructor(
private media: Media,
thumbnailService: ThumbnailLoaderService,
autoLoad: boolean = true
) {
super(thumbnailService);
2018-11-04 19:28:32 +01:00
if (this.media.isThumbnailAvailable()) {
this.src = this.media.getThumbnailPath();
this.available = true;
if (this.onLoad) {
this.onLoad();
}
2018-11-04 19:28:32 +01:00
} else if (this.media.isReplacementThumbnailAvailable()) {
this.src = this.media.getReplacementThumbnailPath();
this.available = true;
}
2017-07-23 22:36:53 +02:00
if (autoLoad) {
this.load();
}
}
2017-07-25 23:40:07 +02:00
set CurrentlyWaiting(value: boolean) {
if (!this.thumbnailTask) {
return;
}
2017-07-25 23:40:07 +02:00
if (value === true) {
2018-11-04 19:28:32 +01:00
if (this.media.isReplacementThumbnailAvailable()) {
2017-07-25 23:40:07 +02:00
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.extraHigh;
}
} else {
2018-11-04 19:28:32 +01:00
if (this.media.isReplacementThumbnailAvailable()) {
2017-07-25 23:40:07 +02:00
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
set Visible(visible: boolean) {
if (!this.thumbnailTask) {
return;
}
if (visible === true) {
2018-11-04 19:28:32 +01:00
if (this.media.isReplacementThumbnailAvailable()) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
}
} else {
2018-11-04 19:28:32 +01:00
if (this.media.isReplacementThumbnailAvailable()) {
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
} else {
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
}
}
}
public load(): void {
2018-11-04 19:28:32 +01:00
if (!this.media.isThumbnailAvailable() && this.thumbnailTask == null) {
2017-07-25 23:40:07 +02:00
// setTimeout(() => {
const listener: ThumbnailLoadingListener = {
2022-04-04 19:37:31 +02:00
onStartedLoading: (): void => {
// onLoadStarted
2018-03-30 15:30:30 -04:00
this.loading = true;
},
2022-04-04 19:37:31 +02:00
onLoad: (): void => {
// onLoaded
2018-11-04 19:28:32 +01:00
this.src = this.media.getThumbnailPath();
if (this.onLoad) {
this.onLoad();
}
2018-03-30 15:30:30 -04:00
this.available = true;
this.loading = false;
this.thumbnailTask = null;
},
2022-04-04 19:37:31 +02:00
onError: (): void => {
// onError
2018-03-30 15:30:30 -04:00
this.thumbnailTask = null;
this.loading = false;
this.error = true;
2022-04-04 19:37:31 +02:00
},
2018-03-30 15:30:30 -04:00
};
2018-11-04 19:28:32 +01:00
if (this.media.isReplacementThumbnailAvailable()) {
2022-04-04 19:37:31 +02:00
this.thumbnailTask = this.thumbnailService.loadImage(
this.media,
ThumbnailLoadingPriority.medium,
listener
);
2018-03-30 15:30:30 -04:00
} else {
2022-04-04 19:37:31 +02:00
this.thumbnailTask = this.thumbnailService.loadImage(
this.media,
ThumbnailLoadingPriority.high,
listener
);
2018-03-30 15:30:30 -04:00
}
2017-07-25 23:40:07 +02:00
// }, 0);
2017-03-20 00:01:41 +01:00
}
}
2017-03-20 00:01:41 +01:00
}