2018-03-31 03:30:30 +08:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {ThumbnailLoaderService, ThumbnailLoadingListener, ThumbnailLoadingPriority, ThumbnailTaskEntity} from './thumnailLoader.service';
|
|
|
|
import {Photo} from './Photo';
|
|
|
|
import {IconPhoto} from './IconPhoto';
|
2017-03-20 07:01:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ThumbnailManagerService {
|
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
constructor(private thumbnailLoader: ThumbnailLoaderService) {
|
|
|
|
}
|
2017-03-20 07:01:41 +08:00
|
|
|
|
2017-06-21 17:33:21 +08:00
|
|
|
public getThumbnail(photo: Photo): Thumbnail {
|
2017-06-11 04:32:56 +08:00
|
|
|
return new Thumbnail(photo, this.thumbnailLoader);
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-07-24 04:36:53 +08:00
|
|
|
public getLazyThumbnail(photo: Photo): Thumbnail {
|
|
|
|
return new Thumbnail(photo, this.thumbnailLoader, false);
|
|
|
|
}
|
|
|
|
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public getIcon(photo: IconPhoto) {
|
|
|
|
return new IconThumbnail(photo, this.thumbnailLoader);
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export abstract class ThumbnailBase {
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
protected available = false;
|
2017-06-11 04:32:56 +08:00
|
|
|
protected src: string = null;
|
2018-05-13 00:19:51 +08:00
|
|
|
protected loading = false;
|
|
|
|
protected error = false;
|
2017-06-11 04:32:56 +08:00
|
|
|
protected onLoad: Function = null;
|
2017-07-24 04:36:53 +08:00
|
|
|
protected thumbnailTask: ThumbnailTaskEntity = null;
|
2017-03-21 04:37:23 +08:00
|
|
|
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
protected constructor(protected thumbnailService: ThumbnailLoaderService) {
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
abstract set Visible(visible: boolean);
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
set OnLoad(onLoad: Function) {
|
|
|
|
this.onLoad = onLoad;
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
|
|
|
|
2017-07-18 05:11:35 +08:00
|
|
|
get Available(): boolean {
|
2017-06-11 04:32:56 +08:00
|
|
|
return this.available;
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-07-18 05:11:35 +08:00
|
|
|
get Src(): string {
|
2017-06-11 04:32:56 +08:00
|
|
|
return this.src;
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-07-18 05:11:35 +08:00
|
|
|
get Loading(): boolean {
|
2017-06-11 04:32:56 +08:00
|
|
|
return this.loading;
|
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-07-18 05:11:35 +08:00
|
|
|
get Error(): boolean {
|
|
|
|
return this.error;
|
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
destroy() {
|
|
|
|
if (this.thumbnailTask != null) {
|
|
|
|
this.thumbnailService.removeTask(this.thumbnailTask);
|
|
|
|
this.thumbnailTask = null;
|
2017-03-21 04:37:23 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-03-20 07:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-21 04:37:23 +08:00
|
|
|
export class IconThumbnail extends ThumbnailBase {
|
2017-03-20 07:01:41 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
constructor(private photo: IconPhoto, thumbnailService: ThumbnailLoaderService) {
|
|
|
|
super(thumbnailService);
|
2018-03-31 03:30:30 +08:00
|
|
|
this.src = '';
|
2017-06-22 03:16:04 +08:00
|
|
|
this.error = false;
|
2017-06-11 04:32:56 +08:00
|
|
|
if (this.photo.isIconAvailable()) {
|
|
|
|
this.src = this.photo.getIconPath();
|
|
|
|
this.available = true;
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.onLoad) {
|
|
|
|
this.onLoad();
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.photo.isIconAvailable()) {
|
2017-06-11 04:56:23 +08:00
|
|
|
setTimeout(() => {
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
const listener: ThumbnailLoadingListener = {
|
|
|
|
onStartedLoading: () => { // onLoadStarted
|
2017-06-11 04:32:56 +08:00
|
|
|
this.loading = true;
|
|
|
|
},
|
2018-05-13 00:19:51 +08:00
|
|
|
onLoad: () => {// onLoaded
|
2017-03-21 04:37:23 +08:00
|
|
|
this.src = this.photo.getIconPath();
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.onLoad) {
|
|
|
|
this.onLoad();
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.available = true;
|
|
|
|
this.loading = false;
|
|
|
|
this.thumbnailTask = null;
|
|
|
|
},
|
2018-05-13 00:19:51 +08:00
|
|
|
onError: (error) => {// onError
|
2017-06-11 04:32:56 +08:00
|
|
|
this.thumbnailTask = null;
|
2017-06-22 03:16:04 +08:00
|
|
|
this.loading = false;
|
|
|
|
this.error = true;
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.thumbnailTask = this.thumbnailService.loadIcon(this.photo, ThumbnailLoadingPriority.high, listener);
|
2017-03-20 07:01:41 +08:00
|
|
|
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-06-11 04:56:23 +08:00
|
|
|
}, 0);
|
2017-03-21 04:37:23 +08:00
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-03-21 04:37:23 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
set Visible(visible: boolean) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (!this.thumbnailTask) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
if (visible === true) {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
|
2017-03-21 04:37:23 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 04:37:23 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Thumbnail extends ThumbnailBase {
|
|
|
|
|
|
|
|
|
2017-07-24 04:36:53 +08:00
|
|
|
constructor(private photo: Photo, thumbnailService: ThumbnailLoaderService, autoLoad: boolean = true) {
|
2017-06-11 04:32:56 +08:00
|
|
|
super(thumbnailService);
|
|
|
|
if (this.photo.isThumbnailAvailable()) {
|
|
|
|
this.src = this.photo.getThumbnailPath();
|
|
|
|
this.available = true;
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.onLoad) {
|
|
|
|
this.onLoad();
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
} else if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.src = this.photo.getReplacementThumbnailPath();
|
|
|
|
this.available = true;
|
|
|
|
}
|
2017-07-24 04:36:53 +08:00
|
|
|
if (autoLoad) {
|
|
|
|
this.load();
|
|
|
|
}
|
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
2017-07-26 05:40:07 +08:00
|
|
|
set CurrentlyWaiting(value: boolean) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (!this.thumbnailTask) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-26 05:40:07 +08:00
|
|
|
if (value === true) {
|
|
|
|
if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.extraHigh;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
set Visible(visible: boolean) {
|
|
|
|
if (!this.thumbnailTask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (visible === true) {
|
|
|
|
if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.high;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.low;
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask.priority = ThumbnailLoadingPriority.medium;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-24 04:36:53 +08:00
|
|
|
public load() {
|
|
|
|
if (!this.photo.isThumbnailAvailable() && this.thumbnailTask == null) {
|
2017-07-26 05:40:07 +08:00
|
|
|
// setTimeout(() => {
|
2018-05-13 00:19:51 +08:00
|
|
|
const listener: ThumbnailLoadingListener = {
|
|
|
|
onStartedLoading: () => { // onLoadStarted
|
2018-03-31 03:30:30 +08:00
|
|
|
this.loading = true;
|
|
|
|
},
|
2018-05-13 00:19:51 +08:00
|
|
|
onLoad: () => {// onLoaded
|
2018-03-31 03:30:30 +08:00
|
|
|
this.src = this.photo.getThumbnailPath();
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.onLoad) {
|
|
|
|
this.onLoad();
|
|
|
|
}
|
2018-03-31 03:30:30 +08:00
|
|
|
this.available = true;
|
|
|
|
this.loading = false;
|
|
|
|
this.thumbnailTask = null;
|
|
|
|
},
|
2018-05-13 00:19:51 +08:00
|
|
|
onError: (error) => {// onError
|
2018-03-31 03:30:30 +08:00
|
|
|
this.thumbnailTask = null;
|
|
|
|
this.loading = false;
|
|
|
|
this.error = true;
|
2017-03-20 07:01:41 +08:00
|
|
|
}
|
2018-03-31 03:30:30 +08:00
|
|
|
};
|
|
|
|
if (this.photo.isReplacementThumbnailAvailable()) {
|
|
|
|
this.thumbnailTask = this.thumbnailService.loadImage(this.photo, ThumbnailLoadingPriority.medium, listener);
|
|
|
|
} else {
|
|
|
|
this.thumbnailTask = this.thumbnailService.loadImage(this.photo, ThumbnailLoadingPriority.high, listener);
|
|
|
|
}
|
2017-07-26 05:40:07 +08:00
|
|
|
// }, 0);
|
2017-03-20 07:01:41 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:01:41 +08:00
|
|
|
}
|
|
|
|
|