From f0ea0d868787fb373a3794f9122dd0aa7d0048e8 Mon Sep 17 00:00:00 2001 From: Ellie92 Date: Thu, 13 Apr 2017 09:23:33 +0200 Subject: [PATCH 1/2] Quotes App V1 --- APIClient.xcodeproj/project.pbxproj | 12 +++++ APIClient/Base.lproj/Main.storyboard | 55 +++++++++++++++++++---- APIClient/quotes.swift | 53 +++++++++++++++++++++++ APIClient/quotesViewController.swift | 65 ++++++++++++++++++++++++++++ APIClient/results.swift | 25 +++++++++++ 5 files changed, 201 insertions(+), 9 deletions(-) create mode 100644 APIClient/quotes.swift create mode 100644 APIClient/quotesViewController.swift create mode 100644 APIClient/results.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index 3b30426..60d76d1 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,6 +8,9 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; + 0D60CECA1E9E174A00A8E67A /* quotesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D60CEC91E9E174A00A8E67A /* quotesViewController.swift */; }; + 0D93F9151E9E1E6A0095065E /* quotes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D93F9141E9E1E6A0095065E /* quotes.swift */; }; + 0D93F9171E9E2C250095065E /* results.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0D93F9161E9E2C250095065E /* results.swift */; }; 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 874D06581CEF295E009A494D /* AppDelegate.swift */; }; 874D065E1CEF295E009A494D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D065C1CEF295E009A494D /* Main.storyboard */; }; 874D06601CEF295E009A494D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 874D065F1CEF295E009A494D /* Assets.xcassets */; }; @@ -34,6 +37,9 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 0D60CEC91E9E174A00A8E67A /* quotesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = quotesViewController.swift; sourceTree = ""; }; + 0D93F9141E9E1E6A0095065E /* quotes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = quotes.swift; sourceTree = ""; }; + 0D93F9161E9E2C250095065E /* results.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = results.swift; sourceTree = ""; }; 198B23CD94C6D84016A5208A /* Pods-APIClientUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientUITests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientUITests/Pods-APIClientUITests.release.xcconfig"; sourceTree = ""; }; 4FFA0A7AA37697BD5CBC3211 /* Pods_APIClientTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 65C820A0F52E3181871F5ECA /* Pods-APIClientTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientTests/Pods-APIClientTests.release.xcconfig"; sourceTree = ""; }; @@ -86,6 +92,8 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 0D60CEC91E9E174A00A8E67A /* quotesViewController.swift */, + 0D93F9141E9E1E6A0095065E /* quotes.swift */, ); name = "View Controller"; sourceTree = ""; @@ -100,6 +108,7 @@ 872A27591CF0D64A00A988C4 /* Model */ = { isa = PBXGroup; children = ( + 0D93F9161E9E2C250095065E /* results.swift */, ); name = Model; sourceTree = ""; @@ -468,7 +477,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 0D93F9151E9E1E6A0095065E /* quotes.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, + 0D93F9171E9E2C250095065E /* results.swift in Sources */, + 0D60CECA1E9E174A00A8E67A /* quotesViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..bc8f27e 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,8 +1,13 @@ - - + + + + + - + + + @@ -21,24 +26,56 @@ - + - + - + - + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + - + diff --git a/APIClient/quotes.swift b/APIClient/quotes.swift new file mode 100644 index 0000000..1bc5b6b --- /dev/null +++ b/APIClient/quotes.swift @@ -0,0 +1,53 @@ +// +// quotes.swift +// APIClient +// +// Created by Elvira Beisel on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya + +enum Quotesapi: Moya.TargetType { + + case quote(category: String) + + var baseURL: URL { + return URL(string: "https://andruxnet-random-famous-quotes.p.mashape.com/")! + } + + var path: String { + + switch self { + case .quote: + return "" + } + } + + var method: Moya.Method { + return.get + } + + var parameters: [String: Any]? { + switch self { + case .quote(category: let category): + return [ + "cat": category + ] + } + } + + var parameterEncoding: ParameterEncoding { + return URLEncoding.default + } + + var sampleData: Data { + return "".data(using: .utf8)! + } + + var task: Moya.Task { + return .request + } + +} diff --git a/APIClient/quotesViewController.swift b/APIClient/quotesViewController.swift new file mode 100644 index 0000000..f9b9dff --- /dev/null +++ b/APIClient/quotesViewController.swift @@ -0,0 +1,65 @@ +// +// quotesViewController.swift +// APIClient +// +// Created by Elvira Beisel on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Moya + +import Freddy + +class quotesViewController: UIViewController, UITextFieldDelegate { + + + @IBOutlet weak var inputTextField: UITextField! + @IBOutlet weak var nameLabel: UILabel! + + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + + guard let input = textField.text else { + return true + } + + let endpointClosure = { (target: Quotesapi) -> Endpoint in + let headers = [ + "X-Mashape-Key": "gpYkdsl5x9msho9B1uCnd5LtHdVqp1932RLjsn3egoFcCcYng9" + ] + let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target) + return defaultEndpoint + .adding(newHTTPHeaderFields: headers) + } + + + + let provider = MoyaProvider(endpointClosure: endpointClosure) + + let query = Quotesapi.quote(category: input) + + provider.request(query) { result in + + switch result { + case .success(let response): + do { + let jsonResponse = try JSON(data: response.data) + print(jsonResponse) + let result = try results (json: jsonResponse) + self.nameLabel.text = result.quote + } catch { + print(error) + } + + + case .failure(let error): + + print(error) + + } + } + + return true + } +} diff --git a/APIClient/results.swift b/APIClient/results.swift new file mode 100644 index 0000000..862ed4e --- /dev/null +++ b/APIClient/results.swift @@ -0,0 +1,25 @@ +// +// results.swift +// APIClient +// +// Created by Elvira Beisel on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy + +struct results: JSONDecodable { + + let quote: String + let author: String + let category: String + + init(json: JSON) throws { + try self.quote = json.getString(at: "quote") + try self.author = json.getString(at: "author") + try self.category = json.getString(at: "category") + + } + +} From 690e284e95c9e5745e49d74387e0d5f550573c0a Mon Sep 17 00:00:00 2001 From: Ellie92 Date: Thu, 13 Apr 2017 11:09:39 +0200 Subject: [PATCH 2/2] Quotes App V2 --- APIClient/Base.lproj/Main.storyboard | 59 ++++++++++++++++++---------- APIClient/quotesViewController.swift | 40 +++++++++++++------ 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index bc8f27e..0f3ee15 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -38,44 +38,63 @@ - + + + + + + + + + + + + + + + - - - - + + + - - - - - - + - + - + + + + + + - + + - + diff --git a/APIClient/quotesViewController.swift b/APIClient/quotesViewController.swift index f9b9dff..589a686 100644 --- a/APIClient/quotesViewController.swift +++ b/APIClient/quotesViewController.swift @@ -15,14 +15,32 @@ import Freddy class quotesViewController: UIViewController, UITextFieldDelegate { - @IBOutlet weak var inputTextField: UITextField! + @IBOutlet weak var activityView: UIActivityIndicatorView! + // @IBOutlet weak var inputTextField: UITextField! @IBOutlet weak var nameLabel: UILabel! + @IBOutlet weak var authorLabel: UILabel! + @IBAction func moviesButton(_ sender: Any) { + + showQuote(for: "movies") + + } + @IBAction func famousButton(_ sender: Any) { + showQuote(for: "famous") + } - func textFieldShouldReturn(_ textField: UITextField) -> Bool { + override func viewDidLoad() { + super.viewDidLoad() - guard let input = textField.text else { - return true - } + nameLabel.text = "" + authorLabel.text = "" + self.title = "It´s your choice!" + self.activityView.hidesWhenStopped = true + + } + + func showQuote(for category: String) { + + activityView.startAnimating() let endpointClosure = { (target: Quotesapi) -> Endpoint in let headers = [ @@ -33,14 +51,14 @@ class quotesViewController: UIViewController, UITextFieldDelegate { .adding(newHTTPHeaderFields: headers) } - - let provider = MoyaProvider(endpointClosure: endpointClosure) - let query = Quotesapi.quote(category: input) + let query = Quotesapi.quote(category: category) + provider.request(query) { result in + self.activityView.stopAnimating() switch result { case .success(let response): do { @@ -48,18 +66,18 @@ class quotesViewController: UIViewController, UITextFieldDelegate { print(jsonResponse) let result = try results (json: jsonResponse) self.nameLabel.text = result.quote + self.authorLabel.text = result.author } catch { print(error) } - + case .failure(let error): print(error) - + } } - return true } }