2021-07-27 22:40:04 +00:00
|
|
|
import Defaults
|
2021-10-21 23:29:10 +00:00
|
|
|
import SDWebImageSwiftUI
|
2021-07-27 21:26:52 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2021-10-21 23:29:10 +00:00
|
|
|
struct VideoCell: View {
|
2021-10-05 20:20:09 +00:00
|
|
|
var video: Video
|
|
|
|
|
|
|
|
@State private var playerNavigationLinkActive = false
|
2021-10-21 23:29:10 +00:00
|
|
|
@State private var lowQualityThumbnail = false
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
@Environment(\.inNavigationView) private var inNavigationView
|
2021-07-27 22:40:04 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
2021-09-29 14:29:17 +00:00
|
|
|
@Environment(\.horizontalCells) private var horizontalCells
|
2021-07-27 22:40:04 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-07-27 21:26:52 +00:00
|
|
|
var body: some View {
|
2021-08-31 21:17:50 +00:00
|
|
|
Group {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button(action: {
|
|
|
|
player.playNow(video)
|
|
|
|
|
|
|
|
if inNavigationView {
|
|
|
|
playerNavigationLinkActive = true
|
|
|
|
} else {
|
|
|
|
player.presentPlayer()
|
2021-08-02 21:10:22 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}) {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
|
|
|
|
NavigationLink(isActive: $playerNavigationLinkActive, destination: {
|
|
|
|
VideoPlayerView()
|
|
|
|
.environment(\.inNavigationView, true)
|
|
|
|
}) {
|
|
|
|
EmptyView()
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:28:42 +00:00
|
|
|
.buttonStyle(.plain)
|
2021-08-02 21:10:22 +00:00
|
|
|
.contentShape(RoundedRectangle(cornerRadius: 12))
|
2021-10-05 20:20:09 +00:00
|
|
|
.contextMenu { VideoContextMenuView(video: video, playerNavigationLinkActive: $playerNavigationLinkActive) }
|
2021-08-01 23:01:24 +00:00
|
|
|
}
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
var content: some View {
|
|
|
|
VStack {
|
2021-09-26 22:28:42 +00:00
|
|
|
#if os(iOS)
|
|
|
|
if verticalSizeClass == .compact, !horizontalCells {
|
|
|
|
horizontalRow
|
|
|
|
.padding(.vertical, 4)
|
|
|
|
} else {
|
2021-08-31 21:17:50 +00:00
|
|
|
verticalRow
|
2021-09-26 22:28:42 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
verticalRow
|
|
|
|
#endif
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.background()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
#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-27 22:40:04 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
|
|
videoDetail(video.title, lineLimit: 5)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
videoDetail(video.author)
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
if additionalDetailsAvailable {
|
|
|
|
Spacer()
|
2021-09-18 20:36:42 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
HStack {
|
|
|
|
if let date = video.publishedDate {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "calendar")
|
|
|
|
Text(date)
|
|
|
|
}
|
2021-09-18 20:36:42 +00:00
|
|
|
}
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
if video.views != 0 {
|
|
|
|
VStack {
|
|
|
|
Image(systemName: "eye")
|
|
|
|
Text(video.viewsCount!)
|
|
|
|
}
|
2021-09-18 20:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
.foregroundColor(.secondary)
|
2021-09-18 20:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
.padding()
|
|
|
|
.frame(minHeight: 180)
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
if video.playTime != nil || video.live || video.upcoming {
|
2021-08-02 21:10:22 +00:00
|
|
|
Spacer()
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
VStack(alignment: .center) {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
if let time = video.playTime {
|
|
|
|
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-02 21:10:22 +00:00
|
|
|
}
|
2021-08-01 23:01:24 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
Spacer()
|
|
|
|
}
|
2021-08-02 21:10:22 +00:00
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
2021-09-29 14:29:17 +00:00
|
|
|
#endif
|
2021-07-27 22:40:04 +00:00
|
|
|
|
|
|
|
var verticalRow: some View {
|
2021-09-18 20:36:42 +00:00
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
2021-08-01 23:01:24 +00:00
|
|
|
thumbnail
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
2021-08-01 23:01:24 +00:00
|
|
|
videoDetail(video.title, lineLimit: additionalDetailsAvailable ? 2 : 3)
|
|
|
|
#if os(tvOS)
|
2021-08-02 21:10:22 +00:00
|
|
|
.frame(minHeight: additionalDetailsAvailable ? 80 : 120, alignment: .top)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.frame(minHeight: 30, alignment: .top)
|
|
|
|
#else
|
|
|
|
.frame(minHeight: 50, alignment: .top)
|
2021-08-01 23:01:24 +00:00
|
|
|
#endif
|
2021-10-05 20:20:09 +00:00
|
|
|
.padding(.bottom, 4)
|
2021-07-27 22:40:04 +00:00
|
|
|
|
2021-09-13 20:41:16 +00:00
|
|
|
Group {
|
|
|
|
if additionalDetailsAvailable {
|
2021-09-18 20:36:42 +00:00
|
|
|
HStack(spacing: 8) {
|
|
|
|
if let date = video.publishedDate {
|
|
|
|
Image(systemName: "calendar")
|
|
|
|
Text(date)
|
|
|
|
}
|
|
|
|
|
|
|
|
if video.views != 0 {
|
|
|
|
Image(systemName: "eye")
|
|
|
|
Text(video.viewsCount!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(.secondary)
|
2021-09-13 20:41:16 +00:00
|
|
|
} else {
|
|
|
|
Spacer()
|
|
|
|
}
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
2021-09-13 20:41:16 +00:00
|
|
|
.frame(minHeight: 30, alignment: .top)
|
2021-09-25 08:18:22 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
.padding(.bottom, 10)
|
|
|
|
#endif
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
2021-09-18 20:36:42 +00:00
|
|
|
.padding(.top, 4)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, alignment: .topLeading)
|
2021-08-01 23:01:24 +00:00
|
|
|
#if os(tvOS)
|
2021-08-02 21:10:22 +00:00
|
|
|
.padding(.horizontal, 8)
|
2021-08-01 23:01:24 +00:00
|
|
|
#endif
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var additionalDetailsAvailable: Bool {
|
|
|
|
video.publishedDate != nil || video.views != 0
|
|
|
|
}
|
|
|
|
|
2021-08-01 23:01:24 +00:00
|
|
|
var thumbnail: some View {
|
2021-07-31 22:10:56 +00:00
|
|
|
ZStack(alignment: .leading) {
|
2021-10-21 23:29:10 +00:00
|
|
|
thumbnailImage(quality: lowQualityThumbnail ? .medium : .maxresdefault)
|
2021-07-27 22:40:04 +00:00
|
|
|
|
|
|
|
VStack {
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
if video.live {
|
|
|
|
DetailBadge(text: "Live", style: .outstanding)
|
|
|
|
} else if video.upcoming {
|
|
|
|
DetailBadge(text: "Upcoming", style: .informational)
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
DetailBadge(text: video.author, style: .prominent)
|
|
|
|
}
|
|
|
|
.padding(10)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
if let time = video.playTime {
|
|
|
|
DetailBadge(text: time, style: .prominent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 23:01:24 +00:00
|
|
|
func thumbnailImage(quality: Thumbnail.Quality) -> some View {
|
2021-10-21 23:29:10 +00:00
|
|
|
WebImage(url: video.thumbnailURL(quality: quality))
|
|
|
|
.resizable()
|
|
|
|
.placeholder {
|
|
|
|
Rectangle().fill(Color("PlaceholderColor"))
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
2021-10-21 23:29:10 +00:00
|
|
|
.onFailure { _ in
|
|
|
|
lowQualityThumbnail = true
|
|
|
|
}
|
|
|
|
.indicator(.progress)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 12))
|
|
|
|
.modifier(AspectRatioModifier())
|
2021-08-02 21:10:22 +00:00
|
|
|
#if os(tvOS)
|
2021-09-26 22:28:42 +00:00
|
|
|
.frame(minHeight: 320)
|
2021-08-02 21:10:22 +00:00
|
|
|
#endif
|
2021-07-27 22:40:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 23:01:24 +00:00
|
|
|
func videoDetail(_ text: String, lineLimit: Int = 1) -> some View {
|
2021-07-27 22:40:04 +00:00
|
|
|
Text(text)
|
2021-08-01 23:01:24 +00:00
|
|
|
.fontWeight(.bold)
|
2021-07-27 22:40:04 +00:00
|
|
|
.lineLimit(lineLimit)
|
|
|
|
.truncationMode(.middle)
|
2021-07-27 21:26:52 +00:00
|
|
|
}
|
2021-08-02 21:10:22 +00:00
|
|
|
|
2021-09-18 20:36:42 +00:00
|
|
|
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-27 21:26:52 +00:00
|
|
|
}
|
2021-10-21 23:29:10 +00:00
|
|
|
|
|
|
|
struct VideoView_Preview: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
VideoCell(video: Video.fixture)
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 300, maxHeight: 200)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|