From a04827cc5683a2e1178a6723e83ea1004d52e9b9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 9 Jan 2022 16:05:05 +0100 Subject: [PATCH] Fix restoring queue --- Model/Player/PlayerModel.swift | 6 ++++-- Model/Player/PlayerQueue.swift | 17 +++++++++++++++++ Shared/Defaults.swift | 1 + Shared/EnvironmentValues.swift | 2 +- Shared/Navigation/ContentView.swift | 4 ++++ Shared/Search/SearchSuggestions.swift | 6 +++--- 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 1fe3923e..e4865bec 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -33,7 +33,7 @@ final class PlayerModel: ObservableObject { @Published var streamSelection: Stream? { didSet { rebuildTVMenu() } } @Published var queue = [PlayerQueueItem]() { didSet { Defaults[.queue] = queue } } - @Published var currentItem: PlayerQueueItem! { didSet { updateWindowTitle() } } + @Published var currentItem: PlayerQueueItem! { didSet { handleCurrentItemChange() } } @Published var historyVideos = [Video]() @Published var preservedTime: CMTime? @@ -804,10 +804,12 @@ final class PlayerModel: ObservableObject { } #endif - func updateWindowTitle() { + func handleCurrentItemChange() { #if os(macOS) Windows.player.window?.title = windowTitle #endif + + Defaults[.lastPlayed] = currentItem } #if os(macOS) diff --git a/Model/Player/PlayerQueue.swift b/Model/Player/PlayerQueue.swift index e19aabc0..647cb5a1 100644 --- a/Model/Player/PlayerQueue.swift +++ b/Model/Player/PlayerQueue.swift @@ -201,4 +201,21 @@ extension PlayerModel { func removeQueueItems() { queue.removeAll() } + + func restoreQueue() { + guard !accounts.current.isNil else { + return + } + + queue = ([Defaults[.lastPlayed]] + Defaults[.queue]).compactMap { $0 } + Defaults[.lastPlayed] = nil + + queue.forEach { item in + accounts.api.loadDetails(item) { newItem in + if let index = self.queue.firstIndex(where: { $0.id == item.id }) { + self.queue[index] = newItem + } + } + } + } } diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index 8f28a0d3..1f52fdf5 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -64,6 +64,7 @@ extension Defaults.Keys { static let recentlyOpened = Key<[RecentItem]>("recentlyOpened", default: []) static let queue = Key<[PlayerQueueItem]>("queue", default: []) + static let lastPlayed = Key("lastPlayed") static let saveHistory = Key("saveHistory", default: true) static let showWatchingProgress = Key("showWatchingProgress", default: true) diff --git a/Shared/EnvironmentValues.swift b/Shared/EnvironmentValues.swift index 6edd4a9f..c84564b8 100644 --- a/Shared/EnvironmentValues.swift +++ b/Shared/EnvironmentValues.swift @@ -30,7 +30,7 @@ private struct CurrentPlaylistID: EnvironmentKey { } private struct LoadMoreContentHandler: EnvironmentKey { - static let defaultValue: LoadMoreContentHandlerType = { } + static let defaultValue: LoadMoreContentHandlerType = {} } typealias LoadMoreContentHandlerType = () -> Void diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 19649272..dba8eba6 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -139,6 +139,10 @@ struct ContentView: View { player.accounts = accounts player.comments = comments + if !accounts.current.isNil { + player.restoreQueue() + } + if !Defaults[.saveRecents] { recents.clear() } diff --git a/Shared/Search/SearchSuggestions.swift b/Shared/Search/SearchSuggestions.swift index 02daa03f..77109e8d 100644 --- a/Shared/Search/SearchSuggestions.swift +++ b/Shared/Search/SearchSuggestions.swift @@ -32,9 +32,9 @@ struct SearchSuggestions: View { HStack(spacing: 0) { if suggestion.hasPrefix(state.suggestionsText.lowercased()) { Text(state.suggestionsText.lowercased()) - .lineLimit(1) - .layoutPriority(2) - .foregroundColor(.secondary) + .lineLimit(1) + .layoutPriority(2) + .foregroundColor(.secondary) } Text(querySuffix(suggestion))