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