HomeView.swift 11 KB

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