From 75003f3867d54274767d4a546b9c4b3dc6461416 Mon Sep 17 00:00:00 2001 From: Alexey Karataev Date: Tue, 5 Mar 2019 18:23:53 +0300 Subject: [PATCH] Fix for "Mentioned issues are not sorted by date" (#2649) --- .../NotificationModelController.swift | 13 ++++- Freetime.xcodeproj/project.pbxproj | 4 ++ .../SortingMentionedByDayTests.swift | 49 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 FreetimeTests/SortingMentionedByDayTests.swift diff --git a/Classes/Notifications/NotificationModelController.swift b/Classes/Notifications/NotificationModelController.swift index 9a962eb9a..2e65b7b85 100644 --- a/Classes/Notifications/NotificationModelController.swift +++ b/Classes/Notifications/NotificationModelController.swift @@ -24,6 +24,14 @@ extension NotificationViewModel { } } +// sorting Array by date for mentioned filter type +extension Array where Element == InboxDashboardModel { + func sortedMentionedByDate(filter: V3IssuesRequest.FilterType) -> [Element] { + if filter == .mentioned { return self.sorted(by: { $0.date > $1.date }) } + return self + } +} + final class NotificationModelController { let githubClient: GithubClient @@ -273,8 +281,9 @@ final class NotificationModelController { state: state ) } - cache.set(values: parsed) - completion(.success((parsed, data.next))) + let sorted = parsed.sortedMentionedByDate(filter: mapped) + cache.set(values: sorted) + completion(.success((sorted, data.next))) } }) } diff --git a/Freetime.xcodeproj/project.pbxproj b/Freetime.xcodeproj/project.pbxproj index 30a1ecf15..0617df6e1 100644 --- a/Freetime.xcodeproj/project.pbxproj +++ b/Freetime.xcodeproj/project.pbxproj @@ -525,6 +525,7 @@ D8C2AEF51F9AA94600A95945 /* DotListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8C2AEF41F9AA94600A95945 /* DotListView.swift */; }; D8D876F81FB6083200A57E2B /* UIPopoverPresentationController+SourceView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D876F71FB6083200A57E2B /* UIPopoverPresentationController+SourceView.swift */; }; D8D876FA1FB6084F00A57E2B /* UIBarButtonItem+TightSpacing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8D876F91FB6084F00A57E2B /* UIBarButtonItem+TightSpacing.swift */; }; + DA3A743F2233EC6E0011C3BA /* SortingMentionedByDayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA3A743E2233EC6E0011C3BA /* SortingMentionedByDayTests.swift */; }; DC3238911F9B9E1A007DD924 /* SearchRecentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC3238901F9B9E1A007DD924 /* SearchRecentViewModel.swift */; }; DC3238931F9BA29D007DD924 /* SearchQuery.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC3238921F9BA29D007DD924 /* SearchQuery.swift */; }; DC5C02C31F9C6D0B00E80B9F /* SearchRecentStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5C02C21F9C6D0A00E80B9F /* SearchRecentStoreTests.swift */; }; @@ -1134,6 +1135,7 @@ D8C2AEF41F9AA94600A95945 /* DotListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DotListView.swift; sourceTree = ""; }; D8D876F71FB6083200A57E2B /* UIPopoverPresentationController+SourceView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIPopoverPresentationController+SourceView.swift"; sourceTree = ""; }; D8D876F91FB6084F00A57E2B /* UIBarButtonItem+TightSpacing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIBarButtonItem+TightSpacing.swift"; sourceTree = ""; }; + DA3A743E2233EC6E0011C3BA /* SortingMentionedByDayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SortingMentionedByDayTests.swift; sourceTree = ""; }; DC3238901F9B9E1A007DD924 /* SearchRecentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchRecentViewModel.swift; sourceTree = ""; }; DC3238921F9BA29D007DD924 /* SearchQuery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchQuery.swift; sourceTree = ""; }; DC5C02C21F9C6D0A00E80B9F /* SearchRecentStoreTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchRecentStoreTests.swift; sourceTree = ""; }; @@ -1752,6 +1754,7 @@ 29827D7321AA5DA300A1B293 /* ViewControllerTestUtil.swift */, 03E8D825221D358000EB792A /* GithubURLTests.swift */, 03C127B7220993300062F7C9 /* InboxZeroLoaderTests.swift */, + DA3A743E2233EC6E0011C3BA /* SortingMentionedByDayTests.swift */, ); path = FreetimeTests; sourceTree = ""; @@ -3433,6 +3436,7 @@ BDB6AA762165B8EA009BB73C /* SwitchBranches.swift in Sources */, DC5C02C71F9C71C400E80B9F /* SearchRecentViewModelTests.swift in Sources */, DC60C6D31F983BB900241271 /* SignatureTests.swift in Sources */, + DA3A743F2233EC6E0011C3BA /* SortingMentionedByDayTests.swift in Sources */, DC60C6D51F983DF800241271 /* IssueLabelCellTests.swift in Sources */, 49FE18FF204B6508001681E8 /* SequenceTests.swift in Sources */, DC5C02C31F9C6D0B00E80B9F /* SearchRecentStoreTests.swift in Sources */, diff --git a/FreetimeTests/SortingMentionedByDayTests.swift b/FreetimeTests/SortingMentionedByDayTests.swift new file mode 100644 index 000000000..ef61bddf5 --- /dev/null +++ b/FreetimeTests/SortingMentionedByDayTests.swift @@ -0,0 +1,49 @@ +// +// SortingMentionedByDayTests.swift +// FreetimeTests +// +// Created by Alexey Karataev on 09.03.2019. +// Copyright © 2019 Ryan Nystrom. All rights reserved. +// + +import XCTest +@testable import Freetime +@testable import StyledTextKit +@testable import GitHubAPI + +class SortingMentionedByDayTests: XCTestCase { + func testSortingMentionedByDay() { + // MARK: - given + let models = 5 + let parsed = InboxDashboardModelBuilder.provide(number: models) + let mentioned = V3IssuesRequest.FilterType.mentioned + let created = V3IssuesRequest.FilterType.created + let assigned = V3IssuesRequest.FilterType.assigned + // MARK: - when + let mentionedSorted = parsed.sortedMentionedByDate(filter: mentioned) + let createdSorted = parsed.sortedMentionedByDate(filter: created) + let assignedSorted = parsed.sortedMentionedByDate(filter: assigned) + // MARK: - then + XCTAssert(zip(mentionedSorted, + parsed.sorted(by: {$0.date > $1.date})) + .allSatisfy { $0.date == $1.date }, "Mentioned is not sorted by day") + XCTAssert(zip(createdSorted, parsed) + .allSatisfy { $0.date == $1.date }, "Sorting affect created feed") + XCTAssert(zip(assignedSorted, parsed) + .allSatisfy { $0.date == $1.date }, "Sorting affect assigned feed") + } +} + +class InboxDashboardModelBuilder { + let model: InboxDashboardModel + private init(date: Date) { + self.model = InboxDashboardModel(owner: "", name: "", number: 0, date: date, + text: StyledTextRenderer(string: StyledTextString(styledTexts: []), + contentSizeCategory: UIContentSizeCategory(rawValue: "")), + isPullRequest: false, state: .open + ) + } + static func provide(number: Int, with interval: TimeInterval = TimeInterval(86400)) -> [InboxDashboardModel] { + return (1...number).map { InboxDashboardModelBuilder(date: Date() + interval * Double($0)).model } + } +}