Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 715fd12

Browse files
author
Ryan Nystrom
committed
github syntax search working
1 parent bf263b7 commit 715fd12

File tree

6 files changed

+908
-12
lines changed

6 files changed

+908
-12
lines changed

Classes/Repository/GQL+RepositoryIssueSummaryType.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,50 @@ extension RepoPullRequestPagesQuery.Data.Repository.PullRequest.Node: Repository
5454
}
5555

5656
}
57+
58+
extension RepoSearchPagesQuery.Data.Search.Node.AsIssue: RepositoryIssueSummaryType {
59+
60+
var labelableFields: LabelableFields {
61+
return fragments.labelableFields
62+
}
63+
64+
var repoEventFields: RepoEventFields {
65+
return fragments.repoEventFields
66+
}
67+
68+
var pullRequest: Bool {
69+
return false
70+
}
71+
72+
var status: IssueStatus {
73+
switch issueState {
74+
case .closed: return .closed
75+
case .open, .__unknown: return .open
76+
}
77+
}
78+
79+
}
80+
81+
extension RepoSearchPagesQuery.Data.Search.Node.AsPullRequest: RepositoryIssueSummaryType {
82+
83+
var labelableFields: LabelableFields {
84+
return fragments.labelableFields
85+
}
86+
87+
var repoEventFields: RepoEventFields {
88+
return fragments.repoEventFields
89+
}
90+
91+
var pullRequest: Bool {
92+
return false
93+
}
94+
95+
var status: IssueStatus {
96+
switch pullRequestState {
97+
case .closed: return .closed
98+
case .merged: return .merged
99+
case .open, .__unknown: return .open
100+
}
101+
}
102+
103+
}

Classes/Repository/RepositoryClient.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ extension RepoPullRequestPagesQuery: RepositoryQuery {
4646

4747
}
4848

49+
extension RepoSearchPagesQuery: RepositoryQuery {
50+
51+
func summaryTypes(from data: GraphQLSelectionSet) -> [RepositoryIssueSummaryType] {
52+
guard let results = data as? RepoSearchPagesQuery.Data else { return [] }
53+
return results.search.nodes?.compactMap { $0?.asIssue ?? $0?.asPullRequest } ?? []
54+
}
55+
56+
func nextPageToken(from data: GraphQLSelectionSet) -> String? {
57+
guard let results = data as? RepoSearchPagesQuery.Data,
58+
results.search.pageInfo.hasNextPage else { return nil }
59+
return results.search.pageInfo.endCursor
60+
}
61+
62+
}
63+
4964
func createSummaryModel(
5065
_ node: RepositoryIssueSummaryType,
5166
contentSizeCategory: UIContentSizeCategory,
@@ -161,4 +176,17 @@ final class RepositoryClient {
161176
)
162177
}
163178

179+
func searchIssues(
180+
query: String,
181+
nextPage: String? = nil,
182+
containerWidth: CGFloat,
183+
completion: @escaping (Result<RepositoryPayload>) -> Void
184+
) {
185+
loadPage(
186+
query: RepoSearchPagesQuery(query: query, page_size: 30),
187+
containerWidth: containerWidth,
188+
completion: completion
189+
)
190+
}
191+
164192
}

Classes/Repository/RepositoryIssuesViewController.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ SearchBarSectionControllerDelegate {
2323
private let client: RepositoryClient
2424
private let type: RepositoryIssuesType
2525
private let searchKey: ListDiffable = "searchKey" as ListDiffable
26-
private var searchQuery: String = ""
26+
private let debouncer = Debouncer()
27+
private var previousSearchString = "is:open"
2728

2829
init(client: GithubClient, repo: RepositoryDetails, type: RepositoryIssuesType) {
2930
self.repo = repo
@@ -59,8 +60,10 @@ SearchBarSectionControllerDelegate {
5960
// MARK: Overrides
6061

6162
override func fetch(page: NSString?) {
62-
let width = view.bounds.width
63-
let block = { [weak self] (result: Result<RepositoryClient.RepositoryPayload>) in
63+
client.searchIssues(
64+
query: fullQueryString,
65+
containerWidth: view.bounds.width
66+
) { [weak self] (result: Result<RepositoryClient.RepositoryPayload>) in
6467
switch result {
6568
case .error:
6669
self?.error(animated: trueUnlessReduceMotionEnabled)
@@ -73,17 +76,15 @@ SearchBarSectionControllerDelegate {
7376
self?.update(page: payload.nextPage as NSString?, animated: trueUnlessReduceMotionEnabled)
7477
}
7578
}
76-
77-
switch type {
78-
case .issues: client.loadIssues(nextPage: page as String?, containerWidth: width, completion: block)
79-
case .pullRequests: client.loadPullRequests(nextPage: page as String?, containerWidth: width, completion: block)
80-
}
8179
}
8280

8381
// MARK: SearchBarSectionControllerDelegate
8482

8583
func didChangeSelection(sectionController: SearchBarSectionController, query: String) {
86-
filter(query: query, animated: trueUnlessReduceMotionEnabled)
84+
let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines)
85+
guard previousSearchString != trimmed else { return }
86+
previousSearchString = trimmed
87+
debouncer.action = { [weak self] in self?.fetch(page: nil) }
8788
}
8889

8990
// MARK: BaseListViewControllerDataSource
@@ -98,7 +99,11 @@ SearchBarSectionControllerDelegate {
9899

99100
func sectionController(model: Any, listAdapter: ListAdapter) -> ListSectionController {
100101
if let object = model as? ListDiffable, object === searchKey {
101-
return SearchBarSectionController(placeholder: Constants.Strings.search, delegate: self)
102+
return SearchBarSectionController(
103+
placeholder: Constants.Strings.search,
104+
delegate: self,
105+
query: previousSearchString
106+
)
102107
}
103108
return RepositorySummarySectionController(client: client.githubClient, repo: repo)
104109
}
@@ -115,4 +120,16 @@ SearchBarSectionControllerDelegate {
115120
type: empty
116121
)
117122
}
123+
124+
// MARK: Private API
125+
126+
var fullQueryString: String {
127+
let typeQuery: String
128+
switch type {
129+
case .issues: typeQuery = "is:issue"
130+
case .pullRequests: typeQuery = "is:pr"
131+
}
132+
return "repo:\(repo.owner)/\(repo.name) \(typeQuery) \(previousSearchString)"
133+
}
134+
118135
}

Classes/Section Controllers/SearchBar/SearchBarSectionController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ protocol SearchBarSectionControllerDelegate: class {
1515

1616
final class SearchBarSectionController: ListSectionController, SearchBarCellDelegate {
1717

18+
public private(set) var query: String
19+
1820
private weak var delegate: SearchBarSectionControllerDelegate?
1921
private let placeholder: String
20-
private var query = ""
2122

22-
init(placeholder: String, delegate: SearchBarSectionControllerDelegate) {
23+
init(placeholder: String, delegate: SearchBarSectionControllerDelegate, query: String = "") {
2324
self.delegate = delegate
2425
self.placeholder = placeholder
26+
self.query = query
2527
super.init()
2628
}
2729

0 commit comments

Comments
 (0)