Skip to content

Commit 208b374

Browse files
authored
Merge pull request #12 from tkothe/on-off-switch
update dependencies and implement on/off toggle
2 parents 699e1bc + 30fba7f commit 208b374

File tree

6 files changed

+97
-48
lines changed

6 files changed

+97
-48
lines changed

app.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
right: 15px;
1414
bottom: 15px;
1515
}
16+
17+
.btn-sendnow {
18+
position: absolute;
19+
left: 15px;
20+
bottom: 15px;
21+
}

app.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
'use strict'
22

33
const React = require('react')
4-
const ipc = require('ipc')
5-
const shell = require('shell')
4+
const ReactDOM = require('react-dom')
5+
const {ipcRenderer} = require('electron')
6+
const {shell} = require('electron')
67
const url = require('url')
78

89
const App = React.createClass({
10+
911
getInitialState () {
1012
return {
1113
slackWebhookUrl: window.localStorage.getItem('slackWebhookUrl'),
12-
listenerName: window.localStorage.getItem('listenerName')
14+
listenerName: window.localStorage.getItem('listenerName'),
15+
autoSend: (window.localStorage.getItem('autoSend') === null || window.localStorage.getItem('autoSend') === 'true') // localstorage only knows strings
1316
}
1417
},
1518

1619
componentDidMount () {
17-
ipc.send('data', this.state)
20+
ipcRenderer.send('data', this.state)
1821
},
1922

2023
componentDidUpdate () {
21-
ipc.send('data', this.state)
24+
ipcRenderer.send('data', this.state)
2225
},
2326

2427
inputIsValid () {
@@ -28,6 +31,15 @@ const App = React.createClass({
2831
render () {
2932
const self = this
3033

34+
const onlyIfChecked = !this.state.autoSend
35+
? React.DOM.button({
36+
className: 'btn btn-default btn-sendnow',
37+
onClick: function () {
38+
ipcRenderer.send('sendnow')
39+
}
40+
}, 'Send Now')
41+
: null
42+
3143
return (
3244
React.DOM.main({
3345
className: 'container'
@@ -67,25 +79,35 @@ const App = React.createClass({
6779
window.localStorage.setItem('listenerName', e.target.value)
6880
}
6981
}),
82+
React.DOM.input({
83+
className: 'checkbox',
84+
id: 'autosend-checkbox',
85+
type: 'checkbox',
86+
defaultChecked: self.state.autoSend,
87+
onChange: function (e) {
88+
self.setState({ autoSend: e.target.checked })
89+
window.localStorage.setItem('autoSend', e.target.checked)
90+
}
91+
}),
92+
React.DOM.label({ htmlFor: 'autosend-checkbox' }, 'Send automatically'),
7093
this.inputIsValid() && React.DOM.div({ className: 'alert alert-success' }, '✔ Play music on iTunes!')
7194
]),
72-
95+
onlyIfChecked,
7396
React.DOM.button({
7497
className: 'btn btn-default btn-quit',
7598
onClick: function () {
76-
ipc.send('terminate')
99+
ipcRenderer.send('terminate')
77100
}
78101
}, 'Quit')
79102
])
80103
)
81104
}
82105
})
83106

84-
React.render(React.createFactory(App)(), document.body)
107+
ReactDOM.render(React.createFactory(App)(), document.getElementById('content'))
85108

86-
var remote = require('remote')
87-
var Menu = remote.require('menu')
88-
var MenuItem = remote.require('menu-item')
109+
const {remote} = require('electron')
110+
const {Menu, MenuItem} = remote
89111

