From 25ff378e3c3d3ba6bd9769cf39fcd170428a46dc Mon Sep 17 00:00:00 2001 From: AlexGolovin Date: Thu, 13 Apr 2017 11:10:31 +0200 Subject: [PATCH] APOD Viewer v.1.0 --- APIClient.xcodeproj/project.pbxproj | 15 ++++++ APIClient/APOD.swift | 52 ++++++++++++++++++++ APIClient/APODResult.swift | 26 ++++++++++ APIClient/APODViewController.swift | 73 ++++++++++++++++++++++++++++ APIClient/Base.lproj/Main.storyboard | 61 +++++++++++++++++++---- 5 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 APIClient/APOD.swift create mode 100644 APIClient/APODResult.swift create mode 100644 APIClient/APODViewController.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index 3b30426..ba3ce03 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 */; }; + 14450E9C1E9E178C0045AB83 /* APODViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14450E9B1E9E178C0045AB83 /* APODViewController.swift */; }; + 14450E9E1E9E1EA80045AB83 /* APOD.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14450E9D1E9E1EA80045AB83 /* APOD.swift */; }; + 14450EA01E9E2BCB0045AB83 /* APODResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14450E9F1E9E2BCB0045AB83 /* APODResult.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 */ + 14450E9B1E9E178C0045AB83 /* APODViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APODViewController.swift; sourceTree = ""; }; + 14450E9D1E9E1EA80045AB83 /* APOD.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APOD.swift; sourceTree = ""; }; + 14450E9F1E9E2BCB0045AB83 /* APODResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APODResult.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 = ( + 14450E9B1E9E178C0045AB83 /* APODViewController.swift */, + 14450E9D1E9E1EA80045AB83 /* APOD.swift */, ); name = "View Controller"; sourceTree = ""; @@ -100,6 +108,7 @@ 872A27591CF0D64A00A988C4 /* Model */ = { isa = PBXGroup; children = ( + 14450E9F1E9E2BCB0045AB83 /* APODResult.swift */, ); name = Model; sourceTree = ""; @@ -265,6 +274,7 @@ TargetAttributes = { 874D06541CEF295E009A494D = { CreatedOnToolsVersion = 7.3.1; + DevelopmentTeam = C4DBNLN23D; LastSwiftMigration = 0830; }; 87EC714E1CFA380F00FBDDDE = { @@ -468,6 +478,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 14450E9C1E9E178C0045AB83 /* APODViewController.swift in Sources */, + 14450E9E1E9E1EA80045AB83 /* APOD.swift in Sources */, + 14450EA01E9E2BCB0045AB83 /* APODResult.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -611,6 +624,7 @@ baseConfigurationReference = 93EFAB13AE62F1B6FB2683B2 /* Pods-APIClient.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = C4DBNLN23D; INFOPLIST_FILE = APIClient/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClient"; @@ -624,6 +638,7 @@ baseConfigurationReference = FFE13D543A95D28F27CFBDA4 /* Pods-APIClient.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + DEVELOPMENT_TEAM = C4DBNLN23D; INFOPLIST_FILE = APIClient/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClient"; diff --git a/APIClient/APOD.swift b/APIClient/APOD.swift new file mode 100644 index 0000000..ca32fe6 --- /dev/null +++ b/APIClient/APOD.swift @@ -0,0 +1,52 @@ +// +// APOD.swift +// APIClient +// +// Created by Alex Golovin on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya + +enum APODapi: Moya.TargetType { + case apodImage(date: String) + + var baseURL: URL { + return URL(string: "https://api.nasa.gov/planetary")! + } + + var path: String { + switch self { + case .apodImage: + return "apod" + } + } + + var method: Moya.Method { + return .get + } + + var parameters: [String : Any]? { + switch self { + case .apodImage(date: let date): + return [ + "api_key": "ytjs7RGfT3lokh41NHQH914MFWra7PfwaVu2bqXx", + "date": date, + ] + + } + } + + var parameterEncoding: ParameterEncoding { + return URLEncoding.default + } + + var sampleData: Data { + return "".data(using: .utf8)! + } + + var task: Moya.Task { + return .request + } +} diff --git a/APIClient/APODResult.swift b/APIClient/APODResult.swift new file mode 100644 index 0000000..de4b90e --- /dev/null +++ b/APIClient/APODResult.swift @@ -0,0 +1,26 @@ +// +// APODResult.swift +// APIClient +// +// Created by Alex Golovin on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy + +struct APODResult: JSONDecodable { + + //let copyright: String + //let date: String + //let explanation: String + //let hdurl: URL + //let media_type: String + let title: String + let url: URL + + init(json: JSON) throws { + try self.url = URL(string:json.getString(at: "url"))! + try self.title = json.getString(at: "title") + } +} diff --git a/APIClient/APODViewController.swift b/APIClient/APODViewController.swift new file mode 100644 index 0000000..e287a27 --- /dev/null +++ b/APIClient/APODViewController.swift @@ -0,0 +1,73 @@ +// +// APODViewController.swift +// APIClient +// +// Created by Alex Golovin on 12.04.17. +// Copyright © 2017 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Moya +import Freddy +import AlamofireImage + +class APODViewController: UIViewController, UITextFieldDelegate { + + @IBOutlet weak var inputAPODQuerry: UITextField! + + @IBOutlet weak var datePickerField: UIDatePicker! + + @IBAction func dateConfirm(_ sender: Any) { + + + + let date = datePickerField.date + let formater = DateFormatter() + formater.dateFormat = "yyyy-MM-dd" + let dateString = formater.string(from: date) + + self.imageLoading.startAnimating() + + let provider = MoyaProvider() + + let query = APODapi.apodImage(date: dateString) + + provider.request(query) {result in + self.imageLoading.stopAnimating() + + switch result { + case .success(let response): + + //print(String(data: response.data)) response.data + do{ + + let jsonResponse = try JSON(data: response.data) + print(jsonResponse) + let apodResult = try APODResult(json: jsonResponse) + self.outputAPOD.af_setImage(withURL: apodResult.url) + } catch { + print(error) + } + + case .failure(let error): + print(error) + } + } + } + @IBOutlet weak var imageLoading: UIActivityIndicatorView! + + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + + guard let input = inputAPODQuerry.text else { + //guard let input = datePickerField.text else { + return true + } + + + inputAPODQuerry.resignFirstResponder() + return true + } + + @IBOutlet weak var outputAPOD: UIImageView! +} diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..a2e3f10 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,13 +1,18 @@ - - + + + + + - + + + @@ -17,28 +22,66 @@ - - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + - +