EventController.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. //self.eventMap.delegate = appDelegate as! MKMapViewDelegate
  105. //self.eventMap.mapType = MKMapType.satellite
  106. //Map centre
  107. NSLog("IVV Coordinates: \(lat) \(lon)")
  108. let centre = CLLocationCoordinate2D(latitude: lat,
  109. longitude: lon)
  110. //Declare span of map
  111. let span = MKCoordinateSpan(latitudeDelta: 0.05,
  112. longitudeDelta: 0.05)
  113. //Set region of the map
  114. let region = MKCoordinateRegion(center: centre, span: span)
  115. self.eventMap.setRegion(region, animated: false)
  116. self.eventMap.regionThatFits(region)
  117. var template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
  118. let carte_indice = MKTileOverlay(urlTemplate:template)
  119. carte_indice.isGeometryFlipped = true
  120. carte_indice.canReplaceMapContent = false
  121. self.eventMap.add(carte_indice)
  122. // Set marker
  123. let annotation = MKPointAnnotation()
  124. annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
  125. self.eventMap.addAnnotation(annotation)
  126. // Center map
  127. let center = CLLocation(latitude: lat, longitude: lon)
  128. let radius: CLLocationDistance = 1000
  129. let coordinateRegion = MKCoordinateRegionMakeWithDistance(center.coordinate,
  130. radius, radius)
  131. self.eventMap.setRegion(coordinateRegion, animated: true)
  132. }
  133. catch {
  134. NSLog(":EVENT:ERROR: Error getting info from event \(id): \(error)")
  135. }
  136. } // if margolari == true
  137. super.viewDidLoad()
  138. // Set button action
  139. self.btClose.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  140. }
  141. /**
  142. Dismisses the controller.
  143. */
  144. func back() {
  145. self.dismiss(animated: true, completion: nil)
  146. }
  147. /**
  148. Dispose of any resources that can be recreated.
  149. */
  150. override func didReceiveMemoryWarning() {
  151. super.didReceiveMemoryWarning()
  152. }
  153. /**
  154. Extracts the time from a date to a string.
  155. :param: text The date.
  156. :return: Time, in string format.
  157. */
  158. func formatTime(date: NSDate) -> String{
  159. let calendar = Calendar.current
  160. let hour: Int = calendar.component(.hour, from: date as Date)
  161. let minute: Int = calendar.component(.minute, from: date as Date)
  162. var strTime = ""
  163. if minute <= 9{
  164. strTime = "\(hour):0\(minute)"
  165. }
  166. else{
  167. strTime = "\(hour):\(minute)"
  168. }
  169. return strTime
  170. }
  171. /**
  172. Gets info about a place in the device language.
  173. :param: place Place id.
  174. :param: lang Device language (only 'es', 'en', or 'eu').
  175. :param: context Application context.
  176. :return: String array. 0-index item contains the place name. The 1-index one contains the address.
  177. */
  178. func getPlace(place: Int, lang: String, context: NSManagedObjectContext) -> [Any]{
  179. var placeName: String = ""
  180. var placeAddress: String = ""
  181. var lat: Double = 0.0
  182. var lon: Double = 0.0
  183. let fetchRequest: NSFetchRequest<Place> = Place.fetchRequest()
  184. fetchRequest.predicate = NSPredicate(format: "id = %i", place)
  185. do {
  186. let searchResults = try context.fetch(fetchRequest)
  187. if (searchResults.count > 0){
  188. let r: NSManagedObject = searchResults[0]
  189. placeName = r.value(forKey: "name_\(lang)")! as! String
  190. placeAddress = r.value(forKey: "address_\(lang)")! as! String
  191. lat = r.value(forKey: "lat")! as! Double
  192. lon = r.value(forKey: "lon")! as! Double
  193. }
  194. else{
  195. placeName = " "
  196. placeAddress = " "
  197. lat = 42.8507807
  198. lon = -2.8394378
  199. }
  200. }
  201. catch{
  202. NSLog(":FUTUREACTIVITY:ERROR: Error getting infor about place \(place): \(error)")
  203. }
  204. return [placeName, placeAddress, lat, lon]
  205. }
  206. func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
  207. {
  208. if overlay is MKTileOverlay
  209. {
  210. var renderer = MKTileOverlayRenderer(overlay:overlay)
  211. renderer.alpha = 0.8
  212. return renderer
  213. }
  214. return nil
  215. }
  216. /**
  217. Gets the device language. The only recognized languages are Spanish, English and Basque.
  218. If the device has another language, Spanish will be selected by default.
  219. :return: Two-letter language code.
  220. */
  221. func getLanguage() -> String{
  222. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  223. if(pre == "es" || pre == "en" || pre == "eu"){
  224. return pre
  225. }
  226. else{
  227. return "es"
  228. }
  229. }
  230. }