ActivitiesView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Outlets.
  28. @IBOutlet var container: ActivitiesView!
  29. @IBOutlet weak var futureSection: Section!
  30. @IBOutlet weak var pastSection: Section!
  31. var delegate: AppDelegate? = nil
  32. var storyboard: UIStoryboard? = nil
  33. var controller: ViewController? = nil
  34. var lang: String? = nil
  35. var futureList: UIStackView? = nil
  36. var pastList: UIStackView? = nil
  37. var context: NSManagedObjectContext? = nil
  38. override init(frame: CGRect){
  39. super.init(frame: frame)
  40. }
  41. /**
  42. Run when the view is started.
  43. */
  44. required init?(coder aDecoder: NSCoder) {
  45. super.init(coder: aDecoder)
  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. /**
  65. Actually populates the section.
  66. */
  67. func populate(){
  68. // Show future activities
  69. var fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  70. var sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  71. var sortDescriptors = [sortDescriptor]
  72. fetchRequest.sortDescriptors = sortDescriptors
  73. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  74. do {
  75. let searchResults = try self.context?.fetch(fetchRequest)
  76. if searchResults?.count == 0{
  77. NSLog(":ACTIVITIES:DEBUG: No future activities: \(String(describing: searchResults?.count))")
  78. let row: RowLabel = RowLabel.init(s: "rowFutureActivity0", i: 0)
  79. row.setText(text: "No hay actividades planeadas proximamente. Pronto organizaremos algo!")
  80. self.futureList?.addArrangedSubview(row)
  81. }
  82. else{
  83. var row : RowFutureActivity
  84. var count = 0
  85. var id: Int
  86. var title: String
  87. var text: String
  88. var image: String
  89. var city: String
  90. var price: Int
  91. var date: NSDate
  92. for r in searchResults! {
  93. count = count + 1
  94. // Create a new row
  95. row = RowFutureActivity.init(s: "rowFutureActivity\(count)", i: count)
  96. id = r.value(forKey: "id")! as! Int
  97. title = r.value(forKey: "title_\(lang!)")! as! String
  98. text = r.value(forKey: "text_\(lang!)")! as! String
  99. city = r.value(forKey: "city")! as! String
  100. price = r.value(forKey: "price")! as! Int
  101. date = r.value(forKey: "date")! as! NSDate
  102. row.setTitle(text: title)
  103. row.setText(text: text)
  104. row.setPrice(price: price)
  105. row.setCity(text: city)
  106. row.setDate(date: date, lang: lang!)
  107. row.id = id
  108. // Get main image
  109. image = ""
  110. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  111. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  112. let imgSortDescriptors = [imgSortDescriptor]
  113. imgFetchRequest.sortDescriptors = imgSortDescriptors
  114. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  115. imgFetchRequest.fetchLimit = 1
  116. do{
  117. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  118. for imgR in imgSearchResults!{
  119. image = imgR.value(forKey: "image")! as! String
  120. row.setImage(filename: image)
  121. }
  122. } catch {
  123. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  124. }
  125. self.futureList?.addArrangedSubview(row)
  126. }
  127. self.futureList?.setNeedsLayout()
  128. self.futureList?.layoutIfNeeded()
  129. }
  130. }
  131. catch {
  132. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  133. }
  134. // Show past activities
  135. fetchRequest = Activity.fetchRequest()
  136. sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  137. sortDescriptors = [sortDescriptor]
  138. fetchRequest.sortDescriptors = sortDescriptors
  139. fetchRequest.predicate = NSPredicate(format: "date < %@", NSDate())
  140. do {
  141. let searchResults = try self.context?.fetch(fetchRequest)
  142. var row: RowPastActivity
  143. var count: Int = 0
  144. var id: Int
  145. var title: String
  146. var text: String
  147. var image: String
  148. for r in searchResults! {
  149. count = count + 1
  150. // Create a new row
  151. row = RowPastActivity.init(s: "rowPastActivity\(count)", i: count)
  152. id = r.value(forKey: "id")! as! Int
  153. title = r.value(forKey: "title_\(lang!)")! as! String
  154. text = r.value(forKey: "text_\(lang!)")! as! String
  155. row.setTitle(text: title)
  156. row.setText(text: text)
  157. row.id = id
  158. // Get main image
  159. image = ""
  160. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  161. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  162. let imgSortDescriptors = [imgSortDescriptor]
  163. imgFetchRequest.sortDescriptors = imgSortDescriptors
  164. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  165. imgFetchRequest.fetchLimit = 1
  166. do{
  167. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  168. for imgR in imgSearchResults!{
  169. image = imgR.value(forKey: "image")! as! String
  170. row.setImage(filename: image)
  171. }
  172. }
  173. catch {
  174. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  175. }
  176. self.pastList?.addArrangedSubview(row)
  177. }
  178. self.pastList?.setNeedsLayout()
  179. self.pastList?.layoutIfNeeded()
  180. } catch {
  181. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  182. }
  183. }
  184. /**
  185. Gets the device language. The only recognized languages are Spanish, English and Basque.
  186. If the device has another language, Spanish will be selected by default.
  187. :return: Two-letter language code.
  188. */
  189. func getLanguage() -> String{
  190. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  191. if(pre == "es" || pre == "en" || pre == "eu"){
  192. return pre
  193. }
  194. else{
  195. return "es"
  196. }
  197. }
  198. }