From 4ede8f41049efa7c03ce1ea437a55cfd89a5bb19 Mon Sep 17 00:00:00 2001 From: Ivan Stepanok Date: Wed, 12 Apr 2023 16:29:14 +0300 Subject: [PATCH] fix comments count in discussions --- .../Data/Model/Data_CommentsResponse.swift | 30 ++-- .../Data/Network/DiscussionRepository.swift | 6 +- .../Comments/Thread/ThreadView.swift | 4 +- .../Comments/Thread/ThreadViewModel.swift | 146 +++++++++--------- 4 files changed, 94 insertions(+), 92 deletions(-) diff --git a/Discussion/Discussion/Data/Model/Data_CommentsResponse.swift b/Discussion/Discussion/Data/Model/Data_CommentsResponse.swift index d9c91c68a..3a8428653 100644 --- a/Discussion/Discussion/Data/Model/Data_CommentsResponse.swift +++ b/Discussion/Discussion/Data/Model/Data_CommentsResponse.swift @@ -106,24 +106,26 @@ public extension DataLayer { } } -public extension DataLayer.Comments { - var domain: UserComment { +public extension DataLayer.CommentsResponse { + var domain: [UserComment] { + self.comments.map { comment in UserComment( - authorName: author ?? DiscussionLocalization.anonymous, - authorAvatar: users?.userName?.profile?.image?.imageURLLarge ?? "", - postDate: Date(iso8601: createdAt), + authorName: comment.author ?? DiscussionLocalization.anonymous, + authorAvatar: comment.users?.userName?.profile?.image?.imageURLLarge ?? "", + postDate: Date(iso8601: comment.createdAt), postTitle: "", - postBody: rawBody, - postBodyHtml: renderedBody, + postBody: comment.rawBody, + postBodyHtml: comment.renderedBody, postVisible: true, - voted: voted, + voted: comment.voted, followed: false, - votesCount: voteCount, - responsesCount: childCount, - threadID: threadID, - commentID: id, - parentID: id, - abuseFlagged: abuseFlagged + votesCount: comment.voteCount, + responsesCount: pagination.count, + threadID: comment.threadID, + commentID: comment.id, + parentID: comment.id, + abuseFlagged: comment.abuseFlagged ) + } } } diff --git a/Discussion/Discussion/Data/Network/DiscussionRepository.swift b/Discussion/Discussion/Data/Network/DiscussionRepository.swift index c04e3f8d5..48a53c4f7 100644 --- a/Discussion/Discussion/Data/Network/DiscussionRepository.swift +++ b/Discussion/Discussion/Data/Network/DiscussionRepository.swift @@ -68,21 +68,21 @@ public class DiscussionRepository: DiscussionRepositoryProtocol { let response = try await api.requestData(DiscussionEndpoint .getDiscussionComments(threadID: threadID, page: page)) let result = try await renameUsers(data: response) - return (result.comments.map { $0.domain }, result.pagination.numPages) + return (result.domain, result.pagination.numPages) } public func getQuestionComments(threadID: String, page: Int) async throws -> ([UserComment], Int) { let response = try await api.requestData(DiscussionEndpoint .getQuestionComments(threadID: threadID, page: page)) let result = try await renameUsers(data: response) - return (result.comments.map { $0.domain }, result.pagination.numPages) + return (result.domain, result.pagination.numPages) } public func getCommentResponses(commentID: String, page: Int) async throws -> ([UserComment], Int) { let response = try await api.requestData(DiscussionEndpoint .getCommentResponses(commentID: commentID, page: page)) let result = try await renameUsers(data: response) - return (result.comments.map { $0.domain }, result.pagination.numPages) + return (result.domain, result.pagination.numPages) } public func addCommentTo(threadID: String, rawBody: String, parentID: String? = nil) async throws -> Post { diff --git a/Discussion/Discussion/Presentation/Comments/Thread/ThreadView.swift b/Discussion/Discussion/Presentation/Comments/Thread/ThreadView.swift index 8d5429084..ff0fc916d 100644 --- a/Discussion/Discussion/Presentation/Comments/Thread/ThreadView.swift +++ b/Discussion/Discussion/Presentation/Comments/Thread/ThreadView.swift @@ -93,8 +93,8 @@ public struct ThreadView: View { HStack { if let responsesCount = viewModel.postComments?.responsesCount { - Text("\(responsesCount - 1)") - Text(DiscussionLocalization.responsesCount(responsesCount - 1)) + Text("\(responsesCount)") + Text(DiscussionLocalization.responsesCount(responsesCount)) Spacer() } }.padding(.top, 40) diff --git a/Discussion/Discussion/Presentation/Comments/Thread/ThreadViewModel.swift b/Discussion/Discussion/Presentation/Comments/Thread/ThreadViewModel.swift index 42cdb44be..4a2e04b40 100644 --- a/Discussion/Discussion/Presentation/Comments/Thread/ThreadViewModel.swift +++ b/Discussion/Discussion/Presentation/Comments/Thread/ThreadViewModel.swift @@ -46,38 +46,38 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject { func generateComments(comments: [UserComment], thread: UserThread) -> Post { var result = Post(authorName: thread.author, - authorAvatar: thread.avatar, - postDate: thread.createdAt, - postTitle: thread.title, - postBodyHtml: thread.renderedBody, - postBody: thread.rawBody, - postVisible: true, - voted: thread.voted, - followed: thread.following, - votesCount: thread.voteCount, - responsesCount: thread.commentCount, - comments: [], - threadID: thread.id, - commentID: thread.courseID, - parentID: nil, - abuseFlagged: thread.abuseFlagged) + authorAvatar: thread.avatar, + postDate: thread.createdAt, + postTitle: thread.title, + postBodyHtml: thread.renderedBody, + postBody: thread.rawBody, + postVisible: true, + voted: thread.voted, + followed: thread.following, + votesCount: thread.voteCount, + responsesCount: comments.last?.responsesCount ?? 0, + comments: [], + threadID: thread.id, + commentID: thread.courseID, + parentID: nil, + abuseFlagged: thread.abuseFlagged) result.comments = comments.map { c in - Post(authorName: c.authorName, - authorAvatar: c.authorAvatar, - postDate: c.postDate, - postTitle: c.postTitle, - postBodyHtml: c.postBodyHtml, - postBody: c.postBody, - postVisible: c.postVisible, - voted: c.voted, - followed: c.followed, - votesCount: c.votesCount, - responsesCount: c.responsesCount, - comments: [], - threadID: c.threadID, - commentID: c.commentID, - parentID: c.parentID, - abuseFlagged: c.abuseFlagged) + Post(authorName: c.authorName, + authorAvatar: c.authorAvatar, + postDate: c.postDate, + postTitle: c.postTitle, + postBodyHtml: c.postBodyHtml, + postBody: c.postBody, + postVisible: c.postVisible, + voted: c.voted, + followed: c.followed, + votesCount: c.votesCount, + responsesCount: c.responsesCount, + comments: [], + threadID: c.threadID, + commentID: c.commentID, + parentID: c.parentID, + abuseFlagged: c.abuseFlagged) } return result } @@ -85,20 +85,20 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject { @MainActor public func postComment(threadID: String, rawBody: String, parentID: String?) async { isShowProgress = true - do { - let newComment = try await interactor.addCommentTo(threadID: threadID, - rawBody: rawBody, - parentID: parentID) - isShowProgress = false - addPostSubject.send(newComment) - } catch let error { - isShowProgress = false - if error.isInternetError { - errorMessage = CoreLocalization.Error.slowOrNoInternetConnection - } else { - errorMessage = CoreLocalization.Error.unknownError - } + do { + let newComment = try await interactor.addCommentTo(threadID: threadID, + rawBody: rawBody, + parentID: parentID) + isShowProgress = false + addPostSubject.send(newComment) + } catch let error { + isShowProgress = false + if error.isInternetError { + errorMessage = CoreLocalization.Error.slowOrNoInternetConnection + } else { + errorMessage = CoreLocalization.Error.unknownError } + } } @MainActor @@ -122,36 +122,36 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject { public func getPosts(thread: UserThread, page: Int) async -> Bool { guard !fetchInProgress else { return false } fetchInProgress = true - do { - try await interactor.readBody(threadID: thread.id) - switch thread.type { - case .question: - let (comments, totalPages) = try await interactor - .getQuestionComments(threadID: thread.id, page: page) - self.totalPages = totalPages - self.comments += comments - - postComments = - generateComments(comments: self.comments, thread: thread) - case .discussion: - let (comments, totalPages) = try await interactor - .getDiscussionComments(threadID: thread.id, page: page) - self.totalPages = totalPages - self.comments += comments - postComments = - generateComments(comments: self.comments, thread: thread) - } - fetchInProgress = false - return true - } catch let error { - if error.isInternetError { - errorMessage = CoreLocalization.Error.slowOrNoInternetConnection - } else { - errorMessage = CoreLocalization.Error.unknownError - } - fetchInProgress = false - return false + do { + try await interactor.readBody(threadID: thread.id) + switch thread.type { + case .question: + let (comments, totalPages) = try await interactor + .getQuestionComments(threadID: thread.id, page: page) + self.totalPages = totalPages + self.comments += comments + + postComments = + generateComments(comments: self.comments, thread: thread) + case .discussion: + let (comments, totalPages) = try await interactor + .getDiscussionComments(threadID: thread.id, page: page) + self.totalPages = totalPages + self.comments += comments + postComments = + generateComments(comments: self.comments, thread: thread) + } + fetchInProgress = false + return true + } catch let error { + if error.isInternetError { + errorMessage = CoreLocalization.Error.slowOrNoInternetConnection + } else { + errorMessage = CoreLocalization.Error.unknownError } + fetchInProgress = false + return false + } } func sendPostFollowedState() {