1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Videos/VideoBanner.swift

215 lines
6.9 KiB
Swift
Raw Normal View History

import CoreMedia
2022-11-19 18:41:04 +05:30
import Defaults
import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct VideoBanner: View {
2022-11-12 07:09:44 +05:30
#if os(tvOS)
static let titleAppend = ""
#else
static let titleAppend = "\n"
#endif
let video: Video?
var playbackTime: CMTime?
var videoDuration: TimeInterval?
init(video: Video? = nil, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) {
self.video = video
self.playbackTime = playbackTime
self.videoDuration = videoDuration
}
var body: some View {
HStack(alignment: stackAlignment, spacing: 12) {
2022-11-13 16:46:44 +05:30
VStack(spacing: thumbnailStackSpacing) {
smallThumbnail
2021-10-21 03:51:50 +05:30
#if !os(tvOS)
progressView
#endif
}
VStack(alignment: .leading, spacing: 4) {
2022-11-10 22:41:28 +05:30
Group {
if let video {
HStack(alignment: .top) {
2022-11-12 07:09:44 +05:30
Text(video.displayTitle + Self.titleAppend)
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)
.lineLimit(2)
.font(.headline)
.frame(alignment: .leading)
HStack {
2022-11-10 22:41:28 +05:30
Group {
if let video {
if !video.isLocal || video.localStreamIsRemoteURL {
Text(video.displayAuthor)
2022-11-13 04:31:04 +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
Spacer()
2022-11-13 04:31:04 +05:30
}
2022-11-19 19:39:09 +05:30
.frame(maxWidth: .infinity)
2022-11-13 04:31:04 +05:30
}
#endif
2022-11-10 22:41:28 +05:30
}
} else {
Text("Video Author")
.redacted(reason: .placeholder)
}
}
.lineLimit(1)
Spacer()
#if os(tvOS)
progressView
#endif
2022-11-13 04:31:04 +05:30
if !(video?.localStreamIsDirectory ?? false) {
Text(videoDurationLabel)
.fontWeight(.light)
}
}
.foregroundColor(.secondary)
}
.padding(.vertical, playbackTime.isNil ? 0 : 5)
}
.contentShape(Rectangle())
2022-11-11 03:21:30 +05:30
#if os(tvOS)
.buttonStyle(.card)
#else
.buttonStyle(.plain)
#endif
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100, alignment: .center)
#if os(tvOS)
.padding(.vertical, 20)
.padding(.trailing, 10)
#endif
}
private var stackAlignment: VerticalAlignment {
#if os(macOS)
playbackTime.isNil ? .center : .top
#else
2021-11-08 21:59:35 +05:30
.center
#endif
}
2022-11-13 16:46:44 +05:30
private var thumbnailStackSpacing: Double {
#if os(tvOS)
8
#else
2
#endif
}
2022-09-01 00:54:46 +05:30
@ViewBuilder private var smallThumbnail: some View {
2022-11-13 04:31:04 +05:30
ZStack {
Color("PlaceholderColor")
2022-11-10 22:41:28 +05:30
if let video {
if let thumbnail = video.thumbnailURL(quality: .medium) {
2022-12-11 05:21:21 +05:30
ThumbnailView(url: thumbnail)
2022-11-13 04:31:04 +05:30
} else if video.isLocal {
Image(systemName: video.localStreamImageSystemName)
2022-11-10 22:41:28 +05:30
}
} else {
Image(systemName: "ellipsis")
}
2022-11-10 22:41:28 +05:30
}
2022-09-12 01:03:08 +05:30
#if os(tvOS)
2022-11-10 22:41:28 +05:30
.frame(width: thumbnailWidth, height: thumbnailHeight)
.mask(RoundedRectangle(cornerRadius: 12))
2022-09-12 01:03:08 +05:30
#else
2022-11-10 22:41:28 +05:30
.frame(width: thumbnailWidth, height: thumbnailHeight)
.mask(RoundedRectangle(cornerRadius: 6))
2022-09-12 01:03:08 +05:30
#endif
}
private var thumbnailWidth: Double {
#if os(tvOS)
2021-11-05 04:55:51 +05:30
250
#else
100
#endif
}
2022-11-10 22:41:28 +05:30
private var thumbnailHeight: Double {
#if os(tvOS)
140
#else
60
#endif
}
2022-11-13 04:31:04 +05:30
private var videoDurationLabel: String {
guard videoDuration != 0 else { return PlayerTimeModel.timePlaceholder }
return (videoDuration ?? video?.length ?? 0).formattedAsPlaybackTime() ?? PlayerTimeModel.timePlaceholder
}
private var progressView: some View {
Group {
if !playbackTime.isNil, !(video?.live ?? false) {
2022-11-19 18:41:04 +05:30
ProgressView(value: watchValue, total: progressViewTotal)
.progressViewStyle(.linear)
.frame(maxWidth: thumbnailWidth)
}
}
}
2022-11-19 18:41:04 +05:30
private var watchValue: Double {
if finished { return progressViewTotal }
return progressViewValue
}
private var progressViewValue: Double {
2022-11-13 04:31:04 +05:30
guard videoDuration != 0 else { return 1 }
return [playbackTime?.seconds, videoDuration].compactMap { $0 }.min() ?? 0
}
private var progressViewTotal: Double {
2022-11-13 04:31:04 +05:30
guard videoDuration != 0 else { return 1 }
return videoDuration ?? video?.length ?? 1
}
2022-11-19 18:41:04 +05:30
private var finished: Bool {
(progressViewValue / progressViewTotal) * 100 > Double(Defaults[.watchedThreshold])
}
}
struct VideoBanner_Previews: PreviewProvider {
static var previews: some View {
VStack(spacing: 20) {
VideoBanner(video: Video.fixture, playbackTime: CMTime(seconds: 400, preferredTimescale: 10000))
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()
}
.frame(maxWidth: 900)
}
}