HomeView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. setUpBlog(context: moc, delegate: appDelegate, lang: lang, parent: blogSection.getContentStack())
  66. setUpFutureActivities(context: moc, delegate: appDelegate, lang: lang, parent: futureActivitiesSection.getContentStack())
  67. pastActivitiesSection.expandSection()
  68. //Always at the end: update scrollview
  69. var h: Int = 0
  70. for view in scrollView.subviews {
  71. //contentRect = contentRect.union(view.frame);
  72. h = h + Int(view.frame.height) + 30 //Why 30?
  73. print("curh: \(h)")
  74. }
  75. // TODO: Calculate at the end
  76. self.scrollView.contentSize.height = 1200// CGFloat(h);
  77. }
  78. func getLanguage() -> String{
  79. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  80. if(pre == "es" || pre == "en" || pre == "eu"){
  81. return pre
  82. }
  83. else{
  84. return "es"
  85. }
  86. }
  87. func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  88. context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  89. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  90. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  91. let sortDescriptors = [sortDescriptor]
  92. fetchRequest.sortDescriptors = sortDescriptors
  93. //TODO Compare dates
  94. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  95. fetchRequest.fetchLimit = 2
  96. do {
  97. //go get the results
  98. let searchResults = try context.fetch(fetchRequest)
  99. //I like to check the size of the returned results!
  100. print ("Activities: \(searchResults.count)")
  101. var row : RowHomePastActivities
  102. var count = 0
  103. var id: Int
  104. var title: String
  105. var text: String
  106. var image: String
  107. //You need to convert to NSManagedObject to use 'for' loops
  108. for r in searchResults as [NSManagedObject] {
  109. count = count + 1
  110. //get the Key Value pairs (although there may be a better way to do that...
  111. print("Perm: \(r.value(forKey: "permalink"))")
  112. //Create a new row
  113. row = RowHomePastActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
  114. id = r.value(forKey: "id")! as! Int
  115. title = r.value(forKey: "title_\(lang)")! as! String
  116. text = r.value(forKey: "text_\(lang)")! as! String
  117. print(title)
  118. row.setTitle(text: title)
  119. row.setText(text: text)
  120. // Get main image
  121. image = ""
  122. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  123. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  124. let imgSortDescriptors = [imgSortDescriptor]
  125. imgFetchRequest.sortDescriptors = imgSortDescriptors
  126. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  127. imgFetchRequest.fetchLimit = 1
  128. do{
  129. let imgSearchResults = try context.fetch(imgFetchRequest)
  130. for imgR in imgSearchResults as [NSManagedObject]{
  131. image = imgR.value(forKey: "image")! as! String
  132. print ("IMAGE: \(image)")
  133. row.setImage(filename: image)
  134. }
  135. } catch {
  136. print("Error getting image for activity \(id): \(error)")
  137. }
  138. print("Row height: \(row.frame.height)")
  139. parent.addArrangedSubview(row)
  140. }
  141. } catch {
  142. print("Error with request: \(error)")
  143. }
  144. }
  145. func setUpBlog(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  146. context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  147. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  148. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  149. let sortDescriptors = [sortDescriptor]
  150. fetchRequest.sortDescriptors = sortDescriptors
  151. fetchRequest.fetchLimit = 2
  152. do {
  153. //go get the results
  154. let searchResults = try context.fetch(fetchRequest)
  155. //I like to check the size of the returned results!
  156. print ("Post: \(searchResults.count)")
  157. var row : RowHomePastActivities
  158. var count = 0
  159. var id: Int
  160. var title: String
  161. var text: String
  162. var image: String
  163. //You need to convert to NSManagedObject to use 'for' loops
  164. for r in searchResults as [NSManagedObject] {
  165. count = count + 1
  166. //get the Key Value pairs (although there may be a better way to do that...
  167. print("Perm: \(r.value(forKey: "permalink"))")
  168. //Create a new row
  169. row = RowHomePastActivities.init(s: "rowHomeBlog\(count)", i: count)
  170. id = r.value(forKey: "id")! as! Int
  171. title = r.value(forKey: "title_\(lang)")! as! String
  172. text = r.value(forKey: "text_\(lang)")! as! String
  173. print(title)
  174. row.setTitle(text: title)
  175. row.setText(text: text)
  176. // Get main image
  177. image = ""
  178. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  179. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  180. let imgSortDescriptors = [imgSortDescriptor]
  181. imgFetchRequest.sortDescriptors = imgSortDescriptors
  182. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  183. imgFetchRequest.fetchLimit = 1
  184. do{
  185. let imgSearchResults = try context.fetch(imgFetchRequest)
  186. for imgR in imgSearchResults as [NSManagedObject]{
  187. image = imgR.value(forKey: "image")! as! String
  188. print ("IMAGE: \(image)")
  189. row.setImage(filename: image)
  190. }
  191. } catch {
  192. print("Error getting image for post \(id): \(error)")
  193. }
  194. print("Row height: \(row.frame.height)")
  195. parent.addArrangedSubview(row)
  196. }
  197. } catch {
  198. print("Error with request: \(error)")
  199. }
  200. }
  201. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  202. context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  203. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  204. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  205. let sortDescriptors = [sortDescriptor]
  206. fetchRequest.sortDescriptors = sortDescriptors
  207. //TODO Compare dates
  208. fetchRequest.predicate = NSPredicate(format: "date <= %@", NSDate())
  209. fetchRequest.fetchLimit = 2
  210. do {
  211. //go get the results
  212. let searchResults = try context.fetch(fetchRequest)
  213. //I like to check the size of the returned results!
  214. print ("Activities: \(searchResults.count)")
  215. var row : RowHomePastActivities
  216. var count = 0
  217. var id: Int
  218. var title: String
  219. var text: String
  220. var image: String
  221. //You need to convert to NSManagedObject to use 'for' loops
  222. for r in searchResults as [NSManagedObject] {
  223. count = count + 1
  224. //get the Key Value pairs (although there may be a better way to do that...
  225. print("Perm: \(r.value(forKey: "permalink"))")
  226. //Create a new row
  227. row = RowHomePastActivities.init(s: "rowHomePastActivities\(count)", i: count)
  228. id = r.value(forKey: "id")! as! Int
  229. title = r.value(forKey: "title_\(lang)")! as! String
  230. text = r.value(forKey: "text_\(lang)")! as! String
  231. print(title)
  232. row.setTitle(text: title)
  233. row.setText(text: text)
  234. // Get main image
  235. image = ""
  236. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  237. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  238. let imgSortDescriptors = [imgSortDescriptor]
  239. imgFetchRequest.sortDescriptors = imgSortDescriptors
  240. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  241. imgFetchRequest.fetchLimit = 1
  242. do{
  243. let imgSearchResults = try context.fetch(imgFetchRequest)
  244. for imgR in imgSearchResults as [NSManagedObject]{
  245. image = imgR.value(forKey: "image")! as! String
  246. print ("IMAGE: \(image)")
  247. row.setImage(filename: image)
  248. }
  249. } catch {
  250. print("Error getting image for activity \(id): \(error)")
  251. }
  252. print("Row height: \(row.frame.height)")
  253. parent.addArrangedSubview(row)
  254. }
  255. } catch {
  256. print("Error with request: \(error)")
  257. }
  258. }
  259. }