-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewTypeViewController.swift
More file actions
115 lines (84 loc) · 3.96 KB
/
NewTypeViewController.swift
File metadata and controls
115 lines (84 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// NewTypeViewController.swift
// LiftApp
//
// Created by Austin Bailey on 10/15/17.
// Copyright © 2017 Austin Bailey. All rights reserved.
//
import UIKit
import CoreData
class NewTypeViewController: UIViewController {
@IBOutlet weak var newTypeInput: UITextField!
@IBOutlet weak var saveActivity: UIActivityIndicatorView!
var user: Int32?
var responseString: String?
@IBOutlet weak var newTypeInputWidth: NSLayoutConstraint!
@IBOutlet weak var newTypePromptCenter: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
let screenSize: CGRect = UIScreen.main.bounds
newTypeInputWidth.constant = screenSize.width * 0.90
newTypePromptCenter.constant = -(newTypeInputWidth.constant / 2.0) + 100
newTypeInput.addBorder(side: .bottom, thickness: 0.7, color: UIColor.lightGray)
saveActivity.hidesWhenStopped = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func addType(_ sender: UIButton) {
user = TabController.currentUser
saveActivity.startAnimating()
let name = self.newTypeInput.text!
DispatchQueue.global().async() {
// Do heavy work here
if name == "" {
self.createAlert(title: "Invalid Input", message: "Please make sure that weight and reps boxes contain numbers, and type is not blank.")
return
}
var notFinished = false
let url = URL(string: "https://www.austinmbailey.com/projects/liftappsite/api/insertLifttype.php")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let postString = "id=" + String(self.user!) + "&name=" + name.replacingOccurrences(of: " ", with: "_")
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response")
}
self.responseString = String(data: data, encoding: .utf8)
print(self.responseString!)
notFinished = true
}
task.resume()
while !notFinished {
}
DispatchQueue.main.sync {
self.saveActivity.stopAnimating()
self.newTypeInput.text! = ""
self.newTypeInput.resignFirstResponder()
}
}
}
func createAlert (title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
//close keyboard when return is hit
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}