LablancaView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Section row list
  40. //var rows: Array<RowGallery> = []
  41. /**
  42. Run when the view is started.
  43. */
  44. override init(frame: CGRect){
  45. super.init(frame: frame)
  46. }
  47. /**
  48. Run when the view is started.
  49. */
  50. required init?(coder aDecoder: NSCoder) {
  51. super.init(coder: aDecoder)
  52. NSLog(":LABLANCA:LOG: Init lablanca section.")
  53. Bundle.main.loadNibNamed("LablancaView", owner: self, options: nil)
  54. self.addSubview(container)
  55. container.frame = self.bounds
  56. // Get viewController from StoryBoard
  57. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  58. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  59. // Read settings and show the required sections
  60. let defaults = UserDefaults.standard
  61. let festivals: Int = 1 //TODO defaults.integer(forKey: "festivals")
  62. if festivals == 1{
  63. showFestivals()
  64. }
  65. else{
  66. showNoFestivals()
  67. }
  68. }
  69. func showFestivals(){
  70. NSLog(":LABLANCA:LOG: Showing festivals section.")
  71. self.nfWindow.isHidden = true
  72. // TODO: Get current year
  73. let year = 2016
  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?
  88. }
  89. } catch {
  90. NSLog(":LABLANCA:ERROR: Error getting festivals info: \(error)")
  91. }
  92. // Set up schedule buttons
  93. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openSchedule(_:)))
  94. fBtSchedule.isUserInteractionEnabled = true
  95. fBtSchedule.addGestureRecognizer(tapRecognizer)
  96. // Get info about the day prices.
  97. let dateFormatter = DateFormatter()
  98. dateFormatter.dateFormat = "yyyy-MM-dd"
  99. dateFormatter.locale = Locale.init(identifier: "en_GB")
  100. // First date of year
  101. var dateString = "\(year)-01-01"
  102. let sDate = dateFormatter.date(from: dateString)
  103. // Last date of year
  104. dateString = "\(year)-12-31"
  105. let eDate = dateFormatter.date(from: dateString)
  106. // Get days
  107. let dayFetchRequest: NSFetchRequest<Festival_day> = Festival_day.fetchRequest()
  108. dayFetchRequest.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", argumentArray: [sDate, eDate])
  109. do {
  110. // Get info from festivals
  111. let daySearchResults = try context.fetch(dayFetchRequest)
  112. var row: RowLablancaDay
  113. var count: Int = 0
  114. for day in daySearchResults as [NSManagedObject]{
  115. let date: NSDate = day.value(forKey: "date")! as! NSDate
  116. let dateString: String = dateFormatter.string(from: date as Date)
  117. let nDay: String = dateString.subStr(start: 8, end: 9)
  118. let month: String = dateString.subStr(start: 5, end: 6)
  119. var nMonth: String = "Agosto" // TODO: Reference
  120. if month == "07"{
  121. nMonth = "Julio" // TODO: Reference
  122. }
  123. let name: String = day.value(forKey: "name_\(lang)")! as! String
  124. let price: Int = day.value(forKey: "price")! as! Int
  125. // Create the row and set data.
  126. row = RowLablancaDay.init(s: "rowLablancaDay\(count)", i: count)
  127. row.name.text = name
  128. row.price.text = "\(price) €"
  129. row.number.text = "\(Int(nDay)!)"
  130. row.month.text = nMonth
  131. // Add the row
  132. fDayList.addArrangedSubview(row)
  133. row.setNeedsLayout()
  134. row.layoutIfNeeded()
  135. // Hide the separator of the last row
  136. if count == daySearchResults.count - 1 {
  137. row.separator.isHidden = true
  138. }
  139. count = count + 1
  140. }
  141. } catch {
  142. NSLog(":LABLANCA:ERROR: Error getting festival days info: \(error)")
  143. }
  144. }
  145. func showNoFestivals(){
  146. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  147. self.fWindow.isHidden = true
  148. // Nothing else to be done.
  149. }
  150. func openSchedule(_ sender:UITapGestureRecognizer? = nil){
  151. NSLog(":LABLANCA:DEBUG: Getting delegate and showing schedule.")
  152. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  153. delegate.controller?.showSchedule(margolari: false)
  154. NSLog(":LABLANCA:DEBUG: Schedule should be shown.")
  155. }
  156. func getLanguage() -> String{
  157. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  158. if(pre == "es" || pre == "en" || pre == "eu"){
  159. return pre
  160. }
  161. else{
  162. return "es"
  163. }
  164. }
  165. }