save preferences immediately when changed

This commit is contained in:
Dan Flettre
2016-10-13 23:23:27 -05:00
committed by Borewit
parent 4ed4d8287f
commit f04017a068
2 changed files with 11 additions and 34 deletions

View File

@@ -16,45 +16,22 @@ module.exports = class PrefsController {
setup: function (cb) { setup: function (cb) {
// initialize preferences // initialize preferences
state.window.title = 'Preferences' state.window.title = 'Preferences'
state.unsaved = Object.assign(state.unsaved || {}, {
prefs: Object.assign({}, state.saved.prefs)
})
ipcRenderer.send('setAllowNav', false) ipcRenderer.send('setAllowNav', false)
cb() cb()
}, },
destroy: () => { destroy: () => {
ipcRenderer.send('setAllowNav', true) ipcRenderer.send('setAllowNav', true)
this.save()
} }
}) })
} }
// Updates a single property in the UNSAVED prefs // Updates a single property in the saved prefs
// For example: updatePreferences('foo.bar', 'baz') // For example: updatePreferences('isFileHandler', true)
// Call save() to save to config.json
update (property, value) { update (property, value) {
const path = property.split('.') if (property === 'isFileHandler') ipcRenderer.send('setDefaultFileHandler', value)
let obj = this.state.unsaved.prefs else if (property === 'startup') ipcRenderer.send('setStartup', value)
let i
for (i = 0; i < path.length - 1; i++) {
if (typeof obj[path[i]] === 'undefined') {
obj[path[i]] = {}
}
obj = obj[path[i]]
}
obj[path[i]] = value
}
// All unsaved prefs take effect atomically, and are saved to config.json this.state.saved.prefs[property] = value
save () {
const state = this.state
if (state.unsaved.prefs.isFileHandler !== state.saved.prefs.isFileHandler) {
ipcRenderer.send('setDefaultFileHandler', state.unsaved.prefs.isFileHandler)
}
if (state.unsaved.prefs.startup !== state.saved.prefs.startup) {
ipcRenderer.send('setStartup', state.unsaved.prefs.startup)
}
state.saved.prefs = Object.assign(state.saved.prefs || {}, state.unsaved.prefs)
dispatch('stateSaveImmediate') dispatch('stateSaveImmediate')
dispatch('checkDownloadPath') dispatch('checkDownloadPath')
} }

View File

@@ -37,7 +37,7 @@ class PreferencesPage extends React.Component {
}} }}
onChange={this.handleDownloadPathChange} onChange={this.handleDownloadPathChange}
title='Download location' title='Download location'
value={this.props.state.unsaved.prefs.downloadPath} /> value={this.props.state.saved.prefs.downloadPath} />
</Preference> </Preference>
) )
} }
@@ -51,7 +51,7 @@ class PreferencesPage extends React.Component {
<Preference> <Preference>
<Checkbox <Checkbox
className='control' className='control'
checked={!this.props.state.unsaved.prefs.openExternalPlayer} checked={!this.props.state.saved.prefs.openExternalPlayer}
label={'Play torrent media files using WebTorrent'} label={'Play torrent media files using WebTorrent'}
onCheck={this.handleOpenExternalPlayerChange} /> onCheck={this.handleOpenExternalPlayerChange} />
</Preference> </Preference>
@@ -81,10 +81,10 @@ class PreferencesPage extends React.Component {
} }
externalPlayerPathSelector () { externalPlayerPathSelector () {
const playerPath = this.props.state.unsaved.prefs.externalPlayerPath const playerPath = this.props.state.saved.prefs.externalPlayerPath
const playerName = this.props.state.getExternalPlayerName() const playerName = this.props.state.getExternalPlayerName()
const description = this.props.state.unsaved.prefs.openExternalPlayer const description = this.props.state.saved.prefs.openExternalPlayer
? `Torrent media files will always play in ${playerName}.` ? `Torrent media files will always play in ${playerName}.`
: `Torrent media files will play in ${playerName} if WebTorrent cannot play them.` : `Torrent media files will play in ${playerName} if WebTorrent cannot play them.`
@@ -162,7 +162,7 @@ class PreferencesPage extends React.Component {
} }
setDefaultAppButton () { setDefaultAppButton () {
const isFileHandler = this.props.state.unsaved.prefs.isFileHandler const isFileHandler = this.props.state.saved.prefs.isFileHandler
if (isFileHandler) { if (isFileHandler) {
return ( return (
<Preference> <Preference>
@@ -195,7 +195,7 @@ class PreferencesPage extends React.Component {
<Preference> <Preference>
<Checkbox <Checkbox
className='control' className='control'
checked={this.props.state.unsaved.prefs.startup} checked={this.props.state.saved.prefs.startup}
label={'Open WebTorrent on startup.'} label={'Open WebTorrent on startup.'}
onCheck={this.handleStartupChange} onCheck={this.handleStartupChange}
/> />