Fix import tab aborting when tab is changed

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/14/head
trivernis 3 years ago
parent 642222e0c4
commit 296591a985
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,8 +1,8 @@
<div class="import-tab-inner"> <div class="import-tab-inner">
<mat-tab-group headerPosition="below"> <mat-tab-group headerPosition="below">
<mat-tab label="Import"> <mat-tab label="Import">
<app-file-import (fileImported)="fileImported.emit($event)" <app-file-import
(importFinished)="importFinished.emit($event)"></app-file-import> [state]="this.state"></app-file-import>
</mat-tab> </mat-tab>
<mat-tab *ngIf="selectedFiles.length > 0" label="Edit Tags"> <mat-tab *ngIf="selectedFiles.length > 0" label="Edit Tags">
<app-tag-edit [files]="selectedFiles"></app-tag-edit> <app-tag-edit [files]="selectedFiles"></app-tag-edit>

@ -1,5 +1,6 @@
import {ChangeDetectionStrategy, Component, EventEmitter, Input, Output} from "@angular/core"; import {ChangeDetectionStrategy, Component, EventEmitter, Input, Output} from "@angular/core";
import {File} from "../../../../../api/models/File"; import {File} from "../../../../../api/models/File";
import {ImportTabState} from "../../../../models/state/ImportTabState";
@Component({ @Component({
selector: "app-import-tab-sidebar", selector: "app-import-tab-sidebar",
@ -9,6 +10,7 @@ import {File} from "../../../../../api/models/File";
}) })
export class ImportTabSidebarComponent { export class ImportTabSidebarComponent {
@Input() state!: ImportTabState;
@Input() selectedFiles: File[] = []; @Input() selectedFiles: File[] = [];
@Output() fileImported = new EventEmitter<File>(); @Output() fileImported = new EventEmitter<File>();
@Output() importFinished = new EventEmitter<void>(); @Output() importFinished = new EventEmitter<void>();

@ -1,8 +1,8 @@
<app-drawer-page> <app-drawer-page>
<app-drawer-page-side> <app-drawer-page-side>
<app-import-tab-sidebar (fileImported)="this.addFileFromImport($event)" <app-import-tab-sidebar
(importFinished)="this.refreshFileView()" [selectedFiles]="selectedFiles"
[selectedFiles]="selectedFiles"></app-import-tab-sidebar> [state]="this.state"></app-import-tab-sidebar>
</app-drawer-page-side> </app-drawer-page-side>
<app-drawer-page-content> <app-drawer-page-content>
<app-file-multiview (fileSelectEvent)="this.onFileSelect($event)" <app-file-multiview (fileSelectEvent)="this.onFileSelect($event)"

@ -22,15 +22,6 @@ export class ImportTabComponent implements OnInit {
this.state.files.subscribe(files => files ? this.files = files : undefined); this.state.files.subscribe(files => files ? this.files = files : undefined);
} }
/**
* Adds an imported file to the list of imported files
* @param {File} file
* @returns {Promise<void>}
*/
public async addFileFromImport(file: File) {
this.state.files.next([...this.state.files.value, file]);
this.changeDetector.markForCheck();
}
public onFileSelect(files: File[]) { public onFileSelect(files: File[]) {
this.selectedFiles = files; this.selectedFiles = files;
@ -50,8 +41,4 @@ export class ImportTabComponent implements OnInit {
return undefined; return undefined;
} }
} }
public refreshFileView(): void {
this.changeDetector.markForCheck();
}
} }

@ -9,7 +9,7 @@
<mat-divider></mat-divider> <mat-divider></mat-divider>
</div> </div>
<div class="import-configuration" fxFlex fxFlexFill> <div class="import-configuration" fxFlex fxFlexFill>
<app-filesystem-import (fileImported)="this.fileImported.emit($event)" <app-filesystem-import
(importFinished)="importFinished.emit()"></app-filesystem-import> [state]="state"></app-filesystem-import>
</div> </div>
</div> </div>

