diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..7298b4d 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,12 +8,15 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; - 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 */; }; 874D06601CEF295E009A494D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 874D065F1CEF295E009A494D /* Assets.xcassets */; }; 874D06631CEF295E009A494D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D06611CEF295E009A494D /* LaunchScreen.storyboard */; }; A526FDEE5BF2D1BC3C97B9FA /* Pods_APIClient.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */; }; + E905C2CD1D6B8D3200DE0FBC /* countryAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */; }; + E905C2CF1D6B9D9A00DE0FBC /* country.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2CE1D6B9D9A00DE0FBC /* country.swift */; }; + E905C2D11D6CEDD600DE0FBC /* CapitalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */; }; + E905C2D31D770DF300DE0FBC /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2D21D770DF300DE0FBC /* APIResource.swift */; }; E9294E7F05F2671186BD1359 /* Pods_APIClientTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4FFA0A7AA37697BD5CBC3211 /* Pods_APIClientTests.framework */; }; /* End PBXBuildFile section */ @@ -39,7 +42,6 @@ 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 = ""; }; 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; 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 = ""; }; 874D065D1CEF295E009A494D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -53,6 +55,10 @@ 93EFAB13AE62F1B6FB2683B2 /* Pods-APIClient.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClient.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClient/Pods-APIClient.debug.xcconfig"; sourceTree = ""; }; BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClient.framework; sourceTree = BUILT_PRODUCTS_DIR; }; C67B3B3E7990BE278400974F /* Pods-APIClientUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientUITests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientUITests/Pods-APIClientUITests.debug.xcconfig"; sourceTree = ""; }; + E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = countryAPI.swift; sourceTree = ""; }; + E905C2CE1D6B9D9A00DE0FBC /* country.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = country.swift; sourceTree = ""; }; + E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapitalViewController.swift; sourceTree = ""; }; + E905C2D21D770DF300DE0FBC /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; F8B9811B1068B05E73195990 /* Pods-APIClientTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientTests/Pods-APIClientTests.debug.xcconfig"; sourceTree = ""; }; FFE13D543A95D28F27CFBDA4 /* Pods-APIClient.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClient.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClient/Pods-APIClient.release.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -88,6 +94,7 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -102,7 +109,9 @@ 872A27591CF0D64A00A988C4 /* Model */ = { isa = PBXGroup; children = ( - 872A275B1CF0D87100A988C4 /* APIResource.swift */, + E905C2D21D770DF300DE0FBC /* APIResource.swift */, + E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */, + E905C2CE1D6B9D9A00DE0FBC /* country.swift */, ); name = Model; sourceTree = ""; @@ -468,7 +477,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + E905C2CF1D6B9D9A00DE0FBC /* country.swift in Sources */, + E905C2CD1D6B8D3200DE0FBC /* countryAPI.swift in Sources */, + E905C2D11D6CEDD600DE0FBC /* CapitalViewController.swift in Sources */, + E905C2D31D770DF300DE0FBC /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..f777145 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,14 +7,20 @@ // import UIKit +import Moya @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let provider = MoyaProvider() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + if let countryViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? CountryViewController { + countryViewController.provider = provider + } return true } diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..9d4ac55 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - - + + @@ -19,26 +19,116 @@ - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/APIClient/CapitalViewController.swift b/APIClient/CapitalViewController.swift new file mode 100644 index 0000000..b6c43d9 --- /dev/null +++ b/APIClient/CapitalViewController.swift @@ -0,0 +1,106 @@ +// +// testViewController.swift +// APIClient +// +// Created by Lucas Moeller on 23.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Freddy +import Moya +import Alamofire + +class CountryViewController: UIViewController { + + var provider: MoyaProvider! + + var countryToDisplay: Country? + + @IBOutlet var countryLabel: UILabel! + @IBOutlet var CapitalLabel: UILabel! + @IBOutlet var populationLabel: UILabel! + + @IBOutlet var showCountryLabel: UILabel! + @IBOutlet var showCapitalLabel: UILabel! + @IBOutlet var showPopulationLabel: UILabel! + + @IBOutlet var searchField: UITextField! + + @IBAction func searchButtonPressed(sender: AnyObject) { + if let countryToFind = searchField.text { + findCountry(countryToFind) + } else { + searchField.text = "please enter a country" + } + } + /* + func getCapitalOf(Country: String) { + provider.request(.fullName(name: Country)) {result in + switch result { + case let .Success(moyaResponse): + let data = moyaResponse.data + var json:AnyObject? + + do + { + json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) + print(json) + } catch { + print(error) + } + + guard let jArray = json as? [NSMutableDictionary] + else { + print("format error") + break + } + + guard let capital = jArray[0]["capital"] as? String else { + print("capital not a String") + break + } + self.capitalLabel.text = capital + + case let .Failure(error): + print(error) + } + } + } + */ + + func findCountry(CountryToFind: String) { + provider.request(.fullName(name: CountryToFind)) {result in + switch result { + case let .Success(moyaResponse): + let data = moyaResponse.data + do { + let json = try JSON(data: data) + let someCountry = try json.array().map(Country.init)[0] + self.countryToDisplay = someCountry + self.displayCountry(self.countryToDisplay) + } + catch { + print(error) + } + + case let .Failure(error): + print(error) + } + } + } + + func displayCountry(countryToDisplay: Country?) { + if countryToDisplay != nil { + showCountryLabel.text = countryToDisplay!.name + showCapitalLabel.text = countryToDisplay!.capital + showPopulationLabel.text = String(countryToDisplay!.population) + } else { + searchField.text = "no country to display" + } + } + + +} + diff --git a/APIClient/country.swift b/APIClient/country.swift new file mode 100644 index 0000000..793e6e0 --- /dev/null +++ b/APIClient/country.swift @@ -0,0 +1,28 @@ +// +// country.swift +// APIClient +// +// Created by Lucas Moeller on 22.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy +import UIKit + +public struct Country { + + let name: String + let languages: [JSON] + let population: Int + let capital: String +} + +extension Country: JSONDecodable { + public init(json: JSON) throws { + name = try json.string("name") + languages = try json.array("languages") + population = try json.int("population") + capital = try json.string("capital") + } +} \ No newline at end of file diff --git a/APIClient/countryAPI.swift b/APIClient/countryAPI.swift new file mode 100644 index 0000000..ebb10ff --- /dev/null +++ b/APIClient/countryAPI.swift @@ -0,0 +1,44 @@ +// +// countryAPI.swift +// APIClient +// +// Created by Lucas Moeller on 22.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + +enum countryAPI: Moya.TargetType, Cacheable { + + case fullName(name: String) + + var baseURL: NSURL {return NSURL(string: "https://restcountries.eu/rest/v1")!} + + var path: String { + switch self { + case .fullName(let countryName): return "/name/\(countryName)" //fullText=true + } + } + + var method: Moya.Method { + return .GET + } + + var parameters: [String : AnyObject]? {return nil} + + var sampleData: NSData { + switch self { + case .fullName(let countryName): return "[{\"name\": \(countryName),\"capital\": \"Paris\",\"altSpellings\": [\"FR\",\"French Republic\",\"République française\"],\"relevance\": \"2.5\",\"region\": \"Europe\",\"subregion\": \"Western Europe\",\"translations\": {\"de\": \"Frankreich\",\"es\": \"Francia\",\"fr\": \"France\",\"ja\": \"フランス\",\"it\": \"Francia\"},\"population\": 66186000,\"latlng\": [46,2],\"demonym\": \"French\",\"area\": 640679,\"gini\": 32.7,\"timezones\": [\"UTC−10:00\",\"UTC−09:30\",\"UTC−09:00\",\"UTC−08:00\",\"UTC−04:00\",\"UTC−03:00\",\"UTC+01:00\",\"UTC+03:00\",\"UTC+04:00\",\"UTC+05:00\",\"UTC+11:00\",\"UTC+12:00\"],\"borders\": [\"AND\",\"BEL\",\"DEU\",\"ITA\",\"LUX\",\"MCO\",\"ESP\",\"CHE\"],\"nativeName\":\"France\",\"callingCodes\": [\"33\"],\"topLevelDomain\": [\".fr\"],\"alpha2Code\": \"FR\",\"alpha3Code\":\"FRA\",\"currencies\": [\"EUR\"],\"languages\": [\"fr\"]}]".dataUsingEncoding(NSUTF8StringEncoding)! + default: return "".dataUsingEncoding(NSUTF8StringEncoding)! + } + } + + var cacheIdentifier: String { + return self.path + } +} + + + diff --git a/tryingOutMoya.playground/Contents.swift b/tryingOutMoya.playground/Contents.swift new file mode 100644 index 0000000..0ee12f5 --- /dev/null +++ b/tryingOutMoya.playground/Contents.swift @@ -0,0 +1,4 @@ +//: Playground - noun: a place where people can play + +import UIKit +import Moya diff --git a/tryingOutMoya.playground/contents.xcplayground b/tryingOutMoya.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/tryingOutMoya.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata b/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + +