Guest User

Untitled

a guest
Jan 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.  
  3. @IBOutlet weak var headerView: UIView!
  4. @IBOutlet weak var textField: UITextField!
  5. @IBOutlet weak var mainView: UIView!
  6. @IBOutlet weak var buttonsView: ButtonsViews!
  7. @IBOutlet weak var topLabel: UILabel!
  8. @IBOutlet weak var mainText: UILabel!
  9. @IBOutlet weak var resultsView: UIView!
  10. @IBOutlet weak var tryButton: UIButton!
  11. @IBOutlet weak var itentLabel: UILabel!
  12. @IBOutlet weak var dateLabel: UILabel!
  13. @IBOutlet weak var conditionLabel: UILabel!
  14. @IBOutlet weak var cityLabel: UILabel!
  15. @IBOutlet weak var countryLabel: UILabel!
  16. @IBOutlet weak var streetLabel: UILabel!
  17. @IBOutlet weak var speechLabel: UILabel!
  18. @IBOutlet weak var outfitLabel: UILabel!
  19. @IBOutlet weak var scoreLabel: UILabel!
  20.  
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. setInitalState()
  24. }
  25.  
  26. override func didReceiveMemoryWarning() {
  27. super.didReceiveMemoryWarning()
  28. // Dispose of any resources that can be recreated.
  29. }
  30.  
  31. // MARK : Create a new A.I. request by text
  32. func newRequest(_ text: String) {
  33.  
  34. //configure AI Request
  35. let aiRequest = AIRequest(query: text, lang: "en")
  36. let aiService = AIService(aiRequest)
  37.  
  38. //Promise block
  39. firstly{
  40. removePreviusSearch(text)
  41. }.then {(finished) -> Promise<AI> in
  42. aiService.getAi()
  43. }.then {(ai) -> Void in
  44. self.updateResults(ai)
  45. }.catch { (error) in
  46. //catch error
  47. }
  48.  
  49. }
  50.  
  51. // MARK : Remove previus search
  52. func removePreviusSearch(_ newText: String) -> Promise<Bool> {
  53. return Promise { fulfill, reject in
  54. UIView.animate(withDuration: 0.5, animations:{
  55. self.topLabel.alpha = 0
  56. self.mainText.alpha = 0
  57. self.resultsView.alpha = 0
  58. self.textField.text = ""
  59. }, completion: { (finished: Bool) in
  60. UIView.animate(withDuration: 0.5) {
  61. self.topLabel.alpha = 1
  62. self.mainText.alpha = 1
  63. }
  64. fulfill(finished)
  65. self.topLabel.text = "user says".uppercased()
  66. self.mainText.text = newText
  67. self.setLabel(self.cityLabel)
  68. self.setLabel(self.streetLabel)
  69. self.setLabel(self.countryLabel)
  70. self.setLabel(self.dateLabel)
  71. self.setLabel(self.speechLabel)
  72. self.setLabel(self.itentLabel)
  73. self.setLabel(self.conditionLabel)
  74. self.setLabel(self.outfitLabel)
  75. self.setLabel(self.scoreLabel)
  76. })
  77. }
  78. }
  79.  
  80. // MARK : Set label disable / enable
  81. func setLabel(_ label: UILabel, value: String? = nil) {
  82. if value != nil && value != "" {
  83. label.text = value
  84. label.alpha = 1
  85. } else {
  86. label.text = "none"
  87. label.alpha = 0.2
  88. }
  89. }
  90.  
  91. // MARK : Try button action
  92. @IBAction func tryButtonAction(_ sender: UIButton) {
  93. if textField.text! != "" {
  94. newRequest(textField.text!)
  95. }
  96. }
  97.  
  98. // MARK : Detect textfield changes
  99. @objc func textFieldDidChange(_ textField: UITextField) {
  100. if textField.text != "" {
  101. tryButton.isEnabled = true
  102. } else {
  103. tryButton.isEnabled = false
  104. }
  105. }
  106.  
  107. // MARK : update results with the AI data
  108. func updateResults(_ ai: AI) {
  109. DispatchQueue.main.async {
  110.  
  111. self.setLabel(self.itentLabel, value: ai.intent.intentName)
  112. self.setLabel(self.scoreLabel, value: ai.score.roundTo(places: 2).description)
  113. self.setLabel(self.speechLabel, value: ai.intent.speech)
  114.  
  115. if ai.intent.dates != nil && ai.intent.dates!.count > 0 {
  116. var dateString = ""
  117. for (index, date) in ai.intent.dates!.enumerated() {
  118. let dateFormatter = DateFormatter()
  119. dateFormatter.dateFormat = "yyyy-MM-dd"
  120. dateString = dateString + dateFormatter.string(from: date)
  121. dateString = index != ai.intent.dates!.count - 1 ? dateString + ", " : dateString
  122. }
  123. self.setLabel(self.dateLabel, value: dateString)
  124. }
  125.  
  126. if ai.intent.address != nil {
  127. self.setLabel(self.cityLabel, value: ai.intent.address!.city)
  128. self.setLabel(self.countryLabel, value: ai.intent.address!.country)
  129. self.setLabel(self.streetLabel, value: ai.intent.address!.street)
  130. }
  131. if ai.intent.weather != nil {
  132. self.setLabel(self.conditionLabel, value: ai.intent.weather!.condition)
  133. self.setLabel(self.outfitLabel, value: ai.intent.weather!.outfit)
  134. }
  135. UIView.animate(withDuration: 0.5) {
  136. self.resultsView.alpha = 1
  137. }
  138. }
  139. }
  140.  
  141. // MARK : Setup initial state of view
  142. func setInitalState() {
  143. //hide items
  144. headerView.alpha = 0
  145. headerView.alpha = 0
  146. mainView.alpha = 0
  147. resultsView.alpha = 0
  148.  
  149. //setup search bar
  150. textField.layer.borderColor = UIColor.grey500.cgColor
  151. textField.layer.borderWidth = 1.0
  152. textField.layer.cornerRadius = 5
  153. textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
  154.  
  155. //setup text
  156. topLabel.text = "A.I. Tester".uppercased()
  157. mainText.text = "Hey there,\nare you ready to test it?\n\nType a question or pick one from the list above."
  158.  
  159. //disable try now button
  160. tryButton.isEnabled = false
  161.  
  162. //setup buttonsView
  163. buttonsView.addAction { text in
  164. self.newRequest(text)
  165. }
  166.  
  167. //show with animation
  168. UIView.animate(withDuration: 0.5, animations:{
  169. self.headerView.alpha = 1
  170.  
  171. }, completion: { (finished: Bool) in
  172. UIView.animate(withDuration: 0.5) {
  173. self.mainView.alpha = 1
  174. }
  175. })
  176. }
  177.  
  178. }
Add Comment
Please, Sign In to add comment