GalleryView.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 GalleryView: UIView {
  27. @IBOutlet var container: UIView!
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. @IBOutlet weak var section: Section!
  30. var delegate: AppDelegate? = nil
  31. override init(frame: CGRect){
  32. super.init(frame: frame)
  33. }
  34. /**
  35. Run when the view is started.
  36. */
  37. required init?(coder aDecoder: NSCoder) {
  38. super.init(coder: aDecoder)
  39. print("GALLERY:DEBUG: init.")
  40. //Load the contents of the HomeView.xib file.
  41. Bundle.main.loadNibNamed("GalleryView", owner: self, options: nil)
  42. self.addSubview(container)
  43. container.frame = self.bounds
  44. section.setTitle(text: "Gallery")
  45. let parent = section.getContentStack()
  46. // Get viewController from StoryBoard
  47. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  48. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  49. // Show albums
  50. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  51. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  52. self.delegate = appDelegate
  53. let lang : String = getLanguage()
  54. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  55. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  56. let sortDescriptor = NSSortDescriptor(key: "time", ascending: false)
  57. let sortDescriptors = [sortDescriptor]
  58. fetchRequest.sortDescriptors = sortDescriptors
  59. do {
  60. //Get the results
  61. let searchResults = try context.fetch(fetchRequest)
  62. //I like to check the size of the returned results!
  63. print ("GALLERY:DEBUG: Total albums: \(searchResults.count)")
  64. var row : RowGallery
  65. var count = 0
  66. var id: Int
  67. var title: String
  68. for r in searchResults as [NSManagedObject] {
  69. count = count + 1
  70. print("GALLERY:DEBUG: album perm: \(r.value(forKey: "permalink"))")
  71. //Create a new row
  72. row = RowGallery.init(s: "rowGallery\(count)", i: count)
  73. id = r.value(forKey: "id")! as! Int
  74. print("GALLERY:DEBUG: Setting title")
  75. title = r.value(forKey: "title_\(lang)")! as! String
  76. row.setTitle(text: title)
  77. print("GALLERY:DEBUG: 1")
  78. // Get 4 random images from the album
  79. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  80. // TODO Order random
  81. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  82. //let imgSortDescriptors = [imgSortDescriptor]
  83. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  84. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  85. imgFetchRequest.fetchLimit = 4
  86. print("GALLERY:DEBUG: 2")
  87. do{
  88. let imgSearchResults = try context.fetch(imgFetchRequest)
  89. // Loop images
  90. for imgR in imgSearchResults as [NSManagedObject]{
  91. let imageId = imgR.value(forKey: "photo")! as! Int
  92. // For each image, I need a new query to fetch from photo entity.
  93. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  94. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  95. singleImgFetchRequest.fetchLimit = 4
  96. do{
  97. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  98. // Loop images
  99. var imageIdx = 0
  100. for sImgR in singleImgSearchResults as [NSManagedObject]{
  101. let image = sImgR.value(forKey: "file")! as! String
  102. print ("GALLERY:DEBUG: Setting image \(imageIdx): \(image)")
  103. row.setImage(idx: imageIdx, filename: image)
  104. imageIdx = imageIdx + 1
  105. }
  106. }
  107. catch {
  108. print("GALLERY:ERROR: Error getting image from photo entity: \(error)")
  109. }
  110. }
  111. } catch {
  112. print("GALLERY:ERROR: Error getting image for post \(id): \(error)")
  113. }
  114. parent.addArrangedSubview(row)
  115. row.setNeedsLayout()
  116. row.layoutIfNeeded()
  117. // TODO: Do this on the row didLoad method
  118. // Set tap recognizer
  119. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  120. //row.isUserInteractionEnabled = true
  121. //row.addGestureRecognizer(tapRecognizer)
  122. }
  123. } catch {
  124. print("GALLERY:ERROR: Error with request: \(error)")
  125. }
  126. //Always at the end: update scrollview
  127. var h: Int = 0
  128. for view in scrollView.subviews {
  129. //contentRect = contentRect.union(view.frame);
  130. h = h + Int(view.frame.height) + 30 //Why 30?
  131. print("GALLERY:DEBUG: curh: \(h)")
  132. }
  133. // TODO: Calculate at the end
  134. self.scrollView.contentSize.height = 2500//CGFloat(h);
  135. // The view controller
  136. //var viewController: ViewController = self.window?.rootViewController as! ViewController
  137. //let viewController = UIApplication.topViewController() as! ViewController
  138. //viewController.showPost(id: 4)
  139. //print("BLOG:DEBUG: Show post on load.")
  140. //self.delegate?.controller?.showPost(id: 4)
  141. //controller.showPost(id: 4)
  142. print("GALLERY:DEBUG: Finished loading GalleryView")
  143. }
  144. func getLanguage() -> String{
  145. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  146. if(pre == "es" || pre == "en" || pre == "eu"){
  147. return pre
  148. }
  149. else{
  150. return "es"
  151. }
  152. }
  153. }