Skip to content

Commit

Permalink
auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
hilanmiao committed May 8, 2019
1 parent 37a54ea commit b4d810a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PocketBook",
"version": "0.0.2",
"version": "0.0.1",
"author": "hilanmiao <hilanmiao@126.com>",
"description": "An electron-vue project",
"license": "MIT",
Expand Down
6 changes: 4 additions & 2 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function autoUpdate() {
// 当更新出现错误时触发
autoUpdater.on('error', (err) => {
// sendUpdateMessage('error')
sendUpdateMessage({action: 'error'})
sendUpdateMessage({action: 'error', errorInfo: err})
})

// 当开始检查更新的时候触发
Expand All @@ -242,7 +242,7 @@ function autoUpdate() {
// 当没有可用更新的时候触发
autoUpdater.on('update-not-available', (info) => {
// sendUpdateMessage('updateNotAva')
sendUpdateMessage({action: 'updateNotAva'})
sendUpdateMessage({action: 'updateNotAva', updateInfo: info})
})

// 更新下载进度事件
Expand All @@ -258,6 +258,8 @@ function autoUpdate() {
* updateUrl String - 更新地址
*/
autoUpdater.on('update-downloaded', (info) => {
// 下载太快可能无法触发downloadProgress事件,所以手动通知一下
mainWindow.webContents.send('downloadProgress', {percent: 100})
// 可以手动选择是否立即退出并更新
ipcMain.on('isUpdateNow', (e, arg) => {
// some code here to handle event
Expand Down
48 changes: 28 additions & 20 deletions src/renderer/views/Notes/Notes.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
<template>
<v-layout row wrap>
<v-flex xs4>
<v-layout column wrap text-xs-center>
<v-layout column wrap text-xs-left>
<v-flex>
<v-btn color="info"
@click="checkForUpdate()">
Check for updates
<v-icon right>new_releases</v-icon>
</v-btn>
</v-flex>
<v-flex class="title" v-show="noNewVersion">
<v-icon>thumb_up</v-icon> Already the latest version
</v-flex>
<v-flex v-show="hasNewVersion">
<v-btn color="info"
:loading="downloading"
:disabled="downloading"
@click="downloadAndUpdate">
Download new version!
Download new version
<v-icon right>cloud_download</v-icon>
<template v-slot:loader>
<span>Loading...</span>
</template>
</v-btn>
<v-progress-circular
:size="100"
:width="15"
:rotate="-90"
:value="downloadPercent"
color="primary"
>
{{ downloadPercent }}
</v-progress-circular>
<v-dialog
v-model="downloading"
persistent
Expand Down Expand Up @@ -50,20 +64,9 @@
</v-card>
</v-dialog>
</v-flex>
<v-flex v-show="hasNewVersion">
<v-progress-circular
:size="100"
:width="15"
:rotate="-90"
:value="downloadPercent"
color="primary"
>
{{ downloadPercent }}
</v-progress-circular>
</v-flex>
<v-flex class="text-xs-left" v-show="showError">
<v-alert type="error" v-model="showError" transition="scale-transition">
update error
error info {{errorInfo}}
</v-alert>
</v-flex>
</v-layout>
Expand All @@ -89,15 +92,17 @@
dialogUpdateNow: false,
downloading: false,
hasNewVersion: false,
noNewVersion: false,
downloadPercent: 0,
showError: false,
info: '',
errorInfo: {},
versionInfoList: []
}),
destroyed() {
// 移除事件监听
ipcRenderer.removeAllListeners('updateMessage')
ipcRenderer.removeAllListeners('downloadProgress')
ipcRenderer.removeAllListeners('isUpdateNow')
},
mounted() {
this.versionInfoList = this.getVersionInfoList()
Expand Down Expand Up @@ -126,20 +131,20 @@
// 开始下载
ipcRenderer.send('downloadUpdate')
ipcRenderer.on('downloadProgress', (event, progressObj) => {
this.progress = JSON.stringify(progressObj)
// console.log(progressObj)
this.downloadPercent = progressObj.percent.toFixed(0) || 0
if(this.downloadPercent = 100) {
// if(this.downloadPercent === 100) { // 这样写为啥不好使呢?
if(progressObj.percent === 100) {
this.downloading = false
// 询问是否立即更新
this.dialogUpdateNow = true
}
})
},
updateNow() {
ipcRenderer.on('isUpdateNow', () => {
ipcRenderer.send('isUpdateNow')
})
// 立刻退出并更新
ipcRenderer.send('isUpdateNow')
},
checkForUpdate() {
// 开始检查
Expand All @@ -152,6 +157,9 @@
this.versionInfoList = this.getVersionInfoList()
} else if (obj.action === 'error') {
this.showError = true
this.errorInfo = obj.errorInfo
} else if(obj.action ==='updateNotAva') {
this.noNewVersion = true
} else {
// console.log(text)
}
Expand Down

0 comments on commit b4d810a

Please sign in to comment.