2021-12-04 19:35:41 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct CommentsView: View {
|
|
|
|
@State private var repliesID: Comment.ID?
|
|
|
|
|
|
|
|
@EnvironmentObject<CommentsModel> private var comments
|
|
|
|
|
|
|
|
var body: some View {
|
2021-12-05 17:14:49 +00:00
|
|
|
Group {
|
|
|
|
if comments.disabled {
|
|
|
|
Text("Comments are disabled for this video")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else if comments.loaded && comments.all.isEmpty {
|
|
|
|
Text("No comments")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
} else if !comments.loaded {
|
2021-12-29 18:39:38 +00:00
|
|
|
PlaceholderProgressView()
|
2021-12-17 19:46:49 +00:00
|
|
|
.onAppear {
|
|
|
|
comments.load()
|
|
|
|
}
|
2021-12-05 17:14:49 +00:00
|
|
|
} else {
|
|
|
|
ScrollView(.vertical, showsIndicators: false) {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
let last = comments.all.last
|
|
|
|
ForEach(comments.all) { comment in
|
|
|
|
CommentView(comment: comment, repliesID: $repliesID)
|
2021-12-04 19:35:41 +00:00
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
if comment != last {
|
|
|
|
Divider()
|
|
|
|
.padding(.vertical, 5)
|
|
|
|
}
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
HStack {
|
|
|
|
if comments.nextPageAvailable {
|
|
|
|
Button {
|
|
|
|
repliesID = nil
|
|
|
|
comments.loadNextPage()
|
|
|
|
} label: {
|
|
|
|
Label("Show more", systemImage: "arrow.turn.down.right")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !comments.firstPage {
|
|
|
|
Button {
|
|
|
|
repliesID = nil
|
|
|
|
comments.load(page: nil)
|
|
|
|
} label: {
|
|
|
|
Label("Show first", systemImage: "arrow.turn.down.left")
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
2021-12-17 19:46:49 +00:00
|
|
|
.font(.system(size: 13))
|
2021-12-05 17:14:49 +00:00
|
|
|
.buttonStyle(.plain)
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
.foregroundColor(.secondary)
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
2021-12-05 17:14:49 +00:00
|
|
|
}
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CommentsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
|
|
|
|
CommentsView()
|
|
|
|
.previewInterfaceOrientation(.landscapeRight)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
|
|
|
|
CommentsView()
|
|
|
|
.injectFixtureEnvironmentObjects()
|
|
|
|
}
|
|
|
|
}
|