diff --git a/Stepic/DiscussionsViewController.swift b/Stepic/DiscussionsViewController.swift index eccef8ae72..af5009f80c 100644 --- a/Stepic/DiscussionsViewController.swift +++ b/Stepic/DiscussionsViewController.swift @@ -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() - //get all replies - for reply in retrievedDiscussions.filter({$0.parentId != nil}) { + var changedDiscussionIds = Set() + // 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) }