PhotoViewController.swift 4.6 KB

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