import { ipcRenderer, remote, IpcRendererEvent } from 'electron'; import { Observable, Subject } from 'rxjs'; import { NativeService, WindowState, NotificationRequest, WindowIdle } from '@ucap-webmessenger/native'; import { share } from 'rxjs/operators'; import { NotificationChannel, UpdaterChannel, FileChannel, WindowStateChannel, IdleStateChannel, ChatChannel } from '../types/channel.type'; import { Injectable } from '@angular/core'; import { TranslateLoaderService } from '../translate/electron-loader'; import { TranslateLoader } from '@ngx-translate/core'; @Injectable({ providedIn: 'root' }) export class ElectronNativeService implements NativeService { private windowStateChangedSubject: Subject | null = null; private windowStateChanged$: Observable | null = null; private idleStateChangedSubject: Subject | null = null; private idleStateChanged$: Observable | null = null; private chatOpenRoomSubject: Subject | null = null; private chatOpenRoom$: Observable | null = null; postAppInit(): void {} notify(noti: NotificationRequest): void { ipcRenderer.send(NotificationChannel.Notify, noti); } closeAllNotify(): void { ipcRenderer.send(NotificationChannel.CloseAllNotify); } checkForUpdates(): Observable { return new Observable(subscriber => { try { subscriber.next(ipcRenderer.sendSync(UpdaterChannel.Check)); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } showImageViewer(): void { ipcRenderer.send(FileChannel.ShowImageViewer); } readFile(path: string): Observable { return new Observable(subscriber => { try { subscriber.next(ipcRenderer.sendSync(FileChannel.ReadFile, path)); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } saveFile( buffer: Buffer, fileName: string, path?: string ): Observable { return new Observable(subscriber => { try { subscriber.next( ipcRenderer.sendSync(FileChannel.SaveFile, buffer, fileName, path) ); } catch (error) { subscriber.error(error); } finally { subscriber.complete(); } }); } windowStateChanged(): Observable { if (!this.windowStateChangedSubject) { this.windowStateChangedSubject = new Subject(); this.windowStateChanged$ = this.windowStateChangedSubject .asObservable() .pipe(share()); } ipcRenderer.on( WindowStateChannel.Changed, (event: IpcRendererEvent, windowState: WindowState) => { this.windowStateChangedSubject.next(windowState); } ); return this.windowStateChanged$; } windowClose(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } currentWindow.close(); } windowMinimize(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } currentWindow.minimize(); } windowMaximize(): void { const currentWindow = remote.getCurrentWindow(); if (!currentWindow) { return; } if (currentWindow.isMaximized()) { currentWindow.unmaximize(); } else { currentWindow.maximize(); } } idleStateChanged(): Observable { if (!this.idleStateChangedSubject) { this.idleStateChangedSubject = new Subject(); this.idleStateChanged$ = this.idleStateChangedSubject .asObservable() .pipe(share()); } ipcRenderer.send(IdleStateChannel.StartCheck); ipcRenderer.on( IdleStateChannel.Changed, (event: IpcRendererEvent, idleState: WindowIdle) => { this.idleStateChangedSubject.next(idleState); } ); return this.idleStateChanged$; } chatOpenRoom(): Observable { if (!this.chatOpenRoomSubject) { this.chatOpenRoomSubject = new Subject(); this.chatOpenRoom$ = this.chatOpenRoomSubject .asObservable() .pipe(share()); } ipcRenderer.on( ChatChannel.OpenRoom, (event: IpcRendererEvent, roomSeq: string) => { this.chatOpenRoomSubject.next(roomSeq); } ); return this.chatOpenRoom$; } getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader { return new TranslateLoaderService(this, prefix, suffix); } constructor() {} }