Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Discussion/Discussion/Data/Model/Data_CommentsResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
6 changes: 3 additions & 3 deletions Discussion/Discussion/Data/Network/DiscussionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,59 +46,59 @@ 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
}

@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
Expand All @@ -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() {
Expand Down