Skip to content

Commit

Permalink
命名し直し#3
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Oishi committed Apr 25, 2021
1 parent 3d377d7 commit 0fcd865
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
28 changes: 14 additions & 14 deletions iOSEngineerCodeCheck/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ class DetailViewController: UIViewController {
@IBOutlet weak var forksLabel: UILabel!
@IBOutlet weak var issuesLabel: UILabel!

var vc1: SearchViewController!
var searchViewController: SearchViewController!

override func viewDidLoad() {
super.viewDidLoad()

let repo = vc1.repositories[vc1.idx]
let repository = searchViewController.repositories[searchViewController.selectedIndex]

languageLabel.text = "Written in \(repo["language"] as? String ?? "")"
starsLabel.text = "\(repo["stargazers_count"] as? Int ?? 0) stars"
watcherLabel.text = "\(repo["wachers_count"] as? Int ?? 0) watchers"
forksLabel.text = "\(repo["forks_count"] as? Int ?? 0) forks"
issuesLabel.text = "\(repo["open_issues_count"] as? Int ?? 0) open issues"
languageLabel.text = "Written in \(repository["language"] as? String ?? "")"
starsLabel.text = "\(repository["stargazers_count"] as? Int ?? 0) stars"
watcherLabel.text = "\(repository["wachers_count"] as? Int ?? 0) watchers"
forksLabel.text = "\(repository["forks_count"] as? Int ?? 0) forks"
issuesLabel.text = "\(repository["open_issues_count"] as? Int ?? 0) open issues"
getImage()
}

func getImage(){

let repo = vc1.repositories[vc1.idx]
let repository = searchViewController.repositories[searchViewController.selectedIndex]

titleLabel.text = repo["full_name"] as? String
titleLabel.text = repository["full_name"] as? String

if let owner = repo["owner"] as? [String: Any] {
if let imgURL = owner["avatar_url"] as? String {
URLSession.shared.dataTask(with: URL(string: imgURL)!) { (data, res, err) in
let img = UIImage(data: data!)!
if let owner = repository["owner"] as? [String: Any] {
if let avatarURL = owner["avatar_url"] as? String {
URLSession.shared.dataTask(with: URL(string: avatarURL)!) { (data, res, err) in
let image = UIImage(data: data!)!
DispatchQueue.main.async {
self.imageView.image = img
self.imageView.image = image
}
}.resume()
}
Expand Down
21 changes: 10 additions & 11 deletions iOSEngineerCodeCheck/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class SearchViewController: UITableViewController, UISearchBarDelegate {

var task: URLSessionTask?
var word: String!
var url: String!
var idx: Int!
var selectedIndex: Int!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -38,10 +37,10 @@ class SearchViewController: UITableViewController, UISearchBarDelegate {
word = searchBar.text!

if word.count != 0 {
url = "https://api.github.com/search/repositories?q=\(word!)"
let url = "https://api.github.com/search/repositories?q=\(word!)"
task = URLSession.shared.dataTask(with: URL(string: url)!) { (data, res, err) in
if let obj = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] {
if let items = obj["items"] as? [[String: Any]] {
if let objects = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] {
if let items = objects["items"] as? [[String: Any]] {
self.repositories = items
DispatchQueue.main.async {
self.tableView.reloadData()
Expand All @@ -55,8 +54,8 @@ class SearchViewController: UITableViewController, UISearchBarDelegate {

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Detail" {
let dtl = segue.destination as! DetailViewController
dtl.vc1 = self
let detailViewController = segue.destination as! DetailViewController
detailViewController.searchViewController = self
}
}

Expand All @@ -66,15 +65,15 @@ class SearchViewController: UITableViewController, UISearchBarDelegate {

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let rp = repositories[indexPath.row]
cell.textLabel?.text = rp["full_name"] as? String ?? ""
cell.detailTextLabel?.text = rp["language"] as? String ?? ""
let repository = repositories[indexPath.row]
cell.textLabel?.text = repository["full_name"] as? String ?? ""
cell.detailTextLabel?.text = repository["language"] as? String ?? ""
cell.tag = indexPath.row
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
idx = indexPath.row
selectedIndex = indexPath.row
performSegue(withIdentifier: "Detail", sender: self)
}
}

0 comments on commit 0fcd865

Please sign in to comment.