ActivitiesView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ActivitiesView: UIView {
  27. @IBOutlet var container: ActivitiesView!
  28. @IBOutlet weak var futureSection: Section!
  29. @IBOutlet weak var pastSection: Section!
  30. var delegate: AppDelegate? = nil
  31. var storyboard: UIStoryboard? = nil
  32. var controller: ViewController? = nil
  33. var lang: String? = nil
  34. var futureList: UIStackView? = nil
  35. var pastList: UIStackView? = nil
  36. var context: NSManagedObjectContext? = nil
  37. override init(frame: CGRect){
  38. super.init(frame: frame)
  39. }
  40. /**
  41. Run when the view is started.
  42. */
  43. required init?(coder aDecoder: NSCoder) {
  44. super.init(coder: aDecoder)
  45. NSLog(":ACTIVITIES:DEBUG: init.")
  46. //Load the contents of the HomeView.xib file.
  47. Bundle.main.loadNibNamed("ActivitiesView", owner: self, options: nil)
  48. self.addSubview(container)
  49. self.container.frame = self.bounds
  50. self.futureSection.setTitle(text: "Próximas actividades")
  51. self.pastSection.setTitle(text: "Últimas actividades")
  52. self.futureList = self.futureSection.getContentStack()
  53. self.pastList = self.pastSection.getContentStack()
  54. // Get viewController from StoryBoard
  55. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  56. self.controller = self.storyboard?.instantiateViewController(withIdentifier: "GMViewController") as? ViewController
  57. self.context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  58. self.delegate = UIApplication.shared.delegate as? AppDelegate
  59. self.lang = self.getLanguage()
  60. self.context?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
  61. // Populate activity lists.
  62. populate()
  63. }
  64. func populate(){
  65. // Show future activities
  66. var fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  67. var sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  68. var sortDescriptors = [sortDescriptor]
  69. fetchRequest.sortDescriptors = sortDescriptors
  70. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  71. do {
  72. let searchResults = try self.context?.fetch(fetchRequest)
  73. NSLog(":ACTIVITIES:DEBUG: Total future activities: \(String(describing: searchResults?.count))")
  74. if searchResults?.count == 0{
  75. NSLog(":ACTIVITIES:DEBUG: No future activities: \(String(describing: searchResults?.count))")
  76. let row: RowLabel = RowLabel.init(s: "rowFutureActivity0", i: 0)
  77. row.setText(text: "No hay actividades planeadas proximamente. Pronto organizaremos algo!")
  78. self.futureList?.addArrangedSubview(row)
  79. }
  80. else{
  81. var row : RowFutureActivity
  82. var count = 0
  83. var id: Int
  84. var title: String
  85. var text: String
  86. var image: String
  87. var city: String
  88. var price: Int
  89. var date: NSDate
  90. for r in searchResults! {
  91. count = count + 1
  92. NSLog(":ACTIVITIES:DEBUG: Activity perm: \(String(describing: r.value(forKey: "permalink")))")
  93. // Create a new row
  94. row = RowFutureActivity.init(s: "rowFutureActivity\(count)", i: count)
  95. id = r.value(forKey: "id")! as! Int
  96. title = r.value(forKey: "title_\(lang!)")! as! String
  97. text = r.value(forKey: "text_\(lang!)")! as! String
  98. city = r.value(forKey: "city")! as! String
  99. price = r.value(forKey: "price")! as! Int
  100. date = r.value(forKey: "date")! as! NSDate
  101. row.setTitle(text: title)
  102. row.setText(text: text)
  103. row.setPrice(price: price)
  104. row.setCity(text: city)
  105. row.setDate(date: date, lang: lang!)
  106. // Get main image
  107. image = ""
  108. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  109. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  110. let imgSortDescriptors = [imgSortDescriptor]
  111. imgFetchRequest.sortDescriptors = imgSortDescriptors
  112. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  113. imgFetchRequest.fetchLimit = 1
  114. do{
  115. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  116. for imgR in imgSearchResults!{
  117. image = imgR.value(forKey: "image")! as! String
  118. NSLog(":ACTIVITIES:DEBUG: Image: \(image)")
  119. row.setImage(filename: image)
  120. }
  121. } catch {
  122. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  123. }
  124. self.futureList?.addArrangedSubview(row)
  125. //row.setNeedsLayout()
  126. //row.layoutIfNeeded()
  127. // TODO: Do this on the row didLoad method
  128. // Set tap recognizer
  129. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openActivity(_:)))
  130. //row.isUserInteractionEnabled = true
  131. //row.addGestureRecognizer(tapRecognizer)
  132. }
  133. self.futureList?.setNeedsLayout()
  134. self.futureList?.layoutIfNeeded()
  135. }
  136. }
  137. catch {
  138. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  139. }
  140. // Show past activities
  141. fetchRequest = Activity.fetchRequest()
  142. sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  143. sortDescriptors = [sortDescriptor]
  144. fetchRequest.sortDescriptors = sortDescriptors
  145. fetchRequest.predicate = NSPredicate(format: "date < %@", NSDate())
  146. do {
  147. let searchResults = try self.context?.fetch(fetchRequest)
  148. NSLog(":ACTIVITIES:DEBUG: Total past activities: \(String(describing: searchResults?.count))")
  149. var row: RowPastActivity
  150. var count: Int = 0
  151. var id: Int
  152. var title: String
  153. var text: String
  154. var image: String
  155. for r in searchResults! {
  156. count = count + 1
  157. NSLog(":ACTIVITIES:DEBUG: Activity perm: \(String(describing: r.value(forKey: "permalink")))")
  158. // Create a new row
  159. row = RowPastActivity.init(s: "rowPastActivity\(count)", i: count)
  160. id = r.value(forKey: "id")! as! Int
  161. title = r.value(forKey: "title_\(lang!)")! as! String
  162. text = r.value(forKey: "text_\(lang!)")! as! String
  163. row.setTitle(text: title)
  164. row.setText(text: text)
  165. // Get main image
  166. image = ""
  167. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  168. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  169. let imgSortDescriptors = [imgSortDescriptor]
  170. imgFetchRequest.sortDescriptors = imgSortDescriptors
  171. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  172. imgFetchRequest.fetchLimit = 1
  173. do{
  174. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  175. for imgR in imgSearchResults!{
  176. image = imgR.value(forKey: "image")! as! String
  177. NSLog(":ACTIVITIES:DEBUG: Image: \(image)")
  178. row.setImage(filename: image)
  179. }
  180. }
  181. catch {
  182. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  183. }
  184. NSLog(":ACTIVITIES:DEBUG: Row height: \(row.frame.height)")
  185. self.pastList?.addArrangedSubview(row)
  186. //row.setNeedsLayout()
  187. //row.layoutIfNeeded()
  188. // TODO: Do this on the row didLoad method
  189. // Set tap recognizer
  190. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openActivity(_:)))
  191. //row.isUserInteractionEnabled = true
  192. //row.addGestureRecognizer(tapRecognizer)
  193. }
  194. self.pastList?.setNeedsLayout()
  195. self.pastList?.layoutIfNeeded()
  196. } catch {
  197. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  198. }
  199. NSLog(":ACTIVITIES:DEBUG: Finished loading ActivitiesView")
  200. }
  201. /*func openActivity(){//(_ sender:UITapGestureRecognizer? = nil){
  202. NSLog(":ACTIVITIES:DEBUG: getting delegate and showing activity.")
  203. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  204. delegate.controller?.showActivity(id: 4)
  205. NSLog(":ACTIVITIES:DEBUG: Activity should be shown.")
  206. }
  207. func openActivity(_ sender:UITapGestureRecognizer? = nil){
  208. NSLog(":ACTIVITIES:DEBUG: getting delegate and showing activity.")
  209. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  210. delegate.controller?.showActivity(id: 4)
  211. NSLog(":ACTIVITIES:DEBUG: Activity should be shown.")
  212. }*/
  213. func getLanguage() -> String{
  214. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  215. if(pre == "es" || pre == "en" || pre == "eu"){
  216. return pre
  217. }
  218. else{
  219. return "es"
  220. }
  221. }
  222. }