90112
var menu = new Menu()
91113
menu.append(new MenuItem({

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<link rel="stylesheet" href="app.css">
77
</head>
88
<body>
9+
<div id="content"></div>
910
<script type="text/javascript" src="./app.js"></script>
1011
</body>
1112
</html>

index.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,40 @@ var detectCountry = require('./lib/detect-country.js')
88

99
var fetch = require('isomorphic-fetch')
1010

11-
module.exports = function run () {
12-
var prevTrack = {}
13-
itunes.on('playing', function (track) {
14-
if (isEqualTrack(track, prevTrack)) return
15-
16-
detectCountry().then(function (country) {
17-
findMusic([ track.name, track.artist, track.album ], {
18-
country: country
19-
}).then(function (music) {
20-
if (music) return music
21-
22-
return findMusic([track.name, track.artist], { country: country })
23-
}).then(function (music) {
24-
notify(track, music)
25-
}).catch(function (err) {
26-
console.error(err.stack)
27-
})
11+
module.exports = {
12+
listen: function run () {
13+
var prevTrack = {}
14+
itunes.on('playing', function (track) {
15+
if (isEqualTrack(track, prevTrack)) return
16+
17+
if (process.env.AUTO_SEND === 'false') {
18+
return
19+
}
20+
21+
findTrackAndNotify(track)
22+
prevTrack = track
23+
}) },
24+
25+
sendNow: function sendNow () {
26+
itunes.currentTrack(function (track) {
27+
findTrackAndNotify(track)
2828
})
29+
}
30+
}
2931

30-
prevTrack = track
32+
function findTrackAndNotify (track) {
33+
detectCountry().then(function (country) {
34+
findMusic([ track.name, track.artist, track.album ], {
35+
country: country
36+
}).then(function (music) {
37+
if (music) return music
38+
39+
return findMusic([track.name, track.artist], { country: country })
40+
}).then(function (music) {
41+
notify(track, music)
42+
}).catch(function (err) {
43+
console.error(err.stack)
44+
})
3145
})
3246
}
3347

@@ -68,7 +82,6 @@ function messageForHipchat (track, music) {
6882

6983
function notify (track, music) {
7084
console.log('🎵 ' + trackToString(track))
71-
7285
if (process.env.HIPCHAT_TOKEN) {
7386
postToHipchat(messageForHipchat(track, music))
7487
}

main.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1+
const path = require('path')
2+
13
var menubar = require('menubar')
24

3-
var ipc = require('ipc')
4-
var run = require('./index.js')
5+
var ipcMain = require('electron').ipcMain
6+
var itunes = require('./index.js')
57

6-
var mb = menubar({ preloadWindow: true })
8+
var mb = menubar({ preloadWindow: true, icon: path.join(__dirname, '/Icon.png') })
79

810
mb.on('ready', function ready () {
9-
require('electron-template-menu')()
10-
11-
ipc.on('data', function (event, data) {
12-
console.log(data)
11+
ipcMain.on('data', function (event, data) {
1312
update(data)
1413
})
1514

16-
ipc.once('data', run)
15+
ipcMain.once('data', itunes.listen)
16+
})
17+
18+
ipcMain.on('terminate', function terminate () {
19+
mb.app.quit()
1720
})
1821

19-
ipc.on('terminate', function terminate () {
20-
mb.app.terminate()
22+
ipcMain.on('sendnow', function sendnow () {
23+
itunes.sendNow()
2124
})
2225

2326
function update (state) {
2427
process.env.SLACK_WEBHOOK_URL = state.slackWebhookUrl
2528
process.env.LISTENER_NAME = state.listenerName
29+
process.env.AUTO_SEND = state.autoSend
2630
}

package.json

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
{
22
"name": "playing",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Send what you're playing on iTunes to Slack",
55
"main": "main.js",
66
"scripts": {
77
"test": "standard",
8-
"start": "node index.js",
9-
"build": "electron-packager ./ playing --out build/ --platform=darwin --arch=x64 --version=0.30.6 --icon=./Icon.icns --overwrite --prune --app-version \"$(node -pe \"require('./package.json').version\")\""
8+
"start": "electron ./",
9+
"build": "electron-packager ./ playing --out build/ --platform=darwin --arch=x64 --version=1.2.6 --icon=./Icon.icns --overwrite --prune --app-version \"$(node -pe \"require('./package.json').version\")\""
1010
},
1111
"repository": {
1212
"type": "git",
1313
"url": "git+https://github.com/uiureo/playing.git"
1414
},
1515
"author": "Kazato Sugimoto",
16+
"contributors": [{
17+
"name": "Till Kothe"
18+
}],
1619
"license": "ISC",
1720
"bugs": {
1821
"url": "https://github.com/uiureo/playing/issues"
1922
},
2023
"homepage": "https://github.com/uiureo/playing#readme",
2124
"dependencies": {
22-
"electron-template-menu": "^1.0.3",
2325
"es6-promise": "^3.0.2",
2426
"hipchatter": "^0.2.0",
2527
"iconv": "^2.1.10",
2628
"isomorphic-fetch": "^2.1.1",
27-
"menubar": "^2.1.2",
29+
"menubar": "^4.1.2",
2830
"once": "^1.3.2",
2931
"playback": "^0.2.0",
30-
"react": "^0.13.3"
32+
"react": "^0.14.8",
33+
"react-dom": "^0.14.7"
3134
},
3235
"devDependencies": {
33-
"electron-packager": "^5.0.2",
34-
"standard": "^5.1.0"
36+
"electron-packager": "^7.2.0",
37+
"standard": "^7.1.2"
3538
}
3639
}

0 commit comments

Comments
 (0)