Skip to content

Leider schon das erste Problem #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions APIClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -8,6 +8,10 @@

/* Begin PBXBuildFile section */
078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; };
830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5861CF437F100203B41 /* StarWarsViewController.swift */; };
830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5881CF57DEE00203B41 /* SWAPI.swift */; };
83229B671CFC842800FF94B7 /* Structs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83229B661CFC842800FF94B7 /* Structs.swift */; };
835E5DB91D0440A300FD7CC3 /* MyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835E5DB81D0440A300FD7CC3 /* MyAPITests.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 */; };
@@ -39,6 +43,10 @@
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 = "<group>"; };
82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
830DA5861CF437F100203B41 /* StarWarsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StarWarsViewController.swift; sourceTree = "<group>"; };
830DA5881CF57DEE00203B41 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = "<group>"; };
83229B661CFC842800FF94B7 /* Structs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Structs.swift; sourceTree = "<group>"; };
835E5DB81D0440A300FD7CC3 /* MyAPITests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyAPITests.swift; sourceTree = "<group>"; };
872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = "<group>"; };
874D06551CEF295E009A494D /* APIClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIClient.app; sourceTree = BUILT_PRODUCTS_DIR; };
874D06581CEF295E009A494D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -88,6 +96,7 @@
872A27571CF0D4E200A988C4 /* View Controller */ = {
isa = PBXGroup;
children = (
830DA5861CF437F100203B41 /* StarWarsViewController.swift */,
);
name = "View Controller";
sourceTree = "<group>";
@@ -103,6 +112,8 @@
isa = PBXGroup;
children = (
872A275B1CF0D87100A988C4 /* APIResource.swift */,
830DA5881CF57DEE00203B41 /* SWAPI.swift */,
83229B661CFC842800FF94B7 /* Structs.swift */,
);
name = Model;
sourceTree = "<group>";
@@ -156,6 +167,7 @@
isa = PBXGroup;
children = (
87EC71531CFA380F00FBDDDE /* Info.plist */,
835E5DB81D0440A300FD7CC3 /* MyAPITests.swift */,
);
path = APIClientTests;
sourceTree = "<group>";
@@ -468,7 +480,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */,
872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */,
83229B671CFC842800FF94B7 /* Structs.swift in Sources */,
830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */,
874D06591CEF295E009A494D /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -477,6 +492,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
835E5DB91D0440A300FD7CC3 /* MyAPITests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -636,10 +652,12 @@
baseConfigurationReference = F8B9811B1068B05E73195990 /* Pods-APIClientTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = APIClientTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClientTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/APIClient.app/APIClient";
};
name = Debug;
@@ -649,6 +667,7 @@
baseConfigurationReference = 65C820A0F52E3181871F5ECA /* Pods-APIClientTests.release.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CLANG_ENABLE_MODULES = YES;
INFOPLIST_FILE = APIClientTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClientTests";
7 changes: 7 additions & 0 deletions APIClient/.xcdatamodeld/Datatest.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="10171" systemVersion="15F34" minimumToolsVersion="Xcode 7.0">
<entity name="Entity" syncable="YES"/>
<elements>
<element name="Entity" positionX="-63" positionY="-18" width="128" height="45"/>
</elements>
</model>
8 changes: 8 additions & 0 deletions APIClient/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -7,14 +7,22 @@
//

import UIKit
import Moya

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let starWAPI = MoyaProvider<SWAPI>()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if let starWarsViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? StarWarsViewController {
// API to ViewController
starWarsViewController.starWAPI = starWAPI
}

return true
}

143 changes: 134 additions & 9 deletions APIClient/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions APIClient/Info.plist
Original file line number Diff line number Diff line change
@@ -22,6 +22,17 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>swapi.co</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
90 changes: 90 additions & 0 deletions APIClient/SWAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// SWAPI.swift
// APIClient
//
// Created by Christoph Blattgerste on 25.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation
import Moya
import Freddy


//adress of the used API: ("http://swapi.co/api/")

enum SWAPI: Moya.TargetType, Cacheable {

case starships (NamedResource<Starships>)
case planets (NamedResource<Planets>)
case species (NamedResource<Species>)

var baseURL: NSURL {return NSURL(string: "http://swapi.co/api")! }

var path : String {
switch self {
case .starships(let namedResource): return "/starships/\(namedResource.name)"
case .planets(let namedResource): return "/planets/\(namedResource.name)"
case .species(let namedResource): return "/species/\(namedResource.name)"
}
}

var method: Moya.Method {return .GET}

var parameters: [String : AnyObject]? {
switch self {
default: return nil
}
}

var sampleData: NSData {
switch self {
// case .starships(let namedResource):
// return
// "name": "CR90 corvette",
// "model": "CR90 corvette",
// "manufacturer": "Corellian Engineering Corporation",
// "cost_in_credits": "3500000",
// "length": "150",
// "max_atmosphering_speed": "950",
// "crew": "165",
// "passengers": "600",
// "cargo_capacity": "3000000",
// "consumables": "1 year",
// "hyperdrive_rating": "2.0",
// "MGLT": "60",
// "starship_class": "corvette",
// "pilots": [],
// "films": [
// "http://swapi.co/api/films/6/",
// "http://swapi.co/api/films/3/",
// "http://swapi.co/api/films/1/"
// ],
// "created": "2014-12-10T14:20:33.369000Z",
// "edited": "2014-12-22T17:35:45.408368Z",
// "url": "http://swapi.co/api/starships/2/"
// .dataUsingEncoding(NSUTF8StringEncoding)!
default:
return "".dataUsingEncoding(NSUTF8StringEncoding)!
}
}

var cacheIdentifier: String {
return self.path
}
}


struct NamedResource<Resource: Freddy.JSONDecodable>: Freddy.JSONDecodable {

let name: String

init(name: String) {
self.name = name
}

init(json: JSON) throws {
self.name = try json.string("name")
}

}
180 changes: 180 additions & 0 deletions APIClient/StarWarsViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//
// StarWarsViewController.swift
// APIClient
//
// Created by Christoph Blattgerste on 24.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import UIKit
import Freddy
import Moya
import AlamofireImage

class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

override func viewDidLoad() {
super.viewDidLoad()
self.sWPickerView.dataSource = self
self.sWPickerView.delegate = self
self.entrieDataSS = nil
self.entrieDataSS = nil
self.entrieDataSp = nil
pickerLabel.text = "Select a \(selectedEndpoint):"
numberSelection.delegate = self
}

var starWAPI : MoyaProvider<SWAPI>!

var entrieDataSS: Starships? {
didSet {
outputOne.text = entrieDataSS?.starshipClass
outputTwo.text = entrieDataSS?.costInCredits
outputZero.text = entrieDataSS?.name
labelOne.text = "Its class:"
labelTwo.text = "Its costs[credits]:"
labelZero.text = "Starships name:"
}
}
var entrieDataPl: Planets? {
didSet {
outputOne.text = entrieDataPl?.terrain
outputTwo.text = entrieDataPl?.population
outputZero.text = entrieDataPl?.name
labelOne.text = "Its terrain:"
labelTwo.text = "Its population:"
labelZero.text = "Planets name:"

}
}
var entrieDataSp: Species? {
didSet {
outputZero.text = entrieDataSp?.name
outputOne.text = entrieDataSp?.homeworld
outputTwo.text = entrieDataSp?.language
labelZero.text = "Species name:"
labelOne.text = "Its homeworld:"
labelTwo.text = "Its language:"
}
}


@IBOutlet weak var sWPickerView: UIPickerView!
@IBOutlet weak var pickerLabel: UILabel!
@IBOutlet weak var labelOne: UILabel!
@IBOutlet weak var labelTwo: UILabel!
@IBOutlet weak var labelZero: UILabel!
@IBOutlet weak var outputOne: UILabel!
@IBOutlet weak var outputTwo: UILabel!
@IBOutlet weak var outputZero: UILabel!
@IBOutlet weak var numberSelection: UITextField!
@IBOutlet weak var loadingIndicator: UIActivityIndicatorView!

@IBAction func actNumberSelection(sender: AnyObject) {
sender.resignFirstResponder()
switch selectedEndpoint {
case "Starship":
loadStarship(NamedResource(name: numberSelection.text ?? ""))
case "Planet":
loadPlanet(NamedResource(name: numberSelection.text ?? ""))
case "Species":
loadSpecies(NamedResource(name: numberSelection.text ?? ""))
default:
break
}
}

let pickerData = ["Starship","Planet","Species"]

private var selectedEndpoint = "Starship"




// Functions to initialize UIPickerView
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// pickerLabel.text = "Select a \(pickerData[row]):"
selectedEndpoint = pickerData[row]
pickerLabel.text = "Select a \(selectedEndpoint):"
}

// load selected Endpoint from API URL
// depending on PickerWheel selection
func loadStarship(starship: NamedResource<Starships>) {
loadingIndicator.startAnimating()
starWAPI.request(.starships(starship)) { result in
self.loadingIndicator.stopAnimating()
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 star_ship = try Starships(json: json)
// Configure view according to model
self.entrieDataSS = star_ship
} catch {
print(error)
}
case .Failure(let error):
print(error)
}
}
}
func loadPlanet(planet: NamedResource<Planets>) {
loadingIndicator.startAnimating()
starWAPI.request(.planets(planet)) { result in
self.loadingIndicator.stopAnimating()
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 pla_net = try Planets(json: json)
// Configure view according to model
self.entrieDataPl = pla_net
} catch {
print(error)
}
case .Failure(let error):
print(error)
}
}
}

func loadSpecies(specie: NamedResource<Species>) {
loadingIndicator.startAnimating()
starWAPI.request(.species(specie)) { result in
self.loadingIndicator.stopAnimating()
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 spe_cies = try Species(json: json)
// Configure view according to model
self.entrieDataSp = spe_cies
} catch {
print(error)
}
case .Failure(let error):
print(error)
}
}
}

}
53 changes: 53 additions & 0 deletions APIClient/Structs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Structs.swift
// APIClient
//
// Created by Christoph Blattgerste on 30.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation
import Freddy
import UIKit

struct Starships: JSONDecodable {

let name : String

let starshipClass : String

let costInCredits : String

init (json: JSON) throws {
self.name = try json.string("name")
self.costInCredits = try json.string("cost_in_credits")
self.starshipClass = try json.string("starship_class")
}
}

struct Planets: JSONDecodable {

let name : String
let population : String
let terrain : String

init(json: JSON) throws {
self.name = try json.string("name")
self.population = try json.string("population")
self.terrain = try json.string("terrain")
}
}

struct Species: JSONDecodable {

let name : String
let homeworld : String
let language : String

init(json: JSON) throws {
self.name = try json.string("name")
self.language = try json.string("language")
self.homeworld = try json.string("homeworld")
}

}
32 changes: 32 additions & 0 deletions APIClientTests/MyAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MyAPITests.swift
// APIClient
//
// Created by Christoph Blattgerste on 05.06.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation

import XCTest

@testable import APIClient

import Nimblek0

class MyAPITests : XCTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
super.tearDown()
}
//eigene Test Funktionen
func testFirstTry() {
expect(1+1) == 2
expect(
}

}