AlbumViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. row = RowAlbum.init(s: "rowAlbum\(i)", i: i)
  86. // Get photo info from Photo entity
  87. let imgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  88. let imgSortDescriptor = NSSortDescriptor(key: "uploaded", ascending: true)
  89. let imgSortDescriptors = [imgSortDescriptor]
  90. imgFetchRequest.sortDescriptors = imgSortDescriptors
  91. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  92. imgFetchRequest.fetchLimit = 1
  93. do{
  94. var imgSearchResults = try context.fetch(imgFetchRequest)
  95. var imgR = imgSearchResults[0]
  96. image = imgR.value(forKey: "file")! as! String
  97. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) left: \(image)")
  98. row.setImage(idx: 0, filename: image)
  99. if i + 1 < searchResults.count {
  100. r = searchResults[i + 1]
  101. photoId = r.value(forKey: "photo")! as! Int
  102. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  103. imgSearchResults = try context.fetch(imgFetchRequest)
  104. imgR = imgSearchResults[0]
  105. image = imgR.value(forKey: "file")! as! String
  106. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: \(image)")
  107. row.setImage(idx: 1, filename: image)
  108. }
  109. else{
  110. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: none")
  111. }
  112. //TODO create row, add images (L+R), add row to container.
  113. //row.setImage(filename: image)
  114. } catch {
  115. NSLog(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  116. }
  117. NSLog(":GALLERYCONTROLLER:DEBUG: Adding row: height: \(row.frame.height)")
  118. albumContainer.addArrangedSubview(row)
  119. rowcount = rowcount + 1
  120. }
  121. } catch {
  122. NSLog(":GALLERYCONTROLLER:ERROR: Error with request: \(error)")
  123. }
  124. }
  125. /**
  126. Gets the device language. The only recognized languages are Spanish, English and Basque.
  127. If the device has another language, Spanish will be selected by default.
  128. :return: Two-letter language code.
  129. */
  130. func getLanguage() -> String{
  131. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  132. if(pre == "es" || pre == "en" || pre == "eu"){
  133. return pre
  134. }
  135. else{
  136. return "es"
  137. }
  138. }
  139. }