import Foundation /// The episode the player last had and where it got to, kept across launches, so "Resume iPX" /// (#427) and the widget's button carry on with it when the app was closed and no page is loaded /// to say what to play. enum LastPlayed { private static let itemKey = "ipx.last.item" private static let positionKey = "ipx.last.position" static var defaults = UserDefaults.standard static var item: Playback.Item? { get { defaults.data(forKey: itemKey).flatMap { try? JSONDecoder().decode(Playback.Item.self, from: $0) } } set { // A different episode starts from where the page put it, not the last one's place. if newValue != item { defaults.removeObject(forKey: positionKey) } defaults.set(newValue.flatMap { try? JSONEncoder().encode($0) }, forKey: itemKey) } } static var position: Double? { get { defaults.object(forKey: positionKey) as? Double } set { defaults.set(newValue, forKey: positionKey) } } }