BlogView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //The main scroll view.
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. //The container of the view.
  30. @IBOutlet var container: UIView!
  31. //Each of the sections of the view.
  32. @IBOutlet weak var section: Section!
  33. override init(frame: CGRect){
  34. super.init(frame: frame)
  35. }
  36. /**
  37. Run when the view is started.
  38. */
  39. required init?(coder aDecoder: NSCoder) {
  40. super.init(coder: aDecoder)
  41. //Load the contents of the HomeView.xib file.
  42. Bundle.main.loadNibNamed("BlogView", owner: self, options: nil)
  43. self.addSubview(container)
  44. container.frame = self.bounds
  45. section.setTitle(text: "Blog")
  46. let parent = section.getContentStack()
  47. // Show posts
  48. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  49. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  50. let lang : String = getLanguage()
  51. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  52. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  53. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  54. let sortDescriptors = [sortDescriptor]
  55. fetchRequest.sortDescriptors = sortDescriptors
  56. /*do {
  57. //go get the results
  58. let searchResults = try context.fetch(fetchRequest)
  59. //I like to check the size of the returned results!
  60. print ("Post: \(searchResults.count)")
  61. var row : RowBlog
  62. var count = 0
  63. var id: Int
  64. var title: String
  65. var text: 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. //Create a new row
  73. row = RowBlog.init(s: "rowBlog\(count)", i: count)
  74. id = r.value(forKey: "id")! as! Int
  75. title = r.value(forKey: "title_\(lang)")! as! String
  76. text = r.value(forKey: "text_\(lang)")! as! String
  77. print(title)
  78. row.setTitle(text: title)
  79. row.setText(text: text)
  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. do{
  89. let imgSearchResults = try context.fetch(imgFetchRequest)
  90. for imgR in imgSearchResults as [NSManagedObject]{
  91. image = imgR.value(forKey: "image")! as! String
  92. print ("IMAGE: \(image)")
  93. row.setImage(filename: image)
  94. }
  95. } catch {
  96. print("Error getting image for post \(id): \(error)")
  97. }
  98. print("Row height: \(row.frame.height)")
  99. parent.addArrangedSubview(row)
  100. }
  101. } catch {
  102. print("Error with request: \(error)")
  103. }*/
  104. //Always at the end: update scrollview
  105. var h: Int = 0
  106. for view in scrollView.subviews {
  107. //contentRect = contentRect.union(view.frame);
  108. h = h + Int(view.frame.height) + 30 //Why 30?
  109. print("Blog curh: \(h)")
  110. }
  111. // TODO: Calculate at the end
  112. //self.scrollView.contentSize.height = 2500//CGFloat(h);
  113. // The view controller
  114. //var viewController: ViewController = self.window?.rootViewController as! ViewController
  115. //let viewController = UIApplication.topViewController() as! ViewController
  116. //viewController.showPost(id: 4)
  117. }
  118. func getLanguage() -> String{
  119. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  120. if(pre == "es" || pre == "en" || pre == "eu"){
  121. return pre
  122. }
  123. else{
  124. return "es"
  125. }
  126. }
  127. }