PostViewController.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 PostViewController: 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. var id: Int = -1
  32. var delegate: AppDelegate?
  33. //Each of the sections of the app.
  34. var passId: Int = -1
  35. func getContext () -> NSManagedObjectContext {
  36. //let appDelegate = UIApplication.shared.delegate as! AppDelegate
  37. //return appDelegate.persistentContainer.viewContext
  38. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  39. }
  40. /**
  41. Run when the app loads.
  42. */
  43. override func viewDidLoad() {
  44. print("POSTCONTROLLER:LOG: viewDidLoad()")
  45. super.viewDidLoad()
  46. self.loadPost(id: id)
  47. }
  48. //TODO implement all the logic for post loading here, including iboutlts
  49. func back() {
  50. self.dismiss(animated: true, completion: nil)
  51. }
  52. /**
  53. Dispose of any resources that can be recreated.
  54. */
  55. override func didReceiveMemoryWarning() {
  56. super.didReceiveMemoryWarning()
  57. }
  58. public func loadPost(id: Int){
  59. print("POSTCONTROLLER:DEBUG: Loading post \(id)")
  60. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  61. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  62. let lang : String = getLanguage()
  63. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  64. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  65. fetchRequest.predicate = NSPredicate(format: "id = %i", id)
  66. do {
  67. //go get the results
  68. let searchResults = try context.fetch(fetchRequest)
  69. var count = 0
  70. var sTitle: String
  71. var sText: String
  72. var image: String
  73. //You need to convert to NSManagedObject to use 'for' loops
  74. for r in searchResults as [NSManagedObject] {
  75. count = count + 1
  76. sTitle = r.value(forKey: "title_\(lang)")! as! String
  77. sText = r.value(forKey: "text_\(lang)")! as! String
  78. postTitle.text = " \(sTitle)"
  79. postText.text = sText.stripHtml()
  80. // Get main image
  81. image = ""
  82. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  83. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  84. let imgSortDescriptors = [imgSortDescriptor]
  85. imgFetchRequest.sortDescriptors = imgSortDescriptors
  86. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  87. imgFetchRequest.fetchLimit = 1
  88. //TODO get more images
  89. do{
  90. let imgSearchResults = try context.fetch(imgFetchRequest)
  91. for imgR in imgSearchResults as [NSManagedObject]{
  92. image = imgR.value(forKey: "image")! as! String
  93. print ("POST:LOG: Image: \(image)")
  94. //TODO set image
  95. //row.setImage(filename: image)
  96. }
  97. } catch {
  98. print("POST:ERROR: Error getting image for post \(id): \(error)")
  99. }
  100. }
  101. } catch {
  102. print("POST:ERROR: Error with request: \(error)")
  103. }
  104. //Always at the end: update scrollview
  105. // TODOs
  106. //var h: Int = 0
  107. //for view in scrollView.subviews {
  108. //contentRect = contentRect.union(view.frame);
  109. // h = h + Int(view.frame.height) + 30 //Why 30?
  110. //}
  111. //print("POST:DEBUG: Height: \(h)")
  112. // TODO: Calculate at the end
  113. //self.scrollView.contentSize.height = 4500//CGFloat(h);
  114. }
  115. func getLanguage() -> String{
  116. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  117. if(pre == "es" || pre == "en" || pre == "eu"){
  118. return pre
  119. }
  120. else{
  121. return "es"
  122. }
  123. }
  124. }