Refactor main.js: prefs controller
This commit is contained in:
51
renderer/controllers/prefs-controller.js
Normal file
51
renderer/controllers/prefs-controller.js
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ const TorrentSummary = require('./lib/torrent-summary')
|
|||||||
|
|
||||||
const MediaController = require('./controllers/media-controller')
|
const MediaController = require('./controllers/media-controller')
|
||||||
const UpdateController = require('./controllers/update-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.
|
// Yo-yo pattern: state object lives here and percolates down thru all the views.
|
||||||
// Events come back up from the views via dispatch(...)
|
// Events come back up from the views via dispatch(...)
|
||||||
@@ -57,7 +58,8 @@ function onState (err, _state) {
|
|||||||
// Create controllers
|
// Create controllers
|
||||||
controllers = {
|
controllers = {
|
||||||
media: new MediaController(state),
|
media: new MediaController(state),
|
||||||
update: new UpdateController(state)
|
update: new UpdateController(state),
|
||||||
|
prefs: new PrefsController(state, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add first page to location history
|
// Add first page to location history
|
||||||
@@ -289,10 +291,10 @@ function dispatch (action, ...args) {
|
|||||||
state.modal = null
|
state.modal = null
|
||||||
}
|
}
|
||||||
if (action === 'preferences') {
|
if (action === 'preferences') {
|
||||||
goToPreferences()
|
controllers.prefs.show()
|
||||||
}
|
}
|
||||||
if (action === 'updatePreferences') {
|
if (action === 'updatePreferences') {
|
||||||
updatePreferences(args[0] /* key */, args[1] /* value */)
|
controllers.prefs.update(args[0] /* key */, args[1] /* value */)
|
||||||
}
|
}
|
||||||
if (action === 'updateAvailable') {
|
if (action === 'updateAvailable') {
|
||||||
controllers.update.updateAvailable(args[0] /* version */)
|
controllers.update.updateAvailable(args[0] /* version */)
|
||||||
@@ -479,28 +481,6 @@ function resumeTorrents () {
|
|||||||
.forEach((torrentSummary) => startTorrentingSummary(torrentSummary))
|
.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
|
// 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)
|
// via any method (drag-drop, drag to app icon, command line)
|
||||||
function onOpen (files) {
|
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
|
// Opens the video player to a specific torrent
|
||||||
function openPlayer (infoHash, index, cb) {
|
function openPlayer (infoHash, index, cb) {
|
||||||
var torrentSummary = getTorrentSummary(infoHash)
|
var torrentSummary = getTorrentSummary(infoHash)
|
||||||
|
|||||||
Reference in New Issue
Block a user