Skip to content

Commit

Permalink
Merge pull request #12 from tkothe/on-off-switch
Browse files Browse the repository at this point in the history
update dependencies and implement on/off toggle
  • Loading branch information
uiur authored Aug 1, 2016
2 parents 699e1bc + 30fba7f commit 208b374
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 48 deletions.
6 changes: 6 additions & 0 deletions app.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@
right: 15px;
bottom: 15px;
}

.btn-sendnow {
position: absolute;
left: 15px;
bottom: 15px;
}
44 changes: 33 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
'use strict'

const React = require('react')
const ipc = require('ipc')
const shell = require('shell')
const ReactDOM = require('react-dom')
const {ipcRenderer} = require('electron')
const {shell} = require('electron')
const url = require('url')

const App = React.createClass({

getInitialState () {
return {
slackWebhookUrl: window.localStorage.getItem('slackWebhookUrl'),
listenerName: window.localStorage.getItem('listenerName')
listenerName: window.localStorage.getItem('listenerName'),
autoSend: (window.localStorage.getItem('autoSend') === null || window.localStorage.getItem('autoSend') === 'true') // localstorage only knows strings
}
},

componentDidMount () {
ipc.send('data', this.state)
ipcRenderer.send('data', this.state)
},

componentDidUpdate () {
ipc.send('data', this.state)
ipcRenderer.send('data', this.state)
},

inputIsValid () {
Expand All @@ -28,6 +31,15 @@ const App = React.createClass({
render () {
const self = this

const onlyIfChecked = !this.state.autoSend
? React.DOM.button({
className: 'btn btn-default btn-sendnow',
onClick: function () {
ipcRenderer.send('sendnow')
}
}, 'Send Now')
: null

return (
React.DOM.main({
className: 'container'
Expand Down Expand Up @@ -67,25 +79,35 @@ const App = React.createClass({
window.localStorage.setItem('listenerName', e.target.value)
}
}),
React.DOM.input({
className: 'checkbox',
id: 'autosend-checkbox',
type: 'checkbox',
defaultChecked: self.state.autoSend,
onChange: function (e) {
self.setState({ autoSend: e.target.checked })
window.localStorage.setItem('autoSend', e.target.checked)
}
}),
React.DOM.label({ htmlFor: 'autosend-checkbox' }, 'Send automatically'),
this.inputIsValid() && React.DOM.div({ className: 'alert alert-success' }, '✔ Play music on iTunes!')
]),

onlyIfChecked,
React.DOM.button({
className: 'btn btn-default btn-quit',
onClick: function () {
ipc.send('terminate')
ipcRenderer.send('terminate')
}
}, 'Quit')
])
)
}
})

React.render(React.createFactory(App)(), document.body)
ReactDOM.render(React.createFactory(App)(), document.getElementById('content'))

var remote = require('remote')
var Menu = remote.require('menu')
var MenuItem = remote.require('menu-item')
const {remote} = require('electron')
const {Menu, MenuItem} = remote

var menu = new Menu()
menu.append(new MenuItem({
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<link rel="stylesheet" href="app.css">
</head>
<body>
<div id="content"></div>
<script type="text/javascript" src="./app.js"></script>
</body>
</html>
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,40 @@ var detectCountry = require('./lib/detect-country.js')

var fetch = require('isomorphic-fetch')

module.exports = function run () {
var prevTrack = {}
itunes.on('playing', function (track) {
if (isEqualTrack(track, prevTrack)) return

detectCountry().then(function (country) {
findMusic([ track.name, track.artist, track.album ], {
country: country
}).then(function (music) {
if (music) return music

return findMusic([track.name, track.artist], { country: country })
}).then(function (music) {
notify(track, music)
}).catch(function (err) {
console.error(err.stack)
})
module.exports = {
listen: function run () {
var prevTrack = {}
itunes.on('playing', function (track) {
if (isEqualTrack(track, prevTrack)) return

if (process.env.AUTO_SEND === 'false') {
return
}

findTrackAndNotify(track)
prevTrack = track
}) },

sendNow: function sendNow () {
itunes.currentTrack(function (track) {
findTrackAndNotify(track)
})
}
}

prevTrack = track
function findTrackAndNotify (track) {
detectCountry().then(function (country) {
findMusic([ track.name, track.artist, track.album ], {
country: country
}).then(function (music) {
if (music) return music

return findMusic([track.name, track.artist], { country: country })
}).then(function (music) {
notify(track, music)
}).catch(function (err) {
console.error(err.stack)
})
})
}

Expand Down Expand Up @@ -68,7 +82,6 @@ function messageForHipchat (track, music) {

function notify (track, music) {
console.log('🎵 ' + trackToString(track))

if (process.env.HIPCHAT_TOKEN) {
postToHipchat(messageForHipchat(track, music))
}
Expand Down
24 changes: 14 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
const path = require('path')

var menubar = require('menubar')

var ipc = require('ipc')
var run = require('./index.js')
var ipcMain = require('electron').ipcMain
var itunes = require('./index.js')

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

mb.on('ready', function ready () {
require('electron-template-menu')()

ipc.on('data', function (event, data) {
console.log(data)
ipcMain.on('data', function (event, data) {
update(data)
})

ipc.once('data', run)
ipcMain.once('data', itunes.listen)
})

ipcMain.on('terminate', function terminate () {
mb.app.quit()
})

ipc.on('terminate', function terminate () {
mb.app.terminate()
ipcMain.on('sendnow', function sendnow () {
itunes.sendNow()
})

function update (state) {
process.env.SLACK_WEBHOOK_URL = state.slackWebhookUrl
process.env.LISTENER_NAME = state.listenerName
process.env.AUTO_SEND = state.autoSend
}
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
{
"name": "playing",
"version": "1.1.0",
"version": "1.1.1",
"description": "Send what you're playing on iTunes to Slack",
"main": "main.js",
"scripts": {
"test": "standard",
"start": "node index.js",
"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\")\""
"start": "electron ./",
"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\")\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/uiureo/playing.git"
},
"author": "Kazato Sugimoto",
"contributors": [{
"name": "Till Kothe"
}],
"license": "ISC",
"bugs": {
"url": "https://github.com/uiureo/playing/issues"
},
"homepage": "https://github.com/uiureo/playing#readme",
"dependencies": {
"electron-template-menu": "^1.0.3",
"es6-promise": "^3.0.2",
"hipchatter": "^0.2.0",
"iconv": "^2.1.10",
"isomorphic-fetch": "^2.1.1",
"menubar": "^2.1.2",
"menubar": "^4.1.2",
"once": "^1.3.2",
"playback": "^0.2.0",
"react": "^0.13.3"
"react": "^0.14.8",
"react-dom": "^0.14.7"
},
"devDependencies": {
"electron-packager": "^5.0.2",
"standard": "^5.1.0"
"electron-packager": "^7.2.0",
"standard": "^7.1.2"
}
}

0 comments on commit 208b374

Please sign in to comment.