Fix using Watch history in player queue

This commit is contained in:
Arkadiusz Fal 2022-04-17 11:33:49 +02:00
parent 51b5c5709a
commit 52df6bf76e
5 changed files with 41 additions and 14 deletions

View File

@ -201,7 +201,7 @@ final class PlayerModel: ObservableObject {
backend.pause() backend.pause()
} }
func play(_ video: Video, at time: TimeInterval? = nil, inNavigationView: Bool = false) { func play(_ video: Video, at time: CMTime? = nil, inNavigationView: Bool = false) {
playNow(video, at: time) playNow(video, at: time)
guard !playingInPictureInPicture else { guard !playingInPictureInPicture else {

View File

@ -42,7 +42,7 @@ extension PlayerModel {
} }
} }
func playNow(_ video: Video, at time: TimeInterval? = nil) { func playNow(_ video: Video, at time: CMTime? = nil) {
if playingInPictureInPicture, closePiPOnNavigation { if playingInPictureInPicture, closePiPOnNavigation {
closePiP() closePiP()
} }
@ -54,7 +54,7 @@ extension PlayerModel {
} }
} }
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) { func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: CMTime? = nil) {
if !playingInPictureInPicture { if !playingInPictureInPicture {
backend.closeItem() backend.closeItem()
} }
@ -64,7 +64,7 @@ extension PlayerModel {
currentItem = item currentItem = item
if !time.isNil { if !time.isNil {
currentItem.playbackTime = .secondsInDefaultTimescale(time!) currentItem.playbackTime = time
} else if currentItem.playbackTime.isNil { } else if currentItem.playbackTime.isNil {
currentItem.playbackTime = .zero currentItem.playbackTime = .zero
} }
@ -105,7 +105,7 @@ extension PlayerModel {
} }
} }
func advanceToItem(_ newItem: PlayerQueueItem, at time: TimeInterval? = nil) { func advanceToItem(_ newItem: PlayerQueueItem, at time: CMTime? = nil) {
prepareCurrentItemForHistory() prepareCurrentItemForHistory()
remove(newItem) remove(newItem)
@ -177,8 +177,8 @@ extension PlayerModel {
} }
} }
func playHistory(_ item: PlayerQueueItem) { func playHistory(_ item: PlayerQueueItem, at time: CMTime? = nil) {
var time = item.playbackTime var time = time ?? item.playbackTime
if item.shouldRestartPlaying { if item.shouldRestartPlaying {
time = .zero time = .zero

View File

@ -1,3 +1,4 @@
import CoreMedia
import Defaults import Defaults
import Foundation import Foundation
import SwiftUI import SwiftUI
@ -11,15 +12,28 @@ struct PlayerQueueRow: View {
@Default(.closePiPOnNavigation) var closePiPOnNavigation @Default(.closePiPOnNavigation) var closePiPOnNavigation
@FetchRequest private var watchRequest: FetchedResults<Watch>
init(item: PlayerQueueItem, history: Bool = false, fullScreen: Binding<Bool> = .constant(false)) {
self.item = item
self.history = history
_fullScreen = fullScreen
_watchRequest = FetchRequest<Watch>(
entity: Watch.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "videoID = %@", item.videoID)
)
}
var body: some View { var body: some View {
Group { Group {
Button { Button {
player.prepareCurrentItemForHistory() player.prepareCurrentItemForHistory()
if history { if history {
player.playHistory(item) player.playHistory(item, at: watchStoppedAt)
} else { } else {
player.advanceToItem(item) player.advanceToItem(item, at: watchStoppedAt)
} }
if fullScreen { if fullScreen {
@ -32,9 +46,21 @@ struct PlayerQueueRow: View {
player.closePiP() player.closePiP()
} }
} label: { } label: {
VideoBanner(video: item.video, playbackTime: item.playbackTime, videoDuration: item.videoDuration) VideoBanner(video: item.video, playbackTime: watchStoppedAt, videoDuration: watch?.videoDuration)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
} }
} }
private var watch: Watch? {
watchRequest.first
}
private var watchStoppedAt: CMTime? {
guard let seconds = watch?.stoppedAt else {
return nil
}
return .secondsInDefaultTimescale(seconds)
}
} }

View File

@ -1,3 +1,4 @@
import CoreMedia
import Foundation import Foundation
struct VideoURLParser { struct VideoURLParser {
@ -11,7 +12,7 @@ struct VideoURLParser {
return queryItemValue("v") return queryItemValue("v")
} }
var time: TimeInterval? { var time: CMTime? {
guard let time = queryItemValue("t") else { guard let time = queryItemValue("t") else {
return nil return nil
} }
@ -24,13 +25,13 @@ struct VideoURLParser {
let seconds = TimeInterval(timeComponents["seconds"] ?? "0") let seconds = TimeInterval(timeComponents["seconds"] ?? "0")
else { else {
if let time = TimeInterval(time) { if let time = TimeInterval(time) {
return time return .secondsInDefaultTimescale(time)
} }
return nil return nil
} }
return seconds + (minutes * 60) + (hours * 60 * 60) return .secondsInDefaultTimescale(seconds + (minutes * 60) + (hours * 60 * 60))
} }
func queryItemValue(_ name: String) -> String? { func queryItemValue(_ name: String) -> String? {

View File

@ -111,7 +111,7 @@ struct VideoContextMenuView: View {
private var continueButton: some View { private var continueButton: some View {
Button { Button {
player.play(video, at: watch!.stoppedAt, inNavigationView: inNavigationView) player.play(video, at: .secondsInDefaultTimescale(watch!.stoppedAt), inNavigationView: inNavigationView)
} label: { } label: {
Label("Continue from \(watch!.stoppedAt.formattedAsPlaybackTime() ?? "where I left off")", systemImage: "playpause") Label("Continue from \(watch!.stoppedAt.formattedAsPlaybackTime() ?? "where I left off")", systemImage: "playpause")
} }