From 03e8078a97c40da6525c040c8cfd3d50c830c487 Mon Sep 17 00:00:00 2001 From: Frederik Date: Sun, 29 May 2016 17:22:56 +0200 Subject: [PATCH 1/2] =?UTF-8?q?"U=CC=88bersetzer"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APIClient.xcodeproj/project.pbxproj | 12 ++++ APIClient/AppDelegate.swift | 3 +- APIClient/Base.lproj/LaunchScreen.storyboard | 2 +- APIClient/Base.lproj/Main.storyboard | 34 +++++++++-- APIClient/GlosbeAPITarget.swift | 39 ++++++++++++ APIClient/GlosbeTranslationData.swift | 15 +++++ APIClient/Info.plist | 11 ++++ APIClient/InitialViewController.swift | 64 ++++++++++++++++++++ 8 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 APIClient/GlosbeAPITarget.swift create mode 100644 APIClient/GlosbeTranslationData.swift create mode 100644 APIClient/InitialViewController.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index d57524a..947634a 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 86AF3E5D1CFA5768005A7331 /* InitialViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */; }; + 86AF3E5F1CFA5899005A7331 /* GlosbeAPITarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.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 */; }; @@ -16,6 +18,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InitialViewController.swift; sourceTree = ""; }; + 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlosbeAPITarget.swift; sourceTree = ""; }; 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 = ""; }; @@ -43,6 +47,7 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -58,6 +63,7 @@ isa = PBXGroup; children = ( 872A275B1CF0D87100A988C4 /* APIResource.swift */, + 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.swift */, ); name = Model; sourceTree = ""; @@ -242,6 +248,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 86AF3E5D1CFA5768005A7331 /* InitialViewController.swift in Sources */, + 86AF3E5F1CFA5899005A7331 /* GlosbeAPITarget.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); @@ -359,6 +367,8 @@ baseConfigurationReference = 93EFAB13AE62F1B6FB2683B2 /* Pods-APIClient.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_WARNINGS = NO; + IBSC_WARNINGS = NO; INFOPLIST_FILE = APIClient/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClient"; @@ -371,6 +381,8 @@ baseConfigurationReference = FFE13D543A95D28F27CFBDA4 /* Pods-APIClient.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_WARNINGS = NO; + IBSC_WARNINGS = NO; 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/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..76d0b18 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,14 +7,15 @@ // import UIKit +import Moya @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + return true } diff --git a/APIClient/Base.lproj/LaunchScreen.storyboard b/APIClient/Base.lproj/LaunchScreen.storyboard index ebf48f6..f6e2022 100644 --- a/APIClient/Base.lproj/LaunchScreen.storyboard +++ b/APIClient/Base.lproj/LaunchScreen.storyboard @@ -1,5 +1,5 @@ - + diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..921d0c7 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -21,10 +21,10 @@ - + - + @@ -32,9 +32,35 @@ + + + + + + - + + + + + + + + + + + + + + + + diff --git a/APIClient/GlosbeAPITarget.swift b/APIClient/GlosbeAPITarget.swift new file mode 100644 index 0000000..af391c8 --- /dev/null +++ b/APIClient/GlosbeAPITarget.swift @@ -0,0 +1,39 @@ +// +// GlosbeAPITarget.swift +// APIClient +// +// Created by Frederik on 29/05/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya + +struct GlosbeAPITarget : TargetType +{ + var phrase: String + var from: String + var dest: String + + //NOTE(Frederik): Moya.TargetType protocol "implementation" + + var baseURL: NSURL { + return NSURL(string: "https://glosbe.com/gapi/translate?from=\(from)&dest=\(dest)&format=json&phrase=\(phrase)")! + } + + var path: String { + return "" + } + + var method: Moya.Method { + return .GET + } + + var parameters: [String : AnyObject]? { + return nil + } + + var sampleData: NSData { + return "".dataUsingEncoding(NSUTF8StringEncoding)! + } +} \ No newline at end of file diff --git a/APIClient/GlosbeTranslationData.swift b/APIClient/GlosbeTranslationData.swift new file mode 100644 index 0000000..49fde7b --- /dev/null +++ b/APIClient/GlosbeTranslationData.swift @@ -0,0 +1,15 @@ +// +// GlosbeTranslationData.swift +// APIClient +// +// Created by Frederik on 29/05/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation + +struct GlosbeTranslationData +{ + var fromPhrase : String + var destPhrase : String +} \ No newline at end of file diff --git a/APIClient/Info.plist b/APIClient/Info.plist index 40c6215..75b4c5d 100644 --- a/APIClient/Info.plist +++ b/APIClient/Info.plist @@ -26,6 +26,17 @@ LaunchScreen UIMainStoryboardFile Main + NSAppTransportSecurity + + NSExceptionDomains + + glosbe.com + + NSExceptionAllowsInsecureHTTPLoads + + + + UIRequiredDeviceCapabilities armv7 diff --git a/APIClient/InitialViewController.swift b/APIClient/InitialViewController.swift new file mode 100644 index 0000000..275b743 --- /dev/null +++ b/APIClient/InitialViewController.swift @@ -0,0 +1,64 @@ +// +// InitialViewController.swift +// APIClient +// +// Created by Frederik on 29/05/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + +class InitialViewController : UIViewController +{ + var glosbeAPI = MoyaProvider() + + func handleTranslationRequestResponseData(data: NSData) throws + { + let j = try JSON(data: data) + let a = try j.array("tuc") + if a.count != 0 { + let A = try a[0].dictionary("phrase") + let J = A["text"] + destContent.text = J?.description + } else { + destContent.text = "(• ε •)" + } + } + + func invokeTranslationRequest(phrase: String, from: String, dest: String) + { + let target = GlosbeAPITarget(phrase: phrase, from: from, dest: dest) + + glosbeAPI.request(target) { result in + switch result { + case .Success(let response): + do { + try response.filterSuccessfulStatusCodes() + try self.handleTranslationRequestResponseData(response.data) + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + + //NOTE(Frederik): Controller implementation + + @IBOutlet weak var destContent: UILabel! + @IBOutlet weak var textInputField: UITextField! + + //NODE(Frederik): Text field input CB + + func textFieldShouldReturn(textField: UITextField) -> Bool { + guard let name = textField.text else { + return true + } + self.invokeTranslationRequest(name, from: "eng", dest: "deu") + return true + } + +} \ No newline at end of file From cc4b527af1d44842652f45d39eeb8eb673c9861b Mon Sep 17 00:00:00 2001 From: Frederik Date: Wed, 22 Jun 2016 23:17:42 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Zeigt=20jetzt=20alles=20an=20was=20von=20Gl?= =?UTF-8?q?osbe.com=20zuru=CC=88ck=20kommt=20und=20la=CC=88sst=20einen=20d?= =?UTF-8?q?ie=20Sprache=20auswa=CC=88hlen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APIClient.xcodeproj/project.pbxproj | 40 ++++- APIClient/Base.lproj/Main.storyboard | 169 ++++++++++++++++-- APIClient/GlosbeAPITarget.swift | 3 +- APIClient/GlosbeTranslationData.swift | 15 -- APIClient/InitialViewController.swift | 64 ------- APIClient/Language.swift | 29 +++ APIClient/LanguageSelectViewController.swift | 31 ++++ APIClient/TopAlignedLabel.swift | 30 ++++ APIClient/TranslationResponse.swift | 38 ++++ APIClient/TranslationResultCell.swift | 17 ++ .../TranslationResultViewController.swift | 51 ++++++ APIClient/TranslationViewController.swift | 155 ++++++++++++++++ APIClient/Utility.swift | 26 +++ 13 files changed, 561 insertions(+), 107 deletions(-) delete mode 100644 APIClient/GlosbeTranslationData.swift delete mode 100644 APIClient/InitialViewController.swift create mode 100644 APIClient/Language.swift create mode 100644 APIClient/LanguageSelectViewController.swift create mode 100644 APIClient/TopAlignedLabel.swift create mode 100644 APIClient/TranslationResponse.swift create mode 100644 APIClient/TranslationResultCell.swift create mode 100644 APIClient/TranslationResultViewController.swift create mode 100644 APIClient/TranslationViewController.swift create mode 100644 APIClient/Utility.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index 947634a..0fe9414 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -7,9 +7,15 @@ objects = { /* Begin PBXBuildFile section */ - 86AF3E5D1CFA5768005A7331 /* InitialViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */; }; + 86AF3E5D1CFA5768005A7331 /* TranslationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E5C1CFA5768005A7331 /* TranslationViewController.swift */; }; 86AF3E5F1CFA5899005A7331 /* GlosbeAPITarget.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.swift */; }; - 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A275B1CF0D87100A988C4 /* APIResource.swift */; }; + 86C637EE1D1ACF8700A642EA /* LanguageSelectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637ED1D1ACF8700A642EA /* LanguageSelectViewController.swift */; }; + 86C637F01D1AD1EB00A642EA /* Language.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637EF1D1AD1EB00A642EA /* Language.swift */; }; + 86C637F21D1AF42900A642EA /* TranslationResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637F11D1AF42900A642EA /* TranslationResponse.swift */; }; + 86C637F41D1B052600A642EA /* TopAlignedLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637F31D1B052600A642EA /* TopAlignedLabel.swift */; }; + 86C637F81D1B10FD00A642EA /* TranslationResultCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637F71D1B10FD00A642EA /* TranslationResultCell.swift */; }; + 86C637FD1D1B155100A642EA /* TranslationResultViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C637FC1D1B155100A642EA /* TranslationResultViewController.swift */; }; + 86C638031D1B328E00A642EA /* Utility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86C638021D1B328E00A642EA /* Utility.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 */; }; @@ -18,9 +24,15 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InitialViewController.swift; sourceTree = ""; }; + 86AF3E5C1CFA5768005A7331 /* TranslationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationViewController.swift; sourceTree = ""; }; 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlosbeAPITarget.swift; sourceTree = ""; }; - 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; + 86C637ED1D1ACF8700A642EA /* LanguageSelectViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LanguageSelectViewController.swift; sourceTree = ""; }; + 86C637EF1D1AD1EB00A642EA /* Language.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Language.swift; sourceTree = ""; }; + 86C637F11D1AF42900A642EA /* TranslationResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationResponse.swift; sourceTree = ""; }; + 86C637F31D1B052600A642EA /* TopAlignedLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TopAlignedLabel.swift; sourceTree = ""; }; + 86C637F71D1B10FD00A642EA /* TranslationResultCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationResultCell.swift; sourceTree = ""; }; + 86C637FC1D1B155100A642EA /* TranslationResultViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationResultViewController.swift; sourceTree = ""; }; + 86C638021D1B328E00A642EA /* Utility.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utility.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 = ""; }; @@ -47,7 +59,9 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( - 86AF3E5C1CFA5768005A7331 /* InitialViewController.swift */, + 86AF3E5C1CFA5768005A7331 /* TranslationViewController.swift */, + 86C637ED1D1ACF8700A642EA /* LanguageSelectViewController.swift */, + 86C637FC1D1B155100A642EA /* TranslationResultViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -55,6 +69,8 @@ 872A27581CF0D4EB00A988C4 /* View */ = { isa = PBXGroup; children = ( + 86C637F31D1B052600A642EA /* TopAlignedLabel.swift */, + 86C637F71D1B10FD00A642EA /* TranslationResultCell.swift */, ); name = View; sourceTree = ""; @@ -62,8 +78,10 @@ 872A27591CF0D64A00A988C4 /* Model */ = { isa = PBXGroup; children = ( - 872A275B1CF0D87100A988C4 /* APIResource.swift */, 86AF3E5E1CFA5899005A7331 /* GlosbeAPITarget.swift */, + 86C637EF1D1AD1EB00A642EA /* Language.swift */, + 86C637F11D1AF42900A642EA /* TranslationResponse.swift */, + 86C638021D1B328E00A642EA /* Utility.swift */, ); name = Model; sourceTree = ""; @@ -248,9 +266,15 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 86AF3E5D1CFA5768005A7331 /* InitialViewController.swift in Sources */, + 86C638031D1B328E00A642EA /* Utility.swift in Sources */, + 86C637F41D1B052600A642EA /* TopAlignedLabel.swift in Sources */, + 86C637EE1D1ACF8700A642EA /* LanguageSelectViewController.swift in Sources */, + 86C637F01D1AD1EB00A642EA /* Language.swift in Sources */, + 86C637F21D1AF42900A642EA /* TranslationResponse.swift in Sources */, + 86C637F81D1B10FD00A642EA /* TranslationResultCell.swift in Sources */, + 86AF3E5D1CFA5768005A7331 /* TranslationViewController.swift in Sources */, 86AF3E5F1CFA5899005A7331 /* GlosbeAPITarget.swift in Sources */, - 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + 86C637FD1D1B155100A642EA /* TranslationResultViewController.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 921d0c7..2df95bf 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -3,12 +3,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -21,50 +79,125 @@ - + - + - + - + + + + - - - - - + + - + - - + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/APIClient/GlosbeAPITarget.swift b/APIClient/GlosbeAPITarget.swift index af391c8..fa0fbe8 100644 --- a/APIClient/GlosbeAPITarget.swift +++ b/APIClient/GlosbeAPITarget.swift @@ -15,8 +15,7 @@ struct GlosbeAPITarget : TargetType var from: String var dest: String - //NOTE(Frederik): Moya.TargetType protocol "implementation" - + var baseURL: NSURL { return NSURL(string: "https://glosbe.com/gapi/translate?from=\(from)&dest=\(dest)&format=json&phrase=\(phrase)")! } diff --git a/APIClient/GlosbeTranslationData.swift b/APIClient/GlosbeTranslationData.swift deleted file mode 100644 index 49fde7b..0000000 --- a/APIClient/GlosbeTranslationData.swift +++ /dev/null @@ -1,15 +0,0 @@ -// -// GlosbeTranslationData.swift -// APIClient -// -// Created by Frederik on 29/05/16. -// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. -// - -import Foundation - -struct GlosbeTranslationData -{ - var fromPhrase : String - var destPhrase : String -} \ No newline at end of file diff --git a/APIClient/InitialViewController.swift b/APIClient/InitialViewController.swift deleted file mode 100644 index 275b743..0000000 --- a/APIClient/InitialViewController.swift +++ /dev/null @@ -1,64 +0,0 @@ -// -// InitialViewController.swift -// APIClient -// -// Created by Frederik on 29/05/16. -// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. -// - -import Foundation -import Moya -import Freddy - -class InitialViewController : UIViewController -{ - var glosbeAPI = MoyaProvider() - - func handleTranslationRequestResponseData(data: NSData) throws - { - let j = try JSON(data: data) - let a = try j.array("tuc") - if a.count != 0 { - let A = try a[0].dictionary("phrase") - let J = A["text"] - destContent.text = J?.description - } else { - destContent.text = "(• ε •)" - } - } - - func invokeTranslationRequest(phrase: String, from: String, dest: String) - { - let target = GlosbeAPITarget(phrase: phrase, from: from, dest: dest) - - glosbeAPI.request(target) { result in - switch result { - case .Success(let response): - do { - try response.filterSuccessfulStatusCodes() - try self.handleTranslationRequestResponseData(response.data) - } catch { - print(error) - } - case .Failure(let error): - print(error) - } - } - } - - //NOTE(Frederik): Controller implementation - - @IBOutlet weak var destContent: UILabel! - @IBOutlet weak var textInputField: UITextField! - - //NODE(Frederik): Text field input CB - - func textFieldShouldReturn(textField: UITextField) -> Bool { - guard let name = textField.text else { - return true - } - self.invokeTranslationRequest(name, from: "eng", dest: "deu") - return true - } - -} \ No newline at end of file diff --git a/APIClient/Language.swift b/APIClient/Language.swift new file mode 100644 index 0000000..cca0152 --- /dev/null +++ b/APIClient/Language.swift @@ -0,0 +1,29 @@ +// +// Language.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +struct Language : Equatable +{ + let name : String + let identifier : String + + static let allLanguages : Array = + [ + Language(name: "Deutsch", identifier: "deu"), + Language(name: "Englisch", identifier: "eng"), + Language(name: "Französisch", identifier: "fra"), + Language(name: "Griechisch", identifier: "ell"), + Language(name: "Japanisch", identifier: "jpn"), + Language(name: "Spanisch", identifier: "spa"), + Language(name: "Russisch", identifier: "rus"), + Language(name: "Polnisch", identifier: "pol") + ] +} + +func ==(lhs: Language, rhs: Language) -> Bool { + return lhs.identifier == rhs.identifier +} \ No newline at end of file diff --git a/APIClient/LanguageSelectViewController.swift b/APIClient/LanguageSelectViewController.swift new file mode 100644 index 0000000..497e47a --- /dev/null +++ b/APIClient/LanguageSelectViewController.swift @@ -0,0 +1,31 @@ +// +// LanguageSelectViewController.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit + +class LanguageSelectViewController : UITableViewController +{ + var selectedLanguage: Language? + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + guard let indexPath = tableView.indexPathForSelectedRow else { + return + } + self.selectedLanguage = Language.allLanguages[indexPath.item] + } + + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return Language.allLanguages.count + } + + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCellWithIdentifier("LanguageCell", forIndexPath: indexPath) + cell.textLabel?.text = Language.allLanguages[indexPath.item].name + return cell + } +} \ No newline at end of file diff --git a/APIClient/TopAlignedLabel.swift b/APIClient/TopAlignedLabel.swift new file mode 100644 index 0000000..453daa4 --- /dev/null +++ b/APIClient/TopAlignedLabel.swift @@ -0,0 +1,30 @@ +// +// TopAllignedLabel.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit + +@IBDesignable class TopAlignedLabel: UILabel { + override func drawTextInRect(rect: CGRect) { + if let stringText = text { + let stringTextAsNSString = stringText as NSString + let labelStringSize = stringTextAsNSString.boundingRectWithSize(CGSizeMake(CGRectGetWidth(self.frame), CGFloat.max), + options: NSStringDrawingOptions.UsesLineFragmentOrigin, + attributes: [NSFontAttributeName: font], + context: nil).size + super.drawTextInRect(CGRectMake(0, 0, CGRectGetWidth(self.frame), ceil(labelStringSize.height))) + } else { + super.drawTextInRect(rect) + } + } + override func prepareForInterfaceBuilder() { + super.prepareForInterfaceBuilder() + layer.borderWidth = 1 + layer.borderColor = UIColor.blackColor().CGColor + } +} \ No newline at end of file diff --git a/APIClient/TranslationResponse.swift b/APIClient/TranslationResponse.swift new file mode 100644 index 0000000..2cac9f8 --- /dev/null +++ b/APIClient/TranslationResponse.swift @@ -0,0 +1,38 @@ +// +// TranslationResult.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Freddy + +struct TranslationResponse +{ + var results : [TranslationResult] = [] + + init(json: JSON) throws { + let array = try json.array("tuc"); + for translation in array { + do { + let phrase = try translation.dictionary("phrase")["text"]?.description; + let meanings = try translation.array("meanings"); + + var result = TranslationResult(phrase: phrase!, meanings: []); + for meaning in meanings { + let entry = try meaning.string("text"); + result.meanings.append(entry); + } + self.results.append(result); + } + catch { } + } + } +} + +struct TranslationResult +{ + var phrase : String + var meanings : [String] +} \ No newline at end of file diff --git a/APIClient/TranslationResultCell.swift b/APIClient/TranslationResultCell.swift new file mode 100644 index 0000000..54e9d5d --- /dev/null +++ b/APIClient/TranslationResultCell.swift @@ -0,0 +1,17 @@ +// +// TranslationResultCell.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit + +class TranslationResultCell: UITableViewCell +{ + @IBOutlet weak var Meaning: UILabel! + @IBOutlet weak var Phrase: UILabel! +} + diff --git a/APIClient/TranslationResultViewController.swift b/APIClient/TranslationResultViewController.swift new file mode 100644 index 0000000..7007e8d --- /dev/null +++ b/APIClient/TranslationResultViewController.swift @@ -0,0 +1,51 @@ +// +// TranslationResultViewController.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit + +class TranslationResultViewController : UITableViewController +{ + var response : TranslationResponse? + + override func viewDidLoad() { + super.viewDidLoad() + + tableView.estimatedRowHeight = 10 + tableView.rowHeight = UITableViewAutomaticDimension + self.tableView.separatorColor = UIColor.clearColor() + } + + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + guard let count = response?.results.count else { + return 0 + } + return count + } + + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCellWithIdentifier("Translation", forIndexPath: indexPath) as! TranslationResultCell + guard let result = response?.results[indexPath.row] else { + return cell + } + cell.Phrase.text = String(htmlEncodedString: result.phrase) + + var meaningContent = "" + for meaning in result.meanings { + meaningContent += String(htmlEncodedString: meaning) + "\n" + } + if meaningContent != "" { + cell.Meaning.hidden = false + cell.Meaning.text = meaningContent + } + else { + cell.Meaning.hidden = true + } + + return cell + } +} \ No newline at end of file diff --git a/APIClient/TranslationViewController.swift b/APIClient/TranslationViewController.swift new file mode 100644 index 0000000..085461c --- /dev/null +++ b/APIClient/TranslationViewController.swift @@ -0,0 +1,155 @@ +// +// InitialViewController.swift +// APIClient +// +// Created by Frederik on 29/05/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + +class TranslationViewController : UIViewController +{ + var glosbeAPI = MoyaProvider() + + var destLang : Language = Language.allLanguages[1] + var fromLang : Language = Language.allLanguages[0] + + var changedLangIdentifier = "" + var translationResult : TranslationResponse? + + var translationResultController : TranslationResultViewController? + + var DestLang : Language { + get { + return destLang + } + set (value) { + destLang = value + SelectDestLanguage.setTitle(value.name, forState: UIControlState.Normal) + } + } + var FromLang : Language { + get { + return fromLang + } + set (value) { + fromLang = value + SelectFromLanguage.setTitle(value.name, forState: UIControlState.Normal) + } + } + + + func handleTranslationRequestResponseData(data: NSData) throws + { + let json = try JSON(data: data) + let result = try TranslationResponse(json: json) + + translationResult = result + TranslationContainer.hidden = false + translationResultController?.response = result + translationResultController?.tableView.reloadData() + } + + func invokeTranslationRequest(phrase: String, from: String, dest: String) + { + let target = GlosbeAPITarget(phrase: phrase, from: from, dest: dest) + + glosbeAPI.request(target) { result in + switch result { + case .Success(let response): + do { + try response.filterSuccessfulStatusCodes() + try self.handleTranslationRequestResponseData(response.data) + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + + //NOTE(Frederik): View Controller Zeugs + + @IBOutlet weak var SelectFromLanguage: UIButton! + @IBOutlet weak var SelectDestLanguage: UIButton! + @IBOutlet weak var SwitchLanguage: UIButton! + @IBOutlet weak var TranslationContainer: UIView! + @IBOutlet weak var TextInputField: UITextField! + + @IBOutlet weak var OutputLabel: UILabel! + + @IBAction func SwitchPressed(_: UIButton) { + let temp = FromLang + FromLang = DestLang + DestLang = temp + } + + override func viewDidLoad() { + super.viewDidLoad() + + DestLang = destLang + FromLang = fromLang + } + + @IBAction override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){ + switch segue.identifier! { + case "FromLanguage": + changedLangIdentifier = "From" + break; + case "DestLanguage": + changedLangIdentifier = "Dest" + break; + case "Translation": + translationResultController = segue.destinationViewController as? TranslationResultViewController + break; + default: break; + } + } + + @IBAction func unwindToTranslation(segue: UIStoryboardSegue){ + switch segue.identifier! { + case "SelectedLanguage": + guard let languageSelectViewController = segue.sourceViewController as? LanguageSelectViewController, + selectedLanguage = languageSelectViewController.selectedLanguage else { + return + } + switch changedLangIdentifier { + case "From": + FromLang = selectedLanguage + if TextInputField.text != "" { + self.invokeTranslationRequest(TextInputField.text!, from: fromLang.identifier, dest: destLang.identifier) + } + break; + case "Dest": + DestLang = selectedLanguage + if TextInputField.text != "" { + self.invokeTranslationRequest(TextInputField.text!, from: fromLang.identifier, dest: destLang.identifier) + } + break; + default: + break; + } + break + default: + break + } + } + + //NOTE(Frederik): Text input field callback + + func textFieldShouldReturn(textField: UITextField) -> Bool { + let name = textField.text! + + self.invokeTranslationRequest(name, from: fromLang.identifier, dest: destLang.identifier) + return true + } +} + + + + + diff --git a/APIClient/Utility.swift b/APIClient/Utility.swift new file mode 100644 index 0000000..12bdde0 --- /dev/null +++ b/APIClient/Utility.swift @@ -0,0 +1,26 @@ +// +// Utility.swift +// APIClient +// +// Created by Frederik on 22/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit + +extension String { + init(htmlEncodedString: String) { + let encodedData = htmlEncodedString.dataUsingEncoding(NSUTF8StringEncoding)! + let attributedOptions : [String: AnyObject] = [ + NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, + NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding + ] + do { + let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil) + self.init(attributedString.string) + } catch { + self.init("") + } + } +}