2021-10-23 02:19:31 +05:30
|
|
|
import CoreMedia
|
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 {
|
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?
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
init(video: Video? = nil, playbackTime: CMTime? = nil, videoDuration: TimeInterval? = nil) {
|
2021-10-23 02:19:31 +05:30
|
|
|
self.video = video
|
|
|
|
self.playbackTime = playbackTime
|
|
|
|
self.videoDuration = videoDuration
|
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
|
|
var body: some View {
|
2021-10-23 15:43:05 +05:30
|
|
|
HStack(alignment: stackAlignment, spacing: 12) {
|
2021-10-23 02:19:31 +05:30
|
|
|
VStack(spacing: thumbnailStackSpacing) {
|
|
|
|
smallThumbnail
|
2021-10-21 03:51:50 +05:30
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
#if !os(tvOS)
|
|
|
|
progressView
|
|
|
|
#endif
|
2021-10-23 02:19:31 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
2021-10-24 23:31:08 +05:30
|
|
|
Text(video?.title ?? "Unknown title")
|
2021-10-06 01:50:09 +05:30
|
|
|
.truncationMode(.middle)
|
|
|
|
.lineLimit(2)
|
|
|
|
.font(.headline)
|
|
|
|
.frame(alignment: .leading)
|
|
|
|
|
|
|
|
HStack {
|
2021-10-24 23:31:08 +05:30
|
|
|
Text(video?.author ?? "Unknown author")
|
2021-10-06 01:50:09 +05:30
|
|
|
.lineLimit(1)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
progressView
|
|
|
|
#endif
|
|
|
|
|
2021-10-24 23:31:08 +05:30
|
|
|
if let time = (videoDuration ?? video?.length ?? 0).formattedAsPlaybackTime() {
|
2021-10-06 01:50:09 +05:30
|
|
|
Text(time)
|
|
|
|
.fontWeight(.light)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
.padding(.vertical, playbackTime.isNil ? 0 : 5)
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
.buttonStyle(.plain)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100, alignment: .center)
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
private var stackAlignment: VerticalAlignment {
|
|
|
|
#if os(macOS)
|
|
|
|
playbackTime.isNil ? .center : .top
|
|
|
|
#else
|
2021-11-08 21:59:35 +05:30
|
|
|
.center
|
2021-10-23 15:43:05 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
private var thumbnailStackSpacing: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
8
|
|
|
|
#else
|
2021-10-23 15:43:05 +05:30
|
|
|
2
|
2021-10-23 02:19:31 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var smallThumbnail: some View {
|
2021-10-24 23:31:08 +05:30
|
|
|
WebImage(url: video?.thumbnailURL(quality: .medium))
|
2021-10-22 04:59:10 +05:30
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
ProgressView()
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
2021-10-22 04:59:10 +05:30
|
|
|
.indicator(.activity)
|
2021-10-06 01:50:09 +05:30
|
|
|
#if os(tvOS)
|
2021-10-23 15:43:05 +05:30
|
|
|
.frame(width: thumbnailWidth, height: 140)
|
2021-10-06 01:50:09 +05:30
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
|
|
|
#else
|
2021-10-23 15:43:05 +05:30
|
|
|
.frame(width: thumbnailWidth, height: 60)
|
2021-10-06 01:50:09 +05:30
|
|
|
.mask(RoundedRectangle(cornerRadius: 6))
|
|
|
|
#endif
|
|
|
|
}
|
2021-10-23 02:19:31 +05:30
|
|
|
|
|
|
|
private var thumbnailWidth: Double {
|
|
|
|
#if os(tvOS)
|
2021-11-05 04:55:51 +05:30
|
|
|
250
|
2021-10-23 02:19:31 +05:30
|
|
|
#else
|
2021-10-23 15:43:05 +05:30
|
|
|
100
|
2021-10-23 02:19:31 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-23 15:43:05 +05:30
|
|
|
private var progressView: some View {
|
|
|
|
Group {
|
2021-12-27 02:44:46 +05:30
|
|
|
if !playbackTime.isNil, !(video?.live ?? false) {
|
2021-10-23 15:43:05 +05:30
|
|
|
ProgressView(value: progressViewValue, total: progressViewTotal)
|
|
|
|
.progressViewStyle(.linear)
|
|
|
|
.frame(maxWidth: thumbnailWidth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
private var progressViewValue: Double {
|
|
|
|
[playbackTime?.seconds, videoDuration].compactMap { $0 }.min() ?? 0
|
|
|
|
}
|
|
|
|
|
|
|
|
private var progressViewTotal: Double {
|
2021-10-24 23:31:08 +05:30
|
|
|
videoDuration ?? video?.length ?? 1
|
2021-10-23 02:19:31 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
struct VideoBanner_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
VStack(spacing: 20) {
|
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)
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 900)
|
|
|
|
}
|
|
|
|
}
|