Skip to content

Commit

Permalink
Show user that new version of the app is available
Browse files Browse the repository at this point in the history
  • Loading branch information
edanchenkov committed Apr 1, 2017
1 parent 93142a6 commit 05713f8
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 129 deletions.
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
.align-right {
text-align: right;
}

#pref-btn > span > .update-available {
color: #ff0057;
}
</style>
</head>

Expand Down Expand Up @@ -111,10 +115,10 @@
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
Expand Down
285 changes: 159 additions & 126 deletions src/ui-controls.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,123 @@
exports.init = function (wv, controls) {
var remote = require('electron').remote;
var Menu = remote.Menu;
var MenuItem = remote.MenuItem;
var BrowserWindow = remote.BrowserWindow;
var app = remote.app;
var shell = remote.shell;
var ipcRenderer = require('electron').ipcRenderer;

var remote = require('electron').remote;
var Menu = remote.Menu;
var MenuItem = remote.MenuItem;
var BrowserWindow = remote.BrowserWindow;
var app = remote.app;
var shell = remote.shell;
var ipcRenderer = require('electron').ipcRenderer;
var urlHandler = require('./urlHandler.js');
var wv = window.wv;

var urlHandler = require('./urlHandler.js');
var clickHandler = function (name, menu) {
switch (name) {
case ('backButton'):
if (wv.canGoBack()) {
wv.goBack();
}
break;
case ('refreshButton'):
wv.reload();
break;
case ('forwardButton'):
if (wv.canGoForward) {
wv.goForward();
}
break;
case ('preferenceButton'):
menu.popup(remote.getCurrentWindow());
break;
}
};

var menu = new Menu();
var dynamicLabel = 'Check for Updates';

var hideAndPause = function () {
ipcRenderer.send('toggleWindow');
wv.send('pause');
};

var menuItems = [
{
label : 'Open in browser',
click : function () {
hideAndPause();
shell.openExternal(urlHandler.getCurrentURL());
},
role : 'help'
var defaultMenuItems = [
{
label : 'Open in browser',
click : function () {
hideAndPause();
shell.openExternal(urlHandler.getCurrentURL());
},
{
type : 'separator'
},
// {
// label : 'Video controls',
// role : 'help',
// submenu: [
// {
// label: 'Play and Pause (>||)',
// enabled : false
// },
// {
// label: 'Go 5 seconds forward (>>)',
// enabled : false
// },
// {
// label: 'Go 5 seconds backward (<<)',
// enabled : false
// }
// ]
// },
{
label : 'Video controls',
click : function () {
var win = new BrowserWindow({
// width : 800,
// height : 600,
frame : true
});
role : 'help'
},
{
type : 'separator'
},
{
label : 'Video controls',
click : function () {
var win = new BrowserWindow({
// width : 800,
// height : 600,
frame : true
});

hideAndPause();
hideAndPause();

var path = app.getAppPath();
win.loadURL('file://' + path + '/views/preferences.html#media-keys');
win.show()
},
role : 'help'
var path = app.getAppPath();
win.loadURL('file://' + path + '/views/preferences.html#media-keys');
win.show()
},
{
label : 'Preferences',
click : function () {
var win = new BrowserWindow({
// width : 800,
// height : 600,
frame : true
});
role : 'help'
},
{
label : 'Preferences',
click : function () {
var win = new BrowserWindow({
// width : 800,
// height : 600,
frame : true
});

hideAndPause();
var path = app.getAppPath();
win.loadURL('file://' + path + '/views/preferences.html');
win.show()
},
role : 'help'
hideAndPause();
var path = app.getAppPath();
win.loadURL('file://' + path + '/views/preferences.html');
win.show()
},
{
label : 'Check for Updates',
click : function () {
hideAndPause();
shell.openExternal('https://github.com/edanchenkov/MenuTube/releases');
},
role : 'help'
role : 'help'
},
{
label : dynamicLabel,
click : function () {
hideAndPause();
shell.openExternal('https://edanchenkov.github.io/MenuTube/');
},
{
type : 'separator'
role : 'help'
},
{
type : 'separator'
},
{
label : 'Reload',
click : function () {
if (typeof window !== 'undefined' &&
typeof window.location !== 'undefined' &&
typeof window.location.reload == 'function') {
window.location.reload();
}
},
{
label : 'Reload',
click : function () {
if (typeof window !== 'undefined' &&
typeof window.location !== 'undefined' &&
typeof window.location.reload == 'function') {
window.location.reload();
}
},
role : 'help',
accelerator : 'Cmd+R'
role : 'help',
accelerator : 'Cmd+R'
},
{
label : 'Quit',
click : function () {
app.quit();
},
{
label : 'Quit',
click : function () {
app.quit();
},
role : 'help',
accelerator : 'Cmd+Q'
}
];
role : 'help',
accelerator : 'Cmd+Q'
}
];

var hideAndPause = function () {
ipcRenderer.send('toggleWindow');
wv.send('pause');
};

var buildMenu = function (menu, menuItems) {

menu.clear();

for (var i = 0; i < menuItems.length; i++) {
var mi = menuItems[i];
Expand All @@ -126,33 +132,60 @@ exports.init = function (wv, controls) {
})
);
}
};

var clickHandler = function (name, e) {
switch (name) {
case ('backButton'):
if (wv.canGoBack()) {
wv.goBack();
}
break;
case ('refreshButton'):
wv.reload();
break;
case ('forwardButton'):
if (wv.canGoForward) {
wv.goForward();
}
break;
case ('preferenceButton'):
menu.popup(remote.getCurrentWindow());
break;
}
};

/*
* This should not be here, but lets make it simple for now
* TODO: Time outs and retries should be handled where this function is called, not within the function
* */
var attempts = 5;
var checkForUpdate = function (menu, controls) {
if (attempts > 0) {
attempts--;
setTimeout(function () {
fetch('https://api.github.com/repos/edanchenkov/MenuTube/releases/latest').then(function (res) {
res.json().then(function (data) {
if (typeof data !== 'undefined' && data.hasOwnProperty('tag_name')) {


if (data.tag_name !== remote.app.getVersion()) {
var menuItems = defaultMenuItems.map(function (mi) {
/*
* Should check against something else probably, not label
* */
if (mi.label === dynamicLabel) {
mi.label = '(!) New version is available';
}
return mi;
});

var prefIcon = controls.preferenceButton.querySelector('i.fa');

prefIcon.classList.remove('fa-bars');
prefIcon.classList.add('fa-exclamation-circle', 'update-available');

buildMenu(menu, menuItems);
}
}
});
}, checkForUpdate.bind(this, menu));
}, 2000);
}
};


exports.init = function (wv, controls) {
var menu = new Menu();
buildMenu(menu, defaultMenuItems);
checkForUpdate(menu, controls);

for (var c in controls) {
var el = controls[c];
if (el && typeof el.addEventListener === 'function') {
el.addEventListener('click', clickHandler.bind(el, c), false);
if (controls.hasOwnProperty(c)) {
var el = controls[c];
if (el && typeof el.addEventListener === 'function') {
el.addEventListener('click', clickHandler.bind(el, c, menu), false);
}
}
}
}
;
};

0 comments on commit 05713f8

Please sign in to comment.