import { Injectable , NgZone } from "@angular/core" ; import { Router } from "@angular/router" ; import { ipcRenderer , remote } from "electron" ; import * as fs from "fs" ; import * as settings from "electron-settings" ; import { FilesSet , AppFile , FileStatus , Import , Clipboard , Sets , getSet , saveSet } from "../../data" ; @ Injectable ( { providedIn : "root" , } ) export class SetService { ipcRenderer : typeof ipcRenderer ; settings : typeof settings ; remote : typeof remote ; fs : typeof fs ; public getFromSettings ( params ) { if ( this . isElectron ) { return this . settings . getSync ( params ) ; } } public hasInSettings ( params ) { if ( this . isElectron ) { return this . settings . has ( params ) ; } } get isElectron ( ) : boolean { return ! ! ( window && window . process && window . process . type ) ; } constructor ( private router , private zone ) { if ( this . isElectron ) { this . settings = window . require ( "electron" ) . remote . require ( "electron-settings" ) ; this . remote = window . require ( "electron" ) . remote ; this . fs = window . require ( "fs" ) ; this . ipcRenderer = window . require ( "electron" ) . ipcRenderer ; if ( this . settings . has ( "sets" ) ) { const sets = [ ... this . settings . getSync ( "sets" ) as Array < any > ] ; sets . forEach ( ( item ) => { const set = new FilesSet ( { ... item } ) ; set . setStatistics ( ) ; Sets . push ( set ) ; } ) ; } } } public saveSets = async ( ) => { if ( this . isElectron ) { await this . settings . set ( "sets" , Sets as any [ ] ) ; } } ; private unsubscribeFile = ( file ) : void => { if ( this . isElectron && file . original . path ) { this . fs . unwatchFile ( file . original . path ) ; } } ; public resetFileStatus = ( setId ) : void => { const set = getSet ( setId ) ; set . files . forEach ( ( file ) => { if ( this . fs . existsSync ( file . original . path ) || ! file . shrinked ) { file . status = FileStatus . needsUpdate ; } } ) ; getSet ( setId ) . setStatistics ( ) ; saveSet ( setId ) ; } ; public async exportFolder ( setId ) { this . ipcRenderer . send ( "set-project-folder" ) ; const set = getSet ( setId ) ; await new Promise ( ( resolve ) => { ipcRenderer . once ( "get-folder-path" , ( event , path ) => { set . path = path [ 0 ] ; resolve ( ) ; } ) ; } ) ; set . files . forEach ( ( file ) => { if ( file . shrinked ) { this . fs . rename ( file . shrinked . path , set . path + "/" + file . shrinked . name , ( ) => { } ) ; file . shrinked . path = set . path + "/" + file . shrinked . name ; } } ) ; if ( this . settings . getSync ( "app.notification" ) ) { const Notification = this . remote . Notification ; new Notification ( { title : `Files are moved` , body : `${ set . files . length } shrinked files moved to the new export folder: ${ set . path }` , silent : true , } ) . show ( ) ; } saveSet ( setId ) ; } public deleteSet = ( setId ) : void => { this . zone . run ( ( ) => { this . router . navigate ( [ "import" ] , { skipLocationChange : true } ) . then ( ( e ) => { Sets . filter ( ( set , index ) => { if ( set . id === setId ) { Sets . splice ( index , 1 ) ; } } ) ; this . saveSets ( ) ; } ) ; } ) ; } ; public updateStatistics = ( ) => { Sets . forEach ( ( set ) => { set . setStatistics ( ) ; } ) ; } ; public addSet = ( name , files ) => { const newSet = new FilesSet ( { name , files } ) ; Sets . push ( newSet ) ; this . saveSets ( ) ; } ; public createSetFromImport = ( ) => { this . addSet ( "From import" , [ ... Import . files ] ) ; Import . files . length = 0 ; setTimeout ( ( ) => { this . router . navigate ( [ "/sets/" + Sets [ Sets . length - 1 ] . id ] ) ; } ) ; } ; public isItStaticSet ( setId ) { return setId === Import . id || setId === Clipboard . id ; } }