EventController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. if r.value(forKey: "description_\(lang)") != nil{
  70. text = r.value(forKey: "description_\(lang)")! as! String
  71. }
  72. start = r.value(forKey: "start")! as! NSDate
  73. placeId = r.value(forKey: "place")! as! Int
  74. place = getPlace(place: placeId, lang: getLanguage(), context: context)
  75. placeName = place[0] as! String
  76. placeAddress = place[1] as! String
  77. lat = place[2] as! Double
  78. lon = place[3] as! Double
  79. // Set labels
  80. eventTitle.text = " \(name.decode().stripHtml())"
  81. if text.length > 0{
  82. eventText.text = " \(text.decode().stripHtml())"
  83. }
  84. else{
  85. self.eventText.isHidden = true
  86. }
  87. self.eventTime.text = formatTime(date: start)
  88. self.eventLocation.text = placeName.decode().stripHtml()
  89. if placeAddress.length == 0 || placeAddress == placeName{
  90. self.eventAddress.isHidden = true
  91. }
  92. else{
  93. self.eventAddress.text = placeAddress.decode().stripHtml()
  94. }
  95. // Set map marker
  96. let marker = GMSMarker()
  97. marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  98. marker.title = name
  99. marker.map = self.eventMap
  100. // Center map
  101. eventMap.isMyLocationEnabled = true
  102. let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 12.0)
  103. self.eventMap.camera = camera
  104. }
  105. }
  106. catch {
  107. NSLog(":EVENT:ERROR: Error getting info from event \(id): \(error)")
  108. }
  109. super.viewDidLoad()
  110. // Set button action
  111. self.btClose.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  112. }
  113. /**
  114. Dismisses the controller.
  115. */
  116. func back() {
  117. self.dismiss(animated: true, completion: nil)
  118. }
  119. /**
  120. Dispose of any resources that can be recreated.
  121. */
  122. override func didReceiveMemoryWarning() {
  123. super.didReceiveMemoryWarning()
  124. }
  125. /**
  126. Extracts the time from a date to a string.
  127. :param: text The date.
  128. :return: Time, in string format.
  129. */
  130. func formatTime(date: NSDate) -> String{
  131. let calendar = Calendar.current
  132. let hour: Int = calendar.component(.hour, from: date as Date)
  133. let minute: Int = calendar.component(.minute, from: date as Date)
  134. var strTime = ""
  135. if minute <= 9{
  136. strTime = "\(hour):0\(minute)"
  137. }
  138. else{
  139. strTime = "\(hour):\(minute)"
  140. }
  141. return strTime
  142. }
  143. /**
  144. Gets info about a place in the device language.
  145. :param: place Place id.
  146. :param: lang Device language (only 'es', 'en', or 'eu').
  147. :param: context Application context.
  148. :return: String array. 0-index item contains the place name. The 1-index one contains the address.
  149. */
  150. func getPlace(place: Int, lang: String, context: NSManagedObjectContext) -> [Any]{
  151. var placeName: String = ""
  152. var placeAddress: String = ""
  153. var lat: Double = 0.0
  154. var lon: Double = 0.0
  155. let fetchRequest: NSFetchRequest<Place> = Place.fetchRequest()
  156. fetchRequest.predicate = NSPredicate(format: "id = %i", place)
  157. do {
  158. let searchResults = try context.fetch(fetchRequest)
  159. if (searchResults.count > 0){
  160. let r: NSManagedObject = searchResults[0]
  161. placeName = r.value(forKey: "name_\(lang)")! as! String
  162. placeAddress = r.value(forKey: "address_\(lang)")! as! String
  163. lat = r.value(forKey: "lat")! as! Double
  164. lon = r.value(forKey: "lon")! as! Double
  165. }
  166. else{
  167. placeName = " "
  168. placeAddress = " "
  169. lat = 42.8507807
  170. lon = -2.8394378
  171. }
  172. }
  173. catch{
  174. NSLog(":FUTUREACTIVITY:ERROR: Error getting infor about place \(place): \(error)")
  175. }
  176. return [placeName, placeAddress, lat, lon]
  177. }
  178. /**
  179. Gets the device language. The only recognized languages are Spanish, English and Basque.
  180. If the device has another language, Spanish will be selected by default.
  181. :return: Two-letter language code.
  182. */
  183. func getLanguage() -> String{
  184. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  185. if(pre == "es" || pre == "en" || pre == "eu"){
  186. return pre
  187. }
  188. else{
  189. return "es"
  190. }
  191. }
  192. }