import {Component, OnDestroy, OnInit} from '@angular/core'; import {IndexingSettingsService} from './indexing.settings.service'; import {AuthenticationService} from '../../model/network/authentication.service'; import {NavigationService} from '../../model/navigation.service'; import {NotificationService} from '../../model/notification.service'; import {ErrorDTO} from '../../../../common/entities/Error'; import {Observable, interval} from 'rxjs'; import {IndexingConfig, ReIndexingSensitivity} from '../../../../common/config/private/IPrivateConfig'; import {SettingsComponent} from '../_abstract/abstract.settings.component'; import {Utils} from '../../../../common/Utils'; import {I18n} from '@ngx-translate/i18n-polyfill'; @Component({ selector: 'app-settings-indexing', templateUrl: './indexing.settings.component.html', styleUrls: ['./indexing.settings.component.css', './../_abstract/abstract.settings.component.css'], providers: [IndexingSettingsService], }) export class IndexingSettingsComponent extends SettingsComponent implements OnInit, OnDestroy { types: { key: number; value: string }[] = []; private subscription: { timer: any, settings: any } = { timer: null, settings: null }; private $counter: Observable = null; updateProgress = async () => { try { await (this._settingsService).getProgress(); } catch (err) { if (this.subscription.timer != null) { this.subscription.timer.unsubscribe(); this.subscription.timer = null; } } if ((this._settingsService).progress.value != null && this.subscription.timer == null) { if (!this.$counter) { this.$counter = interval(5000); } this.subscription.timer = this.$counter.subscribe((x) => this.updateProgress()); } if ((this._settingsService).progress.value == null && this.subscription.timer != null) { this.subscription.timer.unsubscribe(); this.subscription.timer = null; } }; constructor(_authService: AuthenticationService, _navigation: NavigationService, _settingsService: IndexingSettingsService, notification: NotificationService, i18n: I18n) { super(i18n('Indexing'), _authService, _navigation, _settingsService, notification, i18n, s => s.Server.indexing); } async ngOnInit() { super.ngOnInit(); this.types = Utils .enumToArray(ReIndexingSensitivity); this.types.forEach(v => { switch (v.value) { case 'low': v.value = this.i18n('low'); break; case 'medium': v.value = this.i18n('medium'); break; case 'high': v.value = this.i18n('high'); break; } }); this.updateProgress(); } ngOnDestroy() { super.ngOnDestroy(); if (this.subscription.timer != null) { this.subscription.timer.unsubscribe(); this.subscription.timer = null; } if (this.subscription.settings != null) { this.subscription.settings.unsubscribe(); this.subscription.settings = null; } } async index(createThumbnails: boolean) { this.inProgress = true; this.error = ''; try { await this._settingsService.index(createThumbnails); this.updateProgress(); this.notification.success(this.i18n('Folder indexed'), this.i18n('Success')); this.inProgress = false; return true; } catch (err) { console.log(err); if (err.message) { this.error = (err).message; } } this.inProgress = false; return false; } async cancelIndexing() { this.inProgress = true; this.error = ''; try { await (this._settingsService).cancel(); this.notification.success(this.i18n('Folder indexed'), this.i18n('Success')); this.inProgress = false; return true; } catch (err) { console.log(err); if (err.message) { this.error = (err).message; } } this.inProgress = false; return false; } async resetDatabase() { this.inProgress = true; this.error = ''; try { await (this._settingsService).reset(); this.notification.success(this.i18n('Database reset'), this.i18n('Success')); this.inProgress = false; return true; } catch (err) { console.log(err); if (err.message) { this.error = (err).message; } } this.inProgress = false; return false; } }