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

Sort discussion comments #489

Merged
merged 2 commits into from
Jul 2, 2019
Merged
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
63 changes: 31 additions & 32 deletions Stepic/DiscussionsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,57 +227,56 @@ class DiscussionsViewController: UIViewController, ControllerWithStepikPlacehold

//TODO: Check if token should be refreshed before that request
performRequest({
_ = ApiDataDownloader.comments.retrieve(ids, success: {
[weak self]
retrievedDiscussions in
_ = ApiDataDownloader.comments.retrieve(ids, success: { [weak self] retrievedDiscussions in
guard let strongSelf = self else {
return
}

if let s = self {
//get superDiscussions (those who have no parents)
let superDiscussions = Sorter.sort(retrievedDiscussions.filter({$0.parentId == nil}), byIds: ids, canMissElements: true)
// Get superDiscussions (those who have no parents)
let superDiscussions = retrievedDiscussions
.filter({ $0.parentId == nil })
.reordered(order: ids, transform: { $0.id })

s.discussionIds.loaded += ids
s.discussions += superDiscussions
strongSelf.discussionIds.loaded += ids
strongSelf.discussions += superDiscussions
strongSelf.discussions.sort { $0.time.compare($1.time) == .orderedDescending }

var changedDiscussionIds = Set<Int>()
//get all replies
for reply in retrievedDiscussions.filter({$0.parentId != nil}) {
var changedDiscussionIds = Set<Int>()
// Get all replies
retrievedDiscussions
.filter { $0.parentId != nil }
.forEach { reply in
if let parentId = reply.parentId {
if s.replies.loaded[parentId] == nil {
s.replies.loaded[parentId] = []
}
s.replies.loaded[parentId]? += [reply]
strongSelf.replies.loaded[parentId, default: []] += [reply]
changedDiscussionIds.insert(parentId)
}
}

//TODO: Possibly should sort all changed reply values
for discussionId in changedDiscussionIds {
if let index = s.discussions.index(where: {$0.id == discussionId}) {
s.replies.loaded[discussionId]! = Sorter.sort(s.replies.loaded[discussionId]!, byIds: s.discussions[index].repliesIds, canMissElements: true)
}
for discussionId in changedDiscussionIds {
if let discussionIndex = strongSelf.discussions.firstIndex(where: { $0.id == discussionId }) {
strongSelf.replies.loaded[discussionId] = strongSelf.replies.loaded[discussionId, default: []]
.reordered(order: strongSelf.discussions[discussionIndex].repliesIds, transform: { $0.id })
.sorted { $0.time.compare($1.time) == .orderedAscending }
}

success?()
}
}, error: {
[weak self]
errorString in

success?()
}, error: { [weak self] errorString in
print(errorString)
self?.emptyDatasetState = .error
UIThread.performUI {
[weak self] in
self?.refreshControl?.endRefreshing()
}
})
}, error: { [weak self] error in
guard let strongSelf = self else {
return
}
)
}, error: {
[weak self]
error in
guard let s = self else { return }

if error == PerformRequestError.noAccessToRefreshToken {
AuthInfo.shared.token = nil
RoutingManager.auth.routeFrom(controller: s, success: {
[weak self] in
RoutingManager.auth.routeFrom(controller: strongSelf, success: { [weak self] in
self?.reloadDiscussions()
}, cancel: nil)
}
Expand Down