LablancaView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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!
  28. @IBOutlet weak var nfWindow: UIView!
  29. @IBOutlet weak var fWindow: UIView!
  30. // App delegate
  31. var delegate: AppDelegate? = nil
  32. // Section row list
  33. //var rows: Array<RowGallery> = []
  34. /**
  35. Run when the view is started.
  36. */
  37. override init(frame: CGRect){
  38. super.init(frame: frame)
  39. }
  40. /**
  41. Run when the view is started.
  42. */
  43. required init?(coder aDecoder: NSCoder) {
  44. super.init(coder: aDecoder)
  45. NSLog(":LABLANCA:LOG: Init lablanca section.")
  46. Bundle.main.loadNibNamed("LablancaView", owner: self, options: nil)
  47. self.addSubview(container)
  48. container.frame = self.bounds
  49. //section.setTitle(text: "Gallery")
  50. //let parent = section.getContentStack()
  51. // Get viewController from StoryBoard
  52. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  53. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  54. // Read settings and show the required sections
  55. let defaults = UserDefaults.standard
  56. let festivals: Int = 1 //TODO defaults.integer(forKey: "festivals")
  57. if festivals == 1{
  58. showFestivals()
  59. }
  60. else{
  61. showNoFestivals()
  62. }
  63. // Show albums
  64. /*let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  65. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  66. self.delegate = appDelegate
  67. let lang : String = getLanguage()
  68. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  69. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  70. let sortDescriptor = NSSortDescriptor(key: "time", ascending: false)
  71. let sortDescriptors = [sortDescriptor]
  72. fetchRequest.sortDescriptors = sortDescriptors
  73. do {
  74. //Get the results
  75. let searchResults = try context.fetch(fetchRequest)
  76. var row : RowGallery
  77. var count = 0
  78. var id: Int
  79. var title: String
  80. for r in searchResults as [NSManagedObject] {
  81. count = count + 1
  82. //Create a new row
  83. row = RowGallery.init(s: "rowGallery\(count)", i: count)
  84. id = r.value(forKey: "id")! as! Int
  85. title = r.value(forKey: "title_\(lang)")! as! String
  86. row.id = id
  87. row.setTitle(text: title)
  88. // Get 4 random images from the album
  89. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  90. // TODO Order random
  91. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  92. //let imgSortDescriptors = [imgSortDescriptor]
  93. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  94. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  95. imgFetchRequest.fetchLimit = 4
  96. do{
  97. let imgSearchResults = try context.fetch(imgFetchRequest)
  98. // Loop images
  99. var imageIdx = 0
  100. for imgR in imgSearchResults as [NSManagedObject]{
  101. let imageId = imgR.value(forKey: "photo")! as! Int
  102. // For each image, I need a new query to fetch from photo entity.
  103. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  104. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  105. singleImgFetchRequest.fetchLimit = 4
  106. do{
  107. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  108. // Loop image
  109. for sImgR in singleImgSearchResults as [NSManagedObject]{
  110. let image = sImgR.value(forKey: "file")! as! String
  111. row.setImage(idx: imageIdx, filename: image)
  112. }
  113. imageIdx = imageIdx + 1
  114. }
  115. catch {
  116. NSLog(":GALLERY:ERROR: Error getting image from photo entity: \(error)")
  117. }
  118. }
  119. } catch {
  120. NSLog(":GALLERY:ERROR: Error getting image for album \(id): \(error)")
  121. }
  122. parent.addArrangedSubview(row)
  123. // Add to the rows array
  124. self.rows.append(row)
  125. }
  126. } catch {
  127. NSLog(":GALLERY:ERROR: Error with request: \(error)")
  128. }
  129. //Always at the end: update scrollview
  130. var h: Int = 0
  131. for view in scrollView.subviews {
  132. //contentRect = contentRect.union(view.frame);
  133. h = h + Int(view.frame.height) + 30 //Why 30?
  134. }
  135. // TODO: Calculate at the end
  136. self.scrollView.contentSize.height = 2500//CGFloat(h);
  137. self.setUpRowsTapRecognizers()
  138. NSLog(":GALLERY:LOG: Finished loading gallery section.")*/
  139. }
  140. func showFestivals(){
  141. NSLog(":LABLANCA:LOG: Showing festivals section.")
  142. self.nfWindow.isHidden = true
  143. // TODO: Get current year
  144. let year = 2016
  145. }
  146. func showNoFestivals(){
  147. NSLog(":LABLANCA:LOG: Showing no festivals section.")
  148. self.fWindow.isHidden = true
  149. }
  150. func getLanguage() -> String{
  151. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  152. if(pre == "es" || pre == "en" || pre == "eu"){
  153. return pre
  154. }
  155. else{
  156. return "es"
  157. }
  158. }
  159. }