Refactor main.js: prefs controller

This commit is contained in:
DC
2016-06-30 17:44:15 -07:00
parent c3a27dbebe
commit 4319ef2853
2 changed files with 56 additions and 43 deletions

View File

@@ -0,0 +1,51 @@
const State = require('../lib/state')
// Controls the Preferences screen
module.exports = class PrefsController {
constructor (state, config) {
this.state = state
this.config = config
}
// Goes to the Preferences screen
show () {
var state = this.state
state.location.go({
url: 'preferences',
onbeforeload: function (cb) {
// initialize preferences
state.window.title = 'Preferences'
state.unsaved = Object.assign(state.unsaved || {}, {prefs: state.saved.prefs || {}})
cb()
},
onbeforeunload: (cb) => {
// save state after preferences
this.save()
state.window.title = this.config.APP_WINDOW_TITLE
cb()
}
})
}
// Updates a single property in the UNSAVED prefs
// For example: updatePreferences("foo.bar", "baz")
// Call savePreferences to save to config.json
update (property, value) {
var path = property.split('.')
var key = this.state.unsaved.prefs
for (var i = 0; i < path.length - 1; i++) {
if (typeof key[path[i]] === 'undefined') {
key[path[i]] = {}
}
key = key[path[i]]
}
key[path[i]] = value
}
// All unsaved prefs take effect atomically, and are saved to config.json
save () {
var state = this.state
state.saved.prefs = Object.assign(state.saved.prefs || {}, state.unsaved.prefs)
State.save(state)
}
}

View File

@@ -25,6 +25,7 @@ const TorrentSummary = require('./lib/torrent-summary')
const MediaController = require('./controllers/media-controller')
const UpdateController = require('./controllers/update-controller')
const PrefsController = require('./controllers/prefs-controller')
// Yo-yo pattern: state object lives here and percolates down thru all the views.
// Events come back up from the views via dispatch(...)
@@ -57,7 +58,8 @@ function onState (err, _state) {
// Create controllers
controllers = {
media: new MediaController(state),
update: new UpdateController(state)
update: new UpdateController(state),
prefs: new PrefsController(state, config)
}
// Add first page to location history
@@ -289,10 +291,10 @@ function dispatch (action, ...args) {
state.modal = null
}
if (action === 'preferences') {
goToPreferences()
controllers.prefs.show()
}
if (action === 'updatePreferences') {
updatePreferences(args[0] /* key */, args[1] /* value */)
controllers.prefs.update(args[0] /* key */, args[1] /* value */)
}
if (action === 'updateAvailable') {
controllers.update.updateAvailable(args[0] /* version */)
@@ -479,28 +481,6 @@ function resumeTorrents () {
.forEach((torrentSummary) => startTorrentingSummary(torrentSummary))
}
// Updates a single property in the UNSAVED prefs
// For example: updatePreferences("foo.bar", "baz")
// Call savePreferences to save to config.json
function updatePreferences (property, value) {
var path = property.split('.')
var key = state.unsaved.prefs
for (var i = 0; i < path.length - 1; i++) {
if (typeof key[path[i]] === 'undefined') {
key[path[i]] = {}
}
key = key[path[i]]
}
key[path[i]] = value
}
// All unsaved prefs take effect atomically, and are saved to config.json
function savePreferences () {
state.saved.prefs = Object.assign(state.saved.prefs || {}, state.unsaved.prefs)
State.save(state)
update()
}
// Called when the user adds files (.torrent, files to seed, subtitles) to the app
// via any method (drag-drop, drag to app icon, command line)
function onOpen (files) {
@@ -952,24 +932,6 @@ function playFile (infoHash, index) {
})
}
function goToPreferences () {
state.location.go({
url: 'preferences',
onbeforeload: function (cb) {
// initialize preferences
state.window.title = 'Preferences'
state.unsaved = Object.assign(state.unsaved || {}, {prefs: state.saved.prefs || {}})
cb()
},
onbeforeunload: function (cb) {
// save state after preferences
savePreferences()
state.window.title = config.APP_WINDOW_TITLE
cb()
}
})
}
// Opens the video player to a specific torrent
function openPlayer (infoHash, index, cb) {
var torrentSummary = getTorrentSummary(infoHash)