ActivitiesView.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 activities 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. /**
  39. Default contstructor for the interface builder.
  40. */
  41. override init(frame: CGRect){
  42. super.init(frame: frame)
  43. }
  44. /**
  45. Run when the view is started.
  46. */
  47. required init?(coder aDecoder: NSCoder) {
  48. super.init(coder: aDecoder)
  49. //Load the contents of the HomeView.xib file.
  50. Bundle.main.loadNibNamed("ActivitiesView", owner: self, options: nil)
  51. self.addSubview(container)
  52. self.container.frame = self.bounds
  53. self.futureSection.setTitle(text: "Próximas actividades")
  54. self.pastSection.setTitle(text: "Últimas actividades")
  55. self.futureList = self.futureSection.getContentStack()
  56. self.pastList = self.pastSection.getContentStack()
  57. // Get viewController from StoryBoard
  58. self.storyboard = UIStoryboard(name: "Main", bundle: nil)
  59. self.controller = self.storyboard?.instantiateViewController(withIdentifier: "GMViewController") as? ViewController
  60. self.context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  61. self.delegate = UIApplication.shared.delegate as? AppDelegate
  62. self.lang = self.getLanguage()
  63. self.context?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
  64. // Populate activity lists.
  65. populate()
  66. }
  67. /**
  68. Actually populates the section.
  69. */
  70. func populate(){
  71. // Show future activities
  72. var fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
  73. var sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
  74. var sortDescriptors = [sortDescriptor]
  75. fetchRequest.sortDescriptors = sortDescriptors
  76. fetchRequest.predicate = NSPredicate(format: "date > %@", NSDate())
  77. do {
  78. let searchResults = try self.context?.fetch(fetchRequest)
  79. if searchResults?.count == 0{
  80. let row: RowLabel = RowLabel.init(s: "rowFutureActivity0", i: 0)
  81. row.setText(text: "No hay actividades planeadas proximamente. Pronto organizaremos algo!")
  82. self.futureList?.addArrangedSubview(row)
  83. }
  84. else{
  85. var row : RowFutureActivity
  86. var count = 0
  87. var id: Int
  88. var title: String
  89. var text: String
  90. var image: String
  91. var city: String
  92. var price: Int
  93. var date: NSDate
  94. for r in searchResults! {
  95. count = count + 1
  96. // Create a new row
  97. row = RowFutureActivity.init(s: "rowFutureActivity\(count)", i: count)
  98. id = r.value(forKey: "id")! as! Int
  99. title = r.value(forKey: "title_\(lang!)")! as! String
  100. text = r.value(forKey: "text_\(lang!)")! as! String
  101. city = r.value(forKey: "city")! as! String
  102. price = r.value(forKey: "price")! as! Int
  103. date = r.value(forKey: "date")! as! NSDate
  104. row.setTitle(text: title)
  105. row.setText(text: text)
  106. row.setPrice(price: price)
  107. row.setCity(text: city)
  108. row.setDate(date: date, lang: lang!)
  109. row.id = id
  110. // Get main image
  111. image = ""
  112. let imgFetchRequest: NSFetchRequest<Activity_image> = Activity_image.fetchRequest()
  113. let imgSortDescriptor = NSSortDescriptor(key: "idx", ascending: true)
  114. let imgSortDescriptors = [imgSortDescriptor]
  115. imgFetchRequest.sortDescriptors = imgSortDescriptors
  116. imgFetchRequest.predicate = NSPredicate(format: "activity == %i", id)
  117. imgFetchRequest.fetchLimit = 1
  118. do{
  119. let imgSearchResults = try self.context?.fetch(imgFetchRequest)
  120. for imgR in imgSearchResults!{
  121. image = imgR.value(forKey: "image")! as! String
  122. row.setImage(filename: image)
  123. }
  124. } catch {
  125. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  126. }
  127. self.futureList?.addArrangedSubview(row)
  128. }
  129. self.futureList?.setNeedsLayout()
  130. self.futureList?.layoutIfNeeded()
  131. }
  132. }
  133. catch {
  134. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  135. }
  136. // Show past activities
  137. fetchRequest = Activity.fetchRequest()
  138. sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
  139. sortDescriptors = [sortDescriptor]
  140. fetchRequest.sortDescriptors = sortDescriptors
  141. fetchRequest.predicate = NSPredicate(format: "date < %@", NSDate())
  142. do {
  143. let searchResults = try self.context?.fetch(fetchRequest)
  144. var row: RowPastActivity
  145. var count: Int = 0
  146. var id: Int
  147. var title: String
  148. var text: String
  149. var image: String
  150. for r in searchResults! {
  151. count = count + 1
  152. // Create a new row
  153. row = RowPastActivity.init(s: "rowPastActivity\(count)", i: count)
  154. id = r.value(forKey: "id")! as! Int
  155. title = r.value(forKey: "title_\(lang!)")! as! String
  156. if ((r.value(forKey: "after_\(lang!)") != nil) && ((r.value(forKey: "after_\(lang!)")! as! String).length != 0)){
  157. text = r.value(forKey: "after_\(lang!)")! as! String
  158. }
  159. else{
  160. text = r.value(forKey: "text_\(lang!)")! as! String
  161. }
  162. row.setTitle(text: title)
  163. row.setText(text: text)
  164. row.id = id
  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. row.setImage(filename: image)
  178. }
  179. }
  180. catch {
  181. NSLog(":ACTIVITIES:ERROR: Error getting image for activity \(id): \(error)")
  182. }
  183. self.pastList?.addArrangedSubview(row)
  184. }
  185. self.pastList?.setNeedsLayout()
  186. self.pastList?.layoutIfNeeded()
  187. } catch {
  188. NSLog(":ACTIVITIES:ERROR: Error with request: \(error)")
  189. }
  190. }
  191. /**
  192. Gets the device language. The only recognized languages are Spanish, English and Basque.
  193. If the device has another language, Spanish will be selected by default.
  194. :return: Two-letter language code.
  195. */
  196. func getLanguage() -> String{
  197. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  198. if(pre == "es" || pre == "en" || pre == "eu"){
  199. return pre
  200. }
  201. else{
  202. return "es"
  203. }
  204. }
  205. }