BlogView.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle the home view.
  25. */
  26. class BlogView: UIView {
  27. //var window:UIWindow?
  28. //The main scroll view.
  29. @IBOutlet weak var scrollView: UIScrollView!
  30. //The container of the view.
  31. @IBOutlet var container: UIView!
  32. //Each of the sections of the view.
  33. @IBOutlet weak var section: Section!
  34. var controller : ViewController? = nil
  35. override init(frame: CGRect){
  36. super.init(frame: frame)
  37. }
  38. /**
  39. Run when the view is started.
  40. */
  41. required init?(coder aDecoder: NSCoder) {
  42. super.init(coder: aDecoder)
  43. print("BLOG:DEBUG: init.")
  44. //Load the contents of the HomeView.xib file.
  45. Bundle.main.loadNibNamed("BlogView", owner: self, options: nil)
  46. self.addSubview(container)
  47. container.frame = self.bounds
  48. section.setTitle(text: "Blog")
  49. let parent = section.getContentStack()
  50. // Show posts
  51. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  52. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  53. let lang : String = getLanguage()
  54. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  55. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  56. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  57. let sortDescriptors = [sortDescriptor]
  58. fetchRequest.sortDescriptors = sortDescriptors
  59. do {
  60. //go get the results
  61. let searchResults = try context.fetch(fetchRequest)
  62. //I like to check the size of the returned results!
  63. print ("BLOG:DEBUG: Total posts: \(searchResults.count)")
  64. var row : RowBlog
  65. var count = 0
  66. var id: Int
  67. var title: String
  68. var text: String
  69. var image: String
  70. //You need to convert to NSManagedObject to use 'for' loops
  71. for r in searchResults as [NSManagedObject] {
  72. count = count + 1
  73. //get the Key Value pairs (although there may be a better way to do that...
  74. print("BLOG:DEBUG: Post perm: \(r.value(forKey: "permalink"))")
  75. //Create a new row
  76. row = RowBlog.init(s: "rowBlog\(count)", i: count)
  77. id = r.value(forKey: "id")! as! Int
  78. title = r.value(forKey: "title_\(lang)")! as! String
  79. text = r.value(forKey: "text_\(lang)")! as! String
  80. row.setTitle(text: title)
  81. row.setText(text: text)
  82. // Get main image
  83. image = ""
  84. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  85. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  86. let imgSortDescriptors = [imgSortDescriptor]
  87. imgFetchRequest.sortDescriptors = imgSortDescriptors
  88. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  89. imgFetchRequest.fetchLimit = 1
  90. do{
  91. let imgSearchResults = try context.fetch(imgFetchRequest)
  92. for imgR in imgSearchResults as [NSManagedObject]{
  93. image = imgR.value(forKey: "image")! as! String
  94. print ("BLOG:DEBUG: Image: \(image)")
  95. row.setImage(filename: image)
  96. }
  97. } catch {
  98. print("Error getting image for post \(id): \(error)")
  99. }
  100. print("BLOG:DEBUG: Row height: \(row.frame.height)")
  101. // Set tap recognizer
  102. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector (self.openPost (_:)))
  103. row.isUserInteractionEnabled = true
  104. row.addGestureRecognizer(tapRecognizer)
  105. parent.addArrangedSubview(row)
  106. }
  107. } catch {
  108. print("BLOG:ERROR: Error with request: \(error)")
  109. }
  110. //Always at the end: update scrollview
  111. var h: Int = 0
  112. for view in scrollView.subviews {
  113. //contentRect = contentRect.union(view.frame);
  114. h = h + Int(view.frame.height) + 30 //Why 30?
  115. print("BLOG:DEBUG: curh: \(h)")
  116. }
  117. // TODO: Calculate at the end
  118. self.scrollView.contentSize.height = 2500//CGFloat(h);
  119. // The view controller
  120. //var viewController: ViewController = self.window?.rootViewController as! ViewController
  121. //let viewController = UIApplication.topViewController() as! ViewController
  122. //viewController.showPost(id: 4)
  123. }
  124. func openPost(_ sender:UITapGestureRecognizer? = nil){
  125. print("BLOG:DEBUG: getting delegate and showing post.")
  126. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  127. delegate.controller?.showPost(id: 4)
  128. print("BLOG:DEBUG: Post should be shown.")
  129. }
  130. func getLanguage() -> String{
  131. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  132. if(pre == "es" || pre == "en" || pre == "eu"){
  133. return pre
  134. }
  135. else{
  136. return "es"
  137. }
  138. }
  139. func setController(controller: ViewController){
  140. //self.controller = controller
  141. }
  142. }