2021-12-05 01:05:41 +05:30
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
final class CommentsModel: ObservableObject {
|
2022-11-25 02:06:05 +05:30
|
|
|
static let shared = CommentsModel()
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
@Published var all = [Comment]()
|
|
|
|
|
|
|
|
@Published var nextPage: String?
|
|
|
|
@Published var firstPage = true
|
|
|
|
|
2021-12-18 01:16:49 +05:30
|
|
|
@Published var loaded = false
|
2021-12-05 22:44:49 +05:30
|
|
|
@Published var disabled = false
|
|
|
|
|
|
|
|
@Published var replies = [Comment]()
|
2021-12-08 04:36:59 +05:30
|
|
|
@Published var repliesPageID: String?
|
2021-12-05 22:44:49 +05:30
|
|
|
@Published var repliesLoaded = false
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
var player = PlayerModel.shared
|
|
|
|
var accounts = AccountsModel.shared
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2022-07-02 03:44:04 +05:30
|
|
|
var instance: Instance? {
|
2022-11-25 02:06:05 +05:30
|
|
|
accounts.current?.instance
|
2021-12-05 22:44:49 +05:30
|
|
|
}
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
var nextPageAvailable: Bool {
|
|
|
|
!(nextPage?.isEmpty ?? true)
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:22:15 +05:30
|
|
|
func loadIfNeeded() {
|
|
|
|
guard !loaded else { return }
|
|
|
|
load()
|
|
|
|
}
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
func load(page: String? = nil) {
|
2022-07-02 03:44:04 +05:30
|
|
|
guard let video = player.currentVideo else { return }
|
2024-04-01 17:19:09 +05:30
|
|
|
guard firstPage || nextPageAvailable else { return }
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2023-04-22 18:38:33 +05:30
|
|
|
player
|
|
|
|
.playerAPI(video)?
|
|
|
|
.comments(video.videoID, page: page)?
|
2021-12-05 01:05:41 +05:30
|
|
|
.load()
|
|
|
|
.onSuccess { [weak self] response in
|
2024-08-18 18:16:51 +05:30
|
|
|
guard let self else { return }
|
2024-04-01 17:19:09 +05:30
|
|
|
if let commentsPage: CommentsPage = response.typedContent() {
|
|
|
|
self.all += commentsPage.comments
|
|
|
|
self.nextPage = commentsPage.nextPage
|
|
|
|
self.disabled = commentsPage.disabled
|
2021-12-05 01:05:41 +05:30
|
|
|
}
|
|
|
|
}
|
2024-04-01 17:30:02 +05:30
|
|
|
.onFailure { [weak self] _ in
|
|
|
|
self?.disabled = true
|
2022-07-02 03:44:04 +05:30
|
|
|
}
|
2021-12-05 01:05:41 +05:30
|
|
|
.onCompletion { [weak self] _ in
|
|
|
|
self?.loaded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:42:32 +05:30
|
|
|
func loadNextPageIfNeeded(current comment: Comment) {
|
|
|
|
let thresholdIndex = all.index(all.endIndex, offsetBy: -5)
|
|
|
|
if all.firstIndex(where: { $0 == comment }) == thresholdIndex {
|
|
|
|
loadNextPage()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
func loadNextPage() {
|
2023-06-08 02:26:29 +05:30
|
|
|
guard nextPageAvailable else { return }
|
2021-12-05 01:05:41 +05:30
|
|
|
load(page: nextPage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadReplies(page: String) {
|
|
|
|
guard !player.currentVideo.isNil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-08 04:36:59 +05:30
|
|
|
if page == repliesPageID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-05 01:05:41 +05:30
|
|
|
replies = []
|
2021-12-08 04:36:59 +05:30
|
|
|
repliesPageID = page
|
2021-12-05 22:44:49 +05:30
|
|
|
repliesLoaded = false
|
2021-12-05 01:05:41 +05:30
|
|
|
|
2022-11-25 02:06:05 +05:30
|
|
|
accounts.api.comments(player.currentVideo!.videoID, page: page)?
|
2021-12-05 22:44:49 +05:30
|
|
|
.load()
|
|
|
|
.onSuccess { [weak self] response in
|
|
|
|
if let page: CommentsPage = response.typedContent() {
|
|
|
|
self?.replies = page.comments
|
2021-12-18 01:16:49 +05:30
|
|
|
self?.repliesLoaded = true
|
2021-12-05 22:44:49 +05:30
|
|
|
}
|
|
|
|
}
|
2021-12-18 01:16:49 +05:30
|
|
|
.onFailure { [weak self] _ in
|
2021-12-05 22:44:49 +05:30
|
|
|
self?.repliesLoaded = true
|
2021-12-05 01:05:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 22:44:49 +05:30
|
|
|
func reset() {
|
2021-12-05 01:05:41 +05:30
|
|
|
all = []
|
2021-12-05 22:44:49 +05:30
|
|
|
disabled = false
|
2021-12-05 01:05:41 +05:30
|
|
|
firstPage = true
|
|
|
|
nextPage = nil
|
|
|
|
loaded = false
|
2021-12-05 22:44:49 +05:30
|
|
|
replies = []
|
|
|
|
repliesLoaded = false
|
2021-12-05 01:05:41 +05:30
|
|
|
}
|
|
|
|
}
|