PostView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. //The main scroll view.
  27. @IBOutlet weak var scrollView: UIScrollView!
  28. @IBOutlet weak var title: UILabel!
  29. @IBOutlet weak var image: UIImageView!
  30. @IBOutlet weak var text: UILabel!
  31. @IBOutlet weak var imageExtra: UIStackView!
  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. override init(frame: CGRect){
  38. super.init(frame: frame)
  39. }
  40. /**
  41. Run when the view is started.
  42. */
  43. required init?(coder aDecoder: NSCoder) {
  44. super.init(coder: aDecoder)
  45. //Load the contents of the HomeView.xib file.
  46. Bundle.main.loadNibNamed("BlogView", owner: self, options: nil)
  47. //self.addSubview(container)
  48. //container.frame = self.bounds
  49. }
  50. func loadPost(id : Int){
  51. // Show posts
  52. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  53. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  54. let lang : String = getLanguage()
  55. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  56. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  57. fetchRequest.predicate = NSPredicate(format: "post == %i", id)
  58. do {
  59. //go get the results
  60. let searchResults = try context.fetch(fetchRequest)
  61. //I like to check the size of the returned results!
  62. print ("Post: \(searchResults.count)")
  63. var count = 0
  64. var sTitle: String
  65. var sText: String
  66. var image: String
  67. //You need to convert to NSManagedObject to use 'for' loops
  68. for r in searchResults as [NSManagedObject] {
  69. count = count + 1
  70. //get the Key Value pairs (although there may be a better way to do that...
  71. print("Perm: \(r.value(forKey: "permalink"))")
  72. sTitle = r.value(forKey: "title_\(lang)")! as! String
  73. sText = r.value(forKey: "text_\(lang)")! as! String
  74. title.text = sTitle
  75. text.text = sText
  76. // Get main image
  77. image = ""
  78. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  79. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  80. let imgSortDescriptors = [imgSortDescriptor]
  81. imgFetchRequest.sortDescriptors = imgSortDescriptors
  82. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  83. imgFetchRequest.fetchLimit = 1
  84. //TODO get more images
  85. do{
  86. let imgSearchResults = try context.fetch(imgFetchRequest)
  87. for imgR in imgSearchResults as [NSManagedObject]{
  88. image = imgR.value(forKey: "image")! as! String
  89. print ("IMAGE: \(image)")
  90. //TODO set image
  91. //row.setImage(filename: image)
  92. }
  93. } catch {
  94. print("Error getting image for post \(id): \(error)")
  95. }
  96. }
  97. } catch {
  98. print("Error with request: \(error)")
  99. }
  100. //Always at the end: update scrollview
  101. var h: Int = 0
  102. for view in scrollView.subviews {
  103. //contentRect = contentRect.union(view.frame);
  104. h = h + Int(view.frame.height) + 30 //Why 30?
  105. print("Blog curh: \(h)")
  106. }
  107. // TODO: Calculate at the end
  108. self.scrollView.contentSize.height = 2500//CGFloat(h);
  109. }
  110. func getLanguage() -> String{
  111. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  112. if(pre == "es" || pre == "en" || pre == "eu"){
  113. return pre
  114. }
  115. else{
  116. return "es"
  117. }
  118. }
  119. }