EventController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import UIKit
  21. import CoreData
  22. import GoogleMaps
  23. /**
  24. The view controller of the app.
  25. */
  26. class EventController: UIViewController, UIGestureRecognizerDelegate {
  27. @IBOutlet weak var eventTitle: UITextView!
  28. @IBOutlet weak var eventText: UILabel!
  29. @IBOutlet weak var eventTime: UILabel!
  30. @IBOutlet weak var eventLocation: UILabel!
  31. @IBOutlet weak var eventMap: GMSMapView!
  32. @IBOutlet weak var btClose: UIButton!
  33. @IBOutlet weak var eventAddress: UILabel!
  34. var id: Int = -1
  35. var passId: Int = -1
  36. var delegate: AppDelegate?
  37. /**
  38. Gets the application context.
  39. :return: The application context.
  40. */
  41. func getContext () -> NSManagedObjectContext {
  42. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  43. }
  44. /**
  45. Run when the app loads. Sets everything up.
  46. */
  47. override func viewDidLoad() {
  48. // Populate photo array.
  49. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  50. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  51. let lang : String = getLanguage()
  52. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  53. let fetchRequest: NSFetchRequest<Festival_event> = Festival_event.fetchRequest()
  54. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  55. do {
  56. let searchResults = try context.fetch(fetchRequest)
  57. var name: String = ""
  58. var text: String = ""
  59. var start: NSDate
  60. var placeId: Int = -1
  61. var place: [Any]
  62. var placeName: String = ""
  63. var placeAddress: String = ""
  64. var lat: Double = 0.0
  65. var lon: Double = 0.0
  66. if searchResults.count > 0{
  67. let r = searchResults[0]
  68. name = r.value(forKey: "title_\(lang)")! as! String
  69. text = r.value(forKey: "description_\(lang)")! as! String
  70. start = r.value(forKey: "start")! as! NSDate
  71. placeId = r.value(forKey: "place")! as! Int
  72. place = getPlace(place: placeId, lang: getLanguage(), context: context)
  73. placeName = place[0] as! String
  74. placeAddress = place[1] as! String
  75. lat = place[2] as! Double
  76. lon = place[3] as! Double
  77. // Set labels
  78. eventTitle.text = " \(name.decode().stripHtml())"
  79. if text.length > 0{
  80. eventText.text = " \(text.decode().stripHtml())"
  81. }
  82. else{
  83. self.eventText.isHidden = true
  84. }
  85. self.eventTime.text = formatTime(date: start)
  86. self.eventLocation.text = placeName.decode().stripHtml()
  87. if placeAddress.length == 0 || placeAddress == placeName{
  88. self.eventAddress.isHidden = true
  89. }
  90. else{
  91. self.eventAddress.text = placeAddress.decode().stripHtml()
  92. }
  93. // Set map marker
  94. let marker = GMSMarker()
  95. marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  96. marker.title = name
  97. marker.map = self.eventMap
  98. // Center map
  99. eventMap.isMyLocationEnabled = true
  100. let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 12.0)
  101. self.eventMap.camera = camera
  102. }
  103. }
  104. catch {
  105. NSLog(":EVENT:ERROR: Error getting info from event \(id): \(error)")
  106. }
  107. super.viewDidLoad()
  108. // Set button action
  109. self.btClose.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  110. }
  111. /**
  112. Dismisses the controller.
  113. */
  114. func back() {
  115. self.dismiss(animated: true, completion: nil)
  116. }
  117. /**
  118. Dispose of any resources that can be recreated.
  119. */
  120. override func didReceiveMemoryWarning() {
  121. super.didReceiveMemoryWarning()
  122. }
  123. /**
  124. Extracts the time from a date to a string.
  125. :param: text The date.
  126. :return: Time, in string format.
  127. */
  128. func formatTime(date: NSDate) -> String{
  129. let calendar = Calendar.current
  130. let hour: Int = calendar.component(.hour, from: date as Date)
  131. let minute: Int = calendar.component(.minute, from: date as Date)
  132. var strTime = ""
  133. if minute <= 9{
  134. strTime = "\(hour):0\(minute)"
  135. }
  136. else{
  137. strTime = "\(hour):\(minute)"
  138. }
  139. return strTime
  140. }
  141. /**
  142. Gets info about a place in the device language.
  143. :param: place Place id.
  144. :param: lang Device language (only 'es', 'en', or 'eu').
  145. :param: context Application context.
  146. :return: String array. 0-index item contains the place name. The 1-index one contains the address.
  147. */
  148. func getPlace(place: Int, lang: String, context: NSManagedObjectContext) -> [Any]{
  149. var placeName: String = ""
  150. var placeAddress: String = ""
  151. var lat: Double = 0.0
  152. var lon: Double = 0.0
  153. let fetchRequest: NSFetchRequest<Place> = Place.fetchRequest()
  154. fetchRequest.predicate = NSPredicate(format: "id = %i", place)
  155. do {
  156. let searchResults = try context.fetch(fetchRequest)
  157. let r: NSManagedObject = searchResults[0]
  158. placeName = r.value(forKey: "name_\(lang)")! as! String
  159. placeAddress = r.value(forKey: "address_\(lang)")! as! String
  160. lat = r.value(forKey: "lat")! as! Double
  161. lon = r.value(forKey: "lon")! as! Double
  162. }
  163. catch{
  164. NSLog(":FUTUREACTIVITY:ERROR: Error getting infor about place \(place): \(error)")
  165. }
  166. return [placeName, placeAddress, lat, lon]
  167. }
  168. /**
  169. Gets the device language. The only recognized languages are Spanish, English and Basque.
  170. If the device has another language, Spanish will be selected by default.
  171. :return: Two-letter language code.
  172. */
  173. func getLanguage() -> String{
  174. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  175. if(pre == "es" || pre == "en" || pre == "eu"){
  176. return pre
  177. }
  178. else{
  179. return "es"
  180. }
  181. }
  182. }