Skip to content

Commit 52a61a9

Browse files
committed
feat: add repository labels pagination
fix GitHawkApp#2902
1 parent 18fef6c commit 52a61a9

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

Diff for: Classes/Labels/GitHubClient+RepositoryLabels.swift

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// GitHubClient+RepositoryLabels.swift
3+
// Freetime
4+
//
5+
// Created by Quentin Dreyer on 05/03/2020.
6+
// Copyright © 2020 Ryan Nystrom. All rights reserved.
7+
//
8+
9+
import GitHubAPI
10+
import Apollo
11+
12+
private extension FetchRepositoryLabelsQuery.Data {
13+
14+
func labels() -> [String] {
15+
var labels: [String] = []
16+
repository?.labels.map { nodes in
17+
nodes.nodes.map { node in
18+
labels += node.compactMap {
19+
$0?.node?.name,
20+
$0?.node?.color
21+
}
22+
}
23+
}
24+
return labels
25+
}
26+
27+
func nextPageToken() -> String? {
28+
guard repository?.refs?.pageInfo.hasNextPage == true else { return nil }
29+
return repository?.refs?.pageInfo.endCursor
30+
}
31+
32+
}
33+
34+
extension GithubClient {
35+
36+
struct RepositoryLabelsPayload {
37+
let labels: [String]
38+
let nextPage: String?
39+
}
40+
41+
func fetchRepositoryLabels(owner: String,
42+
repo: String,
43+
nextPage: String?,
44+
completion: @escaping (Result<RepositoryLabelsPayload>) -> Void
45+
) {
46+
let query = FetchRepositoryLabelsQuery(owner: owner, name: repo, after: nextPage)
47+
client.query(query, result: { $0 }, completion: { result in
48+
49+
switch result {
50+
case .failure(let error):
51+
completion(.error(error))
52+
53+
case .success(let data):
54+
let payload = RepositoryLabelsPayload(
55+
labels: data.labels(),
56+
nextPage: data.nextPageToken()
57+
)
58+
completion(.success(payload))
59+
}
60+
})
61+
}
62+
}

Diff for: Classes/Labels/LabelsViewController.swift

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ LabelSectionControllerDelegate {
1616

1717
private let selectedLabels: Set<RepositoryLabel>
1818
private var labels = [RepositoryLabel]()
19+
private let owner: String
20+
private let repo: String
1921
private let client: GithubClient
20-
private let request: RepositoryLabelsQuery
2122

2223
init(
2324
selected: [RepositoryLabel],
@@ -27,7 +28,8 @@ LabelSectionControllerDelegate {
2728
) {
2829
self.selectedLabels = Set(selected)
2930
self.client = client
30-
self.request = RepositoryLabelsQuery(owner: owner, repo: repo)
31+
self.owner = owner
32+
self.repo = repo
3133
super.init(emptyErrorMessage: NSLocalizedString("No labels found", comment: ""))
3234
preferredContentSize = Styles.Sizes.contextMenuSize
3335
title = Constants.Strings.labels
@@ -87,20 +89,23 @@ LabelSectionControllerDelegate {
8789
// MARK: Overrides
8890

8991
override func fetch(page: String?) {
90-
client.client.query(request, result: { data in
91-
data.repository?.labels?.nodes
92-
}, completion: { [weak self] result in
92+
client.fetchRepositoryLabels(
93+
owner: owner,
94+
repo: repo,
95+
nextPage: page as String?
96+
) { [weak self] result in
97+
guard let strongSelf = self else { return }
9398
switch result {
94-
case .success(let nodes):
95-
self?.labels = nodes.compactMap {
99+
case .success(let payload):
100+
self?.labels = payload.labels.compactMap {
96101
guard let node = $0 else { return nil }
97102
return RepositoryLabel(color: node.color, name: node.name)
98103
}.sorted { $0.name < $1.name }
99-
self?.update(animated: true)
100-
case .failure(let error):
104+
strongSelf.update(animated: true)
105+
case .error(let error):
101106
Squawk.show(error: error)
102107
}
103-
})
108+
}
104109
}
105110

106111
// MARK: BaseListViewControllerDataSource

Diff for: Freetime.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
29FE635F21AE2E2F00A07A86 /* RepositoryLoadingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE635E21AE2E2F00A07A86 /* RepositoryLoadingViewController.swift */; };
460460
29FE636121AE2E7900A07A86 /* RepositoryErrorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE636021AE2E7900A07A86 /* RepositoryErrorViewController.swift */; };
461461
29FF85A51EE1EA7A007B8762 /* ReactionContent+ReactionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FF85A41EE1EA7A007B8762 /* ReactionContent+ReactionType.swift */; };
462+
2CDD97C22411B61C0016D5CF /* GitHubClient+RepositoryLabels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CDD97C12411B61C0016D5CF /* GitHubClient+RepositoryLabels.swift */; };
462463
3E79A2FF1F8A7DA700E1126B /* ShortcutHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */; };
463464
4920F1A81F72E27200131E9D /* UIViewController+UserActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4920F1A71F72E27200131E9D /* UIViewController+UserActivity.swift */; };
464465
49AF91B1204B416500DFF325 /* MergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49AF91B0204B416500DFF325 /* MergeTests.swift */; };
@@ -1060,6 +1061,7 @@
10601061
29FE635E21AE2E2F00A07A86 /* RepositoryLoadingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryLoadingViewController.swift; sourceTree = "<group>"; };
10611062
29FE636021AE2E7900A07A86 /* RepositoryErrorViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryErrorViewController.swift; sourceTree = "<group>"; };
10621063
29FF85A41EE1EA7A007B8762 /* ReactionContent+ReactionType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ReactionContent+ReactionType.swift"; sourceTree = "<group>"; };
1064+
2CDD97C12411B61C0016D5CF /* GitHubClient+RepositoryLabels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GitHubClient+RepositoryLabels.swift"; sourceTree = "<group>"; };
10631065
36115D494E8C3B4F39AC8CD9 /* Pods-Freetime.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Freetime.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Freetime/Pods-Freetime.debug.xcconfig"; sourceTree = "<group>"; };
10641066
3E106824819769E0A6665A79 /* Pods-FreetimeWatch.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FreetimeWatch.release.xcconfig"; path = "Pods/Target Support Files/Pods-FreetimeWatch/Pods-FreetimeWatch.release.xcconfig"; sourceTree = "<group>"; };
10651067
3E79A2FE1F8A7DA700E1126B /* ShortcutHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutHandler.swift; sourceTree = "<group>"; };
@@ -2154,6 +2156,7 @@
21542156
2924C18A20D5B3A100FCFCFF /* LabelMenuCell.swift */,
21552157
29C8F9B4208C081D0075931C /* LabelSectionController.swift */,
21562158
2924C18C20D5B3DD00FCFCFF /* LabelsViewController.swift */,
2159+
2CDD97C12411B61C0016D5CF /* GitHubClient+RepositoryLabels.swift */,
21572160
);
21582161
path = Labels;
21592162
sourceTree = "<group>";
@@ -3263,6 +3266,7 @@
32633266
031E0241220B433C00A329F1 /* UIImage+Color.swift in Sources */,
32643267
29999734203135E100995FFD /* IssueMergeContextCell.swift in Sources */,
32653268
29EDFE821F661562005BCCEB /* RepositoryReadmeModel.swift in Sources */,
3269+
2CDD97C22411B61C0016D5CF /* GitHubClient+RepositoryLabels.swift in Sources */,
32663270
29EDFE841F661776005BCCEB /* RepositoryReadmeSectionController.swift in Sources */,
32673271
29136BDF200A7A75007317BE /* UIScrollView+LeftRightSafeInset.swift in Sources */,
32683272
298C7E2621D7F56600DD2A60 /* SettingsAccountCell.swift in Sources */,

Diff for: gql/RepositoryLabels.graphql

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
query RepositoryLabels($owner: String!, $repo: String!) {
1+
query RepositoryLabels($owner: String!, $repo: String!, $after: String) {
22
repository(owner: $owner, name: $repo) {
3-
labels(first:100) {
3+
labels(first:100, after: $after) {
44
nodes {
55
name
66
color
77
}
8+
pageInfo {
9+
hasNextPage
10+
endCursor
11+
}
812
}
913
}
1014
}

0 commit comments

Comments
 (0)