2021-10-23 02:19:31 +05:30
|
|
|
import CoreMedia
|
2022-11-19 18:41:04 +05:30
|
|
|
import Defaults
|
2021-10-06 01:50:09 +05:30
|
|
|
import Foundation
|
2021-10-22 04:59:10 +05:30
|
|
|
import SDWebImageSwiftUI
|
2021-10-06 01:50:09 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoBanner: View {
|
2022-12-13 06:20:26 +05:30
|
|
|
var id: String?
|
2021-10-24 23:31:08 +05:30
|
|
|
let video: Video?
|
2021-10-23 02:19:31 +05:30
|
|
|
var playbackTime: CMTime?
|
|
|
|
var videoDuration: TimeInterval?
|
2022-12-13 06:20:26 +05:30
|
|
|
var watch: Watch?
|
2021-10-23 02:19:31 +05:30
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
@Default(.saveHistory) private var saveHistory
|
|
|
|
@Default(.watchedVideoStyle) private var watchedVideoStyle
|
|
|
|
@Default(.watchedVideoBadgeColor) private var watchedVideoBadgeColor
|
2022-12-14 02:26:03 +05:30
|
|
|
@Default(.timeOnThumbnail) private var timeOnThumbnail
|
2022-12-13 06:20:26 +05:30
|
|
|
|
|
|
|
@Environment(\.inChannelView) private var inChannelView
|
|
|
|
@Environment(\.inNavigationView) private var inNavigationView
|
|
|
|
@Environment(\.navigationStyle) private var navigationStyle
|
|
|
|
|
|
|
|
init(
|
|
|
|
id: String? = nil,
|
|
|
|
video: Video? = nil,
|
|
|
|
playbackTime: CMTime? = nil,
|
|
|
|
videoDuration: TimeInterval? = nil,
|
|
|
|
watch: Watch? = nil
|
|
|
|
) {
|
|
|
|
self.id = id
|
2021-10-23 02:19:31 +05:30
|
|
|
self.video = video
|
|
|
|
self.playbackTime = playbackTime
|
|
|
|
self.videoDuration = videoDuration
|
2022-12-13 06:20:26 +05:30
|
|
|
self.watch = watch
|
2021-10-23 02:19:31 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
|
|
var body: some View {
|
2022-12-13 00:16:31 +05:30
|
|
|
HStack(alignment: .top, spacing: 12) {
|
2022-12-14 02:26:03 +05:30
|
|
|
VStack(alignment: .trailing, spacing: 2) {
|
2021-10-23 02:19:31 +05:30
|
|
|
smallThumbnail
|
2022-12-16 03:33:04 +05:30
|
|
|
|
2022-12-14 02:26:03 +05:30
|
|
|
if !timeOnThumbnail, let timeLabel {
|
|
|
|
Text(timeLabel)
|
|
|
|
.font(.caption.monospacedDigit())
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
VStack(alignment: .leading, spacing: 2) {
|
2022-11-10 22:41:28 +05:30
|
|
|
Group {
|
|
|
|
if let video {
|
|
|
|
HStack(alignment: .top) {
|
2022-12-13 00:16:31 +05:30
|
|
|
Text(video.displayTitle)
|
2022-11-10 22:41:28 +05:30
|
|
|
if video.isLocal, let fileExtension = video.localStreamFileExtension {
|
|
|
|
Spacer()
|
|
|
|
Text(fileExtension)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Text("Loading contents of the video, please wait")
|
|
|
|
.redacted(reason: .placeholder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.truncationMode(.middle)
|
2022-12-13 00:16:31 +05:30
|
|
|
.lineLimit(5)
|
2022-11-10 22:41:28 +05:30
|
|
|
.font(.headline)
|
2021-10-06 01:50:09 +05:30
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack {
|
2022-12-14 02:26:03 +05:30
|
|
|
HStack {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Group {
|
|
|
|
if let video {
|
|
|
|
if !inChannelView, !video.isLocal || video.localStreamIsRemoteURL {
|
2022-12-14 17:26:47 +05:30
|
|
|
ChannelLinkView(channel: video.channel) {
|
|
|
|
HStack(spacing: Constants.channelDetailsStackSpacing) {
|
2022-12-14 21:50:24 +05:30
|
|
|
if let url = video.channel.thumbnailURLOrCached, video != .fixture {
|
2022-12-14 17:26:47 +05:30
|
|
|
ThumbnailView(url: url)
|
|
|
|
.frame(width: Constants.channelThumbnailSize, height: Constants.channelThumbnailSize)
|
|
|
|
.clipShape(Circle())
|
|
|
|
}
|
|
|
|
|
|
|
|
channelLabel
|
|
|
|
.font(.subheadline)
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
} else {
|
|
|
|
#if os(iOS)
|
|
|
|
if DocumentsModel.shared.isDocument(video) {
|
|
|
|
HStack(spacing: 6) {
|
|
|
|
if let date = DocumentsModel.shared.formattedCreationDate(video) {
|
|
|
|
Text(date)
|
|
|
|
}
|
|
|
|
if let size = DocumentsModel.shared.formattedSize(video) {
|
|
|
|
Text("•")
|
|
|
|
Text(size)
|
|
|
|
}
|
2022-11-19 19:39:09 +05:30
|
|
|
|
2022-12-14 02:26:03 +05:30
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-13 04:31:04 +05:30
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
} else {
|
|
|
|
Text("Video Author")
|
|
|
|
.redacted(reason: .placeholder)
|
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
|
|
|
|
extraAttributes
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
.foregroundColor(.secondary)
|
2022-12-13 06:20:26 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2022-12-13 06:20:26 +05:30
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
.frame(maxHeight: .infinity)
|
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2022-12-13 06:20:26 +05:30
|
|
|
.fixedSize(horizontal: false, vertical: true)
|
2022-11-11 03:21:30 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.buttonStyle(.card)
|
2022-12-21 03:54:39 +05:30
|
|
|
.padding(.trailing, 10)
|
|
|
|
#elseif os(macOS)
|
2022-11-11 03:21:30 +05:30
|
|
|
.buttonStyle(.plain)
|
|
|
|
#endif
|
2022-12-21 03:54:39 +05:30
|
|
|
.opacity(contentOpacity)
|
2022-12-22 01:47:10 +05:30
|
|
|
.contentShape(Rectangle())
|
2022-11-13 16:46:44 +05:30
|
|
|
}
|
|
|
|
|
2022-12-13 14:39:21 +05:30
|
|
|
private var extraAttributes: some View {
|
|
|
|
HStack(spacing: 16) {
|
|
|
|
if let video {
|
|
|
|
if let date = video.publishedDate {
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Text(date)
|
|
|
|
.allowsTightening(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if video.views > 0 {
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Image(systemName: "eye")
|
|
|
|
Text(video.viewsCount!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.font(.caption)
|
|
|
|
.lineLimit(1)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
|
2022-09-01 00:54:46 +05:30
|
|
|
@ViewBuilder private var smallThumbnail: some View {
|
2022-12-14 02:26:03 +05:30
|
|
|
ZStack(alignment: .bottomTrailing) {
|
2022-12-16 04:21:49 +05:30
|
|
|
ZStack(alignment: .topLeading) {
|
2022-12-14 02:26:03 +05:30
|
|
|
ZStack {
|
|
|
|
Color("PlaceholderColor")
|
|
|
|
|
|
|
|
if let video {
|
|
|
|
if let thumbnail = video.thumbnailURL(quality: .medium) {
|
|
|
|
ThumbnailView(url: thumbnail)
|
|
|
|
} else if video.isLocal {
|
|
|
|
Image(systemName: video.localStreamImageSystemName)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Image(systemName: "ellipsis")
|
2022-12-13 06:20:26 +05:30
|
|
|
}
|
2022-12-14 02:26:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if saveHistory,
|
|
|
|
watchedVideoStyle.isShowingBadge,
|
2022-12-16 04:21:49 +05:30
|
|
|
let video
|
2022-12-14 02:26:03 +05:30
|
|
|
{
|
2022-12-16 04:21:49 +05:30
|
|
|
WatchView(watch: watch, videoID: video.videoID, duration: video.length)
|
|
|
|
.offset(x: 2, y: 2)
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
2022-12-13 06:20:26 +05:30
|
|
|
}
|
|
|
|
|
2022-12-14 02:26:03 +05:30
|
|
|
if timeOnThumbnail {
|
|
|
|
timeView
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
|
|
|
.frame(width: thumbnailWidth, height: thumbnailHeight)
|
2022-12-14 02:26:03 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
2022-09-12 01:03:08 +05:30
|
|
|
#else
|
2022-12-14 02:26:03 +05:30
|
|
|
.mask(RoundedRectangle(cornerRadius: 6))
|
2022-09-12 01:03:08 +05:30
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
private var contentOpacity: Double {
|
|
|
|
guard saveHistory,
|
|
|
|
!watch.isNil,
|
2023-04-23 02:14:59 +05:30
|
|
|
watchedVideoStyle.isDecreasingOpacity
|
2022-12-13 06:20:26 +05:30
|
|
|
else {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return watch!.finished ? 0.5 : 1
|
|
|
|
}
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
private var thumbnailWidth: Double {
|
|
|
|
#if os(tvOS)
|
2022-12-14 17:26:47 +05:30
|
|
|
356
|
2021-10-23 02:19:31 +05:30
|
|
|
#else
|
2022-12-14 02:26:03 +05:30
|
|
|
120
|
2021-10-23 02:19:31 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
private var thumbnailHeight: Double {
|
|
|
|
#if os(tvOS)
|
2022-12-14 17:26:47 +05:30
|
|
|
200
|
2022-11-10 22:41:28 +05:30
|
|
|
#else
|
2022-12-14 02:26:03 +05:30
|
|
|
72
|
2022-11-10 22:41:28 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
private var videoDurationLabel: String? {
|
|
|
|
guard videoDuration != 0 else { return nil }
|
|
|
|
return (videoDuration ?? video?.length)?.formattedAsPlaybackTime()
|
2022-11-13 04:31:04 +05:30
|
|
|
}
|
|
|
|
|
2022-12-13 06:20:26 +05:30
|
|
|
private var watchStoppedAtLabel: String? {
|
|
|
|
guard let watch else { return nil }
|
|
|
|
|
|
|
|
return watch.stoppedAt.formattedAsPlaybackTime(allowZero: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeInfo: Bool {
|
|
|
|
videoDurationLabel != nil && (video == nil || !video!.localStreamIsDirectory)
|
|
|
|
}
|
|
|
|
|
2022-12-14 02:26:03 +05:30
|
|
|
private var timeLabel: String? {
|
|
|
|
if let watch, let watchStoppedAtLabel, let videoDurationLabel, !watch.finished {
|
|
|
|
return "\(watchStoppedAtLabel) / \(videoDurationLabel)"
|
|
|
|
} else if let videoDurationLabel {
|
|
|
|
return videoDurationLabel
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder private var timeView: some View {
|
2022-12-18 18:34:50 +05:30
|
|
|
VStack(alignment: .trailing) {
|
2022-12-17 03:43:16 +05:30
|
|
|
PlayingIndicatorView(video: video, height: 10)
|
|
|
|
.frame(width: 12, alignment: .trailing)
|
|
|
|
.padding(.trailing, 3)
|
2022-12-19 04:40:05 +05:30
|
|
|
.padding(.bottom, timeLabel == nil ? 3 : -5)
|
2022-12-17 03:43:16 +05:30
|
|
|
|
|
|
|
if let timeLabel {
|
|
|
|
Text(timeLabel)
|
|
|
|
.font(.caption2.weight(.semibold).monospacedDigit())
|
|
|
|
.allowsTightening(true)
|
|
|
|
.padding(2)
|
|
|
|
.modifier(ControlBackgroundModifier())
|
|
|
|
}
|
2021-10-23 15:43:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 17:26:47 +05:30
|
|
|
@ViewBuilder private var channelLabel: some View {
|
2022-12-13 06:20:26 +05:30
|
|
|
if let video, !video.displayAuthor.isEmpty {
|
|
|
|
Text(video.displayAuthor)
|
|
|
|
.fontWeight(.semibold)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
struct VideoBanner_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-12-13 06:20:26 +05:30
|
|
|
VStack(spacing: 2) {
|
2021-10-23 02:19:31 +05:30
|
|
|
VideoBanner(video: Video.fixture, playbackTime: CMTime(seconds: 400, preferredTimescale: 10000))
|
2021-10-06 01:50:09 +05:30
|
|
|
VideoBanner(video: Video.fixtureUpcomingWithoutPublishedOrViews)
|
2022-11-10 22:41:28 +05:30
|
|
|
VideoBanner(video: .local(URL(string: "https://apple.com/a/directory/of/video+that+has+very+long+title+that+will+likely.mp4")!))
|
|
|
|
VideoBanner(video: .local(URL(string: "file://a/b/c/d/e/f.mkv")!))
|
|
|
|
VideoBanner()
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2022-12-13 06:20:26 +05:30
|
|
|
.frame(maxWidth: 1300)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
}
|