HomeView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 HomeView: UIView {
  27. //The main scroll view.
  28. @IBOutlet weak var scrollView: UIScrollView!
  29. //The container of the view.
  30. @IBOutlet weak var container: UIView!
  31. //Each of the sections of the view.
  32. @IBOutlet weak var locationSection: Section!
  33. @IBOutlet weak var lablancaSection: Section!
  34. @IBOutlet weak var futureActivitiesSection: Section!
  35. @IBOutlet weak var blogSection: Section!
  36. @IBOutlet weak var gallerySection: Section!
  37. @IBOutlet weak var pastActivitiesSection: Section!
  38. @IBOutlet weak var socialSection: Section!
  39. override init(frame: CGRect){
  40. super.init(frame: frame)
  41. }
  42. /**
  43. Run when the view is started.
  44. */
  45. required init?(coder aDecoder: NSCoder) {
  46. super.init(coder: aDecoder)
  47. //Load the contents of the HomeView.xib file.
  48. Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
  49. self.addSubview(container)
  50. container.frame = self.bounds
  51. //Set titles for each section
  52. locationSection.setTitle(text: "Encuentranos")
  53. lablancaSection.setTitle(text: "La Blanca")
  54. futureActivitiesSection.setTitle(text: "Proximas actividades")
  55. blogSection.setTitle(text: "Ultimos posts")
  56. gallerySection.setTitle(text: "Ultimas fotos")
  57. pastActivitiesSection.setTitle(text: "Ultimas actividades")
  58. socialSection.setTitle(text: "Siguenos")
  59. //Get info to populate sections
  60. let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  61. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  62. let lang : String = getLanguage()
  63. //Populate sections
  64. setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection.getContentStack())
  65. //Always at the end: update scrollview
  66. var h: Int = 0
  67. for view in scrollView.subviews {
  68. //contentRect = contentRect.union(view.frame);
  69. h = h + Int(view.frame.height) + 30 //Why 30?
  70. print("curh: \(h)")
  71. }
  72. self.scrollView.contentSize.height = CGFloat(h);
  73. }
  74. func getLanguage() -> String{
  75. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  76. print("Device language: \(pre)")
  77. if(pre == "es" || pre == "en" || pre == "eu"){
  78. return pre
  79. }
  80. else{
  81. return "es"
  82. }
  83. }
  84. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  85. context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  86. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  87. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  88. let sortDescriptors = [sortDescriptor]
  89. fetchRequest.sortDescriptors = sortDescriptors
  90. //TODO Compare dates
  91. //fetchRequest.predicate = NSPredicate(format: "date < %@", sysdate)
  92. fetchRequest.fetchLimit = 2
  93. do {
  94. //go get the results
  95. let searchResults = try context.fetch(fetchRequest)
  96. //I like to check the size of the returned results!
  97. print ("Activities: \(searchResults.count)")
  98. var row : RowHomePastActivities
  99. var count = 0;
  100. var title : String
  101. var text: String
  102. //You need to convert to NSManagedObject to use 'for' loops
  103. for r in searchResults as [NSManagedObject] {
  104. count = count + 1
  105. //get the Key Value pairs (although there may be a better way to do that...
  106. print("Perm: \(r.value(forKey: "permalink"))")
  107. //Create a new row
  108. row = RowHomePastActivities.init(s: "rowHomePastActivities", i: count)
  109. title = r.value(forKey: "permalink")! as! String
  110. text = r.value(forKey: "text_\(lang)")! as! String
  111. print(title)
  112. row.setTitle(text: title)
  113. row.setText(text: text)
  114. parent.addArrangedSubview(row)
  115. }
  116. } catch {
  117. print("Error with request: \(error)")
  118. }
  119. }
  120. }