refactor: extract dialog functionality of electron into a service so it can be developed centrally
This commit is contained in:
parent
d889a466c8
commit
995101ebec
|
@ -1,6 +1,7 @@
|
|||
import 'reflect-metadata';
|
||||
import { Container, interfaces } from 'inversify';
|
||||
import { MainAppWindow } from '../modules/app-window/main-app-window';
|
||||
import { Dialog } from '../modules/dialog/dialog';
|
||||
import { I18nTranslator } from '../modules/i18n/i18n-translator';
|
||||
import { Logger } from '../modules/logger/logger';
|
||||
import { NhentaiApi } from '../modules/nhentai/nhentai-api';
|
||||
|
@ -27,6 +28,8 @@ container.bind('logger').to(Logger);
|
|||
|
||||
container.bind('i18n-translator').to(I18nTranslator);
|
||||
|
||||
container.bind('dialog').to(Dialog);
|
||||
|
||||
container.bind('store').to(Store);
|
||||
|
||||
container.bind('session-helper').to(SessionHelper);
|
||||
|
|
|
@ -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'],
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { dialog, OpenDialogOptions } from 'electron';
|
||||
|
||||
interface IDialog {
|
||||
selectFolder(options: OpenDialogOptions): ReturnType<typeof dialog.showOpenDialog>;
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import { dialog } from 'electron';
|
||||
import path from 'path';
|
||||
import { createWriteStream } from 'fs-extra';
|
||||
import { container } from '../../core/container';
|
||||
import { IDialog } from '../dialog/i-dialog';
|
||||
import { II18nTranslator } from '../i18n/i-i18n-translator';
|
||||
import { answer } from '../ipc/annotations/answer';
|
||||
import { INhentaiApi } from './i-nhentai-api';
|
||||
|
@ -12,15 +12,17 @@ export class NhentaiIpcController implements IIpcController {
|
|||
|
||||
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.translator = translator;
|
||||
this.dialog = dialog;
|
||||
}
|
||||
|
||||
@answer(IpcChannel.NHENTAI_SAVE_FAVORITES)
|
||||
public async nhentaiSaveFavorites(): Promise<void> {
|
||||
const result = await dialog.showOpenDialog({
|
||||
properties: ['openDirectory'],
|
||||
const result = await this.dialog.selectFolder({
|
||||
title: this.translator.t('Select torrent file save location'),
|
||||
});
|
||||
|
||||
|
@ -39,6 +41,7 @@ export class NhentaiIpcController implements IIpcController {
|
|||
public get(): NhentaiIpcController {
|
||||
const nhentaiApi: INhentaiApi = container.get('nhentai-api');
|
||||
const translator: II18nTranslator = container.get('i18n-translator');
|
||||
return new NhentaiIpcController(nhentaiApi, translator);
|
||||
const dialog: IDialog = container.get('dialog');
|
||||
return new NhentaiIpcController(nhentaiApi, translator, dialog);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue