1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/Components/Shared/MPDServerController.swift
Daniel Barber b581f297c9
Refactor connection logic
The app would crash when connection settings were changed. This
refactors the connection logic to be consistent with the rest of the
mpdclient command structure. This ultimately fixes the bug.
2020-01-20 13:35:59 -05:00

45 lines
873 B
Swift

//
// MPDServerController.swift
// Persephone
//
// Created by Daniel Barber on 2019/4/28.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Foundation
import ReSwift
class MPDServerController {
init() {
App.mpdClient = MPDClient(withDelegate: App.mpdServerDelegate)
App.store.subscribe(self) {
$0.select { $0.preferencesState.mpdServer }
}
}
func connect() {
let mpdServer = App.store.state.preferencesState.mpdServer
App.mpdClient.connect(
host: mpdServer.hostOrDefault,
port: mpdServer.portOrDefault
)
}
func disconnect() {
App.mpdClient.disconnect()
}
}
extension MPDServerController: StoreSubscriber {
typealias StoreSubscriberStateType = MPDServer
func newState(state: MPDServer) {
guard App.mpdClient != nil else { return }
disconnect()
connect()
}
}