Skip to content

Commit c636f3e

Browse files
authored
Merge pull request #43 from marcospojr/feature/api-repos-integration
[Feature] Integration of API call with repos list
2 parents 51c1127 + a39a0cd commit c636f3e

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

solutions/devsprint-6/OnboardingChallenge/Models/RepositoriesModel.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct RepositoriesModel: Codable {
1313
let name: String
1414
let owner: Owner
1515
let stargazersCount: Int
16-
let language: String
16+
let language: String?
1717
let forksCount: Int
1818
}
1919

solutions/devsprint-6/OnboardingChallenge/Screens/List/ListView.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import UIKit
1010
final class ListView: UIView {
1111

1212
private let listViewCellIdentifier = RepositoryCellView.classIdentifier()
13-
14-
private var listItems: [String] = []
13+
14+
private var listItems: [RepositoriesModel] = []
1515

1616
private lazy var tableView: UITableView = {
1717

@@ -23,7 +23,7 @@ final class ListView: UIView {
2323
return tableView
2424
}()
2525

26-
var didSelectedRow: ((String) -> Void)?
26+
var didSelectedRow: ((RepositoriesModel) -> Void)?
2727

2828
init() {
2929
super.init(frame: .zero)
@@ -39,7 +39,7 @@ extension ListView {
3939

4040
func updateView(with configuration: ListViewConfiguration) {
4141

42-
self.listItems = configuration.listItems
42+
self.listItems = configuration.repositories
4343
self.tableView.reloadData()
4444
}
4545
}
@@ -85,8 +85,8 @@ extension ListView: UITableViewDataSource, UITableViewDelegate {
8585
}
8686

8787
// TODO: removing mock repositoryOwnerName when have defined models
88-
cell.updateView(with: RepositoryCellViewConfiguration(repositoryName: self.listItems[indexPath.row],
89-
repositoryOwnerName: "rdgborges"))
88+
cell.updateView(with: RepositoryCellViewConfiguration(repositoryName: self.listItems[indexPath.row].name,
89+
repositoryOwnerName: self.listItems[indexPath.row].owner.login))
9090

9191
return cell
9292
}

solutions/devsprint-6/OnboardingChallenge/Screens/List/ListViewConfiguration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import Foundation
99

1010
struct ListViewConfiguration {
1111

12-
let listItems: [String]
12+
let repositories: [RepositoriesModel]
1313
}

solutions/devsprint-6/OnboardingChallenge/Screens/List/ListViewController.swift

+17-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class ListViewController: UIViewController {
1616
private lazy var searchController: UISearchController = {
1717
let searchController = UISearchController(searchResultsController: nil)
1818
searchController.delegate = self
19+
searchController.searchBar.delegate = self
1920
searchController.searchResultsUpdater = self
2021
searchController.searchBar.placeholder = "Type a GitHub user name"
2122
searchController.hidesNavigationBarDuringPresentation = false
@@ -36,27 +37,15 @@ final class ListViewController: UIViewController {
3637
super.viewDidLoad()
3738
setupUI()
3839
bindEvents()
39-
fetchList()
4040
}
4141

4242
override func loadView() {
4343
view = listView
4444
}
45-
46-
override func viewWillAppear(_ animated: Bool) {
47-
super.viewWillAppear(animated)
48-
navigationController?.navigationBar.prefersLargeTitles = true
49-
}
50-
51-
override func viewWillDisappear(_ animated: Bool) {
52-
super.viewWillDisappear(animated)
53-
navigationController?.navigationBar.prefersLargeTitles = false
54-
}
55-
45+
5646
// MARK: Actions
5747
@objc
5848
func pressedSettings() {
59-
debugPrint("Open Settings")
6049
let settingsViewController = SettingsViewController()
6150
let navBarController = UINavigationController(rootViewController: settingsViewController)
6251
navBarController.navigationBar.backgroundColor = .systemGray5
@@ -76,34 +65,38 @@ final class ListViewController: UIViewController {
7665
}
7766

7867
private func setupNavigationBar() {
68+
navigationController?.navigationBar.prefersLargeTitles = true
69+
7970
let settingsButton = UIBarButtonItem(title: "Settings", style: .plain, target: self, action: #selector(pressedSettings))
8071
navigationItem.rightBarButtonItem = settingsButton
8172
navigationItem.searchController = searchController
8273
navigationItem.hidesSearchBarWhenScrolling = false
8374
}
84-
85-
private func fetchList() {
86-
87-
self.service.fetchList(for: "devpass-tech") { items in
88-
89-
let names = items.map { $0.name }
75+
76+
private func fetchList(for user: String) {
9077

91-
let configuration = ListViewConfiguration(listItems: names)
78+
self.service.fetchList(for: user) { items in
9279

80+
let configuration = ListViewConfiguration(repositories: items)
81+
9382
DispatchQueue.main.async {
9483
self.listView.updateView(with: configuration)
9584
}
9685
}
9786
}
9887

99-
private func instanceDetailsOf(_ item: String) {
88+
private func instanceDetailsOf(_ item: RepositoriesModel) {
10089
let viewController = DetailViewController()
101-
viewController.title = item
102-
navigationController?.pushViewController(viewController, animated: false)
90+
viewController.title = item.name
91+
navigationController?.pushViewController(viewController, animated: true)
10392
}
10493
}
10594

10695
// MARK: Extensions
107-
extension ListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
96+
extension ListViewController: UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
10897
func updateSearchResults(for searchController: UISearchController) { }
98+
99+
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
100+
fetchList(for: searchController.searchBar.text ?? "")
101+
}
109102
}

0 commit comments

Comments
 (0)