EventController.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 MapKit
  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: MKMapView!
  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. //let mapTemplate: String = "http://tile.openstreetmap.org/{z}/{x}/{y}.png";
  49. //let mapOverlay: MKTileOverlay = MKTileOverlay.initWithURLTemplate(mapTemplate)
  50. //overlay.canReplaceMapContent = YES
  51. //self.eventMap addOverlay(overlay level:MKOverlayLevelAboveLabels]; // (4)
  52. // TODO: Pas this
  53. let margolari: Bool = true
  54. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  55. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  56. let lang : String = getLanguage()
  57. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  58. if (margolari == true){
  59. let fetchRequest: NSFetchRequest<Festival_event_gm> = Festival_event_gm.fetchRequest()
  60. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  61. do {
  62. // Get the result
  63. let searchResults = try context.fetch(fetchRequest)
  64. var name: String = ""
  65. var text: String = ""
  66. var start: NSDate
  67. var placeId: Int = -1
  68. var place: [Any]
  69. var placeName: String = ""
  70. var placeAddress: String = ""
  71. var lat: Double = 0.0
  72. var lon: Double = 0.0
  73. if searchResults.count > 0{
  74. let r = searchResults[0]
  75. name = r.value(forKey: "title_\(lang)")! as! String
  76. if r.value(forKey: "description_\(lang)") != nil{
  77. text = r.value(forKey: "description_\(lang)")! as! String
  78. }
  79. start = r.value(forKey: "start")! as! NSDate
  80. placeId = r.value(forKey: "place")! as! Int
  81. place = getPlace(place: placeId, lang: getLanguage(), context: context)
  82. placeName = place[0] as! String
  83. placeAddress = place[1] as! String
  84. lat = place[2] as! Double
  85. lon = place[3] as! Double
  86. // Set labels
  87. eventTitle.text = " \(name.decode().stripHtml())"
  88. if text.length > 0{
  89. eventText.text = " \(text.decode().stripHtml())"
  90. }
  91. else{
  92. self.eventText.isHidden = true
  93. }
  94. self.eventTime.text = formatTime(date: start)
  95. self.eventLocation.text = placeName.decode().stripHtml()
  96. if placeAddress.length == 0 || placeAddress == placeName{
  97. self.eventAddress.isHidden = true
  98. }
  99. else{
  100. self.eventAddress.text = placeAddress.decode().stripHtml()
  101. }
  102. }
  103. // Set up map
  104. let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
  105. let carte_indice = MKTileOverlay(urlTemplate:template)
  106. carte_indice.isGeometryFlipped = true
  107. carte_indice.canReplaceMapContent = false
  108. self.eventMap.add(carte_indice)
  109. // Set marker
  110. let annotation = MKPointAnnotation()
  111. annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  112. self.eventMap.addAnnotation(annotation)
  113. // Center map
  114. let center = CLLocation(latitude: lat, longitude: lon)
  115. let radius: CLLocationDistance = 1000
  116. let coordinateRegion = MKCoordinateRegionMakeWithDistance(center.coordinate,
  117. radius, radius)
  118. self.eventMap.setRegion(coordinateRegion, animated: true)
  119. }
  120. catch {
  121. NSLog(":EVENT:ERROR: Error getting info from event \(id): \(error)")
  122. }
  123. } // if margolari == true
  124. super.viewDidLoad()
  125. // Set button action
  126. self.btClose.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  127. }
  128. /**
  129. Dismisses the controller.
  130. */
  131. func back() {
  132. self.dismiss(animated: true, completion: nil)
  133. }
  134. /**
  135. Dispose of any resources that can be recreated.
  136. */
  137. override func didReceiveMemoryWarning() {
  138. super.didReceiveMemoryWarning()
  139. }
  140. /**
  141. Extracts the time from a date to a string.
  142. :param: text The date.
  143. :return: Time, in string format.
  144. */
  145. func formatTime(date: NSDate) -> String{
  146. let calendar = Calendar.current
  147. let hour: Int = calendar.component(.hour, from: date as Date)
  148. let minute: Int = calendar.component(.minute, from: date as Date)
  149. var strTime = ""
  150. if minute <= 9{
  151. strTime = "\(hour):0\(minute)"
  152. }
  153. else{
  154. strTime = "\(hour):\(minute)"
  155. }
  156. return strTime
  157. }
  158. /**
  159. Gets info about a place in the device language.
  160. :param: place Place id.
  161. :param: lang Device language (only 'es', 'en', or 'eu').
  162. :param: context Application context.
  163. :return: String array. 0-index item contains the place name. The 1-index one contains the address.
  164. */
  165. func getPlace(place: Int, lang: String, context: NSManagedObjectContext) -> [Any]{
  166. var placeName: String = ""
  167. var placeAddress: String = ""
  168. var lat: Double = 0.0
  169. var lon: Double = 0.0
  170. let fetchRequest: NSFetchRequest<Place> = Place.fetchRequest()
  171. fetchRequest.predicate = NSPredicate(format: "id = %i", place)
  172. do {
  173. let searchResults = try context.fetch(fetchRequest)
  174. if (searchResults.count > 0){
  175. let r: NSManagedObject = searchResults[0]
  176. placeName = r.value(forKey: "name_\(lang)")! as! String
  177. placeAddress = r.value(forKey: "address_\(lang)")! as! String
  178. lat = r.value(forKey: "lat")! as! Double
  179. lon = r.value(forKey: "lon")! as! Double
  180. }
  181. else{
  182. placeName = " "
  183. placeAddress = " "
  184. lat = 42.8507807
  185. lon = -2.8394378
  186. }
  187. }
  188. catch{
  189. NSLog(":FUTUREACTIVITY:ERROR: Error getting infor about place \(place): \(error)")
  190. }
  191. return [placeName, placeAddress, lat, lon]
  192. }
  193. func eventMapV(eventMap: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
  194. {
  195. if overlay is MKTileOverlay
  196. {
  197. var renderer = MKTileOverlayRenderer(overlay:overlay)
  198. renderer.alpha = 0.8
  199. return renderer
  200. }
  201. return nil
  202. }
  203. /**
  204. Gets the device language. The only recognized languages are Spanish, English and Basque.
  205. If the device has another language, Spanish will be selected by default.
  206. :return: Two-letter language code.
  207. */
  208. func getLanguage() -> String{
  209. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  210. if(pre == "es" || pre == "en" || pre == "eu"){
  211. return pre
  212. }
  213. else{
  214. return "es"
  215. }
  216. }
  217. }