PostView.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // This file is part of the Gasteizko Margolariak IOS app.
  3. //
  4. // The Gasteizko Margolariak IOS app is free software: you can
  5. // redistribute it and/or modify it under the terms of the
  6. // GNU General Public License as published by the Free Software
  7. // Foundation, either version 3 of the License, or (at your
  8. // option) any later version.
  9. //
  10. // The Gasteizko Margolariak IOS app is distributed in the
  11. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. // without even the implied warranty of MERCHANTABILITY or
  13. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  14. // Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public
  17. // License along with the Gasteizko Margolariak IOS app.
  18. // If not, see <http://www.gnu.org/licenses/>.
  19. import Foundation
  20. import CoreData
  21. import UIKit
  22. /**
  23. Class to handle the post view.
  24. */
  25. class PostView: UIView {
  26. @IBOutlet var container: UIView!
  27. //The main scroll view.
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. @IBOutlet weak var title: UILabel!
  30. @IBOutlet weak var image: UIImageView!
  31. @IBOutlet weak var text: UILabel!
  32. @IBOutlet weak var date: UILabel!
  33. @IBOutlet weak var commentCount: UILabel!
  34. @IBOutlet weak var commentList: UIStackView!
  35. @IBOutlet weak var commentUser: UITextField!
  36. @IBOutlet weak var commentContent: UITextView!
  37. // Post id. Must be set pre-segue.
  38. var id: Int = -1
  39. override init(frame: CGRect){
  40. print("POST:LOG: Init (cgrect). Id is \(self.id)")
  41. super.init(frame: frame)
  42. //Load the contents of the PostView.xib file.
  43. Bundle.main.loadNibNamed("PostView", owner: self, options: nil)
  44. self.addSubview(container)
  45. container.frame = self.bounds
  46. }
  47. /**
  48. Run when the view is started.
  49. */
  50. required init?(coder aDecoder: NSCoder) {
  51. print("POST:LOG: Init (coder). Id is: \(self.id)")
  52. super.init(coder: aDecoder)
  53. //Load the contents of the PostView.xib file.
  54. Bundle.main.loadNibNamed("PostView", owner: self, options: nil)
  55. self.addSubview(container)
  56. container.frame = self.bounds
  57. }
  58. public func loadPost(id: Int) -> Bool{
  59. print("POST: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. title.text = " \(sTitle)"
  79. text.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. return true
  115. }
  116. func getLanguage() -> String{
  117. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  118. if(pre == "es" || pre == "en" || pre == "eu"){
  119. return pre
  120. }
  121. else{
  122. return "es"
  123. }
  124. }
  125. }