LablancaView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. let fetchRequest: NSFetchRequest<Festival> = Festival.fetchRequest()
  157. fetchRequest.predicate = NSPredicate(format: "year = %i", year)
  158. do {
  159. // Get info from festivals
  160. let searchResults = try context.fetch(fetchRequest)
  161. let r = searchResults[0]
  162. // Set description
  163. self.fText.text = r.value(forKey: "text_\(lang)") as! String?
  164. /*// Get photo info from Photo entity
  165. do{
  166. var imgSearchResults = try context.fetch(imgFetchRequest)
  167. var imgR = imgSearchResults[0]
  168. image = imgR.value(forKey: "file")! as! String
  169. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) left: \(image)")
  170. row.setImage(idx: 0, filename: image)
  171. if i + 1 < searchResults.count {
  172. r = searchResults[i + 1]
  173. photoId = r.value(forKey: "photo")! as! Int
  174. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  175. imgSearchResults = try context.fetch(imgFetchRequest)
  176. imgR = imgSearchResults[0]
  177. image = imgR.value(forKey: "file")! as! String
  178. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: \(image)")
  179. row.setImage(idx: 1, filename: image)
  180. }
  181. else{
  182. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: none")
  183. }
  184. //TODO create row, add images (L+R), add row to container.
  185. //row.setImage(filename: image)
  186. } catch {
  187. print(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  188. }
  189. NSLog(":GALLERYCONTROLLER:DEBUG: Adding row: height: \(row.frame.height)")
  190. albumContainer.addArrangedSubview(row)
  191. */
  192. } catch {
  193. NSLog(":LABLANCA:ERROR: Error with request: \(error)")
  194. }
  195. }
  196. func showNoFestivals(){
  197. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  198. self.fWindow.isHidden = true
  199. // Nothing else to be done.
  200. }
  201. func getLanguage() -> String{
  202. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  203. if(pre == "es" || pre == "en" || pre == "eu"){
  204. return pre
  205. }
  206. else{
  207. return "es"
  208. }
  209. }
  210. }