PhotoViewController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 PhotoViewController: UIViewController, UIGestureRecognizerDelegate {
  26. //@IBOutlet weak var barTitle: UILabel!
  27. //@IBOutlet weak var barButton: UIButton!
  28. //@IBOutlet weak var postText: UILabel!
  29. //@IBOutlet weak var postTitle: UILabel!
  30. //@IBOutlet weak var postImage: UIImageView!
  31. @IBOutlet weak var barButton: UIButton!
  32. @IBOutlet weak var barTitle: UILabel!
  33. @IBOutlet weak var photoTitle: UILabel!
  34. @IBOutlet weak var photoImage: UIImageView!
  35. @IBOutlet weak var photoDate: UILabel!
  36. @IBOutlet weak var btPrev: UIButton!
  37. @IBOutlet weak var btNext: UIButton!
  38. var albumId: Int = -1
  39. var photoId: Int = -1
  40. var photoIds: [Int] = []
  41. var passPhotoId: Int = -1
  42. var passAlbumId: Int = -1
  43. var delegate: AppDelegate?
  44. /**
  45. Gets the application context.
  46. :return: The application context.
  47. */
  48. func getContext () -> NSManagedObjectContext {
  49. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  50. }
  51. /**
  52. Run when the app loads.
  53. */
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. self.loadPhoto(id: photoId)
  57. // TODO populate photo array
  58. // Set button action
  59. // TODO barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  60. }
  61. /**
  62. Dismisses the controller.
  63. */
  64. func back() {
  65. NSLog(":PHOTOCONTROLLER:DEBUG: Back")
  66. self.dismiss(animated: true, completion: nil)
  67. }
  68. /**
  69. Dispose of any resources that can be recreated.
  70. */
  71. override func didReceiveMemoryWarning() {
  72. super.didReceiveMemoryWarning()
  73. }
  74. /**
  75. Loads a photo in the viewer.
  76. :param: id
  77. */
  78. public func loadPhoto(id: Int){
  79. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  80. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  81. let lang : String = getLanguage()
  82. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  83. let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  84. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  85. do {
  86. let searchResults = try context.fetch(fetchRequest)
  87. var sTitle: String
  88. var sText: String
  89. var image: String
  90. for r in searchResults as [NSManagedObject] {
  91. sTitle = r.value(forKey: "title_\(lang)")! as! String
  92. // TODO photoTitle.text = " \(sTitle.decode().stripHtml())"
  93. // TODO date
  94. // TODO set photo
  95. /*
  96. // Get main image
  97. image = ""
  98. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  99. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  100. let imgSortDescriptors = [imgSortDescriptor]
  101. imgFetchRequest.sortDescriptors = imgSortDescriptors
  102. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  103. imgFetchRequest.fetchLimit = 1
  104. //TODO get more images
  105. do{
  106. let imgSearchResults = try context.fetch(imgFetchRequest)
  107. for imgR in imgSearchResults as [NSManagedObject]{
  108. image = imgR.value(forKey: "image")! as! String
  109. if image == ""{
  110. self.postImage.isHidden = true
  111. }
  112. else{
  113. let path = "img/actividades/thumb/\(image)"
  114. self.postImage.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
  115. }
  116. }
  117. } catch {
  118. NSLog(":POST:ERROR: Error getting image for post \(id): \(error)")
  119. }*/
  120. }
  121. } catch {
  122. NSLog(":PHOTO:ERROR: Error setting up photo \(id): \(error)")
  123. }
  124. }
  125. // TODO Buttons
  126. /**
  127. Gets the device language. The only recognized languages are Spanish, English and Basque.
  128. If the device has another language, Spanish will be selected by default.
  129. :return: Two-letter language code.
  130. */
  131. func getLanguage() -> String{
  132. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  133. if(pre == "es" || pre == "en" || pre == "eu"){
  134. return pre
  135. }
  136. else{
  137. return "es"
  138. }
  139. }
  140. }