yattee/Shared/Player/PlayerQueueRow.swift

103 lines
2.7 KiB
Swift
Raw Normal View History

import CoreMedia
import Defaults
import Foundation
import SwiftUI
struct PlayerQueueRow: View {
let item: PlayerQueueItem
var history = false
2022-07-11 17:44:49 +00:00
var autoplay = false
private var player = PlayerModel.shared
@Default(.closePiPOnNavigation) var closePiPOnNavigation
@FetchRequest private var watchRequest: FetchedResults<Watch>
2022-12-13 19:15:22 +00:00
init(item: PlayerQueueItem, history: Bool = false, autoplay: Bool = false) {
self.item = item
self.history = history
2022-07-11 17:44:49 +00:00
self.autoplay = autoplay
_watchRequest = FetchRequest<Watch>(
entity: Watch.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "videoID = %@", item.videoID)
)
}
var body: some View {
2022-07-10 17:51:46 +00:00
Button {
2022-11-15 11:22:27 +00:00
guard let video = item.video else { return }
guard video != player.currentVideo else {
player.show()
2022-11-13 17:52:15 +00:00
return
}
2022-11-15 11:22:27 +00:00
2022-11-13 17:52:15 +00:00
if video.localStreamIsFile, let url = video.localStream?.localURL {
2022-11-12 23:01:04 +00:00
URLBookmarkModel.shared.saveBookmark(url)
}
2022-07-10 17:51:46 +00:00
player.prepareCurrentItemForHistory()
2022-07-10 17:51:46 +00:00
player.avPlayerBackend.startPictureInPictureOnPlay = player.playingInPictureInPicture
2022-05-21 20:58:11 +00:00
2022-11-13 17:52:15 +00:00
player.videoBeingOpened = video
2022-11-11 17:50:20 +00:00
let playItem = {
if history {
player.playHistory(item, at: watchStoppedAt)
} else {
player.advanceToItem(item, at: watchStoppedAt)
}
2022-11-11 17:50:20 +00:00
if closePiPOnNavigation, player.playingInPictureInPicture {
player.closePiP()
}
2022-07-11 17:44:49 +00:00
2022-11-11 17:50:20 +00:00
if autoplay {
player.resetAutoplay()
}
2022-07-11 17:44:49 +00:00
}
2022-11-11 17:50:20 +00:00
#if os(iOS)
if player.presentingPlayer {
playItem()
} else {
player.onPresentPlayer.append(playItem)
}
#else
playItem()
#endif
player.show()
2022-07-10 17:51:46 +00:00
} label: {
2022-12-12 23:39:50 +00:00
VideoBanner(video: item.video, playbackTime: watchStoppedAt, videoDuration: watch?.videoDuration, watch: watch)
}
2022-11-10 21:51:30 +00:00
#if os(tvOS)
.buttonStyle(.card)
#else
2022-07-10 17:51:46 +00:00
.buttonStyle(.plain)
2022-11-10 21:51:30 +00:00
#endif
}
private var watch: Watch? {
watchRequest.first
}
private var watchStoppedAt: CMTime? {
guard let seconds = watch?.stoppedAt else {
return nil
}
return .secondsInDefaultTimescale(seconds)
}
}
2022-11-10 17:11:28 +00:00
struct PlayerQueueRow_Previews: PreviewProvider {
static var previews: some View {
PlayerQueueRow(item: .init(
.local(URL(string: "https://apple.com")!)
))
}
}