LablancaView.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. //section.setTitle(text: "Gallery")
  57. //let parent = section.getContentStack()
  58. // Get viewController from StoryBoard
  59. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  60. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  61. // Read settings and show the required sections
  62. let defaults = UserDefaults.standard
  63. let festivals: Int = 1 //TODO defaults.integer(forKey: "festivals")
  64. if festivals == 1{
  65. showFestivals()
  66. }
  67. else{
  68. showNoFestivals()
  69. }
  70. // Show albums
  71. /*let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  72. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  73. self.delegate = appDelegate
  74. let lang : String = getLanguage()
  75. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  76. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  77. let sortDescriptor = NSSortDescriptor(key: "time", ascending: false)
  78. let sortDescriptors = [sortDescriptor]
  79. fetchRequest.sortDescriptors = sortDescriptors
  80. do {
  81. //Get the results
  82. let searchResults = try context.fetch(fetchRequest)
  83. var row : RowGallery
  84. var count = 0
  85. var id: Int
  86. var title: String
  87. for r in searchResults as [NSManagedObject] {
  88. count = count + 1
  89. //Create a new row
  90. row = RowGallery.init(s: "rowGallery\(count)", i: count)
  91. id = r.value(forKey: "id")! as! Int
  92. title = r.value(forKey: "title_\(lang)")! as! String
  93. row.id = id
  94. row.setTitle(text: title)
  95. // Get 4 random images from the album
  96. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  97. // TODO Order random
  98. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  99. //let imgSortDescriptors = [imgSortDescriptor]
  100. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  101. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  102. imgFetchRequest.fetchLimit = 4
  103. do{
  104. let imgSearchResults = try context.fetch(imgFetchRequest)
  105. // Loop images
  106. var imageIdx = 0
  107. for imgR in imgSearchResults as [NSManagedObject]{
  108. let imageId = imgR.value(forKey: "photo")! as! Int
  109. // For each image, I need a new query to fetch from photo entity.
  110. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  111. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  112. singleImgFetchRequest.fetchLimit = 4
  113. do{
  114. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  115. // Loop image
  116. for sImgR in singleImgSearchResults as [NSManagedObject]{
  117. let image = sImgR.value(forKey: "file")! as! String
  118. row.setImage(idx: imageIdx, filename: image)
  119. }
  120. imageIdx = imageIdx + 1
  121. }
  122. catch {
  123. NSLog(":GALLERY:ERROR: Error getting image from photo entity: \(error)")
  124. }
  125. }
  126. } catch {
  127. NSLog(":GALLERY:ERROR: Error getting image for album \(id): \(error)")
  128. }
  129. parent.addArrangedSubview(row)
  130. // Add to the rows array
  131. self.rows.append(row)
  132. }
  133. } catch {
  134. NSLog(":GALLERY:ERROR: Error with request: \(error)")
  135. }
  136. //Always at the end: update scrollview
  137. var h: Int = 0
  138. for view in scrollView.subviews {
  139. //contentRect = contentRect.union(view.frame);
  140. h = h + Int(view.frame.height) + 30 //Why 30?
  141. }
  142. // TODO: Calculate at the end
  143. self.scrollView.contentSize.height = 2500//CGFloat(h);
  144. self.setUpRowsTapRecognizers()
  145. NSLog(":GALLERY:LOG: Finished loading gallery section.")*/
  146. }
  147. func showFestivals(){
  148. NSLog(":LABLANCA:LOG: Showing festivals section.")
  149. self.nfWindow.isHidden = true
  150. // TODO: Get current year
  151. let year = 2016
  152. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  153. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  154. let lang : String = getLanguage()
  155. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  156. // Get info about festivals
  157. let fetchRequest: NSFetchRequest<Festival> = Festival.fetchRequest()
  158. fetchRequest.predicate = NSPredicate(format: "year = %i", year)
  159. do {
  160. // Get info from festivals
  161. let searchResults = try context.fetch(fetchRequest)
  162. let r = searchResults[0]
  163. // Set description
  164. self.fText.text = r.value(forKey: "text_\(lang)") as! String?
  165. } catch {
  166. NSLog(":LABLANCA:ERROR: Error getting festivals info: \(error)")
  167. }
  168. // Get info about the day prices.
  169. let dateFormatter = DateFormatter()
  170. dateFormatter.dateFormat = "yyyy-MM-dd"
  171. dateFormatter.locale = Locale.init(identifier: "en_GB")
  172. // First date of year
  173. var dateString = "\(year)-01-01"
  174. let sDate = dateFormatter.date(from: dateString)
  175. // Last date of year
  176. dateString = "\(year)-12-31"
  177. let eDate = dateFormatter.date(from: dateString)
  178. // Get days
  179. let dayFetchRequest: NSFetchRequest<Festival_day> = Festival_day.fetchRequest()
  180. dayFetchRequest.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", argumentArray: [sDate, eDate])
  181. do {
  182. // Get info from festivals
  183. let daySearchResults = try context.fetch(dayFetchRequest)
  184. var row: RowLablancaDay
  185. var count: Int = 0
  186. for day in daySearchResults as [NSManagedObject]{
  187. let date: NSDate = day.value(forKey: "date")! as! NSDate
  188. let dateString: String = dateFormatter.string(from: date as Date)
  189. //NSLog(":LABLANCA:DEBUG: dateString: \(dateString)")
  190. //let nDay: Int = Int(dateString.subStr(start: 8, end: 9))!
  191. let nDay: String = dateString.subStr(start: 8, end: 9)
  192. //NSLog(":LABLANCA:DEBUG: nDay: \(nDay)")
  193. //NSLog(":LABLANCA:DEBUG: dateString: \(dateString)")
  194. let month: String = dateString.subStr(start: 5, end: 6)
  195. var nMonth: String = "Agosto"
  196. if month == "07"{
  197. nMonth = "Julio"
  198. }
  199. let name: String = day.value(forKey: "name_\(lang)")! as! String
  200. let price: Int = day.value(forKey: "price")! as! Int
  201. row = RowLablancaDay.init(s: "rowLablancaDay\(count)", i: count)
  202. row.name.text = name
  203. row.price.text = "\(price) €"
  204. row.number.text = "\(Int(nDay)!)"
  205. row.month.text = nMonth
  206. // TODO date day and month
  207. fDayList.addArrangedSubview(row)
  208. row.setNeedsLayout()
  209. row.layoutIfNeeded()
  210. NSLog(":LABLANCA:DEBUG: \(nDay) of \(month): Day \(name): \(price)")
  211. // Hide the separator of the last row
  212. if count == daySearchResults.count - 1 {
  213. row.separator.isHidden = true
  214. }
  215. count = count + 1
  216. }
  217. } catch {
  218. NSLog(":LABLANCA:ERROR: Error getting festival days info: \(error)")
  219. }
  220. }
  221. func showNoFestivals(){
  222. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  223. self.fWindow.isHidden = true
  224. // Nothing else to be done.
  225. }
  226. func getLanguage() -> String{
  227. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  228. if(pre == "es" || pre == "en" || pre == "eu"){
  229. return pre
  230. }
  231. else{
  232. return "es"
  233. }
  234. }
  235. }