ActivitiesView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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. row.id = id
  107. // Get main image
  108. image = ""
  109. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  110. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  111. let imgSortDescriptors = [imgSortDescriptor]
  112. imgFetchRequest.sortDescriptors = imgSortDescriptors
  113. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  114. imgFetchRequest.fetchLimit = 1
  115. do{
  116. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  117. for imgR in imgSearchResults!{
  118. image = imgR.value(forKey: "image")! as! String
  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. }
  126. self.futureList?.setNeedsLayout()
  127. self.futureList?.layoutIfNeeded()
  128. }
  129. }
  130. catch {
  131. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  132. }
  133. // Show past activities
  134. fetchRequest = Activity.fetchRequest()
  135. sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  136. sortDescriptors = [sortDescriptor]
  137. fetchRequest.sortDescriptors = sortDescriptors
  138. fetchRequest.predicate = NSPredicate(format: "date < %@", NSDate())
  139. do {
  140. let searchResults = try self.context?.fetch(fetchRequest)
  141. var row: RowPastActivity
  142. var count: Int = 0
  143. var id: Int
  144. var title: String
  145. var text: String
  146. var image: String
  147. for r in searchResults! {
  148. count = count + 1
  149. // Create a new row
  150. row = RowPastActivity.init(s: "rowPastActivity\(count)", i: count)
  151. id = r.value(forKey: "id")! as! Int
  152. title = r.value(forKey: "title_\(lang!)")! as! String
  153. text = r.value(forKey: "text_\(lang!)")! as! String
  154. row.setTitle(text: title)
  155. row.setText(text: text)
  156. row.id = id
  157. // Get main image
  158. image = ""
  159. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  160. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  161. let imgSortDescriptors = [imgSortDescriptor]
  162. imgFetchRequest.sortDescriptors = imgSortDescriptors
  163. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  164. imgFetchRequest.fetchLimit = 1
  165. do{
  166. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  167. for imgR in imgSearchResults!{
  168. image = imgR.value(forKey: "image")! as! String
  169. row.setImage(filename: image)
  170. }
  171. }
  172. catch {
  173. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  174. }
  175. self.pastList?.addArrangedSubview(row)
  176. }
  177. self.pastList?.setNeedsLayout()
  178. self.pastList?.layoutIfNeeded()
  179. } catch {
  180. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  181. }
  182. }
  183. /**
  184. Gets the device language. The only recognized languages are Spanish, English and Basque.
  185. If the device has another language, Spanish will be selected by default.
  186. :return: Two-letter language code.
  187. */
  188. func getLanguage() -> String{
  189. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  190. if(pre == "es" || pre == "en" || pre == "eu"){
  191. return pre
  192. }
  193. else{
  194. return "es"
  195. }
  196. }
  197. }