LablancaView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle the home view.
  25. */
  26. class LablancaView: UIView {
  27. @IBOutlet var container: UIView! // Top container of the view.
  28. @IBOutlet weak var nfWindow: UIView! // The section to show when no festivals.
  29. @IBOutlet weak var fWindow: UIView! // The window to show while the festivals.
  30. @IBOutlet weak var fTitle: UILabel! // The title of the year festivals.
  31. @IBOutlet weak var fImage: UIImageView! // The festivals image
  32. @IBOutlet weak var fText: UILabel! // The festivals description.
  33. @IBOutlet weak var fBtSchedule: UIButton! // Button to show the city schedule.
  34. @IBOutlet weak var fBtGMSchedule: UIButton! // Button to show the Margolari schedule
  35. @IBOutlet weak var fOfferList: UIStackView! // Stack view to hold the offer list.
  36. @IBOutlet weak var fDayList: UIStackView! // Stack view to hold the single days price list.
  37. // App delegate
  38. var delegate: AppDelegate? = nil
  39. /**
  40. Run when the view is started.
  41. */
  42. override init(frame: CGRect){
  43. super.init(frame: frame)
  44. }
  45. /**
  46. Run when the view is started.
  47. */
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. NSLog(":LABLANCA:LOG: Init lablanca section.")
  51. Bundle.main.loadNibNamed("LablancaView", owner: self, options: nil)
  52. self.addSubview(container)
  53. self.container.frame = self.bounds
  54. // Get viewController from StoryBoard
  55. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  56. _ = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  57. // Read settings and show the required sections
  58. let defaults = UserDefaults.standard
  59. let festivals: Int = defaults.integer(forKey: "festivals")
  60. if festivals == 1{
  61. showFestivals()
  62. }
  63. else{
  64. showNoFestivals()
  65. }
  66. }
  67. /**
  68. Displays the section to be shown while (and near) the La Blanca festivals.
  69. */
  70. func showFestivals(){
  71. NSLog(":LABLANCA:LOG: Showing festivals section.")
  72. self.nfWindow.isHidden = true
  73. // TODO: Get current year
  74. let year = 2017
  75. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  76. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  77. let lang : String = getLanguage()
  78. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  79. // Get info about festivals
  80. let fetchRequest: NSFetchRequest<Festival> = Festival.fetchRequest()
  81. fetchRequest.predicate = NSPredicate(format: "year = %i", year)
  82. do {
  83. // Get info from festivals
  84. let searchResults = try context.fetch(fetchRequest)
  85. if searchResults.count > 0 {
  86. let r = searchResults[0]
  87. // Set description
  88. self.fText.text = (r.value(forKey: "text_\(lang)") as! String?)?.decode().stripHtml()
  89. }
  90. }
  91. catch {
  92. NSLog(":LABLANCA:ERROR: Error getting festivals info: \(error)")
  93. }
  94. // Set up schedule buttons
  95. let tapRecognizerSchedule = UITapGestureRecognizer(target: self, action: #selector(openSchedule(_:)))
  96. fBtSchedule.isUserInteractionEnabled = true
  97. fBtSchedule.addGestureRecognizer(tapRecognizerSchedule)
  98. let tapRecognizerGMSchedule = UITapGestureRecognizer(target: self, action: #selector(openGMSchedule(_:)))
  99. fBtGMSchedule.isUserInteractionEnabled = true
  100. fBtGMSchedule.addGestureRecognizer(tapRecognizerGMSchedule)
  101. // Get info about the days and offers.
  102. let dateFormatter = DateFormatter()
  103. dateFormatter.dateFormat = "yyyy-MM-dd"
  104. dateFormatter.locale = Locale.init(identifier: "en_GB")
  105. // First date of year
  106. var dateString = "\(year)-01-01"
  107. let sDate = dateFormatter.date(from: dateString)
  108. // Last date of year
  109. dateString = "\(year)-12-31"
  110. let eDate = dateFormatter.date(from: dateString)
  111. // Get offers
  112. let offerFetchRequest: NSFetchRequest<Festival_offer> = Festival_offer.fetchRequest()
  113. offerFetchRequest.predicate = NSPredicate(format: "year = %i", year)
  114. do {
  115. // Get info from festivals
  116. let offerSearchResults = try context.fetch(offerFetchRequest)
  117. NSLog(":LABLANCA:DEBUG: \(offerSearchResults.count) offes found.")
  118. var row: RowLablancaOffer
  119. var count: Int = 0
  120. for offer in offerSearchResults as [NSManagedObject]{
  121. let name: String = offer.value(forKey: "name_\(lang)")! as! String
  122. let price: Int = offer.value(forKey: "price")! as! Int
  123. let description: String = offer.value(forKey: "description_\(lang)")! as! String
  124. // Create the row and set data.
  125. row = RowLablancaOffer.init(s: "rowLablancaOffer\(count)", i: count)
  126. row.setName(name: name)
  127. row.setPrice(price: price)
  128. row.setDescription(description: description)
  129. // Add the row
  130. fOfferList.addArrangedSubview(row)
  131. row.setNeedsLayout()
  132. row.layoutIfNeeded()
  133. // Hide the separator of the last row
  134. if count == offerSearchResults.count - 1 {
  135. row.hidSeparator()
  136. }
  137. count = count + 1
  138. }
  139. }
  140. catch {
  141. NSLog(":LABLANCA:ERROR: Error getting festival offers: \(error)")
  142. }
  143. // Get days
  144. let dayFetchRequest: NSFetchRequest<Festival_day> = Festival_day.fetchRequest()
  145. dayFetchRequest.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", argumentArray: [sDate!, eDate!])
  146. do {
  147. // Get info from festivals
  148. let daySearchResults = try context.fetch(dayFetchRequest)
  149. var row: RowLablancaDay
  150. var count: Int = 0
  151. for day in daySearchResults as [NSManagedObject]{
  152. let date: NSDate = day.value(forKey: "date")! as! NSDate
  153. let dateString: String = dateFormatter.string(from: date as Date)
  154. let nDay: Int = Int(dateString.subStr(start: 8, end: 9))!
  155. let month: String = dateString.subStr(start: 5, end: 6)
  156. var nMonth: String = "Agosto" // TODO: Reference
  157. if month == "07"{
  158. nMonth = "Julio" // TODO: Reference
  159. }
  160. let name: String = day.value(forKey: "name_\(lang)")! as! String
  161. let price: Int = day.value(forKey: "price")! as! Int
  162. // Create the row and set data.
  163. row = RowLablancaDay.init(s: "rowLablancaDay\(count)", i: count)
  164. row.setDay(number: nDay, month: nMonth, name: name, price: price)
  165. // Add the row
  166. fDayList.addArrangedSubview(row)
  167. row.setNeedsLayout()
  168. row.layoutIfNeeded()
  169. // Hide the separator of the last row
  170. if count == daySearchResults.count - 1 {
  171. row.separator.isHidden = true
  172. }
  173. count = count + 1
  174. }
  175. }
  176. catch {
  177. NSLog(":LABLANCA:ERROR: Error getting festival days info: \(error)")
  178. }
  179. }
  180. /**
  181. Hides the view to be shown during (and near) the festivals.
  182. */
  183. func showNoFestivals(){
  184. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  185. self.fWindow.isHidden = true
  186. // Nothing else to be done.
  187. }
  188. /**
  189. Commands the view controller to show the city schedule view.
  190. :param: sender The view that triggered the event.
  191. */
  192. func openSchedule(_ sender:UITapGestureRecognizer? = nil){
  193. NSLog(":LABLANCA:DEBUG: Getting delegate and showing schedule.")
  194. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  195. delegate.controller?.showSchedule(margolari: false)
  196. NSLog(":LABLANCA:DEBUG: Schedule should be shown.")
  197. }
  198. /**
  199. Commands the view controller to show the Gasteizko Margolariak schedule view.
  200. :param: sender The view that triggered the event.
  201. */
  202. func openGMSchedule(_ sender:UITapGestureRecognizer? = nil){
  203. NSLog(":LABLANCA:DEBUG: Getting delegate and showing GM schedule.")
  204. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  205. delegate.controller?.showSchedule(margolari: true)
  206. NSLog(":LABLANCA:DEBUG: GM schedule should be shown.")
  207. }
  208. /**
  209. Gets the device language. The only recognized languages are Spanish, English and Basque.
  210. If the device has another language, Spanish will be selected by default.
  211. :return: Two-letter language code.
  212. */
  213. func getLanguage() -> String{
  214. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  215. if(pre == "es" || pre == "en" || pre == "eu"){
  216. return pre
  217. }
  218. else{
  219. return "es"
  220. }
  221. }
  222. }