ActivitiesView.swift 8.3 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. //Load the contents of the HomeView.xib file.
  46. Bundle.main.loadNibNamed("ActivitiesView", owner: self, options: nil)
  47. self.addSubview(container)
  48. self.container.frame = self.bounds
  49. self.futureSection.setTitle(text: "Próximas actividades")
  50. self.pastSection.setTitle(text: "Últimas actividades")
  51. self.futureList = self.futureSection.getContentStack()
  52. self.pastList = self.pastSection.getContentStack()
  53. // Get viewController from StoryBoard
  54. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  55. self.controller = self.storyboard?.instantiateViewController(withIdentifier: "GMViewController") as? ViewController
  56. self.context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  57. self.delegate = UIApplication.shared.delegate as? AppDelegate
  58. self.lang = self.getLanguage()
  59. self.context?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
  60. // Populate activity lists.
  61. populate()
  62. }
  63. /**
  64. Actually populates the section.
  65. */
  66. func populate(){
  67. // Show future activities
  68. var fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  69. var sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  70. var sortDescriptors = [sortDescriptor]
  71. fetchRequest.sortDescriptors = sortDescriptors
  72. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  73. do {
  74. let searchResults = try self.context?.fetch(fetchRequest)
  75. if searchResults?.count == 0{
  76. NSLog(":ACTIVITIES:DEBUG: No future activities: \(String(describing: searchResults?.count))")
  77. let row: RowLabel = RowLabel.init(s: "rowFutureActivity0", i: 0)
  78. row.setText(text: "No hay actividades planeadas proximamente. Pronto organizaremos algo!")
  79. self.futureList?.addArrangedSubview(row)
  80. }
  81. else{
  82. var row : RowFutureActivity
  83. var count = 0
  84. var id: Int
  85. var title: String
  86. var text: String
  87. var image: String
  88. var city: String
  89. var price: Int
  90. var date: NSDate
  91. for r in searchResults! {
  92. count = count + 1
  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. row.setImage(filename: image)
  119. }
  120. } catch {
  121. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  122. }
  123. self.futureList?.addArrangedSubview(row)
  124. // TODO: Do this on the row didLoad method
  125. // Set tap recognizer
  126. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openActivity(_:)))
  127. //row.isUserInteractionEnabled = true
  128. //row.addGestureRecognizer(tapRecognizer)
  129. }
  130. self.futureList?.setNeedsLayout()
  131. self.futureList?.layoutIfNeeded()
  132. }
  133. }
  134. catch {
  135. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  136. }
  137. // Show past activities
  138. fetchRequest = Activity.fetchRequest()
  139. sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  140. sortDescriptors = [sortDescriptor]
  141. fetchRequest.sortDescriptors = sortDescriptors
  142. fetchRequest.predicate = NSPredicate(format: "date < %@", NSDate())
  143. do {
  144. let searchResults = try self.context?.fetch(fetchRequest)
  145. var row: RowPastActivity
  146. var count: Int = 0
  147. var id: Int
  148. var title: String
  149. var text: String
  150. var image: String
  151. for r in searchResults! {
  152. count = count + 1
  153. // Create a new row
  154. row = RowPastActivity.init(s: "rowPastActivity\(count)", i: count)
  155. id = r.value(forKey: "id")! as! Int
  156. title = r.value(forKey: "title_\(lang!)")! as! String
  157. text = r.value(forKey: "text_\(lang!)")! as! String
  158. row.setTitle(text: title)
  159. row.setText(text: text)
  160. // Get main image
  161. image = ""
  162. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  163. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  164. let imgSortDescriptors = [imgSortDescriptor]
  165. imgFetchRequest.sortDescriptors = imgSortDescriptors
  166. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  167. imgFetchRequest.fetchLimit = 1
  168. do{
  169. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  170. for imgR in imgSearchResults!{
  171. image = imgR.value(forKey: "image")! as! String
  172. row.setImage(filename: image)
  173. }
  174. }
  175. catch {
  176. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  177. }
  178. self.pastList?.addArrangedSubview(row)
  179. // TODO: Do this on the row didLoad method
  180. // Set tap recognizer
  181. //let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(openActivity(_:)))
  182. //row.isUserInteractionEnabled = true
  183. //row.addGestureRecognizer(tapRecognizer)
  184. }
  185. self.pastList?.setNeedsLayout()
  186. self.pastList?.layoutIfNeeded()
  187. } catch {
  188. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  189. }
  190. NSLog(":ACTIVITIES:DEBUG: Finished loading ActivitiesView")
  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 openActivity(_ sender:UITapGestureRecognizer? = nil){
  199. NSLog(":ACTIVITIES:DEBUG: getting delegate and showing activity.")
  200. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  201. delegate.controller?.showActivity(id: 4)
  202. NSLog(":ACTIVITIES:DEBUG: Activity should be shown.")
  203. }*/
  204. /**
  205. Gets the device language. The only recognized languages are Spanish, English and Basque.
  206. If the device has another language, Spanish will be selected by default.
  207. :return: Two-letter language code.
  208. */
  209. func getLanguage() -> String{
  210. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  211. if(pre == "es" || pre == "en" || pre == "eu"){
  212. return pre
  213. }
  214. else{
  215. return "es"
  216. }
  217. }
  218. }