1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Apple TV/VideoListRowView.swift

178 lines
5.3 KiB
Swift
Raw Normal View History

2021-06-11 04:20:10 +05:30
import SwiftUI
import URLImage
import URLImageStore
2021-06-28 20:32:13 +05:30
struct VideoListRowView: View {
2021-06-11 05:35:59 +05:30
@Environment(\.isFocused) private var focused: Bool
2021-06-11 04:20:10 +05:30
2021-07-12 02:22:49 +05:30
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
2021-06-11 04:20:10 +05:30
var video: Video
var body: some View {
2021-07-12 04:53:04 +05:30
#if os(tvOS) || os(macOS)
2021-07-12 02:22:49 +05:30
NavigationLink(destination: PlayerView(id: video.id)) {
2021-07-12 04:53:04 +05:30
#if os(tvOS)
horizontalRow(detailsOnThumbnail: false)
#else
verticalRow
#endif
2021-07-12 02:22:49 +05:30
}
#else
ZStack {
if verticalSizeClass == .compact {
2021-07-12 04:53:04 +05:30
horizontalRow(padding: 4)
2021-07-12 02:22:49 +05:30
} else {
2021-07-12 04:53:04 +05:30
verticalRow
2021-07-12 02:22:49 +05:30
}
2021-06-11 05:35:59 +05:30
2021-07-12 02:22:49 +05:30
NavigationLink(destination: PlayerView(id: video.id)) {
EmptyView()
}
.buttonStyle(PlainButtonStyle())
.opacity(0)
.frame(height: 0)
}
#endif
}
2021-06-11 04:20:10 +05:30
2021-07-12 04:53:04 +05:30
func horizontalRow(detailsOnThumbnail: Bool = true, padding: Double = 0) -> some View {
HStack(alignment: .top, spacing: 2) {
if detailsOnThumbnail {
thumbnailWithDetails
.padding(padding)
} else {
thumbnail
.frame(width: 320, height: 180)
}
2021-06-11 05:35:59 +05:30
2021-07-12 04:53:04 +05:30
VStack(alignment: .leading, spacing: 0) {
videoDetail(video.title, bold: true)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
if !detailsOnThumbnail {
videoDetail(video.author, color: .secondary, bold: true)
}
Spacer()
additionalDetails
}
.padding()
.frame(minHeight: 180)
if !detailsOnThumbnail, let time = video.playTime {
Spacer()
VStack(alignment: .center) {
Spacer()
HStack(spacing: 8) {
Image(systemName: "clock")
Text(time)
.fontWeight(.bold)
2021-06-11 05:35:59 +05:30
}
2021-07-12 04:53:04 +05:30
Spacer()
2021-07-12 02:22:49 +05:30
}
2021-07-12 04:53:04 +05:30
.foregroundColor(.secondary)
2021-07-12 02:22:49 +05:30
}
}
}
2021-07-12 04:53:04 +05:30
var verticalRow: some View {
2021-07-12 02:22:49 +05:30
VStack(alignment: .leading) {
thumbnailWithDetails
.frame(minWidth: 0, maxWidth: 600)
.padding([.leading, .top, .trailing], 4)
VStack(alignment: .leading) {
videoDetail(video.title, bold: true)
.padding(.bottom)
additionalDetails
.padding(.bottom, 10)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 8)
}
}
2021-07-12 04:53:04 +05:30
var additionalDetails: some View {
VStack {
if !video.published.isEmpty || video.views != 0 {
HStack(spacing: 8) {
if !video.published.isEmpty {
Image(systemName: "calendar")
Text(video.published)
}
if video.views != 0 {
Image(systemName: "eye")
Text(video.viewsCount)
2021-07-12 02:22:49 +05:30
}
}
2021-07-12 04:53:04 +05:30
#if os(tvOS)
.foregroundColor(.secondary)
#else
.foregroundColor(focused ? .white : .secondary)
#endif
}
}
}
var thumbnailWithDetails: some View {
ZStack(alignment: .trailing) {
thumbnail
2021-07-12 02:22:49 +05:30
2021-07-12 04:53:04 +05:30
VStack(alignment: .trailing) {
Text(video.author)
.padding(8)
.background(.thinMaterial)
.mask(RoundedRectangle(cornerRadius: 12))
.offset(x: -5, y: 5)
Spacer()
if let time = video.playTime {
Text(time)
.fontWeight(.bold)
2021-07-12 02:22:49 +05:30
.padding(8)
.background(.thinMaterial)
.mask(RoundedRectangle(cornerRadius: 12))
2021-07-12 04:53:04 +05:30
.offset(x: -5, y: -5)
2021-06-11 05:35:59 +05:30
}
2021-06-11 04:20:10 +05:30
}
}
}
2021-07-12 04:53:04 +05:30
var thumbnail: some View {
Group {
if let thumbnail = video.thumbnailURL(quality: "maxres") {
2021-07-12 02:22:49 +05:30
// to replace with AsyncImage when it is fixed with lazy views
URLImage(thumbnail) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
}
.mask(RoundedRectangle(cornerRadius: 12))
} else {
Image(systemName: "exclamationmark.square")
}
}
2021-07-12 04:53:04 +05:30
.frame(minWidth: 320, maxWidth: .infinity, minHeight: 180, maxHeight: .infinity)
2021-07-12 02:22:49 +05:30
}
func videoDetail(_ text: String, color: Color? = .primary, bold: Bool = false) -> some View {
Text(text)
.fontWeight(bold ? .bold : .regular)
#if os(tvOS)
.foregroundColor(color)
.lineLimit(1)
.truncationMode(.middle)
#elseif os(iOS) || os(macOS)
.foregroundColor(focused ? .white : color)
#endif
}
}