@ -1,5 +1,5 @@
import {ChangeDetectionStrategy, Component, EventEmitter, Output} from "@angular/core"; import {ChangeDetectionStrategy, Component, Input} from "@angular/core";
import {File} from "../../../../../api/models/File"; import {ImportTabState} from "../../../../models/state/ImportTabState";
@Component({ @Component({
selector: "app-file-import", selector: "app-file-import",
@ -9,8 +9,7 @@ import {File} from "../../../../../api/models/File";
}) })
export class FileImportComponent { export class FileImportComponent {
@Output() fileImported = new EventEmitter<File>(); @Input() state!: ImportTabState;
@Output() importFinished = new EventEmitter<void>();
constructor() { constructor() {
} }

@ -1,10 +1,10 @@
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Output} from "@angular/core"; import {ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit} from "@angular/core";
import {ImportService} from "../../../../../services/import/import.service"; import {ImportService} from "../../../../../services/import/import.service";
import {LoggingService} from "../../../../../services/logging/logging.service"; import {LoggingService} from "../../../../../services/logging/logging.service";
import {AddFileOptions} from "../../../../../models/AddFileOptions"; import {AddFileOptions} from "../../../../../models/AddFileOptions";
import {File} from "../../../../../../api/models/File";
import {DialogFilter} from "@tauri-apps/api/dialog"; import {DialogFilter} from "@tauri-apps/api/dialog";
import {FileOsMetadata} from "../../../../../../api/api-types/files"; import {FileOsMetadata} from "../../../../../../api/api-types/files";
import {ImportTabState} from "../../../../../models/state/ImportTabState";
@Component({ @Component({
selector: "app-filesystem-import", selector: "app-filesystem-import",
@ -12,10 +12,9 @@ import {FileOsMetadata} from "../../../../../../api/api-types/files";
styleUrls: ["./filesystem-import.component.scss"], styleUrls: ["./filesystem-import.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush changeDetection: ChangeDetectionStrategy.OnPush
}) })
export class FilesystemImportComponent { export class FilesystemImportComponent implements OnInit {
@Output() fileImported = new EventEmitter<File>(); @Input() state!: ImportTabState;
@Output() importFinished = new EventEmitter<void>();
public fileCount: number = 0; public fileCount: number = 0;
public files: FileOsMetadata[] = []; public files: FileOsMetadata[] = [];
@ -45,12 +44,30 @@ export class FilesystemImportComponent {
) { ) {
} }
public ngOnInit(): void {
this.state.selectedPaths.subscribe(paths => {
this.files = paths;
this.fileCount = paths.length;
});
this.state.importing.subscribe(importing => {
this.importing = importing;
this.changeDetector.markForCheck();
});
this.state.importedCount.subscribe(count => {
this.importingProgressTotal = count;
});
this.state.importingProgress.subscribe(prog => {
this.importingProgress = prog;
this.changeDetector.markForCheck();
});
}
public async setSelectedPaths(paths: string[]) { public async setSelectedPaths(paths: string[]) {
this.changeDetector.markForCheck(); this.changeDetector.markForCheck();
this.resolving = true; this.resolving = true;
try { try {
this.files = await this.importService.resolvePathsToFiles(paths); const selectedPaths = await this.importService.resolvePathsToFiles(paths);
this.fileCount = this.files.length; this.state.selectedPaths.next(selectedPaths);
} catch (err: any) { } catch (err: any) {
console.log(err); console.log(err);
this.errorBroker.error(err); this.errorBroker.error(err);
@ -60,7 +77,7 @@ export class FilesystemImportComponent {
} }
public async import() { public async import() {
this.importing = true; this.state.importing.next(true);
this.importingProgress = 0; this.importingProgress = 0;
let count = 0; let count = 0;
@ -71,17 +88,17 @@ export class FilesystemImportComponent {
file, file,
this.importOptions this.importOptions
); );
this.fileImported.emit(resultFile); this.state.addImportedFile(resultFile);
} catch (err: any) { } catch (err: any) {
console.log(err); console.log(err);
this.errorBroker.error(err); this.errorBroker.error(err);
} }
count++; count++;
this.importingProgress = (count / this.fileCount) * 100; this.state.importedCount.next(count);
this.importingProgressTotal = count; this.state.importingProgress.next((count / this.fileCount) * 100);
} }
this.importing = false; this.state.importing.next(false);
this.importFinished.emit(); this.state.selectedPaths.next([]);
} }
} }

