import { Component , OnDestroy , OnInit , ViewEncapsulation } from '@angular/core' ; import { MatDialog , MatDialogRef } from '@angular/material/dialog' ; import { Router } from '@angular/router' ; import { Subscription } from 'rxjs' ; import { Operation } from '../../core/enums' ; import { ProductInformation } from '../../core/product-information' ; import { CollectionService } from '../../services/collection/collection.service' ; import { SnackBarService } from '../../services/snack-bar/snack-bar.service' ; import { TranslatorService } from '../../services/translator/translator.service' ; import { TrashService } from '../../services/trash/trash.service' ; import { UpdateService } from '../../services/update/update.service' ; import { ConfirmationDialogComponent } from '../dialogs/confirmation-dialog/confirmation-dialog.component' ; import { ErrorDialogComponent } from '../dialogs/error-dialog/error-dialog.component' ; import { InputDialogComponent } from '../dialogs/input-dialog/input-dialog.component' ; import { RenameCollectionDialogComponent } from '../dialogs/rename-collection-dialog/rename-collection-dialog.component' ; @ Component ( { selector : 'app-collection-switcher' , templateUrl : './collection-switcher.component.html' , styleUrls : [ './collection-switcher.component.scss' ] , encapsulation : ViewEncapsulation . None , } ) export class CollectionSwitcherComponent implements OnInit , OnDestroy { private subscription : Subscription ; constructor ( private dialog , public collection , private snackBar , private translator , private trash , public update , public router ) { } public applicationName : string = ProductInformation . applicationName ; public collections : string [ ] ; public activeCollection : string = '' ; public ngOnDestroy ( ) { this . subscription . unsubscribe ( ) ; } public async ngOnInit ( ) { await this . collection . initializeAsync ( ) ; await this . getCollectionsAsync ( ) ; this . subscription = this . collection . collectionsChanged$ . subscribe ( ( ) => this . router . navigate ( [ '/loading' ] ) ) ; } public async addCollectionAsync ( ) { if ( this . collection . hasOpenNotes ( ) ) { this . snackBar . closeNoteBeforeChangingCollectionsAsync ( ) ; return ; } const titleText = await this . translator . getAsync ( 'DialogTitles.AddCollection' ) ; const placeholderText = await this . translator . getAsync ( 'Input.Collection' ) ; const dialogRef = this . dialog . open ( InputDialogComponent , { width : '450px' , data : { titleText : titleText , placeholderText : placeholderText } , } ) ; dialogRef . afterClosed ( ) . subscribe ( async ( result ) => { if ( result ) { const collection = dialogRef . componentInstance . inputText ; const operation = await this . collection . addCollectionAsync ( collection ) ; switch ( operation ) { case Operation . Duplicate : { this . snackBar . duplicateCollectionAsync ( collection ) ; break ; } case Operation . Error : { const errorText = await this . translator . getAsync ( 'ErrorTexts.AddCollectionError' , { collection : collection , } ) ; this . dialog . open ( ErrorDialogComponent , { width : '450px' , data : { errorText : errorText } , } ) ; break ; } default : { break ; } } } } ) ; } public async activateCollection ( collection ) { if ( collection === this . activeCollection ) { return ; } if ( this . collection . hasOpenNotes ( ) ) { this . snackBar . closeNoteBeforeChangingCollectionsAsync ( ) ; return ; } this . collection . activateCollection ( collection ) ; } public renameCollection ( collection ) { if ( this . collection . hasOpenNotes ( ) ) { this . snackBar . closeNoteBeforeChangingCollectionsAsync ( ) ; return ; } const dialogRef = this . dialog . open ( RenameCollectionDialogComponent , { width : '450px' , data : { oldCollection : collection } , } ) ; } public async deleteCollectionAsync ( collection ) { if ( this . collection . hasOpenNotes ( ) ) { this . snackBar . closeNoteBeforeChangingCollectionsAsync ( ) ; return ; } const title = await this . translator . getAsync ( 'DialogTitles.ConfirmDeleteCollection' ) ; const text = await this . translator . getAsync ( 'DialogTexts.ConfirmDeleteCollection' , { collection : collection } ) ; const dialogRef = this . dialog . open ( ConfirmationDialogComponent , { width : '450px' , data : { dialogTitle : title , dialogText : text } , } ) ; dialogRef . afterClosed ( ) . subscribe ( async ( result ) => { if ( result ) { const operation = await this . collection . deleteCollectionAsync ( collection ) ; if ( operation === Operation . Error ) { const errorText = await this . translator . getAsync ( 'ErrorTexts.DeleteCollectionError' , { collection : collection , } ) ; this . dialog . open ( ErrorDialogComponent , { width : '450px' , data : { errorText : errorText } , } ) ; } } } ) ; } private async getCollectionsAsync ( ) { this . collections = await this . collection . getCollectionsAsync ( ) ; this . activeCollection = this . collection . getActiveCollection ( ) ; } public openTrash ( ) { this . trash . openTrash ( ) ; } public downloadLatestRelease ( ) { this . update . downloadLatestRelease ( ) ; } }