LablancaView.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 La Blanca 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. Default constructor for the interface builder.
  41. :param: frame Window frame.
  42. */
  43. override init(frame: CGRect){
  44. super.init(frame: frame)
  45. }
  46. /**
  47. Run when the view is started.
  48. */
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  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 = 2018
  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. // Set image
  90. let filename: String = r.value(forKey: "img") as! String
  91. if (filename == ""){
  92. // Hide the imageview
  93. self.fImage.isHidden = true;
  94. }
  95. else{
  96. let path = "img/fiestas/thumb/\(filename)"
  97. self.fImage.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
  98. }
  99. }
  100. }
  101. catch {
  102. NSLog(":LABLANCA:ERROR: Error getting festivals info: \(error)")
  103. }
  104. // Set up schedule buttons
  105. let tapRecognizerSchedule = UITapGestureRecognizer(target: self, action: #selector(openSchedule(_:)))
  106. fBtSchedule.isUserInteractionEnabled = true
  107. fBtSchedule.addGestureRecognizer(tapRecognizerSchedule)
  108. let tapRecognizerGMSchedule = UITapGestureRecognizer(target: self, action: #selector(openGMSchedule(_:)))
  109. fBtGMSchedule.isUserInteractionEnabled = true
  110. fBtGMSchedule.addGestureRecognizer(tapRecognizerGMSchedule)
  111. // Get info about the days and offers.
  112. let dateFormatter = DateFormatter()
  113. dateFormatter.dateFormat = "yyyy-MM-dd"
  114. dateFormatter.locale = Locale.init(identifier: "en_GB")
  115. // First date of year
  116. var dateString = "\(year)-01-01"
  117. let sDate = dateFormatter.date(from: dateString)
  118. // Last date of year
  119. dateString = "\(year)-12-31"
  120. let eDate = dateFormatter.date(from: dateString)
  121. // Get offers
  122. let offerFetchRequest: NSFetchRequest<Festival_offer> = Festival_offer.fetchRequest()
  123. offerFetchRequest.predicate = NSPredicate(format: "year = %i", year)
  124. do {
  125. // Get info from festivals
  126. let offerSearchResults = try context.fetch(offerFetchRequest)
  127. var row: RowLablancaOffer
  128. var count: Int = 0
  129. for offer in offerSearchResults as [NSManagedObject]{
  130. let name: String = offer.value(forKey: "name_\(lang)")! as! String
  131. let price: Int = offer.value(forKey: "price")! as! Int
  132. let description: String = offer.value(forKey: "description_\(lang)")! as! String
  133. // Create the row and set data.
  134. row = RowLablancaOffer.init(s: "rowLablancaOffer\(count)", i: count)
  135. row.setName(name: name)
  136. row.setPrice(price: price)
  137. row.setDescription(description: description)
  138. // Add the row
  139. fOfferList.addArrangedSubview(row)
  140. row.setNeedsLayout()
  141. row.layoutIfNeeded()
  142. // Hide the separator of the last row
  143. if count == offerSearchResults.count - 1 {
  144. row.hidSeparator()
  145. }
  146. count = count + 1
  147. }
  148. }
  149. catch {
  150. NSLog(":LABLANCA:ERROR: Error getting festival offers: \(error)")
  151. }
  152. // Get days
  153. let dayFetchRequest: NSFetchRequest<Festival_day> = Festival_day.fetchRequest()
  154. dayFetchRequest.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", argumentArray: [sDate!, eDate!])
  155. do {
  156. // Get info from festivals
  157. let daySearchResults = try context.fetch(dayFetchRequest)
  158. var row: RowLablancaDay
  159. var count: Int = 0
  160. for day in daySearchResults as [NSManagedObject]{
  161. let date: NSDate = day.value(forKey: "date")! as! NSDate
  162. let dateString: String = dateFormatter.string(from: date as Date)
  163. let nDay: Int = Int(dateString.subStr(start: 8, end: 9))!
  164. let month: String = dateString.subStr(start: 5, end: 6)
  165. var nMonth: String = "Agosto" // TODO: Reference
  166. if month == "07"{
  167. nMonth = "Julio" // TODO: Reference
  168. }
  169. let name: String = day.value(forKey: "name_\(lang)")! as! String
  170. let price: Int = day.value(forKey: "price")! as! Int
  171. // Create the row and set data.
  172. row = RowLablancaDay.init(s: "rowLablancaDay\(count)", i: count)
  173. row.setDay(number: nDay, month: nMonth, name: name, price: price)
  174. // Add the row
  175. fDayList.addArrangedSubview(row)
  176. row.setNeedsLayout()
  177. row.layoutIfNeeded()
  178. // Hide the separator of the last row
  179. if count == daySearchResults.count - 1 {
  180. row.separator.isHidden = true
  181. }
  182. count = count + 1
  183. }
  184. }
  185. catch {
  186. NSLog(":LABLANCA:ERROR: Error getting festival days info: \(error)")
  187. }
  188. }
  189. /**
  190. Hides the view to be shown during (and near) the festivals.
  191. */
  192. func showNoFestivals(){
  193. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  194. self.fWindow.isHidden = true
  195. }
  196. /**
  197. Commands the view controller to show the city schedule view.
  198. */
  199. @objc func openSchedule(_ sender:UITapGestureRecognizer? = nil){
  200. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  201. delegate.controller?.showSchedule(margolari: false)
  202. }
  203. /**
  204. Commands the view controller to show the Gasteizko Margolariak schedule view.
  205. */
  206. @objc func openGMSchedule(_ sender:UITapGestureRecognizer? = nil){
  207. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  208. delegate.controller?.showSchedule(margolari: true)
  209. }
  210. /**
  211. Gets the device language. The only recognized languages are Spanish, English and Basque.
  212. If the device has another language, Spanish will be selected by default.
  213. :return: Two-letter language code.
  214. */
  215. func getLanguage() -> String{
  216. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  217. if(pre == "es" || pre == "en" || pre == "eu"){
  218. return pre
  219. }
  220. else{
  221. return "es"
  222. }
  223. }
  224. }