import { Injectable , NgZone } from '@angular/core' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { TranslatorService } from '../translator/translator.service' ; @ Injectable ( { providedIn : 'root' , } ) export class SnackBarService { constructor ( private zone , private translator , private matSnackBar ) { } public async duplicateCollectionAsync ( collection ) { const message = await this . translator . getAsync ( 'SnackBarMessages.DuplicateCollection' , { collection : collection } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async duplicateNotebookAsync ( notebookName ) { const message = await this . translator . getAsync ( 'SnackBarMessages.DuplicateNotebook' , { notebookName : notebookName } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async noteTitleCannotBeEmptyAsync ( ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NoteTitleCannotBeEmpty' ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async closeNoteBeforeChangingCollectionsAsync ( ) { const message = await this . translator . getAsync ( 'SnackBarMessages.CloseNoteBeforeChangingCollections' ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async noteExportedAsync ( noteTitle ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NoteExported' , { noteTitle : noteTitle } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async noteMovedToNotebookAsync ( notebookName ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NoteMovedToNotebook' , { notebookName : notebookName } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async notesMovedToNotebookAsync ( notebookName ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NotesMovedToNotebook' , { notebookName : notebookName } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async notesImportedIntoNotebookAsync ( notebookName ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NotesImportedIntoNotebook' , { notebookName : notebookName , } ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async notesImportedAsync ( ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NotesImported' ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } public async noNoteFilesToImportAsync ( ) { const message = await this . translator . getAsync ( 'SnackBarMessages.NoNoteFilesToImport' ) ; const action = await this . translator . getAsync ( 'SnackBarActions.Ok' ) ; this . showActionSnackBar ( message , action , false ) ; } private showActionLessSnackBar ( message ) { this . zone . run ( ( ) => { this . matSnackBar . open ( message , '' , { duration : this . calculateDuration ( message ) } ) ; } ) ; } private showActionSnackBar ( message , action , keepOpen ) { let config = { duration : this . calculateDuration ( message ) } ; if ( keepOpen ) { config = { } ; } this . matSnackBar . open ( message , action , config ) ; } private calculateDuration ( message ) { return Math . min ( Math . max ( message . length * 75 , 2000 ) , 7000 ) ; } }