GalleryView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // Get 4 random images from the album
  78. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  79. // TODO Order random
  80. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  81. //let imgSortDescriptors = [imgSortDescriptor]
  82. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  83. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  84. imgFetchRequest.fetchLimit = 4
  85. do{
  86. let imgSearchResults = try context.fetch(imgFetchRequest)
  87. // Loop images
  88. for imgR in imgSearchResults as [NSManagedObject]{
  89. let imageId = imgR.value(forKey: "id")! as! Int
  90. // For each image, I need a new query to fetch from photo entity.
  91. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  92. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  93. singleImgFetchRequest.fetchLimit = 1
  94. do{
  95. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  96. // Loop images
  97. var imageIdx = 0
  98. for sImgR in singleImgSearchResults as [NSManagedObject]{
  99. let image = sImgR.value(forKey: "file")! as! String
  100. print ("GALLERY:DEBUG: Setting image: \(image)")
  101. row.setImage(idx: imageIdx, filename: image)
  102. imageIdx = imageIdx + 1
  103. }
  104. }
  105. catch {
  106. print("GALLERY:ERROR: Error getting image from photo entity: \(error)")
  107. }
  108. }
  109. } catch {
  110. print("GALLERY:ERROR: Error getting image for post \(id): \(error)")
  111. }
  112. parent.addArrangedSubview(row)
  113. row.setNeedsLayout()
  114. row.layoutIfNeeded()
  115. // TODO: Do this on the row didLoad method
  116. // Set tap recognizer
  117. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  118. //row.isUserInteractionEnabled = true
  119. //row.addGestureRecognizer(tapRecognizer)
  120. }
  121. } catch {
  122. print("GALLERY:ERROR: Error with request: \(error)")
  123. }
  124. //Always at the end: update scrollview
  125. var h: Int = 0
  126. for view in scrollView.subviews {
  127. //contentRect = contentRect.union(view.frame);
  128. h = h + Int(view.frame.height) + 30 //Why 30?
  129. print("GALLERY:DEBUG: curh: \(h)")
  130. }
  131. // TODO: Calculate at the end
  132. self.scrollView.contentSize.height = 2500//CGFloat(h);
  133. // The view controller
  134. //var viewController: ViewController = self.window?.rootViewController as! ViewController
  135. //let viewController = UIApplication.topViewController() as! ViewController
  136. //viewController.showPost(id: 4)
  137. //print("BLOG:DEBUG: Show post on load.")
  138. //self.delegate?.controller?.showPost(id: 4)
  139. //controller.showPost(id: 4)
  140. print("GALLERY:DEBUG: Finished loading GalleryView")
  141. }
  142. func getLanguage() -> String{
  143. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  144. if(pre == "es" || pre == "en" || pre == "eu"){
  145. return pre
  146. }
  147. else{
  148. return "es"
  149. }
  150. }
  151. }