ActivitiesView.swift 8.3 KB

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