2021-12-04 19:35:41 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
final class CommentsModel: ObservableObject {
|
|
|
|
@Published var all = [Comment]()
|
|
|
|
|
|
|
|
@Published var nextPage: String?
|
|
|
|
@Published var firstPage = true
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
@Published var loaded = true
|
|
|
|
@Published var disabled = false
|
|
|
|
|
|
|
|
@Published var replies = [Comment]()
|
2021-12-07 23:06:59 +00:00
|
|
|
@Published var repliesPageID: String?
|
2021-12-05 17:14:49 +00:00
|
|
|
@Published var repliesLoaded = false
|
2021-12-04 19:35:41 +00:00
|
|
|
|
|
|
|
var accounts: AccountsModel!
|
|
|
|
var player: PlayerModel!
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
var instance: Instance? {
|
|
|
|
InstancesModel.find(Defaults[.commentsInstanceID])
|
|
|
|
}
|
|
|
|
|
|
|
|
var api: VideosAPI? {
|
|
|
|
instance.isNil ? nil : PipedAPI(account: instance!.anonymousAccount)
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
static var enabled: Bool {
|
2021-12-05 17:10:33 +00:00
|
|
|
!Defaults[.commentsInstanceID].isNil && !Defaults[.commentsInstanceID]!.isEmpty
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:11:19 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
static var placement: CommentsPlacement {
|
|
|
|
Defaults[.commentsPlacement]
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
var nextPageAvailable: Bool {
|
|
|
|
!(nextPage?.isEmpty ?? true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func load(page: String? = nil) {
|
|
|
|
guard Self.enabled else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
reset()
|
2021-12-04 19:35:41 +00:00
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
guard !instance.isNil,
|
|
|
|
!(player?.currentVideo.isNil ?? true)
|
2021-12-04 19:35:41 +00:00
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
firstPage = page.isNil || page!.isEmpty
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
api?.comments(player.currentVideo!.videoID, page: page)?
|
2021-12-04 19:35:41 +00:00
|
|
|
.load()
|
|
|
|
.onSuccess { [weak self] response in
|
|
|
|
if let page: CommentsPage = response.typedContent() {
|
|
|
|
self?.all = page.comments
|
|
|
|
self?.nextPage = page.nextPage
|
2021-12-05 17:14:49 +00:00
|
|
|
self?.disabled = page.disabled
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.onCompletion { [weak self] _ in
|
|
|
|
self?.loaded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadNextPage() {
|
|
|
|
load(page: nextPage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadReplies(page: String) {
|
|
|
|
guard !player.currentVideo.isNil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-07 23:06:59 +00:00
|
|
|
if page == repliesPageID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
replies = []
|
2021-12-07 23:06:59 +00:00
|
|
|
repliesPageID = page
|
2021-12-05 17:14:49 +00:00
|
|
|
repliesLoaded = false
|
2021-12-04 19:35:41 +00:00
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
api?.comments(player.currentVideo!.videoID, page: page)?
|
|
|
|
.load()
|
|
|
|
.onSuccess { [weak self] response in
|
|
|
|
if let page: CommentsPage = response.typedContent() {
|
|
|
|
self?.replies = page.comments
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onCompletion { [weak self] _ in
|
|
|
|
self?.repliesLoaded = true
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 17:14:49 +00:00
|
|
|
func reset() {
|
2021-12-04 19:35:41 +00:00
|
|
|
all = []
|
2021-12-05 17:14:49 +00:00
|
|
|
disabled = false
|
2021-12-04 19:35:41 +00:00
|
|
|
firstPage = true
|
|
|
|
nextPage = nil
|
|
|
|
loaded = false
|
2021-12-05 17:14:49 +00:00
|
|
|
replies = []
|
|
|
|
repliesLoaded = false
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
}
|