diff --git a/TeamMiniApp-API.xcodeproj/project.pbxproj b/TeamMiniApp-API.xcodeproj/project.pbxproj index 05293fe..691da23 100644 --- a/TeamMiniApp-API.xcodeproj/project.pbxproj +++ b/TeamMiniApp-API.xcodeproj/project.pbxproj @@ -15,6 +15,13 @@ 8781411927FD11460095AECD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8781411827FD11460095AECD /* Assets.xcassets */; }; 8781411C27FD11460095AECD /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8781411A27FD11460095AECD /* LaunchScreen.storyboard */; }; 8781412527FD1A910095AECD /* .swiftlint.yml in Resources */ = {isa = PBXBuildFile; fileRef = 8781412427FD1A910095AECD /* .swiftlint.yml */; }; + 9553D519280158F200B4C78F /* SelectNumberTecArticleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D518280158F200B4C78F /* SelectNumberTecArticleViewController.swift */; }; + 9553D51C2801597700B4C78F /* TecArticleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D51B2801597700B4C78F /* TecArticleViewController.swift */; }; + 9553D51E2801598C00B4C78F /* WebTecArticleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D51D2801598C00B4C78F /* WebTecArticleViewController.swift */; }; + 9553D52128015DAE00B4C78F /* TecArticleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D51F28015DAE00B4C78F /* TecArticleTableViewCell.swift */; }; + 9553D52228015DAE00B4C78F /* TecArticleTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9553D52028015DAE00B4C78F /* TecArticleTableViewCell.xib */; }; + 9553D52428015E5700B4C78F /* GetArticle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D52328015E5700B4C78F /* GetArticle.swift */; }; + 9553D52628015E7B00B4C78F /* NewsArticle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9553D52528015E7B00B4C78F /* NewsArticle.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -27,6 +34,13 @@ 8781411B27FD11460095AECD /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 8781411D27FD11460095AECD /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 8781412427FD1A910095AECD /* .swiftlint.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; path = .swiftlint.yml; sourceTree = ""; }; + 9553D518280158F200B4C78F /* SelectNumberTecArticleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectNumberTecArticleViewController.swift; sourceTree = ""; }; + 9553D51B2801597700B4C78F /* TecArticleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TecArticleViewController.swift; sourceTree = ""; }; + 9553D51D2801598C00B4C78F /* WebTecArticleViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebTecArticleViewController.swift; sourceTree = ""; }; + 9553D51F28015DAE00B4C78F /* TecArticleTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TecArticleTableViewCell.swift; sourceTree = ""; }; + 9553D52028015DAE00B4C78F /* TecArticleTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = TecArticleTableViewCell.xib; sourceTree = ""; }; + 9553D52328015E5700B4C78F /* GetArticle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetArticle.swift; sourceTree = ""; }; + 9553D52528015E7B00B4C78F /* NewsArticle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsArticle.swift; sourceTree = ""; }; B171D68812E28A942DA80197 /* Pods-TeamMiniApp-API.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TeamMiniApp-API.debug.xcconfig"; path = "Target Support Files/Pods-TeamMiniApp-API/Pods-TeamMiniApp-API.debug.xcconfig"; sourceTree = ""; }; F67185BABEF2E648D9E54220 /* Pods_TeamMiniApp_API.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TeamMiniApp_API.framework; sourceTree = BUILT_PRODUCTS_DIR; }; FBC26C711A97690244CDC229 /* Pods-TeamMiniApp-API.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TeamMiniApp-API.release.xcconfig"; path = "Target Support Files/Pods-TeamMiniApp-API/Pods-TeamMiniApp-API.release.xcconfig"; sourceTree = ""; }; @@ -73,6 +87,7 @@ 8781410E27FD11460095AECD /* TeamMiniApp-API */ = { isa = PBXGroup; children = ( + 9553D51A2801596000B4C78F /* Tec */, 8781410F27FD11460095AECD /* AppDelegate.swift */, 8781411127FD11460095AECD /* SceneDelegate.swift */, 8781411327FD11460095AECD /* ViewController.swift */, @@ -85,6 +100,20 @@ path = "TeamMiniApp-API"; sourceTree = ""; }; + 9553D51A2801596000B4C78F /* Tec */ = { + isa = PBXGroup; + children = ( + 9553D518280158F200B4C78F /* SelectNumberTecArticleViewController.swift */, + 9553D51B2801597700B4C78F /* TecArticleViewController.swift */, + 9553D51D2801598C00B4C78F /* WebTecArticleViewController.swift */, + 9553D51F28015DAE00B4C78F /* TecArticleTableViewCell.swift */, + 9553D52028015DAE00B4C78F /* TecArticleTableViewCell.xib */, + 9553D52328015E5700B4C78F /* GetArticle.swift */, + 9553D52528015E7B00B4C78F /* NewsArticle.swift */, + ); + path = Tec; + sourceTree = ""; + }; BCDCF2C3849305B3EDE8DD7A /* Pods */ = { isa = PBXGroup; children = ( @@ -159,6 +188,7 @@ 8781411C27FD11460095AECD /* LaunchScreen.storyboard in Resources */, 8781411927FD11460095AECD /* Assets.xcassets in Resources */, 8781411727FD11460095AECD /* Main.storyboard in Resources */, + 9553D52228015DAE00B4C78F /* TecArticleTableViewCell.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -229,7 +259,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9553D519280158F200B4C78F /* SelectNumberTecArticleViewController.swift in Sources */, + 9553D51E2801598C00B4C78F /* WebTecArticleViewController.swift in Sources */, 8781411427FD11460095AECD /* ViewController.swift in Sources */, + 9553D52128015DAE00B4C78F /* TecArticleTableViewCell.swift in Sources */, + 9553D52628015E7B00B4C78F /* NewsArticle.swift in Sources */, + 9553D51C2801597700B4C78F /* TecArticleViewController.swift in Sources */, + 9553D52428015E5700B4C78F /* GetArticle.swift in Sources */, 8781411027FD11460095AECD /* AppDelegate.swift in Sources */, 8781411227FD11460095AECD /* SceneDelegate.swift in Sources */, ); diff --git a/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/UserInterfaceState.xcuserstate b/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/UserInterfaceState.xcuserstate index 1adb2e1..f40dffb 100644 Binary files a/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/UserInterfaceState.xcuserstate and b/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..03622e9 --- /dev/null +++ b/TeamMiniApp-API.xcworkspace/xcuserdata/m.kondo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,6 @@ + + + diff --git a/TeamMiniApp-API/Base.lproj/Main.storyboard b/TeamMiniApp-API/Base.lproj/Main.storyboard index 8c07bcf..89552a0 100644 --- a/TeamMiniApp-API/Base.lproj/Main.storyboard +++ b/TeamMiniApp-API/Base.lproj/Main.storyboard @@ -90,21 +90,122 @@ - + - + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TeamMiniApp-API/Tec/GetArticle.swift b/TeamMiniApp-API/Tec/GetArticle.swift new file mode 100644 index 0000000..6bc8c20 --- /dev/null +++ b/TeamMiniApp-API/Tec/GetArticle.swift @@ -0,0 +1,35 @@ +// +// GetArticleData.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// +import Foundation +import Alamofire +protocol GetArticleDelegate: AnyObject{ + func catchArticleData(articles: Article) +} + +class GetArticle{ + weak var delegate: GetArticleDelegate? + func request(size: Int){ + let url = "https://newsapi.org/v2/top-headlines?country=jp&pageSize=\(size)&category=technology&apiKey=2dadbe9531074cfe86934358180c7e24" + // エンコードしないと、category名が日本語の時に対応できない + let encorderUrlString:String = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! + AF.request(encorderUrlString, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { + response in + switch response.result{ + case .success: + do{ + let articles = try JSONDecoder().decode(Article.self, from: response.data!) + self.delegate?.catchArticleData(articles: articles) + } + catch{ + print("取得失敗") + } + case .failure(_): + print("switchFail") + } + } + } +} diff --git a/TeamMiniApp-API/Tec/NewsArticle.swift b/TeamMiniApp-API/Tec/NewsArticle.swift new file mode 100644 index 0000000..a17a723 --- /dev/null +++ b/TeamMiniApp-API/Tec/NewsArticle.swift @@ -0,0 +1,16 @@ +// +// NewsArticle.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// + +import Foundation +struct Article: Codable { + var articles: [ArticleInfo] +} +struct ArticleInfo: Codable { + let publishedAt: String? + let title: String? + let url: String? +} diff --git a/TeamMiniApp-API/Tec/SelectNumberTecArticleViewController.swift b/TeamMiniApp-API/Tec/SelectNumberTecArticleViewController.swift new file mode 100644 index 0000000..3a846e0 --- /dev/null +++ b/TeamMiniApp-API/Tec/SelectNumberTecArticleViewController.swift @@ -0,0 +1,30 @@ +// +// SelectNumberTecArticeViewController.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// + +import UIKit + +class SelectNumberTecArticleViewController: UIViewController { + @IBOutlet private weak var slider: UISlider! + @IBOutlet private weak var numberTecArticle: UILabel! + + @IBAction private func tappedSlider(_ sender: Any) { + numberTecArticle.text = String(Int(slider.value)) + } + + @IBAction private func tappedTecArticleViewButton(_ sender: Any) { + performSegue(withIdentifier: "tecArticleViewControllerSegue", sender: nil) + } + + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + // 画面遷移時にスライダーの値(記事数)を渡す + if segue.identifier == "tecArticleViewControllerSegue" { + let tecArticleVC = segue.destination as! TecArticleViewController + tecArticleVC.catchNumberTecArticle = Int(slider.value) + } + } +} diff --git a/TeamMiniApp-API/Tec/TecArticleTableViewCell.swift b/TeamMiniApp-API/Tec/TecArticleTableViewCell.swift new file mode 100644 index 0000000..c0f7d4e --- /dev/null +++ b/TeamMiniApp-API/Tec/TecArticleTableViewCell.swift @@ -0,0 +1,25 @@ +// +// TecArticleTableViewCell.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// + +import UIKit + +class TecArticleTableViewCell: UITableViewCell { + + @IBOutlet weak var titleLabel: UILabel! + @IBOutlet weak var publishedAtLabel: UILabel! + override func awakeFromNib() { + super.awakeFromNib() + // Initialization code + } + + override func setSelected(_ selected: Bool, animated: Bool) { + super.setSelected(selected, animated: animated) + + // Configure the view for the selected state + } + +} diff --git a/TeamMiniApp-API/Tec/TecArticleTableViewCell.xib b/TeamMiniApp-API/Tec/TecArticleTableViewCell.xib new file mode 100644 index 0000000..567850b --- /dev/null +++ b/TeamMiniApp-API/Tec/TecArticleTableViewCell.xib @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TeamMiniApp-API/Tec/TecArticleViewController.swift b/TeamMiniApp-API/Tec/TecArticleViewController.swift new file mode 100644 index 0000000..c95dcce --- /dev/null +++ b/TeamMiniApp-API/Tec/TecArticleViewController.swift @@ -0,0 +1,70 @@ +// +// TecArticleViewController.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// + +import UIKit + +class TecArticleViewController: UIViewController { + @IBOutlet weak var tableView: UITableView! + + private var getArticle = GetArticle() + private var selectedArticleUrl: String? + private (set) var catchArticles: Article? + public var catchNumberTecArticle: Int = 0 + + override func viewDidLoad() { + super.viewDidLoad() + getArticle.delegate = self + // 取得した記事数分、NewsAPIにリクエストを送る + getArticle.request(size: catchNumberTecArticle) + setUpTableView() + } + + private func setUpTableView() { + tableView.delegate = self + tableView.dataSource = self + tableView.register(UINib(nibName: "TecArticleTableViewCell", bundle: nil), forCellReuseIdentifier: "tecArticleCell") + } + + override func prepare(for segue: UIStoryboardSegue, sender: Any?) { + if segue.identifier == "WebTecArticleViewControllerSegue"{ + let webTecArticleVC = segue.destination as! WebTecArticleViewController + webTecArticleVC.catchWebUrl = selectedArticleUrl ?? "URLなしs" + } + } + +} + +extension TecArticleViewController: UITableViewDelegate,UITableViewDataSource{ + // 指定された記事数分、tableViewを返す + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + catchNumberTecArticle + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "tecArticleCell", for: indexPath) as! TecArticleTableViewCell + cell.titleLabel.text = catchArticles?.articles[indexPath.row].title + cell.publishedAtLabel.text = catchArticles?.articles[indexPath.row].publishedAt + return cell + } + + func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { + return 140 + } + + // セルをタップした時にWebURL記事に飛ぶ + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + selectedArticleUrl = catchArticles?.articles[indexPath.row].url ?? "URLなし" + performSegue(withIdentifier: "WebTecArticleViewControllerSegue", sender: nil) + } +} + +extension TecArticleViewController: GetArticleDelegate{ + func catchArticleData(articles: Article) { + catchArticles = articles + tableView.reloadData() + } +} diff --git a/TeamMiniApp-API/Tec/WebTecArticleViewController.swift b/TeamMiniApp-API/Tec/WebTecArticleViewController.swift new file mode 100644 index 0000000..b0cc47b --- /dev/null +++ b/TeamMiniApp-API/Tec/WebTecArticleViewController.swift @@ -0,0 +1,22 @@ +// +// WebTecArticleViewController.swift +// TeamMiniApp-API +// +// Created by 近藤米功 on 2022/04/09. +// + +import UIKit +import WebKit +class WebTecArticleViewController: UIViewController { + public var catchWebUrl: String = "" + @IBOutlet private weak var webView: WKWebView! + + override func viewDidLoad() { + super.viewDidLoad() + let url = URL(string: catchWebUrl) + guard let url = url else { + return + } + webView.load(URLRequest(url: url)) + } +}