BlogView.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  35. var delegate: AppDelegate? = nil
  36. override init(frame: CGRect){
  37. super.init(frame: frame)
  38. }
  39. /**
  40. Run when the view is started.
  41. */
  42. required init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44. print("BLOG:DEBUG: init.")
  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. section.setTitle(text: "Blog")
  50. let parent = section.getContentStack()
  51. // Get viewController from StoryBoard
  52. let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  53. let controller = storyboard.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  54. // Show posts
  55. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  56. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  57. self.delegate = appDelegate
  58. let lang : String = getLanguage()
  59. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  60. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  61. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  62. let sortDescriptors = [sortDescriptor]
  63. fetchRequest.sortDescriptors = sortDescriptors
  64. do {
  65. //go get the results
  66. let searchResults = try context.fetch(fetchRequest)
  67. //I like to check the size of the returned results!
  68. print ("BLOG:DEBUG: Total posts: \(searchResults.count)")
  69. var row : RowBlog
  70. var count = 0
  71. var id: Int
  72. var title: String
  73. var text: String
  74. var image: String
  75. //You need to convert to NSManagedObject to use 'for' loops
  76. for r in searchResults as [NSManagedObject] {
  77. count = count + 1
  78. //get the Key Value pairs (although there may be a better way to do that...
  79. print("BLOG:DEBUG: Post perm: \(r.value(forKey: "permalink"))")
  80. //Create a new row
  81. row = RowBlog.init(s: "rowBlog\(count)", i: count)
  82. id = r.value(forKey: "id")! as! Int
  83. title = r.value(forKey: "title_\(lang)")! as! String
  84. text = r.value(forKey: "text_\(lang)")! as! String
  85. row.setTitle(text: title)
  86. row.setText(text: text)
  87. // Get main image
  88. image = ""
  89. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  90. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  91. let imgSortDescriptors = [imgSortDescriptor]
  92. imgFetchRequest.sortDescriptors = imgSortDescriptors
  93. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  94. imgFetchRequest.fetchLimit = 1
  95. do{
  96. let imgSearchResults = try context.fetch(imgFetchRequest)
  97. for imgR in imgSearchResults as [NSManagedObject]{
  98. image = imgR.value(forKey: "image")! as! String
  99. print ("BLOG:DEBUG: Image: \(image)")
  100. row.setImage(filename: image)
  101. }
  102. } catch {
  103. print("BLOG:ERROR: Error getting image for post \(id): \(error)")
  104. }
  105. print("BLOG:DEBUG: Row height: \(row.frame.height)")
  106. parent.addArrangedSubview(row)
  107. row.setNeedsLayout()
  108. row.layoutIfNeeded()
  109. // TODO: Do this on the row didLoad method
  110. // Set tap recognizer
  111. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  112. //row.isUserInteractionEnabled = true
  113. //row.addGestureRecognizer(tapRecognizer)
  114. }
  115. } catch {
  116. print("BLOG:ERROR: Error with request: \(error)")
  117. }
  118. print("BLOG:DEBUG: Finished loading BlogView")
  119. }
  120. /*func openPost(){//(_ sender:UITapGestureRecognizer? = nil){
  121. print("BLOG:DEBUG: getting delegate and showing post.")
  122. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  123. delegate.controller?.showPost(id: 4)
  124. print("BLOG:DEBUG: Post should be shown.")
  125. }
  126. func openPost(_ sender:UITapGestureRecognizer? = nil){
  127. print("BLOG:DEBUG: getting delegate and showing post.")
  128. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  129. delegate.controller?.showPost(id: 4)
  130. print("BLOG:DEBUG: Post should be shown.")
  131. }*/
  132. func getLanguage() -> String{
  133. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  134. if(pre == "es" || pre == "en" || pre == "eu"){
  135. return pre
  136. }
  137. else{
  138. return "es"
  139. }
  140. }
  141. func setController(controller: ViewController){
  142. //self.controller = controller
  143. }
  144. }