AlbumViewController.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 UIKit
  21. import CoreData
  22. /**
  23. The view controller to show albums.
  24. */
  25. class AlbumViewController: UIViewController, UIGestureRecognizerDelegate {
  26. @IBOutlet weak var barButton: UIButton!
  27. @IBOutlet weak var barTitle: UILabel!
  28. @IBOutlet weak var albumTitle: UILabel!
  29. @IBOutlet weak var albumContainer: UIStackView!
  30. @IBOutlet weak var albumView: UIView!
  31. // Album ID
  32. var id: Int = -1
  33. // App delegate
  34. var delegate: AppDelegate?
  35. // Id received from segue.
  36. var passId: Int = -1
  37. var passAlbum: Int = -1
  38. /**
  39. Gets the app context.
  40. :return: Application context.
  41. */
  42. func getContext () -> NSManagedObjectContext {
  43. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  44. }
  45. /**
  46. Run when the app loads.
  47. */
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. self.loadAlbum(id: id)
  51. // Set button action
  52. barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  53. // Set this controller in the delegate
  54. self.delegate = UIApplication.shared.delegate as? AppDelegate
  55. self.delegate?.albumController = self
  56. // Get album title
  57. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  58. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  59. let lang : String = getLanguage()
  60. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  61. let fetchRequest: NSFetchRequest<Album> = Album.fetchRequest()
  62. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  63. do {
  64. let results = try context.fetch(fetchRequest)
  65. let r = results[0]
  66. let title: String = r.value(forKey: "title_\(lang)")! as! String
  67. self.albumTitle.text = " \(title.decode().stripHtml())"
  68. }
  69. catch {
  70. NSLog(":GALLERYCONTROLLER:ERROR: Error getting album info: \(error)")
  71. }
  72. }
  73. /**
  74. Returns to the main view controller.
  75. */
  76. @objc func back() {
  77. self.dismiss(animated: true, completion: nil)
  78. }
  79. /**
  80. Dispose of any resources that can be recreated.
  81. */
  82. override func didReceiveMemoryWarning() {
  83. super.didReceiveMemoryWarning()
  84. }
  85. /**
  86. Loads the photos of the album
  87. */
  88. public func loadAlbum(id: Int){
  89. var row : RowAlbum
  90. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  91. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  92. let lang : String = getLanguage()
  93. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  94. let fetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  95. fetchRequest.predicate = NSPredicate(format: "album = %i", id)
  96. var rowcount = 0
  97. do {
  98. // Get the photo list
  99. let searchResults = try context.fetch(fetchRequest)
  100. var image: String
  101. for i in (0..<searchResults.count) where i % 2 == 0 {
  102. var r = searchResults[i]
  103. var photoId = r.value(forKey: "photo")! as! Int
  104. var leftId: Int = -1
  105. var rightId: Int = -1
  106. row = RowAlbum.init(s: "rowAlbum\(i)", i: i)
  107. row.id = id
  108. // Get photo info from Photo entity
  109. let imgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  110. let imgSortDescriptor = NSSortDescriptor(key: "uploaded", ascending: true)
  111. let imgSortDescriptors = [imgSortDescriptor]
  112. imgFetchRequest.sortDescriptors = imgSortDescriptors
  113. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  114. imgFetchRequest.fetchLimit = 1
  115. do{
  116. var imgSearchResults = try context.fetch(imgFetchRequest)
  117. var imgR = imgSearchResults[0]
  118. image = imgR.value(forKey: "file")! as! String
  119. leftId = imgR.value(forKey: "id")! as! Int
  120. row.setImage(idx: 0, filename: image)
  121. if i + 1 < searchResults.count {
  122. r = searchResults[i + 1]
  123. photoId = r.value(forKey: "photo")! as! Int
  124. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  125. imgSearchResults = try context.fetch(imgFetchRequest)
  126. imgR = imgSearchResults[0]
  127. image = imgR.value(forKey: "file")! as! String
  128. rightId = imgR.value(forKey: "id")! as! Int
  129. row.setImage(idx: 1, filename: image)
  130. }
  131. }
  132. catch {
  133. NSLog(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  134. }
  135. row.setIds(left: leftId, right: rightId)
  136. albumContainer.addArrangedSubview(row)
  137. rowcount = rowcount + 1
  138. }
  139. }
  140. catch {
  141. NSLog(":GALLERYCONTROLLER:ERROR: Error with request: \(error)")
  142. }
  143. }
  144. /**
  145. Run before performing a segue.
  146. Assigns id if neccessary.
  147. :param: segue The segue to perform.
  148. :sender: The calling view.
  149. */
  150. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  151. if segue.identifier == "SeguePhotoAlbum"{
  152. (segue.destination as! PhotoViewController).albumId = passAlbum
  153. (segue.destination as! PhotoViewController).photoId = passId
  154. }
  155. }
  156. /**
  157. Gets the device language. The only recognized languages are Spanish, English and Basque.
  158. If the device has another language, Spanish will be selected by default.
  159. :return: Two-letter language code.
  160. */
  161. func getLanguage() -> String{
  162. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  163. if(pre == "es" || pre == "en" || pre == "eu"){
  164. return pre
  165. }
  166. else{
  167. return "es"
  168. }
  169. }
  170. /**
  171. Shows a photo.
  172. :param: album The album id.
  173. :param: photo The photo id.
  174. */
  175. func showPhoto(album: Int, photo: Int){
  176. self.passAlbum = album
  177. self.passId = photo
  178. performSegue(withIdentifier: "SeguePhotoAlbum", sender: nil)
  179. }
  180. }