mirror of
https://github.com/yattee/yattee.git
synced 2024-12-13 22:00:31 +05:30
80 lines
1.9 KiB
Swift
80 lines
1.9 KiB
Swift
|
import Defaults
|
||
|
import Foundation
|
||
|
import SwiftyJSON
|
||
|
|
||
|
final class CommentsModel: ObservableObject {
|
||
|
@Published var all = [Comment]()
|
||
|
@Published var replies = [Comment]()
|
||
|
|
||
|
@Published var nextPage: String?
|
||
|
@Published var firstPage = true
|
||
|
|
||
|
@Published var loaded = false
|
||
|
|
||
|
var accounts: AccountsModel!
|
||
|
var player: PlayerModel!
|
||
|
|
||
|
static var enabled: Bool {
|
||
|
!Defaults[.commentsInstanceID].isNil
|
||
|
}
|
||
|
|
||
|
var nextPageAvailable: Bool {
|
||
|
!(nextPage?.isEmpty ?? true)
|
||
|
}
|
||
|
|
||
|
func load(page: String? = nil) {
|
||
|
guard Self.enabled else {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
loaded = false
|
||
|
clear()
|
||
|
|
||
|
guard let instance = InstancesModel.find(Defaults[.commentsInstanceID]),
|
||
|
!player.currentVideo.isNil
|
||
|
else {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
firstPage = page.isNil || page!.isEmpty
|
||
|
|
||
|
PipedAPI(account: instance.anonymousAccount).comments(player.currentVideo!.videoID, page: page)?
|
||
|
.load()
|
||
|
.onSuccess { [weak self] response in
|
||
|
if let page: CommentsPage = response.typedContent() {
|
||
|
self?.all = page.comments
|
||
|
self?.nextPage = page.nextPage
|
||
|
}
|
||
|
}
|
||
|
.onCompletion { [weak self] _ in
|
||
|
self?.loaded = true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func loadNextPage() {
|
||
|
load(page: nextPage)
|
||
|
}
|
||
|
|
||
|
func loadReplies(page: String) {
|
||
|
guard !player.currentVideo.isNil else {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
replies = []
|
||
|
|
||
|
accounts.api.comments(player.currentVideo!.videoID, page: page)?.load().onSuccess { response in
|
||
|
if let page: CommentsPage = response.typedContent() {
|
||
|
self.replies = page.comments
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func clear() {
|
||
|
all = []
|
||
|
replies = []
|
||
|
firstPage = true
|
||
|
nextPage = nil
|
||
|
loaded = false
|
||
|
}
|
||
|
}
|