2021-07-28 04:10:04 +05:30
|
|
|
import Defaults
|
2021-10-22 04:59:10 +05:30
|
|
|
import SDWebImageSwiftUI
|
2021-07-28 02:56:52 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-22 04:59:10 +05:30
|
|
|
struct VideoCell: View {
|
2021-10-06 01:50:09 +05:30
|
|
|
var video: Video
|
2021-10-25 03:56:25 +05:30
|
|
|
@State private var mediumQualityThumbnail = false
|
2021-10-06 01:50:09 +05:30
|
|
|
|
|
|
|
@Environment(\.inNavigationView) private var inNavigationView
|
2021-07-28 04:10:04 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
2021-09-29 19:59:17 +05:30
|
|
|
@Environment(\.horizontalCells) private var horizontalCells
|
2021-07-28 04:10:04 +05:30
|
|
|
#endif
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-10-25 03:56:25 +05:30
|
|
|
@EnvironmentObject<ThumbnailsModel> private var thumbnails
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-11-05 04:55:51 +05:30
|
|
|
@Default(.channelOnThumbnail) private var channelOnThumbnail
|
|
|
|
@Default(.timeOnThumbnail) private var timeOnThumbnail
|
|
|
|
|
2021-07-28 02:56:52 +05:30
|
|
|
var body: some View {
|
2021-09-01 02:47:50 +05:30
|
|
|
Group {
|
2021-10-06 01:50:09 +05:30
|
|
|
Button(action: {
|
|
|
|
player.playNow(video)
|
|
|
|
|
2021-10-28 22:44:55 +05:30
|
|
|
guard !player.playingInPictureInPicture else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-06 01:50:09 +05:30
|
|
|
if inNavigationView {
|
2021-10-23 04:34:03 +05:30
|
|
|
player.playerNavigationLinkActive = true
|
2021-10-06 01:50:09 +05:30
|
|
|
} else {
|
|
|
|
player.presentPlayer()
|
2021-08-03 02:40:22 +05:30
|
|
|
}
|
2021-10-06 01:50:09 +05:30
|
|
|
}) {
|
|
|
|
content
|
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
2021-09-27 03:58:42 +05:30
|
|
|
.buttonStyle(.plain)
|
2021-08-03 02:40:22 +05:30
|
|
|
.contentShape(RoundedRectangle(cornerRadius: 12))
|
2021-10-23 04:34:03 +05:30
|
|
|
.contextMenu { VideoContextMenuView(video: video, playerNavigationLinkActive: $player.playerNavigationLinkActive) }
|
2021-08-02 04:31:24 +05:30
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-09-01 02:47:50 +05:30
|
|
|
var content: some View {
|
|
|
|
VStack {
|
2021-09-27 03:58:42 +05:30
|
|
|
#if os(iOS)
|
|
|
|
if verticalSizeClass == .compact, !horizontalCells {
|
|
|
|
horizontalRow
|
|
|
|
.padding(.vertical, 4)
|
|
|
|
} else {
|
2021-09-01 02:47:50 +05:30
|
|
|
verticalRow
|
2021-09-27 03:58:42 +05:30
|
|
|
}
|
|
|
|
#else
|
|
|
|
verticalRow
|
|
|
|
#endif
|
2021-09-01 02:47:50 +05:30
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.background()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(iOS)
|
|
|
|
var horizontalRow: some View {
|
|
|
|
HStack(alignment: .top, spacing: 2) {
|
|
|
|
Section {
|
|
|
|
#if os(tvOS)
|
|
|
|
thumbnailImage(quality: .medium)
|
|
|
|
#else
|
|
|
|
thumbnail
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 320)
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
videoDetail(video.title, lineLimit: 5)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-11-05 04:55:51 +05:30
|
|
|
if !channelOnThumbnail {
|
|
|
|
Text(video.channel.name)
|
|
|
|
.fontWeight(.semibold)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
if additionalDetailsAvailable {
|
|
|
|
Spacer()
|
2021-09-19 02:06:42 +05:30
|
|
|
|
2021-11-05 04:55:51 +05:30
|
|
|
HStack(spacing: 15) {
|
2021-09-29 19:59:17 +05:30
|
|
|
if let date = video.publishedDate {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "calendar")
|
2021-11-05 04:55:51 +05:30
|
|
|
.frame(height: 15)
|
2021-09-29 19:59:17 +05:30
|
|
|
Text(date)
|
|
|
|
}
|
2021-09-19 02:06:42 +05:30
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
if video.views > 0 {
|
2021-09-29 19:59:17 +05:30
|
|
|
VStack {
|
|
|
|
Image(systemName: "eye")
|
2021-11-05 04:55:51 +05:30
|
|
|
.frame(height: 15)
|
2021-09-29 19:59:17 +05:30
|
|
|
Text(video.viewsCount!)
|
|
|
|
}
|
2021-09-19 02:06:42 +05:30
|
|
|
}
|
2021-11-05 04:55:51 +05:30
|
|
|
|
|
|
|
if !timeOnThumbnail, let time = video.length.formattedAsPlaybackTime() {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "clock")
|
|
|
|
.frame(height: 15)
|
|
|
|
Text(time)
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 02:06:42 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
.foregroundColor(.secondary)
|
2021-09-19 02:06:42 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
.padding()
|
|
|
|
.frame(minHeight: 180)
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
#if os(tvOS)
|
2021-10-23 02:19:31 +05:30
|
|
|
if let time = video.length.formattedAsPlaybackTime() || video.live || video.upcoming {
|
2021-08-03 02:40:22 +05:30
|
|
|
Spacer()
|
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
VStack(alignment: .center) {
|
|
|
|
Spacer()
|
|
|
|
|
2021-10-23 02:19:31 +05:30
|
|
|
if let time = video.length.formattedAsPlaybackTime() {
|
2021-09-29 19:59:17 +05:30
|
|
|
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-03 02:40:22 +05:30
|
|
|
}
|
2021-08-02 04:31:24 +05:30
|
|
|
|
2021-09-29 19:59:17 +05:30
|
|
|
Spacer()
|
|
|
|
}
|
2021-10-23 04:34:03 +05:30
|
|
|
.lineLimit(1)
|
2021-08-03 02:40:22 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
2021-09-29 19:59:17 +05:30
|
|
|
#endif
|
2021-07-28 04:10:04 +05:30
|
|
|
|
|
|
|
var verticalRow: some View {
|
2021-09-19 02:06:42 +05:30
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
2021-08-02 04:31:24 +05:30
|
|
|
thumbnail
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
2021-11-05 04:55:51 +05:30
|
|
|
Group {
|
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
videoDetail(video.title, lineLimit: channelOnThumbnail ? 3 : 2)
|
|
|
|
#if os(tvOS)
|
|
|
|
.frame(minHeight: 60, alignment: .top)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.frame(minHeight: 32, alignment: .top)
|
|
|
|
#else
|
|
|
|
.frame(minHeight: 40, alignment: .top)
|
|
|
|
#endif
|
|
|
|
if !channelOnThumbnail {
|
|
|
|
Text(video.channel.name)
|
|
|
|
.fontWeight(.semibold)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.padding(.top, 4)
|
|
|
|
.padding(.bottom, 6)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 04:31:24 +05:30
|
|
|
#if os(tvOS)
|
2021-11-05 04:55:51 +05:30
|
|
|
.frame(minHeight: channelOnThumbnail ? 80 : 120, alignment: .top)
|
2021-08-03 02:40:22 +05:30
|
|
|
#elseif os(macOS)
|
2021-11-05 04:55:51 +05:30
|
|
|
.frame(minHeight: 60, alignment: .top)
|
2021-08-03 02:40:22 +05:30
|
|
|
#else
|
2021-11-05 04:55:51 +05:30
|
|
|
.frame(minHeight: 80, alignment: .top)
|
2021-08-02 04:31:24 +05:30
|
|
|
#endif
|
2021-10-06 01:50:09 +05:30
|
|
|
.padding(.bottom, 4)
|
2021-07-28 04:10:04 +05:30
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
HStack(spacing: 8) {
|
|
|
|
if let date = video.publishedDate {
|
2021-11-05 04:55:51 +05:30
|
|
|
HStack(spacing: 2) {
|
|
|
|
Image(systemName: "calendar")
|
|
|
|
Text(date)
|
|
|
|
.allowsTightening(true)
|
|
|
|
}
|
2021-11-02 03:26:18 +05:30
|
|
|
}
|
2021-09-19 02:06:42 +05:30
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
if video.views > 0 {
|
2021-11-05 04:55:51 +05:30
|
|
|
HStack(spacing: 2) {
|
|
|
|
Image(systemName: "eye")
|
|
|
|
Text(video.viewsCount!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let time = video.length.formattedAsPlaybackTime(), !timeOnThumbnail {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack(spacing: 2) {
|
|
|
|
Image(systemName: "clock")
|
|
|
|
Text(time)
|
|
|
|
}
|
2021-09-14 02:11:16 +05:30
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
2021-11-05 04:55:51 +05:30
|
|
|
.lineLimit(1)
|
2021-11-02 03:26:18 +05:30
|
|
|
.foregroundColor(.secondary)
|
2021-09-14 02:11:16 +05:30
|
|
|
.frame(minHeight: 30, alignment: .top)
|
2021-09-25 13:48:22 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.bottom, 10)
|
|
|
|
#endif
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
2021-09-19 02:06:42 +05:30
|
|
|
.padding(.top, 4)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .topLeading)
|
2021-08-02 04:31:24 +05:30
|
|
|
#if os(tvOS)
|
2021-08-03 02:40:22 +05:30
|
|
|
.padding(.horizontal, 8)
|
2021-08-02 04:31:24 +05:30
|
|
|
#endif
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var additionalDetailsAvailable: Bool {
|
2021-11-05 04:55:51 +05:30
|
|
|
video.publishedDate != nil || video.views != 0 || (!timeOnThumbnail && !video.length.formattedAsPlaybackTime().isNil)
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
|
|
|
|
2021-08-02 04:31:24 +05:30
|
|
|
var thumbnail: some View {
|
2021-08-01 03:40:56 +05:30
|
|
|
ZStack(alignment: .leading) {
|
2021-10-25 03:56:25 +05:30
|
|
|
thumbnailImage(quality: mediumQualityThumbnail ? .medium : .maxresdefault)
|
2021-07-28 04:10:04 +05:30
|
|
|
|
|
|
|
VStack {
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
if video.live {
|
|
|
|
DetailBadge(text: "Live", style: .outstanding)
|
|
|
|
} else if video.upcoming {
|
|
|
|
DetailBadge(text: "Upcoming", style: .informational)
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-11-05 04:55:51 +05:30
|
|
|
if channelOnThumbnail {
|
|
|
|
DetailBadge(text: video.author, style: .prominent)
|
|
|
|
}
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
|
|
|
.padding(10)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
Spacer()
|
|
|
|
|
2021-11-05 04:55:51 +05:30
|
|
|
if timeOnThumbnail, let time = video.length.formattedAsPlaybackTime() {
|
2021-07-28 04:10:04 +05:30
|
|
|
DetailBadge(text: time, style: .prominent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(10)
|
|
|
|
}
|
2021-10-23 04:34:03 +05:30
|
|
|
.lineLimit(1)
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 04:31:24 +05:30
|
|
|
func thumbnailImage(quality: Thumbnail.Quality) -> some View {
|
2021-10-25 03:56:25 +05:30
|
|
|
Group {
|
|
|
|
if let url = thumbnails.loadableURL(video.thumbnailURL(quality: quality)) {
|
|
|
|
WebImage(url: url)
|
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
Rectangle().fill(Color("PlaceholderColor"))
|
|
|
|
}
|
|
|
|
.retryOnAppear(false)
|
|
|
|
.onFailure { _ in
|
|
|
|
if let url = video.thumbnailURL(quality: quality) {
|
|
|
|
thumbnails.insertUnloadable(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !thumbnails.isUnloadable(video.thumbnailURL(quality: .medium)) {
|
|
|
|
mediumQualityThumbnail = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.indicator(.activity)
|
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
.frame(minHeight: 320)
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
ZStack {
|
|
|
|
Color("PlaceholderColor")
|
|
|
|
Image(systemName: "exclamationmark.triangle")
|
|
|
|
}
|
|
|
|
.font(.system(size: 30))
|
2021-10-22 04:59:10 +05:30
|
|
|
}
|
2021-10-25 03:56:25 +05:30
|
|
|
}
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
|
|
|
.modifier(AspectRatioModifier())
|
2021-07-28 04:10:04 +05:30
|
|
|
}
|
|
|
|
|
2021-08-02 04:31:24 +05:30
|
|
|
func videoDetail(_ text: String, lineLimit: Int = 1) -> some View {
|
2021-07-28 04:10:04 +05:30
|
|
|
Text(text)
|
2021-08-02 04:31:24 +05:30
|
|
|
.fontWeight(.bold)
|
2021-07-28 04:10:04 +05:30
|
|
|
.lineLimit(lineLimit)
|
|
|
|
.truncationMode(.middle)
|
2021-07-28 02:56:52 +05:30
|
|
|
}
|
2021-08-03 02:40:22 +05:30
|
|
|
|
2021-09-19 02:06:42 +05:30
|
|
|
struct AspectRatioModifier: ViewModifier {
|
|
|
|
@Environment(\.horizontalCells) private var horizontalCells
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
|
|
|
Group {
|
|
|
|
if horizontalCells {
|
|
|
|
content
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
.aspectRatio(1.777, contentMode: .fill)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 02:56:52 +05:30
|
|
|
}
|
2021-10-22 04:59:10 +05:30
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
struct VideoCell_Preview: PreviewProvider {
|
2021-10-22 04:59:10 +05:30
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
VideoCell(video: Video.fixture)
|
|
|
|
}
|
2021-11-05 04:55:51 +05:30
|
|
|
#if os(macOS)
|
|
|
|
.frame(maxWidth: 600, maxHeight: 400)
|
|
|
|
#elseif os(iOS)
|
|
|
|
.frame(maxWidth: 300, maxHeight: 200)
|
|
|
|
#endif
|
2021-10-22 04:59:10 +05:30
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|