PostView.swift 4.6 KB

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