diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..4730aa5 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,6 +8,10 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; + 1652B9AB1D05E48F00443CF1 /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */; }; + 1652B9AD1D05E67F00443CF1 /* InformationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */; }; + 1652B9AF1D05E81F00443CF1 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */; }; + 1652B9B11D05EBDF00443CF1 /* Peoples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9B01D05EBDF00443CF1 /* Peoples.swift */; }; 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A275B1CF0D87100A988C4 /* APIResource.swift */; }; 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 874D06581CEF295E009A494D /* AppDelegate.swift */; }; 874D065E1CEF295E009A494D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D065C1CEF295E009A494D /* Main.storyboard */; }; @@ -35,6 +39,10 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = ""; }; + 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InformationViewController.swift; sourceTree = ""; }; + 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = ""; }; + 1652B9B01D05EBDF00443CF1 /* Peoples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Peoples.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 = ""; }; @@ -88,6 +96,8 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */, + 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -103,6 +113,8 @@ isa = PBXGroup; children = ( 872A275B1CF0D87100A988C4 /* APIResource.swift */, + 1652B9B01D05EBDF00443CF1 /* Peoples.swift */, + 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */, ); name = Model; sourceTree = ""; @@ -468,8 +480,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 1652B9AB1D05E48F00443CF1 /* SearchViewController.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, + 1652B9AF1D05E81F00443CF1 /* SWAPI.swift in Sources */, + 1652B9B11D05EBDF00443CF1 /* Peoples.swift in Sources */, + 1652B9AD1D05E67F00443CF1 /* InformationViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/APIClient/APIResource.swift b/APIClient/APIResource.swift index 6bb455f..ef6e7d1 100644 --- a/APIClient/APIResource.swift +++ b/APIClient/APIResource.swift @@ -12,19 +12,6 @@ import Freddy import AwesomeCache - -/** - Representation for a resource that may or may not be loaded yet. - - It is based on two generic types: - - - The `Target` is a `Moya.TargetType` that abstracts the API. It must also be `Cacheable` so that responses can be cached. - - The `Resource` is a `Freddy.JSONDecodable` type so that responses from the API can be directly parsed to JSON and decoded to the expected type. - - You are free to use this utility or implement your own way to keep track of loaded and not loaded resources. - - - seealso: MoyaProvider.request(_:completion:) -*/ enum APIResource { case notLoaded(Target) @@ -34,7 +21,6 @@ enum APIResource() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + if let SearchViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? SearchViewController { + + SearchViewController.starWarsAPI = swAPI + + } + return true } + } diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..38a283e 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -21,10 +21,10 @@ - + - + @@ -32,13 +32,109 @@ + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/APIClient/Info.plist b/APIClient/Info.plist index 40c6215..8c7d69f 100644 --- a/APIClient/Info.plist +++ b/APIClient/Info.plist @@ -22,6 +22,17 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSExceptionDomains + + swapi.co + + NSExceptionAllowsInsecureHTTPLoads + + + + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/APIClient/InformationViewController.swift b/APIClient/InformationViewController.swift new file mode 100644 index 0000000..a6348a6 --- /dev/null +++ b/APIClient/InformationViewController.swift @@ -0,0 +1,41 @@ +// +// InformationViewController.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + + +import Foundation +import UIKit +import Freddy +import Moya +import AlamofireImage + + +class InformationViewController: UIViewController { + + var peoples : SWPeoples? + + @IBOutlet weak var name: UILabel! + + @IBOutlet weak var gender: UILabel! + + @IBOutlet weak var height: UILabel! + + @IBOutlet weak var mass: UILabel! + + override func viewDidLoad() { + super.viewDidLoad() + name.text = peoples?.name + gender.text = peoples?.gender + height.text = peoples?.height + mass.text = peoples?.mass + + } + + +} + + diff --git a/APIClient/Peoples.swift b/APIClient/Peoples.swift new file mode 100644 index 0000000..5a15faa --- /dev/null +++ b/APIClient/Peoples.swift @@ -0,0 +1,33 @@ +// +// Peoples.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy +import UIKit + +// Informationen über Figur + +struct SWPeoples: JSONDecodable { + + let name: String + let gender: String + let height: String + let mass: String + + + // Durchsucht Datei von API nach Schlagwörtern + + init(json: JSON) throws { + self.name = try json.string("name") + self.gender = try json.string("gender") + self.height = try json.string("height") + self.mass = try json.string("mass") + + } + +} diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift new file mode 100644 index 0000000..a762354 --- /dev/null +++ b/APIClient/SWAPI.swift @@ -0,0 +1,61 @@ +// +// SWAPI.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + + +enum SWAPI: Moya.TargetType, Cacheable { + + case people(id: Int) + + var baseURL: NSURL { return NSURL(string: "http://swapi.co/api")! } + + var path: String { + switch self { + case .people(let id): return "/people/\(id)/" + } + } + + var method: Moya.Method { return .GET } + + var parameters: [String : AnyObject]? { + switch self { + default: return nil + } + } + + var sampleData: NSData { + switch self { + default: return "".dataUsingEncoding(NSUTF8StringEncoding)! + } + } + + var cacheIdentifier: String { + return self.path + } + + +} + +// Represents a resource provided by the SWAPI by its name + +struct NamedResource: Freddy.JSONDecodable { + + let name: String + + init(name: String) { + self.name = name + } + + init(json: JSON) throws { + self.name = try json.string("name") + } + +} diff --git a/APIClient/SearchViewController.swift b/APIClient/SearchViewController.swift new file mode 100644 index 0000000..51def53 --- /dev/null +++ b/APIClient/SearchViewController.swift @@ -0,0 +1,79 @@ +// +// SearchViewController.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Freddy +import Moya +import AlamofireImage + + +class SearchViewController: UIViewController { + + var starWarsAPI : MoyaProvider! + + var peoples: SWPeoples? { + didSet { + +// Nur um Textfeld bei Zurückgehen auf Suchbegriff zu setzen + +// self.searchTextfield.text = peoples?.name + } + } + + @IBOutlet weak var searchTextfield: UITextField! + @IBAction func buttonLoad(sender: AnyObject) { + loadPeoples(Int(searchTextfield.text!) ?? 0) + } + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + if segue.identifier == "showInformation" { + + guard let informationViewController = (segue.destinationViewController as?InformationViewController) else { + return + } + guard let peoplesInfo = self.peoples else { + return + } + +// Übergabe + informationViewController.peoples = peoplesInfo + + } + + } + + func loadPeoples(peoples: Int) { + starWarsAPI.request(.people(id: peoples)) { result in + switch result { + case .Success(let response): + do { + try response.filterSuccessfulStatusCodes() + // Try to parse the response to JSON + let json = try JSON(data: response.data) + // Try to decode the JSON to the required type + let people = try SWPeoples(json: json) + // Configure view according to model + self.peoples = people + self.performSegueWithIdentifier("showInformation", sender: nil) + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + +} + + + + + +