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

120 lines
2.9 KiB
Swift
Raw Normal View History

2021-12-05 01:05:41 +05:30
import Defaults
import Foundation
import SwiftyJSON
final class CommentsModel: ObservableObject {
@Published var all = [Comment]()
@Published var nextPage: String?
@Published var firstPage = true
2021-12-18 01:16:49 +05:30
@Published var loaded = false
@Published var disabled = false
@Published var replies = [Comment]()
@Published var repliesPageID: String?
@Published var repliesLoaded = false
2021-12-05 01:05:41 +05:30
var player: PlayerModel!
2021-12-18 01:16:49 +05:30
static var instance: Instance? {
InstancesModel.find(Defaults[.commentsInstanceID])
}
var api: VideosAPI? {
2021-12-18 01:16:49 +05:30
Self.instance.isNil ? nil : PipedAPI(account: Self.instance!.anonymousAccount)
}
2021-12-05 01:05:41 +05:30
static var enabled: Bool {
2021-12-18 01:16:49 +05:30
!instance.isNil
2021-12-05 01:05:41 +05:30
}
#if !os(tvOS)
static var placement: CommentsPlacement {
Defaults[.commentsPlacement]
}
#endif
2021-12-05 01:05:41 +05:30
var nextPageAvailable: Bool {
!(nextPage?.isEmpty ?? true)
}
func load(page: String? = nil) {
guard Self.enabled, !loaded else {
2021-12-05 01:05:41 +05:30
return
}
2021-12-18 01:16:49 +05:30
guard !Self.instance.isNil,
let video = player.currentVideo
2021-12-05 01:05:41 +05:30
else {
return
}
2022-01-05 21:42:32 +05:30
if !firstPage && !nextPageAvailable {
return
}
2021-12-05 01:05:41 +05:30
firstPage = page.isNil || page!.isEmpty
api?.comments(video.videoID, page: page)?
2021-12-05 01:05:41 +05:30
.load()
.onSuccess { [weak self] response in
if let page: CommentsPage = response.typedContent() {
2022-01-05 21:42:32 +05:30
self?.all += page.comments
2021-12-05 01:05:41 +05:30
self?.nextPage = page.nextPage
self?.disabled = page.disabled
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() {
load(page: nextPage)
}
func loadReplies(page: String) {
guard !player.currentVideo.isNil else {
return
}
if page == repliesPageID {
return
}
2021-12-05 01:05:41 +05:30
replies = []
repliesPageID = page
repliesLoaded = false
2021-12-05 01:05:41 +05:30
api?.comments(player.currentVideo!.videoID, page: page)?
.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-18 01:16:49 +05:30
.onFailure { [weak self] _ in
self?.repliesLoaded = true
2021-12-05 01:05:41 +05:30
}
}
func reset() {
2021-12-05 01:05:41 +05:30
all = []
disabled = false
2021-12-05 01:05:41 +05:30
firstPage = true
nextPage = nil
loaded = false
replies = []
repliesLoaded = false
2021-12-05 01:05:41 +05:30
}
}