HomeView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  40. Run when the view is started.
  41. */
  42. required init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44. //Load the contents of the HomeView.xib file.
  45. Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
  46. self.addSubview(container)
  47. container.frame = self.bounds
  48. //Set titles for each section
  49. locationSection.setTitle(text: "Encuentranos")
  50. lablancaSection.setTitle(text: "La Blanca")
  51. futureActivitiesSection.setTitle(text: "Proximas actividades")
  52. blogSection.setTitle(text: "Ultimos posts")
  53. gallerySection.setTitle(text: "Ultimas fotos")
  54. pastActivitiesSection.setTitle(text: "Ultimas actividades")
  55. socialSection.setTitle(text: "Siguenos")
  56. //Get info to populate sections
  57. let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  58. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  59. let lang : String = getLanguage()
  60. //Populate sections
  61. setUpPastActivities(context: moc, delegate: appDelegate, lang: lang, parent: pastActivitiesSection)
  62. //Always at the end: update scrollview
  63. var h: Int = 0
  64. for view in scrollView.subviews {
  65. //contentRect = contentRect.union(view.frame);
  66. h = h + Int(view.frame.height) + 30 //Why 30?
  67. print("curh: \(h)")
  68. }
  69. self.scrollView.contentSize.height = CGFloat(h);
  70. }
  71. func getLanguage() -> String{
  72. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  73. print("Device language: \(pre)")
  74. if(pre == "es" || pre == "en" || pre == "eu"){
  75. return pre
  76. }
  77. else{
  78. return "es"
  79. }
  80. }
  81. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : Section){
  82. context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  83. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  84. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  85. let sortDescriptors = [sortDescriptor]
  86. fetchRequest.sortDescriptors = sortDescriptors
  87. //TODO Compare dates
  88. //fetchRequest.predicate = NSPredicate(format: "date < %@", sysdate)
  89. fetchRequest.fetchLimit = 2
  90. do {
  91. //go get the results
  92. let searchResults = try context.fetch(fetchRequest)
  93. //I like to check the size of the returned results!
  94. print ("Activities: \(searchResults.count)")
  95. var row : RowHomePastActivities
  96. var count = 0;
  97. var title : String
  98. //You need to convert to NSManagedObject to use 'for' loops
  99. for r in searchResults as [NSManagedObject] {
  100. count = count + 1
  101. //get the Key Value pairs (although there may be a better way to do that...
  102. print("Perm: \(r.value(forKey: "permalink"))")
  103. //Create a new row
  104. row = RowHomePastActivities(s: "rowHomePastActivities", i: count) //frame: CGRect(top: 0, left: 0, width: 200, height: 50)
  105. //row.frame = CGRect(x: 15, y: 15, width: 100, height: 100)
  106. //row = RowHomePastActivities(frame: CGRect(x: 13, y: 13, width: 100, height: 100))
  107. title = r.value(forKey: "permalink")! as! String
  108. print(title)
  109. row.setTitle(text: title)
  110. row.backgroundColor = UIColor.red
  111. row.setBounds(rect: parent.bounds)
  112. parent.addSubview(row)
  113. let lbl = UILabel()
  114. lbl.text = "ABBA"
  115. //view.addEntry(view: row)
  116. //parent.addSubview(lbl)
  117. var testView: UIView = UIView(frame: CGRect(x: 13, y: 13, width: 100, height: 100))
  118. testView.backgroundColor = UIColor.blue
  119. testView.alpha = 0.5
  120. testView.tag = 100
  121. testView.isUserInteractionEnabled = true
  122. parent.addSubview(testView)
  123. //parent.addConstraint(NSLayoutConstraint(item: row, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: parent, attribute: NSLayoutAttribute.right, multiplier: 1, constant: -10))
  124. //Íparent.addConstraint(NSLayoutConstraint(item: row, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: parent, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -10))
  125. //row = RowHomePastActivities(s: "rowHomePastActivities", i: count)//Bundle.mainBundle("RowPastActivities").loadNibNamed("", owner: nil, options: nil)[0] as! RowHomePastActivities
  126. //row.setupWithSuperView(superView: self)
  127. }
  128. } catch {
  129. print("Error with request: \(error)")
  130. }
  131. }
  132. }