@ -4,12 +4,13 @@ import {StateServices} from "./StateServices";
import {TabCategory} from "./TabCategory"; import {TabCategory} from "./TabCategory";
import {BehaviorSubject} from "rxjs"; import {BehaviorSubject} from "rxjs";
import {File} from "../../../api/models/File"; import {File} from "../../../api/models/File";
import {FileBasicData} from "../../../api/api-types/files"; import {FileBasicData, FileOsMetadata} from "../../../api/api-types/files";
import {mapNew} from "../../../api/models/adaptors"; import {mapNew} from "../../../api/models/adaptors";
export type ImportTabSaveState = { export type ImportTabSaveState = {
selectedCd: string | undefined, selectedCd: string | undefined,
mode: "grid" | "gallery", mode: "grid" | "gallery",
selectedPaths: FileOsMetadata[],
files: FileBasicData[], files: FileBasicData[],
} & TabSaveState; } & TabSaveState;
@ -18,16 +19,27 @@ export class ImportTabState extends TabState implements SaveState<ImportTabSaveS
public mode = new BehaviorSubject<"grid" | "gallery">("grid"); public mode = new BehaviorSubject<"grid" | "gallery">("grid");
public selectedCD = new BehaviorSubject<string | undefined>(undefined); public selectedCD = new BehaviorSubject<string | undefined>(undefined);
public files = new BehaviorSubject<File[]>([]); public files = new BehaviorSubject<File[]>([]);
public selectedPaths = new BehaviorSubject<FileOsMetadata[]>([]);
// used when files are being imported
public importing = new BehaviorSubject<boolean>(false);
public importingProgress = new BehaviorSubject<number>(0);
public importedCount = new BehaviorSubject<number>(0);
constructor(uuid: number, services: StateServices) { constructor(uuid: number, services: StateServices) {
super(uuid, TabCategory.Import, services); super(uuid, TabCategory.Import, services);
} }
public addImportedFile(file: File) {
this.files.next([...this.files.value, file]);
}
public restoreSaveState(state: ImportTabSaveState) { public restoreSaveState(state: ImportTabSaveState) {
super.restoreSaveState(state); super.restoreSaveState(state);
this.selectedCD = new BehaviorSubject<string | undefined>(state.selectedCd); this.selectedCD = new BehaviorSubject<string | undefined>(state.selectedCd);
this.files = new BehaviorSubject<File[]>(state.files.map(mapNew(File))); this.files = new BehaviorSubject<File[]>(state.files.map(mapNew(File)) ?? []);
this.mode = new BehaviorSubject<"grid" | "gallery">(state.mode); this.mode = new BehaviorSubject<"grid" | "gallery">(state.mode ?? "grid");
this.selectedPaths = new BehaviorSubject<FileOsMetadata[]>(state.selectedPaths ?? []);
return self; return self;
} }
@ -38,7 +50,8 @@ export class ImportTabState extends TabState implements SaveState<ImportTabSaveS
category: this.category, category: this.category,
selectedCd: this.selectedCD.value, selectedCd: this.selectedCD.value,
files: this.files.value.map(f => f.rawData), files: this.files.value.map(f => f.rawData),
mode: this.mode.value mode: this.mode.value,
selectedPaths: this.selectedPaths.value,
}; };
} }
} }

@ -79,6 +79,7 @@ export class StateService {
tabs.forEach((tab) => this.subscribeToTab(tab)); tabs.forEach((tab) => this.subscribeToTab(tab));
this.stateChange.next(); this.stateChange.next();
}); });
state.selectedTab.subscribe(() => this.stateChange.next());
} }
private subscribeToTab(tab: TabState) { private subscribeToTab(tab: TabState) {

Loading…
Cancel
Save