Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for discussion pull to refresh #393

Merged
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
26 changes: 20 additions & 6 deletions Discussion/Discussion/Domain/Model/Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,27 @@ public struct Post {
public let commentID: String
public let parentID: String?
public var abuseFlagged: Bool
public let closed: Bool
public var closed: Bool

public init(authorName: String, authorAvatar: String, postDate: Date, postTitle: String, postBodyHtml: String,
postBody: String,
postVisible: Bool, voted: Bool, followed: Bool, votesCount: Int, responsesCount: Int,
comments: [Post], threadID: String, commentID: String, parentID: String?, abuseFlagged: Bool,
closed: Bool) {
public init(
authorName: String,
authorAvatar: String,
postDate: Date,
postTitle: String,
postBodyHtml: String,
postBody: String,
postVisible: Bool,
voted: Bool,
followed: Bool,
votesCount: Int,
responsesCount: Int,
comments: [Post],
threadID: String,
commentID: String,
parentID: String?,
abuseFlagged: Bool,
closed: Bool
) {
self.authorName = authorName
self.authorAvatar = authorAvatar
self.postDate = postDate
Expand Down
21 changes: 17 additions & 4 deletions Discussion/Discussion/Domain/Model/UserComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,23 @@ public struct UserComment: Hashable {
public let parentID: String?
public var abuseFlagged: Bool

public init(authorName: String, authorAvatar: String, postDate: Date, postTitle: String, postBody: String,
postBodyHtml: String,
postVisible: Bool, voted: Bool, followed: Bool, votesCount: Int, responsesCount: Int,
threadID: String, commentID: String, parentID: String?, abuseFlagged: Bool) {
public init(
authorName: String,
authorAvatar: String,
postDate: Date,
postTitle: String,
postBody: String,
postBodyHtml: String,
postVisible: Bool,
voted: Bool,
followed: Bool,
votesCount: Int,
responsesCount: Int,
threadID: String,
commentID: String,
parentID: String?,
abuseFlagged: Bool
) {
self.authorName = authorName
self.authorAvatar = authorAvatar
self.postDate = postDate
Expand Down
29 changes: 24 additions & 5 deletions Discussion/Discussion/Domain/Model/UserThread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,35 @@ public struct UserThread {
public let closed: Bool
public var following: Bool
public var commentCount: Int
public let avatar: String
public var avatar: String
public var unreadCommentCount: Int
public var abuseFlagged: Bool
public let hasEndorsed: Bool
public let numPages: Int

public init(id: String, author: String, authorLabel: String, createdAt: Date, updatedAt: Date, rawBody: String,
renderedBody: String, voted: Bool, voteCount: Int, courseID: String, type: PostType, title: String,
pinned: Bool, closed: Bool, following: Bool, commentCount: Int, avatar: String, unreadCommentCount: Int,
abuseFlagged: Bool, hasEndorsed: Bool, numPages: Int) {
public init(
id: String,
author: String,
authorLabel: String,
createdAt: Date,
updatedAt: Date,
rawBody: String,
renderedBody: String,
voted: Bool,
voteCount: Int,
courseID: String,
type: PostType,
title: String,
pinned: Bool,
closed: Bool,
following: Bool,
commentCount: Int,
avatar: String,
unreadCommentCount: Int,
abuseFlagged: Bool,
hasEndorsed: Bool,
numPages: Int
) {
self.id = id
self.author = author
self.authorLabel = authorLabel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct ResponsesView: View {
self.viewModel = viewModel
self.router = router
Task {
await viewModel.getComments(commentID: commentID, parentComment: parentComment, page: 1)
await viewModel.getResponsesData(commentID: commentID, parentComment: parentComment, page: 1)
}
viewModel.addCommentsIsVisible = false
self.viewModel.isBlackedOut = isBlackedOut
Expand All @@ -48,10 +48,11 @@ public struct ResponsesView: View {
ZStack(alignment: .top) {
RefreshableScrollViewCompat(action: {
viewModel.comments = []
_ = await viewModel.getComments(
_ = await viewModel.getResponsesData(
commentID: commentID,
parentComment: parentComment,
page: 1
page: 1,
refresh: true
)
}) {
VStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public class ResponsesViewModel: BaseResponsesViewModel, ObservableObject {
if index == comments.count - 3 {
if totalPages != 1 {
if nextPage != totalPages + 1 {
if await self.getComments(commentID: commentID,
parentComment: parentComment,
page: nextPage) {
if await self.getResponsesData(
commentID: commentID,
parentComment: parentComment,
page: nextPage
) {
self.nextPage += 1
}
}
Expand All @@ -88,19 +90,23 @@ public class ResponsesViewModel: BaseResponsesViewModel, ObservableObject {
}

@MainActor
func getComments(commentID: String, parentComment: Post, page: Int) async -> Bool {
func getResponsesData(commentID: String, parentComment: Post, page: Int, refresh: Bool = false) async -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have renamed the getComments to getResponsesData but there are other elements which are according to the old method name like self.comments. Do we need to update them as well to it's ok to just update the method name?

Copy link
Contributor Author

@rnr rnr Apr 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to getResponsesData because now we get not only the comments here, but also the parent post data - so now it doesn't just work with comments but with several data types.

guard !fetchInProgress else { return false }
do {
let (comments, pagination) = try await interactor
.getCommentResponses(commentID: commentID, page: page)
self.totalPages = pagination.numPages
self.itemsCount = pagination.count
var parentPost = parentComment
if page == 1 {
self.comments = comments
if refresh {
parentPost = await getParentPost(parentComment: parentComment)
}
} else {
self.comments += comments
}
postComments = generateCommentsResponses(comments: self.comments, parentComment: parentComment)
postComments = generateCommentsResponses(comments: self.comments, parentComment: parentPost)
return true
} catch let error {
if error.isInternetError {
Expand All @@ -112,6 +118,18 @@ public class ResponsesViewModel: BaseResponsesViewModel, ObservableObject {
}
}

func getParentPost(parentComment: Post) async -> Post {
do {
let parentCommentData = try await interactor.getResponse(responseID: parentComment.commentID)
var parentPost = parentCommentData.post
parentPost.closed = parentComment.closed
parentPost.authorAvatar = parentComment.authorAvatar
return parentPost
} catch {
return parentComment
}
}

func sendThreadLikeState() {
if let postComments {
threadStateSubject.send(.voted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct ThreadView: View {
VStack {
ZStack(alignment: .top) {
RefreshableScrollViewCompat(action: {
_ = await viewModel.getPosts(thread: thread, page: 1)
_ = await viewModel.getThreadData(thread: thread, page: 1, refresh: true)
}) {
VStack {
if let comments = viewModel.postComments {
Expand Down Expand Up @@ -241,7 +241,7 @@ public struct ThreadView: View {
}
.onFirstAppear {
Task {
await viewModel.getPosts(thread: thread, page: 1)
await viewModel.getThreadData(thread: thread, page: 1)
}
}
.onDisappear {
Expand All @@ -255,13 +255,6 @@ public struct ThreadView: View {
)
}
}

private func reloadPage(onSuccess: @escaping () -> Void) {
Task {
if await viewModel.getPosts(thread: thread,
page: viewModel.nextPage-1) { onSuccess() }
}
}
}

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject {
if index == comments.count - 3 {
if totalPages != 1 {
if nextPage != totalPages+1 {
if await self.getPosts(thread: thread, page: nextPage) {
if await self.getThreadData(thread: thread, page: nextPage) {
self.nextPage += 1
return true
}
Expand All @@ -125,7 +125,7 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject {
}

@MainActor
public func getPosts(thread: UserThread, page: Int) async -> Bool {
public func getThreadData(thread: UserThread, page: Int, refresh: Bool = false) async -> Bool {
guard !fetchInProgress else { return false }
fetchInProgress = true
do {
Expand All @@ -136,23 +136,31 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject {
.getQuestionComments(threadID: thread.id, page: page)
self.totalPages = pagination.numPages
self.itemsCount = pagination.count
var threadPost = thread
if page == 1 {
self.comments = comments
if refresh {
threadPost = await getThreadPost(thread: thread)
}
} else {
self.comments += comments
}
postComments = generateComments(comments: self.comments, thread: thread)
postComments = generateComments(comments: self.comments, thread: threadPost)
case .discussion:
let (comments, pagination) = try await interactor
.getDiscussionComments(threadID: thread.id, page: page)
self.totalPages = pagination.numPages
self.itemsCount = pagination.count
var threadPost = thread
if page == 1 {
self.comments = comments
if refresh {
threadPost = await getThreadPost(thread: thread)
}
} else {
self.comments += comments
}
postComments = generateComments(comments: self.comments, thread: thread)
postComments = generateComments(comments: self.comments, thread: threadPost)
}
fetchInProgress = false
return true
Expand All @@ -167,6 +175,16 @@ public class ThreadViewModel: BaseResponsesViewModel, ObservableObject {
}
}

func getThreadPost(thread: UserThread) async -> UserThread {
do {
var threadPost = try await interactor.getThread(threadID: thread.id)
threadPost.avatar = thread.avatar
return threadPost
} catch {
return thread
}
}

func sendPostFollowedState() {
if let postComments {
postStateSubject.send(.followed(id: postComments.threadID, postComments.followed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ final class ThreadViewModelTests: XCTestCase {
count: 1,
numPages: 1))))

result = await viewModel.getPosts(thread: threads.threads[0], page: 1)
result = await viewModel.getThreadData(thread: threads.threads[0], page: 1)

Verify(interactor, .readBody(threadID: .value(threads.threads[0].id)))
Verify(interactor, .getQuestionComments(threadID: .value(threads.threads[0].id), page: .value(1)))
Expand Down Expand Up @@ -250,7 +250,7 @@ final class ThreadViewModelTests: XCTestCase {
count: 1,
numPages: 1))))

result = await viewModel.getPosts(thread: threads.threads[1], page: 1)
result = await viewModel.getThreadData(thread: threads.threads[1], page: 1)

Verify(interactor, .readBody(threadID: .value(threads.threads[1].id)))
Verify(interactor, .getDiscussionComments(threadID: .value(threads.threads[1].id), page: .value(1)))
Expand All @@ -277,7 +277,7 @@ final class ThreadViewModelTests: XCTestCase {
Given(interactor, .readBody(threadID: .any, willThrow: noInternetError))
Given(interactor, .getQuestionComments(threadID: .any, page: .any, willThrow: noInternetError))

result = await viewModel.getPosts(thread: threads.threads[0], page: 1)
result = await viewModel.getThreadData(thread: threads.threads[0], page: 1)

viewModel.postComments = postComments

Expand Down Expand Up @@ -306,7 +306,7 @@ final class ThreadViewModelTests: XCTestCase {
Given(interactor, .readBody(threadID: .any, willThrow: NSError()))
Given(interactor, .getQuestionComments(threadID: .any, page: .any, willThrow: NSError()))

result = await viewModel.getPosts(thread: threads.threads[0], page: 1)
result = await viewModel.getThreadData(thread: threads.threads[0], page: 1)

viewModel.postComments = postComments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ final class ResponsesViewModelTests: XCTestCase {
count: 1,
numPages: 1))))

result = await viewModel.getComments(commentID: "1", parentComment: post, page: 1)
result = await viewModel.getResponsesData(commentID: "1", parentComment: post, page: 1)

Verify(interactor, .getCommentResponses(commentID: .any, page: .any))

Expand All @@ -141,7 +141,7 @@ final class ResponsesViewModelTests: XCTestCase {

Given(interactor, .getCommentResponses(commentID: .any, page: .any, willThrow: noInternetError))

result = await viewModel.getComments(commentID: "1", parentComment: post, page: 1)
result = await viewModel.getResponsesData(commentID: "1", parentComment: post, page: 1)

Verify(interactor, .getCommentResponses(commentID: .any, page: .any))

Expand All @@ -165,7 +165,7 @@ final class ResponsesViewModelTests: XCTestCase {

Given(interactor, .getCommentResponses(commentID: .any, page: .any, willThrow: NSError()))

result = await viewModel.getComments(commentID: "1", parentComment: post, page: 1)
result = await viewModel.getResponsesData(commentID: "1", parentComment: post, page: 1)

Verify(interactor, .getCommentResponses(commentID: .any, page: .any))

Expand Down
Loading