AlbumViewController.swift 5.0 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 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. @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. /**
  38. Gets the app context.
  39. :return: Application context.
  40. */
  41. func getContext () -> NSManagedObjectContext {
  42. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  43. }
  44. /**
  45. Run when the app loads.
  46. */
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. self.loadAlbum(id: id)
  50. // Set button action
  51. barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  52. }
  53. /**
  54. Returns to the main view controller.
  55. */
  56. func back() {
  57. self.dismiss(animated: true, completion: nil)
  58. }
  59. /**
  60. Dispose of any resources that can be recreated.
  61. */
  62. override func didReceiveMemoryWarning() {
  63. super.didReceiveMemoryWarning()
  64. }
  65. /**
  66. Loads the photos of the album
  67. */
  68. public func loadAlbum(id: Int){
  69. var row : RowAlbum
  70. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  71. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  72. let lang : String = getLanguage()
  73. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  74. let fetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  75. fetchRequest.predicate = NSPredicate(format: "album = %i", id)
  76. var rowcount = 0
  77. do {
  78. // Get the photo list
  79. let searchResults = try context.fetch(fetchRequest)
  80. var image: String
  81. NSLog(":GALLERYCONTROLLER:DEBUG: Total photos: \(searchResults.count)")
  82. for i in (0..<searchResults.count) where i % 2 == 0 {
  83. var r = searchResults[i]
  84. var photoId = r.value(forKey: "photo")! as! Int
  85. var leftId: Int = -1
  86. var rightId: Int = -1
  87. row = RowAlbum.init(s: "rowAlbum\(i)", i: i)
  88. row.id = id
  89. // Get photo info from Photo entity
  90. let imgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  91. let imgSortDescriptor = NSSortDescriptor(key: "uploaded", ascending: true)
  92. let imgSortDescriptors = [imgSortDescriptor]
  93. imgFetchRequest.sortDescriptors = imgSortDescriptors
  94. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  95. imgFetchRequest.fetchLimit = 1
  96. do{
  97. var imgSearchResults = try context.fetch(imgFetchRequest)
  98. var imgR = imgSearchResults[0]
  99. image = imgR.value(forKey: "file")! as! String
  100. leftId = imgR.value(forKey: "id")! as! Int
  101. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) left: \(image)")
  102. row.setImage(idx: 0, filename: image)
  103. if i + 1 < searchResults.count {
  104. r = searchResults[i + 1]
  105. photoId = r.value(forKey: "photo")! as! Int
  106. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  107. imgSearchResults = try context.fetch(imgFetchRequest)
  108. imgR = imgSearchResults[0]
  109. image = imgR.value(forKey: "file")! as! String
  110. rightId = imgR.value(forKey: "id")! as! Int
  111. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: \(image)")
  112. row.setImage(idx: 1, filename: image)
  113. }
  114. else{
  115. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: none")
  116. }
  117. //TODO create row, add images (L+R), add row to container.
  118. //row.setImage(filename: image)
  119. } catch {
  120. NSLog(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  121. }
  122. row.setIds(left: leftId, right: rightId)
  123. albumContainer.addArrangedSubview(row)
  124. rowcount = rowcount + 1
  125. }
  126. } catch {
  127. NSLog(":GALLERYCONTROLLER:ERROR: Error with request: \(error)")
  128. }
  129. }
  130. /**
  131. Gets the device language. The only recognized languages are Spanish, English and Basque.
  132. If the device has another language, Spanish will be selected by default.
  133. :return: Two-letter language code.
  134. */
  135. func getLanguage() -> String{
  136. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  137. if(pre == "es" || pre == "en" || pre == "eu"){
  138. return pre
  139. }
  140. else{
  141. return "es"
  142. }
  143. }
  144. }