AlbumViewController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 of the app.
  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. // Album ID
  31. var id: Int = -1
  32. // App delegate
  33. var delegate: AppDelegate?
  34. // Id received from segue.
  35. var passId: Int = -1
  36. func getContext () -> NSManagedObjectContext {
  37. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  38. }
  39. /**
  40. Run when the app loads.
  41. */
  42. override func viewDidLoad() {
  43. NSLog(":ALBUMCONTROLLER:LOG: Init album.")
  44. super.viewDidLoad()
  45. self.loadAlbum(id: id)
  46. // Set button action
  47. barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  48. }
  49. /**
  50. Returns to the main view controller.
  51. */
  52. func back() {
  53. NSLog(":ALBUMCONTROLLER:DEBUG: Back")
  54. self.dismiss(animated: true, completion: nil)
  55. }
  56. /**
  57. Dispose of any resources that can be recreated.
  58. */
  59. override func didReceiveMemoryWarning() {
  60. super.didReceiveMemoryWarning()
  61. }
  62. /**
  63. Loads the photos of the album
  64. */
  65. public func loadAlbum(id: Int){
  66. NSLog(":ALBUMCONTROLLER:DEBUG: Loading album \(id)")
  67. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  68. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  69. let lang : String = getLanguage()
  70. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  71. let fetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  72. fetchRequest.predicate = NSPredicate(format: "album = %i", id)
  73. do {
  74. // Get the photo list
  75. let searchResults = try context.fetch(fetchRequest)
  76. var image: String
  77. NSLog(":GALLERYCONTROLLER:DEBUG: Total photos: \(searchResults.count)")
  78. for i in (0..<searchResults.count) where i % 2 == 0 {
  79. var r = searchResults[i]
  80. var photoId = r.value(forKey: "photo")! as! Int
  81. // Get photo info from Photo entity
  82. let imgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  83. let imgSortDescriptor = NSSortDescriptor(key: "uploaded", ascending: true)
  84. let imgSortDescriptors = [imgSortDescriptor]
  85. imgFetchRequest.sortDescriptors = imgSortDescriptors
  86. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  87. imgFetchRequest.fetchLimit = 1
  88. do{
  89. var imgSearchResults = try context.fetch(imgFetchRequest)
  90. var imgR = imgSearchResults[0]
  91. image = imgR.value(forKey: "file")! as! String
  92. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) left: \(image)")
  93. if i + 1 < searchResults.count {
  94. r = searchResults[i + 1]
  95. photoId = r.value(forKey: "photo")! as! Int
  96. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  97. imgSearchResults = try context.fetch(imgFetchRequest)
  98. imgR = imgSearchResults[0]
  99. image = imgR.value(forKey: "file")! as! String
  100. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: \(image)")
  101. }
  102. else{
  103. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: none")
  104. }
  105. //TODO create row, add images (L+R), add row to container.
  106. //row.setImage(filename: image)
  107. } catch {
  108. print(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  109. }
  110. }
  111. } catch {
  112. print("POST:ERROR: Error with request: \(error)")
  113. }
  114. //Always at the end: update scrollview
  115. // TODOs
  116. //var h: Int = 0
  117. //for view in scrollView.subviews {
  118. //contentRect = contentRect.union(view.frame);
  119. // h = h + Int(view.frame.height) + 30 //Why 30?
  120. //}
  121. //print("POST:DEBUG: Height: \(h)")
  122. // TODO: Calculate at the end
  123. //self.scrollView.contentSize.height = 4500//CGFloat(h);
  124. }
  125. /**
  126. Gets the device language.
  127. :return: Two letter language code of the device.
  128. */
  129. func getLanguage() -> String{
  130. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  131. if(pre == "es" || pre == "en" || pre == "eu"){
  132. return pre
  133. }
  134. else{
  135. return "es"
  136. }
  137. }
  138. }