Skip to content

Commit

Permalink
✨ feat: schedules save local path
Browse files Browse the repository at this point in the history
🐞 fix: create schedule the form not reset
  • Loading branch information
xing403 committed Oct 18, 2023
1 parent 0a12f31 commit 0e3bd7f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schedule",
"version": "0.1.11",
"version": "0.1.12",
"private": true,
"packageManager": "[email protected]",
"description": "A Schedule Tools, You can create a scheduled task by using the cron string",
Expand Down
6 changes: 6 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<script setup lang="ts">
window.OS_API.readSchedule().then((res: any) => {
schedules.value = JSON.parse(res.schedules)
})
</script>

<template>
<RouterView />
</template>
22 changes: 14 additions & 8 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
// Electron entry file
import { Notification, app, ipcMain } from 'electron'
import { createGlobalShortcut, createTray, createWindow } from './electron'
import path from 'node:path'
import { app } from 'electron'
import {
createGlobalShortcut,
createIPC,
createTray,
createWindow,
} from './electron'

const windowMap: WindowMap = new Map()

app.setAppUserModelId('Schedule Notification')
app.setPath('userData', path.join(app.getPath('userData'), 'data'))

app.whenReady().then(() => {
const mainWindow = createWindow()
windowMap.set('main', mainWindow)
const tray = createTray(windowMap)
createGlobalShortcut(mainWindow)
process.argv[2] ? mainWindow.loadURL(process.argv[2]) : mainWindow.loadFile('index.html')

ipcMain.handle('notification', (_event, title: string, body: string) => {
new Notification({
title,
body,
}).show()
})
createIPC()
mainWindow.on('closed', () => {
windowMap.delete('main')
})

tray.on('click', () => {
mainWindow.isVisible()
? mainWindow.hide()
Expand Down
4 changes: 4 additions & 0 deletions src/composables/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export const isDark = useDark()
export const toggleDark = useToggle(isDark)

export const schedules = ref<Schedule[]>([])

watchArray(schedules, (newValue) => {
window.OS_API.saveSchedule(JSON.stringify(newValue))
}, { deep: true })
1 change: 1 addition & 0 deletions src/electron/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './main'
export * from './tray'
export * from './globalShortcut'
export * from './ipc'
26 changes: 26 additions & 0 deletions src/electron/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'node:fs'
import path from 'node:path'
import { Notification, app, ipcMain } from 'electron'

export function createIPC() {
ipcMain.handle('notification', (_event, title: string, body: string) => {
new Notification({
title,
body,
icon: path.relative(process.cwd(), path.join('dist/256x256.ico')),
}).show()
})

ipcMain.handle('read-schedule', () => {
return fs.existsSync(path.join(app.getPath('userData'), 'schedule.json'))
? JSON.parse(fs.readFileSync(path.join(app.getPath('userData'), 'schedule.json'), 'utf-8'))
: { schedules: [] }
})

ipcMain.handle('save-schedule', (event, list: string) => {
fs.writeFileSync(
fs.openSync(path.join(app.getPath('userData'), 'schedule.json'), 'w'),
JSON.stringify({ schedules: list }, null, 4))
return true
})
}
5 changes: 2 additions & 3 deletions src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ function handleOpenUpdateSchedule(schedule: Schedule) {
function handleSaveUpdateSchedule() {
update_form.value && update_form.value.validate((valid: boolean) => {
if (valid) {
const index = schedules.value.findIndex((item: Schedule) => {
return item.id === schedule_form.value.id
})
const index = schedules.value.findIndex((item: Schedule) => item.id === schedule_form.value.id)
// clear old schedule setTimeOut task
schedules.value[index].status = false
Expand Down
12 changes: 11 additions & 1 deletion src/pages/schedule-add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ function handleAddSchedule() {
schedules.value.push(schedule)
ElMessage.success('添加成功')
insertFormRef.value && insertFormRef.value.resetFields()
stepActive.value = 0
cron.value = {
hour: [],
minute: [],
dayOfWeek: [],
dayOfMonth: [],
month: [],
second: [0],
}
}
})
}
Expand Down Expand Up @@ -239,7 +249,7 @@ function handleCloseDrawer() {
</div>
<template #footer>
<div flex="~ row gap-2" mt-10px justify-center>
<el-button v-if="stepActive > 0" type="info" plain @click="handleBackStep">
<el-button v-if="stepActive > 0 && stepActive <= 4" type="info" plain @click="handleBackStep">
上一步
</el-button>
<el-button v-if="stepActive < 4" type="primary" plain @click="handleNextStep">
Expand Down
2 changes: 2 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import { contextBridge, ipcRenderer } from 'electron'

contextBridge.exposeInMainWorld('OS_API', {
notification: (title: string, body: string) => ipcRenderer.invoke('notification', title, body),
readSchedule: () => ipcRenderer.invoke('read-schedule'),
saveSchedule: (schedule: string) => ipcRenderer.invoke('save-schedule', schedule),
})

0 comments on commit 0e3bd7f

Please sign in to comment.