Skip to content

Commit

Permalink
Merge pull request #13 from Tomoya113/fix/issue-#2
Browse files Browse the repository at this point in the history
ソースコードの安全性の向上
  • Loading branch information
txxxxc authored Feb 22, 2022
2 parents fe46315 + f99aa7e commit a9b4c57
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 49 deletions.
53 changes: 29 additions & 24 deletions iOSEngineerCodeCheck/RepositoryDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ import UIKit

class RepositoryDetailViewController: UIViewController {

@IBOutlet weak var avatarImageView: UIImageView!

@IBOutlet weak var titleLabel: UILabel!

@IBOutlet weak var languageLabel: UILabel!
@IBOutlet weak private var avatarImageView: UIImageView!
@IBOutlet weak private var titleLabel: UILabel!
@IBOutlet weak private var languageLabel: UILabel!
@IBOutlet weak private var starCountLabel: UILabel!
@IBOutlet weak private var watchersLabel: UILabel!
@IBOutlet weak private var forksCountLabel: UILabel!
@IBOutlet weak private var openIssuesCountLabel: UILabel!

@IBOutlet weak var starCountLabel: UILabel!
@IBOutlet weak var watchersLabel: UILabel!
@IBOutlet weak var forksCountLabel: UILabel!
@IBOutlet weak var openIssuesCountLabel: UILabel!

var searchRepositoriesController: SearchRepositoriesViewController!
var searchRepositoriesViewController: SearchRepositoriesViewController?
private var repository: [String: Any] = [:]

override func viewDidLoad() {
super.viewDidLoad()

let repository = searchRepositoriesController.repositories[searchRepositoriesController.selectedRepositoryIndex]
guard
let searchRepositoriesViewController = searchRepositoriesViewController,
let selectedRepositoryIndex = searchRepositoriesViewController.selectedRepositoryIndex
else {
return
}
repository = searchRepositoriesViewController.repositories[selectedRepositoryIndex]

languageLabel.text = "Written in \(repository["language"] as? String ?? "")"
starCountLabel.text = "\(repository["stargazers_count"] as? Int ?? 0) stars"
Expand All @@ -38,21 +41,23 @@ class RepositoryDetailViewController: UIViewController {
}

func getRepositoryAvatar() {
titleLabel.text = repository["full_name"] as? String ?? ""

let repository = searchRepositoriesController.repositories[searchRepositoriesController.selectedRepositoryIndex]
guard
let owner = repository["owner"] as? [String: Any],
let avatarURLString = owner["avatar_url"] as? String
else { return }

titleLabel.text = repository["full_name"] as? String
guard let avatarURL = URL(string: avatarURLString) else { return }

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.avatarImageView.image = image
}
}.resume()
URLSession.shared.dataTask(with: avatarURL) { (data, res, err) in
guard let data = data else { return }
let image = UIImage(data: data)
guard let image = image else { return }
DispatchQueue.main.async {
self.avatarImageView.image = image
}
}
}.resume()

}

Expand Down
53 changes: 28 additions & 25 deletions iOSEngineerCodeCheck/SearchRepositoriesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import UIKit

class SearchRepositoriesViewController: UITableViewController {

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak private var searchBar: UISearchBar!

var repositories: [[String: Any]] = []

var task: URLSessionTask?
var searchWord: String!
var apiURL: String!
var selectedRepositoryIndex: Int!
private var task: URLSessionTask?
private var searchWord: String = ""
var selectedRepositoryIndex: Int?

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -27,9 +26,11 @@ class SearchRepositoriesViewController: UITableViewController {

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// NOTE:画面遷移時に呼ばれる
if segue.identifier == "Detail"{
let destinationViewController = segue.destination as! RepositoryDetailViewController
destinationViewController.searchRepositoriesController = self
if segue.identifier == "Detail" {
guard let destinationViewController = segue.destination as? RepositoryDetailViewController else {
fatalError("This line can not be reached")
}
destinationViewController.searchRepositoriesViewController = self
}
}

Expand All @@ -56,7 +57,6 @@ extension SearchRepositoriesViewController {
// NOTE:セルを選択した時に呼ばれる
selectedRepositoryIndex = indexPath.row
performSegue(withIdentifier: "Detail", sender: self)

}

}
Expand All @@ -74,24 +74,27 @@ extension SearchRepositoriesViewController: UISearchBarDelegate {
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let searchWord = searchBar.text else { return }
if searchWord.isEmpty { return }

searchWord = searchBar.text!

if searchWord.count != 0 {
apiURL = "https://api.github.com/search/repositories?q=\(searchWord!)"
task = URLSession.shared.dataTask(with: URL(string: apiURL)!) { (data, res, err) in
if let obj = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] {
if let items = obj["items"] as? [[String: Any]] {
self.repositories = items
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
// NOTE:タスクを実行する(resumeを実行しないとタスクが実行されない)
task?.resume()
let apiURLString = "https://api.github.com/search/repositories?q=\(searchWord)"
guard let apiURL = URL(string: apiURLString) else {
print("invalid URL")
return
}

task = URLSession.shared.dataTask(with: apiURL) { (data, res, err) in
guard let data = data else { return }
guard let json = try? JSONSerialization.jsonObject(with: data) else { return }
guard let obj = json as? [String: Any] else { return }
guard let items = obj["items"] as? [[String: Any]] else { return }

self.repositories = items
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
// NOTE:タスクを実行する(resumeを実行しないとタスクが実行されない)
task?.resume()
}
}

0 comments on commit a9b4c57

Please sign in to comment.