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

192 lines
6.0 KiB
Swift
Raw Normal View History

2021-08-23 00:43:33 +05:30
import Foundation
import SwiftUI
struct VideoDetails: View {
2021-08-26 03:42:59 +05:30
@EnvironmentObject<Subscriptions> private var subscriptions
@State private var subscribed = false
@State private var confirmationShown = false
2021-08-23 00:43:33 +05:30
var video: Video
var body: some View {
VStack(alignment: .leading) {
Text(video.title)
.font(.title2.bold())
2021-08-26 03:42:59 +05:30
.padding(.bottom, 0)
Divider()
2021-08-23 00:43:33 +05:30
2021-08-26 03:42:59 +05:30
HStack(alignment: .center) {
HStack(spacing: 4) {
if subscribed {
Image(systemName: "star.circle.fill")
}
VStack(alignment: .leading) {
Text(video.channel.name)
.font(.system(size: 13))
.bold()
if !video.channel.subscriptionsCount.isEmpty {
Text("\(video.channel.subscriptionsCount) subscribers")
.font(.caption2)
}
}
}
2021-08-23 00:43:33 +05:30
.foregroundColor(.secondary)
2021-08-26 03:42:59 +05:30
Spacer()
Section {
if subscribed {
Button("Unsubscribe") {
confirmationShown = true
}
#if os(iOS)
.tint(.gray)
#endif
.confirmationDialog("Are you you want to unsubscribe from \(video.channel.name)?", isPresented: $confirmationShown) {
Button("Unsubscribe") {
subscriptions.unsubscribe(video.channel.id)
withAnimation {
subscribed.toggle()
}
}
}
} else {
Button("Subscribe") {
subscriptions.subscribe(video.channel.id)
withAnimation {
subscribed.toggle()
}
}
.tint(.blue)
}
}
.font(.system(size: 13))
.buttonStyle(.borderless)
.buttonBorderShape(.roundedRectangle)
}
.padding(.bottom, -1)
Divider()
2021-08-23 00:43:33 +05:30
HStack(spacing: 4) {
if let published = video.publishedDate {
Text(published)
}
if let publishedAt = video.publishedAt {
if video.publishedDate != nil {
Text("")
.foregroundColor(.secondary)
.opacity(0.3)
}
Text(publishedAt.formatted(date: .abbreviated, time: .omitted))
}
}
.font(.system(size: 12))
2021-08-26 03:42:59 +05:30
.padding(.bottom, -1)
2021-08-23 00:43:33 +05:30
.foregroundColor(.secondary)
2021-08-26 03:42:59 +05:30
Divider()
2021-08-23 00:43:33 +05:30
HStack {
2021-08-26 03:42:59 +05:30
Spacer()
2021-08-23 00:43:33 +05:30
if let views = video.viewsCount {
2021-08-26 03:42:59 +05:30
videoDetail(label: "Views", value: views, symbol: "eye.fill")
2021-08-23 00:43:33 +05:30
}
if let likes = video.likesCount {
2021-08-26 03:42:59 +05:30
Divider()
videoDetail(label: "Likes", value: likes, symbol: "hand.thumbsup.circle.fill")
2021-08-23 00:43:33 +05:30
}
if let dislikes = video.dislikesCount {
2021-08-26 03:42:59 +05:30
Divider()
videoDetail(label: "Dislikes", value: dislikes, symbol: "hand.thumbsdown.circle.fill")
2021-08-23 00:43:33 +05:30
}
2021-08-26 03:42:59 +05:30
Spacer()
2021-08-23 00:43:33 +05:30
}
2021-08-26 03:42:59 +05:30
.frame(maxHeight: 35)
.foregroundColor(.secondary)
Divider()
2021-08-23 00:43:33 +05:30
#if os(macOS)
ScrollView(.vertical) {
Text(video.description)
.font(.caption)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 100, alignment: .leading)
}
#else
Text(video.description)
.font(.caption)
#endif
ScrollView(.horizontal, showsIndicators: showScrollIndicators) {
HStack {
ForEach(video.keywords, id: \.self) { keyword in
HStack(alignment: .center, spacing: 0) {
Text("#")
.font(.system(size: 11).bold())
Text(keyword)
.frame(maxWidth: 500)
}.foregroundColor(.white)
.padding(.vertical, 4)
.padding(.horizontal, 8)
.background(Color("VideoDetailLikesSymbolColor"))
.mask(RoundedRectangle(cornerRadius: 3))
.font(.caption)
}
}
.padding(.bottom, 10)
}
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding([.horizontal, .bottom])
2021-08-26 03:42:59 +05:30
.onAppear {
subscribed = subscriptions.subscribed(video.channel.id)
}
}
func videoDetail(label: String, value: String, symbol: String) -> some View {
VStack(spacing: 4) {
HStack(spacing: 2) {
Image(systemName: symbol)
Text(label.uppercased())
}
.font(.system(size: 9))
.opacity(0.6)
Text(value)
}
.frame(maxWidth: 100)
2021-08-23 00:43:33 +05:30
}
var showScrollIndicators: Bool {
#if os(macOS)
false
#else
true
#endif
}
}
2021-08-26 03:42:59 +05:30
struct VideoDetails_Previews: PreviewProvider {
static var previews: some View {
VideoDetails(video: Video.fixture)
.environmentObject(Subscriptions())
2021-08-23 00:43:33 +05:30
}
}