AlbumViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(":GALLERYCONTROLLER:LOG: Init album.")
  44. super.viewDidLoad()
  45. self.loadGallery(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(":GALLERYCONTROLLER: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(":GALLERYCONTROLLER:DEBUG: Loading post \(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<Post> = Post.fetchRequest()
  72. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  73. do {
  74. //go get the results
  75. let searchResults = try context.fetch(fetchRequest)
  76. var count = 0
  77. var sTitle: String
  78. var sText: String
  79. var image: String
  80. //You need to convert to NSManagedObject to use 'for' loops
  81. for r in searchResults as [NSManagedObject] {
  82. count = count + 1
  83. sTitle = r.value(forKey: "title_\(lang)")! as! String
  84. sText = r.value(forKey: "text_\(lang)")! as! String
  85. postTitle.text = " \(sTitle)"
  86. postText.text = sText.stripHtml()
  87. // Get main image
  88. image = ""
  89. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  90. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  91. let imgSortDescriptors = [imgSortDescriptor]
  92. imgFetchRequest.sortDescriptors = imgSortDescriptors
  93. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  94. imgFetchRequest.fetchLimit = 1
  95. //TODO get more images
  96. do{
  97. let imgSearchResults = try context.fetch(imgFetchRequest)
  98. for imgR in imgSearchResults as [NSManagedObject]{
  99. image = imgR.value(forKey: "image")! as! String
  100. print ("POST:LOG: Image: \(image)")
  101. //TODO set image
  102. //row.setImage(filename: image)
  103. }
  104. } catch {
  105. print("POST:ERROR: Error getting image for post \(id): \(error)")
  106. }
  107. }
  108. } catch {
  109. print("POST:ERROR: Error with request: \(error)")
  110. }
  111. */
  112. //Always at the end: update scrollview
  113. // TODOs
  114. //var h: Int = 0
  115. //for view in scrollView.subviews {
  116. //contentRect = contentRect.union(view.frame);
  117. // h = h + Int(view.frame.height) + 30 //Why 30?
  118. //}
  119. //print("POST:DEBUG: Height: \(h)")
  120. // TODO: Calculate at the end
  121. //self.scrollView.contentSize.height = 4500//CGFloat(h);
  122. }
  123. /**
  124. Gets the device language.
  125. :return: Two letter language code of the device.
  126. */
  127. func getLanguage() -> String{
  128. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  129. if(pre == "es" || pre == "en" || pre == "eu"){
  130. return pre
  131. }
  132. else{
  133. return "es"
  134. }
  135. }
  136. }