HomeView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. let lang: String
  40. let moc: NSManagedObjectContext
  41. let appDelegate: AppDelegate
  42. override init(frame: CGRect){
  43. super.init(frame: frame)
  44. }
  45. /**
  46. Run when the view is started.
  47. */
  48. required init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. //Load the contents of the HomeView.xib file.
  51. Bundle.main.loadNibNamed("HomeView", owner: self, options: nil)
  52. self.addSubview(container)
  53. container.frame = self.bounds
  54. //Set titles for each section
  55. locationSection.setTitle(text: "Encuentranos")
  56. lablancaSection.setTitle(text: "La Blanca")
  57. futureActivitiesSection.setTitle(text: "Proximas actividades")
  58. blogSection.setTitle(text: "Ultimos posts")
  59. gallerySection.setTitle(text: "Ultimas fotos")
  60. pastActivitiesSection.setTitle(text: "Ultimas actividades")
  61. socialSection.setTitle(text: "Siguenos")
  62. //Get info to populate sections
  63. self.moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  64. self.appDelegate = UIApplication.shared.delegate as! AppDelegate
  65. self.moc.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  66. self.lang = getLanguage()
  67. //Populate sections
  68. self.populate()
  69. //Always at the end: update scrollview
  70. //var h: Int = 0
  71. //for view in scrollView.subviews {
  72. // //contentRect = contentRect.union(view.frame);
  73. // h = h + Int(view.frame.height) + 30 //Why 30?
  74. // print("curh: \(h)")
  75. //}
  76. // TODO: Calculate at the end
  77. //self.scrollView.contentSize.height = 1300// CGFloat(h);
  78. }
  79. /**
  80. Triggers a reloading (or initial loading) of all sections.
  81. */
  82. func populate(){
  83. //Populate sections
  84. self.setUpPastActivities(context: self.moc, delegate: appDelegate, lang: self.lang, parent: self.pastActivitiesSection.getContentStack())
  85. self.setUpBlog(context: self.moc, delegate: self.appDelegate, lang: self.lang, parent: self.blogSection.getContentStack())
  86. self.setUpFutureActivities(context: self.moc, delegate: self.appDelegate, lang: self.lang, parent: self.futureActivitiesSection.getContentStack())
  87. self.setUpSocial(parent: self.socialSection.getContentStack())
  88. self.setUpGallery(context: self.moc, delegate: self.appDelegate, parent: self.gallerySection.getContentStack())
  89. self.pastActivitiesSection.expandSection()
  90. }
  91. func getLanguage() -> String{
  92. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  93. if(pre == "es" || pre == "en" || pre == "eu"){
  94. return pre
  95. }
  96. else{
  97. return "es"
  98. }
  99. }
  100. func setUpFutureActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  101. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  102. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  103. let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  104. let sortDescriptors = [sortDescriptor]
  105. fetchRequest.sortDescriptors = sortDescriptors
  106. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  107. fetchRequest.fetchLimit = 2
  108. do {
  109. //go get the results
  110. let searchResults = try context.fetch(fetchRequest)
  111. //I like to check the size of the returned results!
  112. print ("Activities: \(searchResults.count)")
  113. var row : RowHomePastActivities
  114. var count = 0
  115. var id: Int
  116. var title: String
  117. var text: String
  118. var image: String
  119. //You need to convert to NSManagedObject to use 'for' loops
  120. for r in searchResults as [NSManagedObject] {
  121. count = count + 1
  122. //get the Key Value pairs (although there may be a better way to do that...
  123. print("Perm: \(r.value(forKey: "permalink"))")
  124. //Create a new row
  125. row = RowHomePastActivities.init(s: "rowHomeFutureActivities\(count)", i: count)
  126. id = r.value(forKey: "id")! as! Int
  127. title = r.value(forKey: "title_\(lang)")! as! String
  128. text = r.value(forKey: "text_\(lang)")! as! String
  129. print(title)
  130. row.setTitle(text: title)
  131. row.setText(text: text)
  132. // Get main image
  133. image = ""
  134. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  135. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  136. let imgSortDescriptors = [imgSortDescriptor]
  137. imgFetchRequest.sortDescriptors = imgSortDescriptors
  138. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  139. imgFetchRequest.fetchLimit = 1
  140. do{
  141. let imgSearchResults = try context.fetch(imgFetchRequest)
  142. for imgR in imgSearchResults as [NSManagedObject]{
  143. image = imgR.value(forKey: "image")! as! String
  144. print ("IMAGE: \(image)")
  145. row.setImage(filename: image)
  146. }
  147. } catch {
  148. print("Error getting image for activity \(id): \(error)")
  149. }
  150. print("Row height: \(row.frame.height)")
  151. parent.addArrangedSubview(row)
  152. }
  153. } catch {
  154. print("Error with request: \(error)")
  155. }
  156. }
  157. func setUpBlog(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  158. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  159. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  160. let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
  161. let sortDescriptors = [sortDescriptor]
  162. fetchRequest.sortDescriptors = sortDescriptors
  163. fetchRequest.fetchLimit = 2
  164. do {
  165. //go get the results
  166. let searchResults = try context.fetch(fetchRequest)
  167. //I like to check the size of the returned results!
  168. print ("Post: \(searchResults.count)")
  169. var row : RowHomeBlog
  170. var count = 0
  171. var id: Int
  172. var title: String
  173. var text: String
  174. var image: String
  175. //You need to convert to NSManagedObject to use 'for' loops
  176. for r in searchResults as [NSManagedObject] {
  177. count = count + 1
  178. //get the Key Value pairs (although there may be a better way to do that...
  179. print("Perm: \(r.value(forKey: "permalink"))")
  180. //Create a new row
  181. row = RowHomeBlog.init(s: "rowHomeBlog\(count)", i: count)
  182. id = r.value(forKey: "id")! as! Int
  183. title = r.value(forKey: "title_\(lang)")! as! String
  184. text = r.value(forKey: "text_\(lang)")! as! String
  185. row.id = id
  186. row.setTitle(text: title)
  187. row.setText(text: text)
  188. // Get main image
  189. image = ""
  190. let imgFetchRequest: NSFetchRequest<Post_image> = Post_image.fetchRequest()
  191. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  192. let imgSortDescriptors = [imgSortDescriptor]
  193. imgFetchRequest.sortDescriptors = imgSortDescriptors
  194. imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
  195. imgFetchRequest.fetchLimit = 1
  196. do{
  197. let imgSearchResults = try context.fetch(imgFetchRequest)
  198. for imgR in imgSearchResults as [NSManagedObject]{
  199. image = imgR.value(forKey: "image")! as! String
  200. print ("IMAGE: \(image)")
  201. row.setImage(filename: image)
  202. }
  203. } catch {
  204. print("Error getting image for post \(id): \(error)")
  205. }
  206. print("Row height: \(row.frame.height)")
  207. parent.addArrangedSubview(row)
  208. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openPost(_:)))
  209. row.isUserInteractionEnabled = true
  210. row.addGestureRecognizer(tapRecognizer)
  211. }
  212. } catch {
  213. print("Error with request: \(error)")
  214. }
  215. }
  216. func setUpPastActivities(context : NSManagedObjectContext, delegate: AppDelegate, lang: String, parent : UIStackView){
  217. //context.persistentStoreCoordinator = delegate.persistentStoreCoordinator
  218. let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  219. let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  220. let sortDescriptors = [sortDescriptor]
  221. fetchRequest.sortDescriptors = sortDescriptors
  222. fetchRequest.predicate = NSPredicate(format: "date <= %@", NSDate())
  223. fetchRequest.fetchLimit = 2
  224. do {
  225. //go get the results
  226. let searchResults = try context.fetch(fetchRequest)
  227. //I like to check the size of the returned results!
  228. print ("Activities: \(searchResults.count)")
  229. var row : RowHomePastActivities
  230. var count = 0
  231. var id: Int
  232. var title: String
  233. var text: String
  234. var image: String
  235. //You need to convert to NSManagedObject to use 'for' loops
  236. for r in searchResults as [NSManagedObject] {
  237. count = count + 1
  238. //get the Key Value pairs (although there may be a better way to do that...
  239. print("Perm: \(r.value(forKey: "permalink"))")
  240. //Create a new row
  241. row = RowHomePastActivities.init(s: "rowHomePastActivities\(count)", i: count)
  242. id = r.value(forKey: "id")! as! Int
  243. title = r.value(forKey: "title_\(lang)")! as! String
  244. text = r.value(forKey: "text_\(lang)")! as! String
  245. row.setTitle(text: title)
  246. row.setText(text: text)
  247. // Get main image
  248. image = ""
  249. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  250. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  251. let imgSortDescriptors = [imgSortDescriptor]
  252. imgFetchRequest.sortDescriptors = imgSortDescriptors
  253. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  254. imgFetchRequest.fetchLimit = 1
  255. do{
  256. let imgSearchResults = try context.fetch(imgFetchRequest)
  257. for imgR in imgSearchResults as [NSManagedObject]{
  258. image = imgR.value(forKey: "image")! as! String
  259. print ("IMAGE: \(image)")
  260. row.setImage(filename: image)
  261. }
  262. } catch {
  263. print("Error getting image for activity \(id): \(error)")
  264. }
  265. print("Row height: \(row.frame.height)")
  266. parent.addArrangedSubview(row)
  267. }
  268. } catch {
  269. print("Error with request: \(error)")
  270. }
  271. }
  272. func setUpGallery(context : NSManagedObjectContext, delegate: AppDelegate,parent: UIStackView){
  273. print("HOME:LOG: Setting up gallery.")
  274. // Create the row
  275. var row: RowHomeGallery
  276. row = RowHomeGallery.init(s: "rowHomeGallery", i: 0)
  277. parent.addArrangedSubview(row)
  278. // Set images
  279. let fetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  280. let sortDescriptor = NSSortDescriptor(key: "uploaded", ascending: false)
  281. let sortDescriptors = [sortDescriptor]
  282. fetchRequest.sortDescriptors = sortDescriptors
  283. fetchRequest.fetchLimit = 4
  284. do {
  285. //go get the results
  286. let searchResults = try context.fetch(fetchRequest)
  287. var id: Int
  288. var image: String
  289. // Loop images
  290. var i = 0
  291. for r in searchResults as [NSManagedObject] {
  292. image = r.value(forKey: "file")! as! String
  293. row.setImage(idx: i, filename: image)
  294. //TODO Set click listener
  295. i = i + 1
  296. }
  297. }
  298. catch{
  299. print("HOME:ERROR: Error setting gallery up: \(error)")
  300. }
  301. }
  302. func setUpSocial(parent : UIStackView){
  303. print("SETING UP SOCIAL")
  304. //Create a new row
  305. var row : RowHomeSocial
  306. row = RowHomeSocial.init(s: "rowHomeSocial", i: 0)
  307. print("ROW CREATED")
  308. parent.addArrangedSubview(row)
  309. }
  310. func openPost(_ sender:UITapGestureRecognizer? = nil){
  311. print("HOME:DEBUG: getting delegate and showing post.")
  312. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  313. delegate.controller?.showPost(id: (sender?.view as! RowHomeBlog).id)
  314. print("HOME:DEBUG: Post should be shown.")
  315. }
  316. }