Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(settings): refactor settings service #648

Merged
merged 2 commits into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 37 additions & 41 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

import 'reflect-metadata' // Required by TypoORM.
;('use strict')
import { app, protocol, BrowserWindow, ipcMain, shell, Menu } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
import { updateConnectionMessage } from '@/api/connection'
import { quitAndRenameLogger } from './utils/logger'
import db from './database/index'
import updateChecker from './main/updateChecker'
import getMenuTemplate from './main/getMenuTemplate'
import saveFile from './main/saveFile'
Expand All @@ -14,13 +13,14 @@ import newWindow from './main/newWindow'
import useConnection, { initOptionModel } from '@/database/useConnection'
import useServices from '@/database/useServices'

interface WindowSizeModel {
width: number
height: number
}

declare const __static: string

let theme: Theme = 'light'
let autoCheckUpdate: boolean = true
const windowSize = {
width: 1025,
height: 749,
}
const isDevelopment: boolean = process.env.NODE_ENV !== 'production'
const isMac: boolean = process.platform === 'darwin'

Expand All @@ -30,10 +30,6 @@ let win: BrowserWindow | null

let menu: Menu | null

const windowSize = db.get<WindowSizeModel>('windowSize')

const theme = db.get<Theme>('settings.currentTheme')

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }])

Expand Down Expand Up @@ -85,16 +81,18 @@ function beforeAppQuit() {
app.quit()
}

async function beforeWindowReady() {
await ConnectionInit({
doMigrations: true,
undoMigrations: false,
} as initOptionModel)
async function createWindow() {
// Init tables and connect to local database.
await ConnectionInit({ doMigrations: true } as initOptionModel)
const { settingService } = useServices()
await settingService.setSetting()
}

function createWindow() {
const setting = await settingService.getSetting()
if (setting) {
theme = setting.currentTheme
autoCheckUpdate = setting.autoCheck
windowSize.height = setting.height
windowSize.width = setting.width
}
// Create the browser window.
win = new BrowserWindow({
...windowSize,
Expand Down Expand Up @@ -133,8 +131,27 @@ function createWindow() {
win = null
beforeAppQuit()
})
handleIpcMessages()
if (autoCheckUpdate) {
updateChecker()
}
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS)
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
})

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
Expand All @@ -152,27 +169,6 @@ app.on('activate', () => {
}
})

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
await beforeWindowReady()
const autoCheckUpdate = db.get<boolean>('settings.autoCheck')
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
await installExtension(VUEJS_DEVTOOLS)
} catch (e) {
console.error('Vue Devtools failed to install:', e.toString())
}
}
createWindow()
handleIpcMessages()
if (autoCheckUpdate) {
updateChecker()
}
})

// Disabled create new window
app.on('web-contents-created', (event, contents) => {
// tslint:disable-next-line:no-shadowed-variable
Expand Down
12 changes: 12 additions & 0 deletions src/database/services/SettingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export default class SettingService {
) {}

public async setSetting() {
const data = await this.collectionRepository.find()
if (data.length) {
return
}
return await this.collectionRepository.insert({})
}

public async getSetting() {
const data = await this.collectionRepository.find()
if (!data.length) {
return
}
return data[0]
}
}
5 changes: 3 additions & 2 deletions src/database/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const useConnection = () => {
useContainer(Container)
sqlConnection = await createConnection(ORMConfig)
if (initOption.doMigrations) {
sqlConnection.runMigrations()
await sqlConnection.runMigrations()
} else if (initOption.undoMigrations) {
sqlConnection.undoLastMigration()
await sqlConnection.undoLastMigration()
}
return sqlConnection
}
async function ConnectionDestory() {
if (sqlConnection) {
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import 'reflect-metadata' // Required by TypoORM.
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import VueClipboard from 'vue-clipboard2'
import path from 'path'
import log4js from 'log4js'

import 'element-ui/lib/theme-chalk/index.css'
import ElementLocale from 'element-ui/lib/locale'

import App from './App.vue'
import router from './router/index'
import store from './store/index'
import VueI18n from 'vue-i18n'
import VueClipboard from 'vue-clipboard2'
import Lang from './lang'
import element from './utils/element'
import VueLog4js from './plugins/logPlugin/index'
import log4js from 'log4js'
import { getOrCreateLogDir } from './utils/logger'
import logConfig from './plugins/logPlugin/logConfig.json'
import path from 'path'
import useConnection, { initOptionModel } from './database/useConnection'

const { ConnectionInit } = useConnection()

// Init typeORM connection before Vue APP start, after this DI services are available.
ConnectionInit({ doMigrations: false, undoMigrations: false } as initOptionModel).then(() => {
const LOG_DIR = getOrCreateLogDir()
Expand Down