Comments improvements

* Show text when there is no comments or comments are disabled
* Show progress indicator for loading comments/replies
* Improve layout of icons and text spacing
This commit is contained in:
Arkadiusz Fal
2021-12-05 18:14:49 +01:00
parent 37b99c59e1
commit 1f495562fc
7 changed files with 142 additions and 52 deletions

View File

@@ -72,10 +72,12 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
}
configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in
let comments = content.json.dictionaryValue["comments"]?.arrayValue.map { PipedAPI.extractComment(from: $0)! } ?? []
let nextPage = content.json.dictionaryValue["nextpage"]?.stringValue
let details = content.json.dictionaryValue
let comments = details["comments"]?.arrayValue.map { PipedAPI.extractComment(from: $0)! } ?? []
let nextPage = details["nextpage"]?.stringValue
let disabled = details["disabled"]?.boolValue ?? false
return CommentsPage(comments: comments, nextPage: nextPage)
return CommentsPage(comments: comments, nextPage: nextPage, disabled: disabled)
}
if account.token.isNil {

View File

@@ -4,16 +4,27 @@ 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
@Published var loaded = true
@Published var disabled = false
@Published var replies = [Comment]()
@Published var repliesLoaded = false
var accounts: AccountsModel!
var player: PlayerModel!
var instance: Instance? {
InstancesModel.find(Defaults[.commentsInstanceID])
}
var api: VideosAPI? {
instance.isNil ? nil : PipedAPI(account: instance!.anonymousAccount)
}
static var enabled: Bool {
!Defaults[.commentsInstanceID].isNil && !Defaults[.commentsInstanceID]!.isEmpty
}
@@ -27,23 +38,23 @@ final class CommentsModel: ObservableObject {
return
}
loaded = false
clear()
reset()
guard let instance = InstancesModel.find(Defaults[.commentsInstanceID]),
!player.currentVideo.isNil
guard !instance.isNil,
!(player?.currentVideo.isNil ?? true)
else {
return
}
firstPage = page.isNil || page!.isEmpty
PipedAPI(account: instance.anonymousAccount).comments(player.currentVideo!.videoID, page: page)?
api?.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
self?.disabled = page.disabled
}
}
.onCompletion { [weak self] _ in
@@ -61,19 +72,27 @@ final class CommentsModel: ObservableObject {
}
replies = []
repliesLoaded = false
accounts.api.comments(player.currentVideo!.videoID, page: page)?.load().onSuccess { response in
if let page: CommentsPage = response.typedContent() {
self.replies = page.comments
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
}
}
}
func clear() {
func reset() {
all = []
replies = []
disabled = false
firstPage = true
nextPage = nil
loaded = false
replies = []
repliesLoaded = false
}
}

View File

@@ -3,4 +3,5 @@ import Foundation
struct CommentsPage {
var comments = [Comment]()
var nextPage: String?
var disabled = false
}

View File

@@ -37,7 +37,7 @@ extension PlayerModel {
}
func playItem(_ item: PlayerQueueItem, video: Video? = nil, at time: TimeInterval? = nil) {
comments.clear()
comments.reset()
currentItem = item
if !time.isNil {