GalleryView.swift 5.5 KB

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