LablancaView.swift 8.6 KB

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