2022-09-01 16:51:59 +00:00
|
|
|
import CachedAsyncImage
|
2021-10-22 20:49:31 +00:00
|
|
|
import CoreMedia
|
2021-10-05 20:20:09 +00:00
|
|
|
import Foundation
|
2021-10-21 23:29:10 +00:00
|
|
|
import SDWebImageSwiftUI
|
2021-10-05 20:20:09 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoBanner: View {
|
2021-10-24 18:01:08 +00:00
|
|
|
let video: Video?
|
2021-10-22 20:49:31 +00:00
|
|
|
var playbackTime: CMTime?
|
|
|
|
var videoDuration: TimeInterval?
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
init(video: Video? = nil, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) {
|
2021-10-22 20:49:31 +00:00
|
|
|
self.video = video
|
|
|
|
self.playbackTime = playbackTime
|
|
|
|
self.videoDuration = videoDuration
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2021-10-23 10:13:05 +00:00
|
|
|
HStack(alignment: stackAlignment, spacing: 12) {
|
2021-10-22 20:49:31 +00:00
|
|
|
VStack(spacing: thumbnailStackSpacing) {
|
|
|
|
smallThumbnail
|
2021-10-20 22:21:50 +00:00
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
progressView
|
|
|
|
#endif
|
2021-10-22 20:49:31 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
2022-06-26 11:12:32 +00:00
|
|
|
Text(video?.title ?? "Loading...")
|
2021-10-05 20:20:09 +00:00
|
|
|
.truncationMode(.middle)
|
|
|
|
.lineLimit(2)
|
|
|
|
.font(.headline)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
|
|
|
|
HStack {
|
2022-06-26 11:12:32 +00:00
|
|
|
Text(video?.author ?? "")
|
2021-10-05 20:20:09 +00:00
|
|
|
.lineLimit(1)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
progressView
|
|
|
|
#endif
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
if let time = (videoDuration ?? video?.length ?? 0).formattedAsPlaybackTime() {
|
2021-10-05 20:20:09 +00:00
|
|
|
Text(time)
|
|
|
|
.fontWeight(.light)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-22 20:49:31 +00:00
|
|
|
.padding(.vertical, playbackTime.isNil ? 0 : 5)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100, alignment: .center)
|
|
|
|
}
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
private var stackAlignment: VerticalAlignment {
|
|
|
|
#if os(macOS)
|
|
|
|
playbackTime.isNil ? .center : .top
|
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.center
|
2021-10-23 10:13:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
private var thumbnailStackSpacing: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
8
|
|
|
|
#else
|
2021-10-23 10:13:05 +00:00
|
|
|
2
|
2021-10-22 20:49:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:24:46 +00:00
|
|
|
@ViewBuilder private var smallThumbnail: some View {
|
|
|
|
let url = video?.thumbnailURL(quality: .medium)
|
2022-09-01 23:19:30 +00:00
|
|
|
Group {
|
|
|
|
if #available(iOS 15, macOS 12, *) {
|
|
|
|
CachedAsyncImage(url: url) { image in
|
|
|
|
image
|
|
|
|
.resizable()
|
|
|
|
} placeholder: {
|
|
|
|
Rectangle().foregroundColor(Color("PlaceholderColor"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WebImage(url: url)
|
2022-08-31 19:24:46 +00:00
|
|
|
.resizable()
|
2022-09-01 23:19:30 +00:00
|
|
|
.placeholder {
|
|
|
|
ProgressView()
|
|
|
|
}
|
|
|
|
.indicator(.activity)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2022-08-31 19:24:46 +00:00
|
|
|
}
|
2022-09-01 23:19:30 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.frame(width: thumbnailWidth, height: 140)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
|
|
|
#else
|
|
|
|
.frame(width: thumbnailWidth, height: 60)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 6))
|
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-22 20:49:31 +00:00
|
|
|
|
|
|
|
private var thumbnailWidth: Double {
|
|
|
|
#if os(tvOS)
|
2021-11-04 23:25:51 +00:00
|
|
|
250
|
2021-10-22 20:49:31 +00:00
|
|
|
#else
|
2021-10-23 10:13:05 +00:00
|
|
|
100
|
2021-10-22 20:49:31 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-23 10:13:05 +00:00
|
|
|
private var progressView: some View {
|
|
|
|
Group {
|
2021-12-26 21:14:46 +00:00
|
|
|
if !playbackTime.isNil, !(video?.live ?? false) {
|
2021-10-23 10:13:05 +00:00
|
|
|
ProgressView(value: progressViewValue, total: progressViewTotal)
|
|
|
|
.progressViewStyle(.linear)
|
|
|
|
.frame(maxWidth: thumbnailWidth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
private var progressViewValue: Double {
|
|
|
|
[playbackTime?.seconds, videoDuration].compactMap { $0 }.min() ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
private var progressViewTotal: Double {
|
2021-10-24 18:01:08 +00:00
|
|
|
videoDuration ?? video?.length ?? 1
|
2021-10-22 20:49:31 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct VideoBanner_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
VStack(spacing: 20) {
|
2021-10-22 20:49:31 +00:00
|
|
|
VideoBanner(video: Video.fixture, playbackTime: CMTime(seconds: 400, preferredTimescale: 10000))
|
2021-10-05 20:20:09 +00:00
|
|
|
VideoBanner(video: Video.fixtureUpcomingWithoutPublishedOrViews)
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 900)
|
|
|
|
}
|
|
|
|
}
|