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

275 lines
7.8 KiB
Swift
Raw Normal View History

2021-12-05 01:05:41 +05:30
import SDWebImageSwiftUI
import SwiftUI
struct CommentView: View {
let comment: Comment
@Binding var repliesID: Comment.ID?
2021-12-18 01:16:49 +05:30
@State private var subscribed = false
2021-12-05 01:05:41 +05:30
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
#endif
2021-12-18 01:16:49 +05:30
@Environment(\.colorScheme) private var colorScheme
2021-12-05 01:05:41 +05:30
@Environment(\.navigationStyle) private var navigationStyle
@EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<RecentsModel> private var recents
2021-12-18 01:16:49 +05:30
@EnvironmentObject<SubscriptionsModel> private var subscriptions
2021-12-05 01:05:41 +05:30
var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .center, spacing: 10) {
2021-12-18 01:16:49 +05:30
HStack(spacing: 10) {
ZStack(alignment: .bottomTrailing) {
authorAvatar
if subscribed {
Image(systemName: "star.circle.fill")
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
#else
.background(Color.background)
#endif
.clipShape(Circle())
.foregroundColor(.secondary)
}
}
.onAppear {
subscribed = subscriptions.isSubscribing(comment.channel.id)
}
2021-12-05 01:05:41 +05:30
2021-12-18 01:16:49 +05:30
authorAndTime
}
.contextMenu {
Button(action: openChannelAction) {
Label("\(comment.channel.name) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
}
2021-12-05 01:05:41 +05:30
2021-12-18 01:16:49 +05:30
Spacer()
2021-12-05 01:05:41 +05:30
2021-12-18 01:16:49 +05:30
Group {
#if os(iOS)
if horizontalSizeClass == .regular {
Group {
statusIcons
likes
2021-12-05 01:05:41 +05:30
}
} else {
2021-12-18 01:16:49 +05:30
VStack(alignment: .trailing, spacing: 8) {
likes
statusIcons
2021-12-05 01:05:41 +05:30
}
}
2021-12-18 01:16:49 +05:30
#else
2021-12-05 01:05:41 +05:30
statusIcons
likes
2021-12-18 01:16:49 +05:30
#endif
}
2021-12-05 01:05:41 +05:30
}
2021-12-18 01:16:49 +05:30
#if os(tvOS)
.font(.system(size: 25).bold())
#else
.font(.system(size: 15))
#endif
2021-12-05 01:05:41 +05:30
Group {
commentText
if comment.hasReplies {
HStack(spacing: repliesButtonStackSpacing) {
repliesButton
ProgressView()
.scaleEffect(progressViewScale, anchor: .center)
.opacity(repliesID == comment.id && !comments.repliesLoaded ? 1 : 0)
.frame(maxHeight: 0)
}
2021-12-05 01:05:41 +05:30
if comment.id == repliesID {
repliesList
}
}
}
}
#if os(tvOS)
.padding(.horizontal, 20)
#endif
}
private var authorAvatar: some View {
WebImage(url: URL(string: comment.authorAvatarURL)!)
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
2021-12-25 00:51:11 +05:30
.retryOnAppear(true)
2021-12-05 01:05:41 +05:30
.indicator(.activity)
.mask(RoundedRectangle(cornerRadius: 60))
#if os(tvOS)
2021-12-18 01:16:49 +05:30
.frame(width: 80, height: 80, alignment: .leading)
2021-12-05 01:05:41 +05:30
.focusable()
2021-12-18 01:16:49 +05:30
#else
.frame(width: 45, height: 45, alignment: .leading)
2021-12-05 01:05:41 +05:30
#endif
}
private var authorAndTime: some View {
VStack(alignment: .leading) {
Text(comment.author)
2021-12-18 01:16:49 +05:30
#if os(tvOS)
.font(.system(size: 30).bold())
#else
.font(.system(size: 14).bold())
#endif
2021-12-05 01:05:41 +05:30
Text(comment.time)
2021-12-18 01:16:49 +05:30
.font(.caption2)
2021-12-05 01:05:41 +05:30
.foregroundColor(.secondary)
}
.lineLimit(1)
}
private var statusIcons: some View {
HStack(spacing: 15) {
if comment.pinned {
Image(systemName: "pin.fill")
}
if comment.hearted {
Image(systemName: "heart.fill")
}
}
2021-12-18 01:16:49 +05:30
#if !os(tvOS)
.font(.system(size: 12))
#endif
2021-12-05 01:05:41 +05:30
.foregroundColor(.secondary)
}
private var likes: some View {
Group {
if comment.likeCount > 0 {
HStack(spacing: 5) {
Image(systemName: "hand.thumbsup")
Text("\(comment.likeCount.formattedAsAbbreviation())")
}
2021-12-18 01:16:49 +05:30
#if !os(tvOS)
.font(.system(size: 12))
#endif
2021-12-05 01:05:41 +05:30
}
}
.foregroundColor(.secondary)
}
private var repliesButton: some View {
Button {
repliesID = repliesID == comment.id ? nil : comment.id
guard !repliesID.isNil, !comment.repliesPage.isNil else {
return
}
comments.loadReplies(page: comment.repliesPage!)
} label: {
HStack(spacing: 5) {
Image(systemName: repliesID == comment.id ? "arrow.turn.left.up" : "arrow.turn.right.down")
2021-12-05 01:05:41 +05:30
Text("Replies")
}
#if os(tvOS)
.padding(10)
#endif
}
.buttonStyle(.plain)
.padding(.vertical, 2)
2021-12-05 01:05:41 +05:30
#if os(tvOS)
.padding(.leading, 5)
#else
2021-12-18 01:16:49 +05:30
.font(.system(size: 13))
2021-12-05 01:05:41 +05:30
.foregroundColor(.secondary)
#endif
}
private var repliesButtonStackSpacing: Double {
#if os(tvOS)
24
#elseif os(iOS)
4
#else
2
#endif
}
private var progressViewScale: Double {
#if os(macOS)
0.4
#else
2021-12-18 01:16:49 +05:30
0.6
#endif
}
2021-12-05 01:05:41 +05:30
private var repliesList: some View {
Group {
let last = comments.replies.last
ForEach(comments.replies) { comment in
CommentView(comment: comment, repliesID: $repliesID)
#if os(tvOS)
.focusable()
#endif
if comment != last {
Divider()
.padding(.vertical, 5)
}
}
}
.padding(.leading, 22)
2021-12-05 01:05:41 +05:30
}
private var commentText: some View {
Group {
let text = Text(comment.text)
#if os(macOS)
.font(.system(size: 14))
#elseif os(iOS)
.font(.system(size: 15))
#endif
.lineSpacing(3)
.fixedSize(horizontal: false, vertical: true)
if #available(iOS 15.0, macOS 12.0, *) {
text
#if !os(tvOS)
.textSelection(.enabled)
#endif
} else {
text
}
}
}
private func openChannelAction() {
2021-12-18 01:16:49 +05:30
NavigationModel.openChannel(
comment.channel,
player: player,
recents: recents,
navigation: navigation,
navigationStyle: navigationStyle
)
2021-12-05 01:05:41 +05:30
}
}
struct CommentView_Previews: PreviewProvider {
static var fixture: Comment {
Comment.fixture
}
static var previews: some View {
CommentView(comment: fixture, repliesID: .constant(fixture.id))
2021-12-18 01:16:49 +05:30
.environmentObject(SubscriptionsModel())
.padding(5)
}
}