EventController.swift 6.4 KB

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