yattee/Shared/Videos/VideoCell.swift

514 lines
17 KiB
Swift
Raw Normal View History

2022-04-17 09:34:26 +00:00
import CoreMedia
2021-07-27 22:40:04 +00:00
import Defaults
import SDWebImageSwiftUI
2021-07-27 21:26:52 +00:00
import SwiftUI
struct VideoCell: View {
2022-12-11 16:19:21 +00:00
var id: String?
private var video: Video
private var watch: Watch?
2022-08-25 23:38:04 +00:00
@Environment(\.horizontalCells) private var horizontalCells
2022-05-29 18:26:56 +00:00
@Environment(\.inChannelView) private var inChannelView
2022-08-25 23:38:04 +00:00
@Environment(\.navigationStyle) private var navigationStyle
2021-07-27 22:40:04 +00:00
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
2021-11-04 23:25:51 +00:00
@Default(.channelOnThumbnail) private var channelOnThumbnail
@Default(.timeOnThumbnail) private var timeOnThumbnail
@Default(.roundedThumbnails) private var roundedThumbnails
@Default(.saveHistory) private var saveHistory
@Default(.showWatchingProgress) private var showWatchingProgress
@Default(.watchedVideoStyle) private var watchedVideoStyle
@Default(.watchedVideoBadgeColor) private var watchedVideoBadgeColor
@Default(.watchedVideoPlayNowBehavior) private var watchedVideoPlayNowBehavior
2021-11-04 23:25:51 +00:00
2022-08-31 22:00:28 +00:00
private var navigation: NavigationModel { .shared }
private var player: PlayerModel { .shared }
init(id: String? = nil, video: Video, watch: Watch? = nil) {
2022-12-11 16:19:21 +00:00
self.id = id
self.video = video
self.watch = watch
}
2021-10-28 17:14:55 +00:00
var body: some View {
2022-08-13 14:46:45 +00:00
Button(action: playAction) {
content
2022-08-25 23:38:04 +00:00
#if os(tvOS)
.frame(width: 580, height: channelOnThumbnail ? 470 : 500)
2022-08-25 23:38:04 +00:00
#endif
2021-07-27 22:40:04 +00:00
}
.opacity(contentOpacity)
2022-08-25 23:38:04 +00:00
#if os(tvOS)
.buttonStyle(.card)
#else
.buttonStyle(.plain)
#endif
.contentShape(RoundedRectangle(cornerRadius: thumbnailRoundingCornerRadius))
.contextMenu {
VideoContextMenuView(video: video)
}
2022-12-11 16:19:21 +00:00
.id(id ?? video.videoID)
2021-08-01 23:01:24 +00:00
}
2021-07-27 22:40:04 +00:00
private var thumbnailRoundingCornerRadius: Double {
#if os(tvOS)
return Double(12)
#else
return Double(roundedThumbnails ? 12 : 0)
#endif
}
private func playAction() {
2022-04-17 09:34:26 +00:00
DispatchQueue.main.async {
guard video.videoID != Video.fixtureID else {
return
}
2022-06-07 21:27:48 +00:00
if player.musicMode {
player.toggleMusicMode()
}
2022-04-17 09:34:26 +00:00
if watchingNow {
if !player.playingInPictureInPicture {
player.show()
}
2022-04-17 09:34:26 +00:00
if !playNowContinues {
player.backend.seek(to: .zero, seekType: .userInteracted)
2022-04-17 09:34:26 +00:00
}
2022-04-17 09:34:26 +00:00
player.play()
2022-04-17 09:34:26 +00:00
return
}
2022-04-17 09:34:26 +00:00
var playAt: CMTime?
if saveHistory,
playNowContinues,
2022-04-17 09:34:26 +00:00
!watch.isNil,
!watch!.finished
{
playAt = .secondsInDefaultTimescale(watch!.stoppedAt)
}
2022-05-21 20:58:11 +00:00
player.avPlayerBackend.startPictureInPictureOnPlay = player.playingInPictureInPicture
player.play(video, at: playAt)
2022-04-17 09:34:26 +00:00
}
}
private var playNowContinues: Bool {
watchedVideoPlayNowBehavior == .continue
}
private var finished: Bool {
watch?.finished ?? false
}
private var watchingNow: Bool {
player.currentVideo == video
}
private var content: some View {
VStack {
2021-09-26 22:28:42 +00:00
#if os(iOS)
if verticalSizeClass == .compact, !horizontalCells {
horizontalRow
.padding(.vertical, 4)
} else {
verticalRow
2021-09-26 22:28:42 +00:00
}
#else
verticalRow
#endif
}
#if os(macOS)
.background(Color.secondaryBackground)
#endif
}
private var contentOpacity: Double {
guard saveHistory,
!watch.isNil,
2023-04-22 20:44:59 +00:00
watchedVideoStyle.isDecreasingOpacity
else {
return 1
}
return watch!.finished ? 0.5 : 1
}
#if os(iOS)
private var horizontalRow: some View {
HStack(alignment: .top, spacing: 2) {
Section {
#if os(tvOS)
2021-11-05 20:53:43 +00:00
thumbnailImage
#else
thumbnail
#endif
}
.frame(maxWidth: 320)
2021-07-27 22:40:04 +00:00
VStack(alignment: .leading, spacing: 0) {
2022-11-11 18:19:48 +00:00
videoDetail(video.displayTitle, lineLimit: 5)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
2021-07-27 22:40:04 +00:00
HStack(spacing: Constants.channelDetailsStackSpacing) {
2022-12-13 23:07:32 +00:00
if !inChannelView,
2022-12-14 16:20:24 +00:00
let url = video.channel.thumbnailURLOrCached,
video != .fixture
2022-12-13 23:07:32 +00:00
{
ChannelLinkView(channel: video.channel) {
ThumbnailView(url: url)
.frame(width: Constants.channelThumbnailSize, height: Constants.channelThumbnailSize)
.clipShape(Circle())
}
2022-12-13 23:07:32 +00:00
}
2022-12-13 23:07:32 +00:00
if !channelOnThumbnail, !inChannelView {
ChannelLinkView(channel: video.channel) {
channelLabel(badge: false)
}
2022-12-13 23:07:32 +00:00
}
2021-11-04 23:25:51 +00:00
}
2021-07-27 22:40:04 +00:00
if additionalDetailsAvailable {
Spacer()
2021-09-18 20:36:42 +00:00
2021-11-04 23:25:51 +00:00
HStack(spacing: 15) {
if let date = video.publishedDate {
VStack {
Image(systemName: "calendar")
2021-11-04 23:25:51 +00:00
.frame(height: 15)
Text(date)
}
2021-09-18 20:36:42 +00:00
}
2021-07-27 22:40:04 +00:00
2021-10-22 23:04:03 +00:00
if video.views > 0 {
VStack {
Image(systemName: "eye")
2021-11-04 23:25:51 +00:00
.frame(height: 15)
Text(video.viewsCount!)
}
2021-09-18 20:36:42 +00:00
}
2021-11-04 23:25:51 +00:00
2022-12-04 11:07:06 +00:00
if !timeOnThumbnail, let time = videoDuration {
2021-11-04 23:25:51 +00:00
VStack {
Image(systemName: "clock")
.frame(height: 15)
Text(time)
}
}
2021-09-18 20:36:42 +00:00
}
.foregroundColor(.secondary)
2021-09-18 20:36:42 +00:00
}
}
.padding()
.frame(minHeight: 180)
2021-07-27 22:40:04 +00:00
#if os(tvOS)
2022-12-04 11:07:06 +00:00
if let time = videoDuration || video.live || video.upcoming {
2021-08-02 21:10:22 +00:00
Spacer()
VStack {
Spacer()
2022-12-04 11:07:06 +00:00
if let time = videoDuration {
HStack(spacing: 4) {
Image(systemName: "clock")
Text(time)
.fontWeight(.bold)
}
.foregroundColor(.secondary)
} else if video.live {
DetailBadge(text: "Live", style: .outstanding)
} else if video.upcoming {
DetailBadge(text: "Upcoming", style: .informational)
2021-08-02 21:10:22 +00:00
}
2021-08-01 23:01:24 +00:00
Spacer()
}
2021-10-22 23:04:03 +00:00
.lineLimit(1)
2021-08-02 21:10:22 +00:00
}
#endif
}
2021-07-27 22:40:04 +00:00
}
#endif
2021-07-27 22:40:04 +00:00
2022-12-04 11:07:06 +00:00
private var videoDuration: String? {
let length = video.length.isZero ? watch?.videoDuration : video.length
return length?.formattedAsPlaybackTime()
}
private var verticalRow: some View {
2021-09-18 20:36:42 +00:00
VStack(alignment: .leading, spacing: 0) {
2021-08-01 23:01:24 +00:00
thumbnail
2021-07-27 22:40:04 +00:00
2021-09-25 08:18:22 +00:00
VStack(alignment: .leading, spacing: 0) {
2021-11-04 23:25:51 +00:00
Group {
VStack(alignment: .leading, spacing: 0) {
2022-11-11 18:19:48 +00:00
videoDetail(video.displayTitle, lineLimit: 2)
2021-11-04 23:25:51 +00:00
#if os(tvOS)
.frame(minHeight: 60, alignment: .top)
#elseif os(macOS)
.frame(minHeight: 35, alignment: .top)
2021-11-04 23:25:51 +00:00
#else
.frame(minHeight: 43, alignment: .top)
2021-11-04 23:25:51 +00:00
#endif
if !channelOnThumbnail, !inChannelView {
ChannelLinkView(channel: video.channel) {
HStack(spacing: Constants.channelDetailsStackSpacing) {
2022-12-14 16:20:24 +00:00
if let url = video.channel.thumbnailURLOrCached,
video != .fixture
{
ThumbnailView(url: url)
.frame(width: Constants.channelThumbnailSize, height: Constants.channelThumbnailSize)
.clipShape(Circle())
}
channelLabel(badge: false)
}
}
2021-11-04 23:25:51 +00:00
}
}
2021-11-07 22:27:09 +00:00
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
2021-11-04 23:25:51 +00:00
}
2021-08-01 23:01:24 +00:00
#if os(tvOS)
2021-11-08 16:29:35 +00:00
.frame(minHeight: channelOnThumbnail ? 80 : 120, alignment: .top)
2021-08-02 21:10:22 +00:00
#elseif os(macOS)
.frame(minHeight: channelOnThumbnail ? 52 : 75, alignment: .top)
2021-08-02 21:10:22 +00:00
#else
.frame(minHeight: channelOnThumbnail ? 50 : 70, alignment: .top)
2021-08-01 23:01:24 +00:00
#endif
.padding(.bottom, 4)
2021-07-27 22:40:04 +00:00
2021-11-01 21:56:18 +00:00
HStack(spacing: 8) {
if channelOnThumbnail,
!inChannelView,
2022-12-14 16:20:24 +00:00
let url = video.channel.thumbnailURLOrCached,
video != .fixture
2022-12-13 23:07:32 +00:00
{
ChannelLinkView(channel: video.channel) {
ThumbnailView(url: url)
.frame(width: Constants.channelThumbnailSize, height: Constants.channelThumbnailSize)
.clipShape(Circle())
}
2022-12-13 23:07:32 +00:00
}
2021-11-01 21:56:18 +00:00
if let date = video.publishedDate {
2021-11-04 23:25:51 +00:00
HStack(spacing: 2) {
Text(date)
.allowsTightening(true)
}
2021-11-01 21:56:18 +00:00
}
2021-09-18 20:36:42 +00:00
2021-11-01 21:56:18 +00:00
if video.views > 0 {
2021-11-04 23:25:51 +00:00
HStack(spacing: 2) {
Image(systemName: "eye")
Text(video.viewsCount!)
}
}
2022-09-28 14:27:01 +00:00
if let time, !timeOnThumbnail {
2021-11-04 23:25:51 +00:00
Spacer()
HStack(spacing: 2) {
Text(time)
}
2021-09-13 20:41:16 +00:00
}
2021-07-27 22:40:04 +00:00
}
2021-11-04 23:25:51 +00:00
.lineLimit(1)
2021-11-01 21:56:18 +00:00
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, minHeight: 35, alignment: .topLeading)
2021-09-25 08:18:22 +00:00
#if os(tvOS)
.padding(.bottom, 10)
#endif
2021-07-27 22:40:04 +00:00
}
2021-09-18 20:36:42 +00:00
.padding(.top, 4)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .topLeading)
2021-08-01 23:01:24 +00:00
#if os(tvOS)
2022-08-25 23:38:04 +00:00
.padding(.horizontal, horizontalCells ? 10 : 20)
2021-08-01 23:01:24 +00:00
#endif
2021-07-27 22:40:04 +00:00
}
}
@ViewBuilder private func channelLabel(badge: Bool = true) -> some View {
if badge {
DetailBadge(text: video.author, style: .prominent)
.foregroundColor(.primary)
} else {
2023-04-22 08:55:36 +00:00
Text(verbatim: video.channel.name)
.fontWeight(.semibold)
.foregroundColor(.secondary)
2022-03-27 10:50:36 +00:00
}
}
private var additionalDetailsAvailable: Bool {
video.publishedDate != nil || video.views != 0 ||
2022-12-04 11:07:06 +00:00
(!timeOnThumbnail && !videoDuration.isNil)
2021-07-27 22:40:04 +00:00
}
private var thumbnail: some View {
ZStack(alignment: .leading) {
ZStack(alignment: .bottomLeading) {
thumbnailImage
2022-12-15 22:51:49 +00:00
ProgressView(value: watch?.progress ?? 0, total: 100)
.progressViewStyle(LinearProgressViewStyle(tint: Color("AppRedColor")))
#if os(tvOS)
.padding(.horizontal, 16)
#else
.padding(.horizontal, 10)
#endif
#if os(macOS)
.offset(x: 0, y: 4)
#else
.offset(x: 0, y: -3)
#endif
2023-05-23 16:00:33 +00:00
.opacity(watch?.isShowingProgress ?? false ? 1 : 0)
}
2021-07-27 22:40:04 +00:00
VStack {
HStack(alignment: .top) {
2022-12-15 22:51:49 +00:00
if saveHistory,
2023-02-19 13:18:19 +00:00
watchedVideoStyle.isShowingBadge
2022-12-15 22:51:49 +00:00
{
WatchView(watch: watch, videoID: video.videoID, duration: video.length)
}
2021-07-27 22:40:04 +00:00
if video.live {
DetailBadge(text: "Live", style: .outstanding)
} else if video.upcoming {
DetailBadge(text: "Upcoming", style: .informational)
}
Spacer()
if channelOnThumbnail, !inChannelView {
ChannelLinkView(channel: video.channel) {
channelLabel()
}
2021-11-04 23:25:51 +00:00
}
2021-07-27 22:40:04 +00:00
}
#if os(tvOS)
.padding(16)
#else
2021-07-27 22:40:04 +00:00
.padding(10)
#endif
2021-07-27 22:40:04 +00:00
Spacer()
2022-12-16 22:13:16 +00:00
VStack(alignment: .trailing, spacing: 4) {
PlayingIndicatorView(video: video, height: 20)
.frame(width: 15, alignment: .trailing)
.padding(.trailing, 3)
HStack {
Spacer()
2021-07-27 22:40:04 +00:00
2022-12-16 22:13:16 +00:00
if timeOnThumbnail,
!video.live,
let time
{
DetailBadge(text: time, style: .prominent)
}
2021-07-27 22:40:04 +00:00
}
}
#if os(tvOS)
.padding(16)
#else
2021-07-27 22:40:04 +00:00
.padding(10)
#endif
2021-07-27 22:40:04 +00:00
}
2021-10-22 23:04:03 +00:00
.lineLimit(1)
2021-07-27 22:40:04 +00:00
}
}
private var thumbnailImage: some View {
2021-10-24 22:26:25 +00:00
Group {
2022-12-20 22:24:39 +00:00
VideoCellThumbnail(video: video)
2022-09-11 19:33:08 +00:00
#if os(tvOS)
.frame(minHeight: 320)
#endif
2021-10-24 22:26:25 +00:00
}
.mask(RoundedRectangle(cornerRadius: thumbnailRoundingCornerRadius))
2021-10-24 22:26:25 +00:00
.modifier(AspectRatioModifier())
2021-07-27 22:40:04 +00:00
}
private var time: String? {
2022-12-04 11:07:06 +00:00
guard var videoTime = videoDuration else {
return nil
}
if !saveHistory || !showWatchingProgress || watch?.finished ?? false {
return videoTime
}
if let stoppedAt = watch?.stoppedAt,
stoppedAt.isFinite,
let stoppedAtFormatted = stoppedAt.formattedAsPlaybackTime()
{
2022-03-27 10:50:36 +00:00
if (watch?.videoDuration ?? 0) > 0 {
videoTime = watch!.videoDuration.formattedAsPlaybackTime() ?? "?"
}
return "\(stoppedAtFormatted) / \(videoTime)"
}
return videoTime
}
private func videoDetail(_ text: String, lineLimit: Int = 1) -> some View {
2023-04-22 08:55:36 +00:00
Text(verbatim: text)
2021-08-01 23:01:24 +00:00
.fontWeight(.bold)
2021-07-27 22:40:04 +00:00
.lineLimit(lineLimit)
.truncationMode(.middle)
2021-07-27 21:26:52 +00:00
}
2021-08-02 21:10:22 +00:00
2021-09-18 20:36:42 +00:00
struct AspectRatioModifier: ViewModifier {
@Environment(\.horizontalCells) private var horizontalCells
func body(content: Content) -> some View {
Group {
if horizontalCells {
content
} else {
content
.aspectRatio(
VideoPlayerView.defaultAspectRatio,
contentMode: .fill
)
2021-09-18 20:36:42 +00:00
}
}
}
}
2021-07-27 21:26:52 +00:00
}
2022-12-20 22:24:39 +00:00
struct VideoCellThumbnail: View {
let video: Video
@ObservedObject private var thumbnails = ThumbnailsModel.shared
var body: some View {
ThumbnailView(url: thumbnails.best(video))
}
}
2021-10-22 23:04:03 +00:00
struct VideoCell_Preview: PreviewProvider {
static var previews: some View {
Group {
VideoCell(video: Video.fixture)
}
2021-11-04 23:25:51 +00:00
#if os(macOS)
2021-11-08 16:29:35 +00:00
.frame(maxWidth: 300, maxHeight: 250)
2021-11-04 23:25:51 +00:00
#elseif os(iOS)
2022-12-13 23:07:32 +00:00
.frame(maxWidth: 600, maxHeight: 200)
2021-11-04 23:25:51 +00:00
#endif
.injectFixtureEnvironmentObjects()
}
}