BlogView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 blog view.
  25. */
  26. class BlogView: UIView {
  27. // Outlets
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. @IBOutlet var container: UIView!
  30. @IBOutlet weak var section: Section!
  31. var delegate: AppDelegate? = nil
  32. var postList: UIStackView? = nil
  33. var storyboard: UIStoryboard? = nil
  34. var controller: ViewController? = nil
  35. var context: NSManagedObjectContext? = nil;
  36. var lang: String? = nil
  37. /**
  38. Default constructor for the interface builder.
  39. */
  40. override init(frame: CGRect){
  41. super.init(frame: frame)
  42. }
  43. /**
  44. Run when the view is started.
  45. */
  46. required init?(coder aDecoder: NSCoder) {
  47. super.init(coder: aDecoder)
  48. // Load the contents of the HomeView.xib file.
  49. Bundle.main.loadNibNamed("BlogView", owner: self, options: nil)
  50. self.addSubview(self.container)
  51. self.container.frame = self.bounds
  52. self.section.setTitle(text: "Blog")
  53. self.postList = self.section.getContentStack()
  54. // Get viewController from StoryBoard
  55. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  56. self.controller = storyboard?.instantiateViewController(withIdentifier: "GMViewController") as! ViewController
  57. self.context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  58. self.delegate = UIApplication.shared.delegate as? AppDelegate
  59. self.lang = getLanguage()
  60. self.context?.persistentStoreCoordinator = delegate?.persistentStoreCoordinator
  61. populate()
  62. }
  63. /**
  64. Actually populates the section.
  65. */
  66. func populate(){
  67. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  68. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  69. let sortDescriptors = [sortDescriptor]
  70. fetchRequest.sortDescriptors = sortDescriptors
  71. do {
  72. // Clear the list
  73. for v in (self.postList?.subviews)!{
  74. v.removeFromSuperview()
  75. }
  76. // Get the results
  77. let searchResults = try self.context?.fetch(fetchRequest)
  78. var count: Int = 0
  79. var row : RowBlog
  80. var id: Int
  81. var title: String
  82. var text: String
  83. var image: String
  84. for r in searchResults! {
  85. //Create a new row
  86. row = RowBlog.init(s: "rowBlog\(count)", i: count)
  87. id = r.value(forKey: "id")! as! Int
  88. title = r.value(forKey: "title_\(lang!)")! as! String
  89. text = r.value(forKey: "text_\(lang!)")! as! String
  90. row.setTitle(text: title)
  91. row.setText(text: text)
  92. row.id = id
  93. // Get main image
  94. image = ""
  95. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  96. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  97. let imgSortDescriptors = [imgSortDescriptor]
  98. imgFetchRequest.sortDescriptors = imgSortDescriptors
  99. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  100. imgFetchRequest.fetchLimit = 1
  101. do{
  102. let imgSearchResults = try context?.fetch(imgFetchRequest)
  103. for imgR in imgSearchResults!{
  104. image = imgR.value(forKey: "image")! as! String
  105. row.setImage(filename: image)
  106. }
  107. } catch {
  108. NSLog(":BLOG:ERROR: Error getting image for post \(id): \(error)")
  109. }
  110. self.postList?.addArrangedSubview(row)
  111. count = count + 1
  112. }
  113. // Trigger a layout update
  114. postList?.setNeedsLayout()
  115. postList?.layoutIfNeeded()
  116. } catch {
  117. NSLog(":BLOG:ERROR: Error with request: \(error)")
  118. }
  119. }
  120. /**
  121. Gets the device language. The only recognized languages are Spanish, English and Basque.
  122. If the device has another language, Spanish will be selected by default.
  123. :return: Two-letter language code.
  124. */
  125. func getLanguage() -> String{
  126. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  127. if(pre == "es" || pre == "en" || pre == "eu"){
  128. return pre
  129. }
  130. else{
  131. return "es"
  132. }
  133. }
  134. }