GalleryView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 gallery view.
  25. */
  26. class GalleryView: UIView {
  27. // Outlets
  28. @IBOutlet var container: UIView!
  29. @IBOutlet weak var scrollView: UIScrollView!
  30. @IBOutlet weak var section: Section!
  31. // App delegate
  32. var delegate: AppDelegate? = nil
  33. // Section row list
  34. var rows: Array<RowGallery> = []
  35. /**
  36. Default constructor for the interface builder.
  37. */
  38. override init(frame: CGRect){
  39. super.init(frame: frame)
  40. }
  41. /**
  42. Run when the view is started.
  43. */
  44. required init?(coder aDecoder: NSCoder) {
  45. super.init(coder: aDecoder)
  46. //Load the contents of the HomeView.xib file.
  47. Bundle.main.loadNibNamed("GalleryView", owner: self, options: nil)
  48. self.addSubview(container)
  49. container.frame = self.bounds
  50. section.setTitle(text: "Gallery")
  51. let parent = section.getContentStack()
  52. // Get viewController from StoryBoard
  53. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  54. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  55. // Show albums
  56. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  57. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  58. self.delegate = appDelegate
  59. let lang : String = getLanguage()
  60. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  61. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  62. let sortDescriptor = NSSortDescriptor(key: "id", ascending: false)
  63. let sortDescriptors = [sortDescriptor]
  64. fetchRequest.sortDescriptors = sortDescriptors
  65. do {
  66. //Get the results
  67. let searchResults = try context.fetch(fetchRequest)
  68. var row : RowGallery
  69. var count = 0
  70. var id: Int
  71. var title: String
  72. for r in searchResults as [NSManagedObject] {
  73. count = count + 1
  74. //Create a new row
  75. row = RowGallery.init(s: "rowGallery\(count)", i: count)
  76. id = r.value(forKey: "id")! as! Int
  77. title = r.value(forKey: "title_\(lang)")! as! String
  78. row.id = id
  79. row.setTitle(text: title)
  80. // Get 4 random images from the album
  81. let imgFetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  82. // TODO Order random
  83. //let imgSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
  84. //let imgSortDescriptors = [imgSortDescriptor]
  85. //simgFetchRequest.sortDescriptors = imgSortDescriptors
  86. imgFetchRequest.predicate = NSPredicate(format: "album == %i", id)
  87. imgFetchRequest.fetchLimit = 4
  88. do{
  89. let imgSearchResults = try context.fetch(imgFetchRequest)
  90. // Loop images
  91. var imageIdx = 0
  92. for imgR in imgSearchResults as [NSManagedObject]{
  93. let imageId = imgR.value(forKey: "photo")! as! Int
  94. // For each image, I need a new query to fetch from photo entity.
  95. let singleImgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  96. singleImgFetchRequest.predicate = NSPredicate(format: "id == %i", imageId)
  97. singleImgFetchRequest.fetchLimit = 4
  98. do{
  99. let singleImgSearchResults = try context.fetch(singleImgFetchRequest)
  100. // Loop image
  101. for sImgR in singleImgSearchResults as [NSManagedObject]{
  102. let image = sImgR.value(forKey: "file")! as! String
  103. row.setImage(idx: imageIdx, filename: image)
  104. }
  105. imageIdx = imageIdx + 1
  106. }
  107. catch {
  108. NSLog(":GALLERY:ERROR: Error getting image from photo entity: \(error)")
  109. }
  110. }
  111. } catch {
  112. NSLog(":GALLERY:ERROR: Error getting image for album \(id): \(error)")
  113. }
  114. parent.addArrangedSubview(row)
  115. // Add to the rows array
  116. self.rows.append(row)
  117. }
  118. }
  119. catch {
  120. NSLog(":GALLERY:ERROR: Error with request: \(error)")
  121. }
  122. self.setUpRowsTapRecognizers()
  123. }
  124. /**
  125. Opens an album.
  126. */
  127. @objc func openAlbum(_ sender:UITapGestureRecognizer? = nil){
  128. let id = (sender?.view as! RowGallery).id
  129. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  130. delegate.controller?.showAlbum(id: id)
  131. }
  132. /**
  133. Gets the device language. The only recognized languages are Spanish, English and Basque.
  134. If the device has another language, Spanish will be selected by default.
  135. :return: Two-letter language code.
  136. */
  137. func getLanguage() -> String{
  138. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  139. if(pre == "es" || pre == "en" || pre == "eu"){
  140. return pre
  141. }
  142. else{
  143. return "es"
  144. }
  145. }
  146. /**
  147. Handles the click events to open albums.
  148. */
  149. func setUpRowsTapRecognizers(){
  150. for row in self.rows{
  151. let tapRecognizer = UITapGestureRecognizer(target: row, action: #selector(openAlbum(_:)))
  152. row.isUserInteractionEnabled = true
  153. container.addGestureRecognizer(tapRecognizer)
  154. }
  155. }
  156. }