import { Injectable } from '@angular/core' ; import { ChatServerConnection } from '../models/app.models' ; import { ChatFlowPack } from '../models/chatflow.models' ; import { InfoDialogService } from '../services/info-dialog.service' ; @ Injectable ( ) export class SettingsService { constructor ( private infoDialog ) { } loadSavedConnections ( ) { var loaded = JSON . parse ( localStorage . getItem ( SettingKey . SavedConnsKey ) ) as ChatServerConnection [ ] ; if ( loaded ) return loaded ; else return [ ] ; } saveSavedConnections ( connections ) { localStorage . setItem ( SettingKey . SavedConnsKey , JSON . stringify ( connections ) ) ; } saveChatProject ( chatProjectName , pack , quite , next ? ) { chatProjectName += '.anaproj' ; if ( quite ) { localStorage . setItem ( chatProjectName , JSON . stringify ( pack ) ) ; if ( next ) next ( ) ; } else { let existing = localStorage . getItem ( chatProjectName ) ; if ( existing ) { this . infoDialog . confirm ( 'Sure?' , 'Chat project the given name already exists. Do you want to overwrite it?' , ( ok ) => { if ( ok ) { localStorage . setItem ( chatProjectName , JSON . stringify ( pack ) ) ; if ( next ) next ( ) ; } } ) ; } else { localStorage . setItem ( chatProjectName , JSON . stringify ( pack ) ) ; if ( next ) next ( ) ; } } } getChatProject ( chatProjectName ) { chatProjectName += '.anaproj' ; let existing = localStorage . getItem ( chatProjectName ) ; if ( ! existing ) { this . infoDialog . alert ( 'Not found' , `Chat Project with name '${ chatProjectName }' does not exist` ) ; return null ; } return JSON . parse ( existing ) as ChatFlowPack ; } listSavedChatProjectNames ( ) { let savedProjs = [ ] ; for ( var key in localStorage ) { if ( key . endsWith ( '.anaproj' ) ) { let name = key . replace ( new RegExp ( '\.anaproj$' ) , '' ) ; savedProjs . push ( name ) ; } } return savedProjs . sort ( ( x , y ) => ( ( x && y ) ? x . localeCompare ( y ) : 1 ) ) ; } renameChatProject ( oldName , newName ) { if ( oldName == newName ) return ; oldName += ".anaproj" ; newName += ".anaproj" ; let temp = localStorage . getItem ( oldName ) ; if ( ! temp ) { this . infoDialog . alert ( 'Not found' , `${ oldName } not found` ) ; return ; } localStorage . setItem ( newName , temp ) ; localStorage . removeItem ( oldName ) ; } deleteChatProject ( name ) { name += ".anaproj" ; let exists = localStorage . getItem ( name ) ; if ( ! exists ) { this . infoDialog . alert ( 'Not found' , `${ name } not found` ) ; return false ; } localStorage . removeItem ( name ) ; return true ; } } enum SettingKey { SavedConnsKey = 'SAVED_CONNECTIONS' }