Skip to content

Commit

Permalink
Merge pull request #146 from hql287/auto-check-for-update
Browse files Browse the repository at this point in the history
Added auto check for update
  • Loading branch information
hql287 authored Jan 12, 2018
2 parents 2ce71da + 83a48d3 commit d9d96c1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ function setInitialValues() {
language: 'en',
sound: 'default',
muted: false,
checkUpdate: 'daily',
lastCheck: Date.now(),
},
invoice: {
exportDir: os.homedir(),
Expand Down
15 changes: 15 additions & 0 deletions app/components/settings/General.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ class General extends Component {
</div>
</div>
</div>
<div className="row">
<div className="col-md-6">
<div className="pageItem">
<label className="itemLabel">Auto Check For Update</label>
<select
name="checkUpdate"
value={this.state.checkUpdate}
onChange={this.handleInputChange}
>
<option value="daily">Everyday (Recommended)</option>
<option value="weekly">Once A Week</option>
</select>
</div>
</div>
</div>
</div>
);
}
Expand Down
23 changes: 23 additions & 0 deletions main/auto-check-updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Libs
const { BrowserWindow, ipcMain } = require('electron');
const { autoUpdater } = require('electron-updater');
const { checkForUpdate } = require('./updater');
const appConfig = require('electron-settings');

const checkUpdate = appConfig.get('general.checkUpdate');
const lastCheck = appConfig.get('general.lastCheck');
const now = Date.now();

const daysSinceLastCheck = Math.round(
Math.abs(now - lastCheck) / (1000 * 60 * 60 * 24)
);

if (checkUpdate === 'daily' && daysSinceLastCheck >= 1) {
checkForUpdate();
appConfig.set('general.lastCheck', Date.now()); // Reset last check timeStamp
}

if (checkUpdate === 'weekly' && daysSinceLastCheck >= 7) {
checkForUpdate();
appConfig.set('general.lastCheck', Date.now()); // Reset last check timeStamp
}
24 changes: 18 additions & 6 deletions main/updater.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const { ipcMain } = require('electron');
// Libs
const { BrowserWindow, ipcMain } = require('electron');
const { autoUpdater } = require('electron-updater');
const appConfig = require('electron-settings');

// Disable Auto Downloading update;
autoUpdater.autoDownload = false;
let mainWindow;

ipcMain.on('check-for-updates', event => {
// Set mainWindow
mainWindow = event.sender;
// Set mainWindow
const mainWindowID = appConfig.get('mainWindowID');
const mainWindow = BrowserWindow.fromId(mainWindowID);

// Check for Updates
autoUpdater.checkForUpdates();
ipcMain.on('check-for-updates', event => {
checkForUpdate();
});

// Start Download
Expand Down Expand Up @@ -55,3 +58,12 @@ autoUpdater.on('download-progress', progressObj => {
autoUpdater.on('update-downloaded', info => {
mainWindow.send('update-downloaded', info);
});

// Helper
function checkForUpdate() {
autoUpdater.checkForUpdates();
}

module.exports = {
checkForUpdate,
};

0 comments on commit d9d96c1

Please sign in to comment.