Create event with boatclass

This commit is contained in:
Raimund Wege
2017-08-25 14:13:52 +02:00
parent 23460cff1c
commit 40e68ffb45
5 changed files with 69 additions and 14 deletions
@@ -52,7 +52,7 @@ struct Images {
static let RedHighlighted = UIImage(color: Colors.RedHighlighted)
}
let BoatClasses = [
let BoatClassNames = [
"18Footer",
"2.4 Meter",
"12 Meter",
@@ -9,13 +9,17 @@
import UIKit
class CreateTrainingViewController: UIViewController {
weak var trainingController: TrainingController!
@IBOutlet weak var boatClassPickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
// Setup
// MARK: - Setup
fileprivate func setup() {
setupLocalization()
@@ -30,12 +34,17 @@ class CreateTrainingViewController: UIViewController {
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIImageView(image: UIImage(named: "sap_logo")))
}
// Actions
// MARK: - Actions
@IBAction func cancelButtonTapped(_ sender: Any) {
presentingViewController?.dismiss(animated: true)
}
@IBAction func createTrainingButtonTapped(_ sender: Any) {
let boatClassName = BoatClassNames[boatClassPickerView.selectedRow(inComponent: 0)]
trainingController.createEvent(boatClassName: boatClassName)
}
}
// MARK: - UIPickerViewDataSource
@@ -47,7 +56,7 @@ extension CreateTrainingViewController: UIPickerViewDataSource {
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return BoatClasses.count
return BoatClassNames.count
}
}
@@ -57,7 +66,7 @@ extension CreateTrainingViewController: UIPickerViewDataSource {
extension CreateTrainingViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return BoatClasses[row]
return BoatClassNames[row]
}
}
@@ -494,6 +494,7 @@
<userDefinedRuntimeAttribute type="boolean" keyPath="layer.masksToBounds" value="YES"/>
</userDefinedRuntimeAttributes>
<connections>
<action selector="createTrainingButtonTapped:" destination="RRc-Lh-2o4" eventType="touchUpInside" id="kIk-FP-clA"/>
<action selector="startTrackingButtonTapped:" destination="dMx-RA-QpG" eventType="touchUpInside" id="JOf-ml-Kyg"/>
<action selector="startTrackingButtonTapped:" destination="G3a-SV-Maf" eventType="touchUpInside" id="VMQ-cx-5yE"/>
</connections>
@@ -545,6 +546,9 @@
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="boatClassPickerView" destination="SQb-uc-DaV" id="wQ5-3s-ZkY"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="kAU-Am-SqM" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
@@ -765,7 +769,7 @@
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<segue destination="Pn3-0n-ZdQ" kind="presentation" id="Kqa-dt-WR3"/>
<segue destination="Pn3-0n-ZdQ" kind="presentation" identifier="CreateTraining" id="Kqa-dt-WR3"/>
</connections>
</button>
</subviews>
@@ -9,8 +9,7 @@
import UIKit
enum TrainingRequestManagerError: Error {
case checkInDataIsIncomplete
case communicationFailed
case invalidResponse
case postCreateEventFailed
}
@@ -59,15 +58,24 @@ class TrainingRequestManager: NSObject {
body["boatclassname"] = boatClassName as AnyObject
// body["numberofraces"] = "" as AnyObject
manager.post(urlString, parameters: body, success: { (requestOperation, responseObject) in
self.postCreateEventSuccess(responseObject: responseObject, success: success)
self.postCreateEventSuccess(responseObject: responseObject, success: success, failure: failure)
}) { (requestOperation, error) in
self.postCreateEventFailure(error: error, failure: failure)
}
}
fileprivate func postCreateEventSuccess(responseObject: Any, success: () -> Void) {
logInfo(name: "\(#function)", info: responseObjectToString(responseObject: responseObject))
success()
fileprivate func postCreateEventSuccess(responseObject: Any?, success: () -> Void, failure: (_ error: Error, _ message: String?) -> Void) {
if let data = responseObject as? Data {
do {
let jsonObject = try JSONSerialization.jsonObject(with: fixJSON(data: data))
print(jsonObject)
success()
} catch {
postCreateEventFailure(error: TrainingRequestManagerError.invalidResponse, failure: failure)
}
} else {
postCreateEventFailure(error: TrainingRequestManagerError.invalidResponse, failure: failure)
}
}
fileprivate func postCreateEventFailure(error: Error, failure: (_ error: Error, _ message: String?) -> Void) {
@@ -86,4 +94,22 @@ class TrainingRequestManager: NSObject {
return String(data: data as Data, encoding: String.Encoding.utf8)
}
func fixJSON(data: Data) -> Data {
guard let json = String(data: data, encoding: String.Encoding.utf8) else { return data }
guard let newData = fixJSON(string: json).data(using: String.Encoding.utf8) else { return data }
return newData
}
func fixJSON(string: String) -> String {
let pattern = "[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matches(in: string, options: [], range: NSRange(location: 0, length: string.characters.count))
var newString = string
matches.forEach { (match) in
let uuid = (string as NSString).substring(with: match.range)
newString = newString.replacingOccurrences(of: uuid, with: "\"\(uuid)\"")
}
return newString
}
}
@@ -12,6 +12,7 @@ class TrainingTableViewController: UIViewController {
fileprivate struct Segue {
static let About = "About"
static let CreateTraining = "CreateTraining"
static let Settings = "Settings"
}
@@ -73,6 +74,16 @@ class TrainingTableViewController: UIViewController {
signUpController.logoutWithViewController(self)
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == Segue.CreateTraining) {
let createTrainingNC = segue.destination as! UINavigationController
let createTrainingVC = createTrainingNC.viewControllers[0] as! CreateTrainingViewController
createTrainingVC.trainingController = trainingController
}
}
// MARK: - Properties
fileprivate lazy var signUpController: SignUpController = {
@@ -81,6 +92,11 @@ class TrainingTableViewController: UIViewController {
return signUpController
}()
fileprivate lazy var trainingController: TrainingController = {
let trainingController = TrainingController(baseURLString: "https://ubilabstest.sapsailing.com")
return trainingController
}()
}
// MARK: - UITableViewDataSource