refactor: extract dialog functionality of electron into a service so it can be developed centrally

This commit is contained in:
Xymorot 2021-01-04 22:11:14 +01:00
parent d889a466c8
commit 995101ebec
4 changed files with 42 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import 'reflect-metadata'; import 'reflect-metadata';
import { Container, interfaces } from 'inversify'; import { Container, interfaces } from 'inversify';
import { MainAppWindow } from '../modules/app-window/main-app-window'; import { MainAppWindow } from '../modules/app-window/main-app-window';
import { Dialog } from '../modules/dialog/dialog';
import { I18nTranslator } from '../modules/i18n/i18n-translator'; import { I18nTranslator } from '../modules/i18n/i18n-translator';
import { Logger } from '../modules/logger/logger'; import { Logger } from '../modules/logger/logger';
import { NhentaiApi } from '../modules/nhentai/nhentai-api'; import { NhentaiApi } from '../modules/nhentai/nhentai-api';
@ -27,6 +28,8 @@ container.bind('logger').to(Logger);
container.bind('i18n-translator').to(I18nTranslator); container.bind('i18n-translator').to(I18nTranslator);
container.bind('dialog').to(Dialog);
container.bind('store').to(Store); container.bind('store').to(Store);
container.bind('session-helper').to(SessionHelper); container.bind('session-helper').to(SessionHelper);

View File

@ -0,0 +1,26 @@
import { dialog, OpenDialogOptions } from 'electron';
import { injectable } from 'inversify';
import { inject } from '../../core/inject';
import { II18nTranslator } from '../i18n/i-i18n-translator';
import { IDialog } from './i-dialog';
@injectable()
export class Dialog implements IDialog {
private readonly translator: II18nTranslator;
public constructor(@inject('i18n-translator') translator: II18nTranslator) {
this.translator = translator;
}
public selectFolder(options: OpenDialogOptions): ReturnType<typeof dialog.showOpenDialog> {
return dialog.showOpenDialog({
...{
title: this.translator.t('select a folder'),
},
...options,
...{
properties: ['openDirectory', 'showHiddenFiles'],
},
});
}
}

5
src/main/modules/dialog/i-dialog.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import { dialog, OpenDialogOptions } from 'electron';
interface IDialog {
selectFolder(options: OpenDialogOptions): ReturnType<typeof dialog.showOpenDialog>;
}

View File

@ -1,7 +1,7 @@
import { dialog } from 'electron';
import path from 'path'; import path from 'path';
import { createWriteStream } from 'fs-extra'; import { createWriteStream } from 'fs-extra';
import { container } from '../../core/container'; import { container } from '../../core/container';
import { IDialog } from '../dialog/i-dialog';
import { II18nTranslator } from '../i18n/i-i18n-translator'; import { II18nTranslator } from '../i18n/i-i18n-translator';
import { answer } from '../ipc/annotations/answer'; import { answer } from '../ipc/annotations/answer';
import { INhentaiApi } from './i-nhentai-api'; import { INhentaiApi } from './i-nhentai-api';
@ -12,15 +12,17 @@ export class NhentaiIpcController implements IIpcController {
private readonly translator: II18nTranslator; private readonly translator: II18nTranslator;
private constructor(nhentaiApi: INhentaiApi, translator: II18nTranslator) { private readonly dialog: IDialog;
private constructor(nhentaiApi: INhentaiApi, translator: II18nTranslator, dialog: IDialog) {
this.nhentaiApi = nhentaiApi; this.nhentaiApi = nhentaiApi;
this.translator = translator; this.translator = translator;
this.dialog = dialog;
} }
@answer(IpcChannel.NHENTAI_SAVE_FAVORITES) @answer(IpcChannel.NHENTAI_SAVE_FAVORITES)
public async nhentaiSaveFavorites(): Promise<void> { public async nhentaiSaveFavorites(): Promise<void> {
const result = await dialog.showOpenDialog({ const result = await this.dialog.selectFolder({
properties: ['openDirectory'],
title: this.translator.t('Select torrent file save location'), title: this.translator.t('Select torrent file save location'),
}); });
@ -39,6 +41,7 @@ export class NhentaiIpcController implements IIpcController {
public get(): NhentaiIpcController { public get(): NhentaiIpcController {
const nhentaiApi: INhentaiApi = container.get('nhentai-api'); const nhentaiApi: INhentaiApi = container.get('nhentai-api');
const translator: II18nTranslator = container.get('i18n-translator'); const translator: II18nTranslator = container.get('i18n-translator');
return new NhentaiIpcController(nhentaiApi, translator); const dialog: IDialog = container.get('dialog');
return new NhentaiIpcController(nhentaiApi, translator, dialog);
} }
} }