From f290b68e2faef4614de1b915c5664d89bdc73c56 Mon Sep 17 00:00:00 2001 From: Ryan Nystrom Date: Sun, 30 Dec 2018 16:30:44 -0500 Subject: [PATCH] retain bookmark order when syncing (#2572) --- Classes/Bookmark/GitHubClient+Bookmarks.swift | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Classes/Bookmark/GitHubClient+Bookmarks.swift b/Classes/Bookmark/GitHubClient+Bookmarks.swift index c45ddea14..bc60dbd73 100644 --- a/Classes/Bookmark/GitHubClient+Bookmarks.swift +++ b/Classes/Bookmark/GitHubClient+Bookmarks.swift @@ -16,9 +16,10 @@ private extension String { } } -private func bookmarkGraphQLIDs(from json: [String: Any]) -> [String] { - return json.compactMap { - guard let item = $0.value as? [String: Any] else { return nil } +private func bookmarkGraphQLIDs(from json: [String: Any], originalKeys: [String]) -> [String] { + // fetch by originalKeys to retain original ordering + return originalKeys.compactMap { + guard let item = json[$0] as? [String: Any] else { return nil } if let issueOrPullRequest = item["issueOrPullRequest"] as? [String: Any] { return issueOrPullRequest["id"] as? String } @@ -33,30 +34,33 @@ extension GithubClient: BookmarkViewController.Client { bookmarks: [BookmarkCloudMigratorClientBookmarks], completion: @escaping (BookmarkCloudMigratorClientResult) -> Void ) { - let subQueries: [String] = bookmarks.map { + let subQueries: [(key: String, query: String)] = bookmarks.map { switch $0 { case .issueOrPullRequest(let owner, let name, let number): let key = "\(owner)\(name)\(number)".graphQLSafeKey - return """ + return (key, """ \(key): repository(owner: "\(owner)", name: "\(name)") { issueOrPullRequest(number: \(number)) { ... on Issue { id } ... on PullRequest { id } } } - """ + """) case .repo(let owner, let name): let key = "\(owner)\(name)".graphQLSafeKey - return """ + return (key, """ \(key): repository(owner: "\(owner)", name: "\(name)") { id } - """ + """) } } - let query = "query{\(subQueries.joined(separator: " "))}" + let query = "query{\(subQueries.map({ $0.query }).joined(separator: " "))}" client.send(ManualGraphQLRequest(query: query)) { result in switch result { case .success(let json): - let ids = bookmarkGraphQLIDs(from: json.data) + let ids = bookmarkGraphQLIDs( + from: json.data, + originalKeys: subQueries.map({ $0.key }) + ) let delta = bookmarks.count - ids.count if delta == 0 { completion(.success(ids))