ScheduleViewController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 UIKit
  21. import CoreData
  22. /**
  23. The view controller of the app.
  24. */
  25. class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
  26. // Outlets
  27. @IBOutlet weak var btPrev: UIButton!
  28. @IBOutlet weak var btNext: UIButton!
  29. @IBOutlet weak var lbDayNumber: UILabel!
  30. @IBOutlet weak var lbDayMonth: UILabel!
  31. @IBOutlet weak var lbDayName: UILabel!
  32. @IBOutlet weak var svScheduleList: UIStackView!
  33. // Margolari schedule indicator
  34. var margolari: Bool = false
  35. // App delegate
  36. var delegate: AppDelegate?
  37. func getContext () -> NSManagedObjectContext {
  38. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  39. }
  40. /**
  41. Run when the app loads.
  42. */
  43. override func viewDidLoad() {
  44. NSLog(":SCHEDULECONTROLLER:LOG: Init schedule.")
  45. super.viewDidLoad()
  46. self.loadSchedule(margolari: margolari)
  47. //TODO Title
  48. // TODO Set button action
  49. //barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  50. }
  51. /**
  52. Returns to the main view controller.
  53. */
  54. func back() {
  55. NSLog(":SCHEDULECONTROLLER:DEBUG: Back")
  56. self.dismiss(animated: true, completion: nil)
  57. }
  58. /**
  59. Dispose of any resources that can be recreated.
  60. */
  61. override func didReceiveMemoryWarning() {
  62. super.didReceiveMemoryWarning()
  63. }
  64. /**
  65. Loads the schedule.
  66. */
  67. public func loadSchedule(margolari: Bool){
  68. NSLog(":SCHEDULECONTROLLER:DEBUG: Loading schedule: Margolariak \(margolari)")
  69. var rowcount = 0
  70. var row: RowSchedule
  71. var start: NSDate
  72. var title: String
  73. var text: String
  74. var locationId: Int
  75. var location: String
  76. NSLog(":SCHEDULECONTROLLER:DEBUG: 1")
  77. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  78. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  79. let lang : String = getLanguage()
  80. NSLog(":SCHEDULECONTROLLER:DEBUG: 2")
  81. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  82. let fetchRequest: NSFetchRequest<Festival_event> = Festival_event.fetchRequest()
  83. let sortDescriptor = NSSortDescriptor(key: "start", ascending: false)
  84. let sortDescriptors = [sortDescriptor]
  85. NSLog(":SCHEDULECONTROLLER:DEBUG: 3")
  86. fetchRequest.sortDescriptors = sortDescriptors
  87. if margolari == true{
  88. fetchRequest.predicate = NSPredicate(format: "gm = %i", 1)
  89. }
  90. else{
  91. fetchRequest.predicate = NSPredicate(format: "gm = %i", 0)
  92. }
  93. // TODO: filter by date, not by number
  94. fetchRequest.fetchLimit = 20
  95. NSLog(":SCHEDULECONTROLLER:DEBUG: 4")
  96. do {
  97. // Get the result
  98. let searchResults = try context.fetch(fetchRequest)
  99. NSLog(":SCHEDULECONTROLLER:DEBUG: 5")
  100. NSLog(":SCHEDULECONTROLLER:DEBUG: Total events: \(searchResults.count)")
  101. for r in searchResults as [NSManagedObject] {
  102. title = r.value(forKey: "title_\(lang)") as! String
  103. if let tx = r.value(forKey: "description_\(lang)"){
  104. text = r.value(forKey: "description_\(lang)") as! String
  105. }
  106. else{
  107. text = ""
  108. }
  109. start = r.value(forKey: "start")! as! NSDate
  110. locationId = r.value(forKey: "place")! as! Int
  111. row = RowSchedule.init(s: "rowSchedule\(rowcount)", i: rowcount)
  112. row.setTitle(text: title)
  113. row.setText(text: text)
  114. row.setTime(dtime: start)
  115. // Get location info from Place entity
  116. let locationFetchRequest: NSFetchRequest<Place> = Place.fetchRequest()
  117. locationFetchRequest.predicate = NSPredicate(format: "id == %i", locationId)
  118. locationFetchRequest.fetchLimit = 1
  119. do{
  120. var locationSearchResults = try context.fetch(locationFetchRequest)
  121. var locationR = locationSearchResults[0]
  122. location = locationR.value(forKey: "name_\(lang)")! as! String
  123. row.setLocation(text: location)
  124. } catch {
  125. NSLog(":SCHEDULECONTROLLER:ERROR: Error getting location: \(error)")
  126. }
  127. NSLog(":SCHEDULECONTROLLER:DEBUG: Adding row: height: \(row.frame.height)")
  128. svScheduleList.addArrangedSubview(row)
  129. row.setNeedsLayout()
  130. row.layoutIfNeeded()
  131. rowcount = rowcount + 1
  132. }
  133. } catch {
  134. NSLog(":SCHEDULECONTROLLER:ERROR: Error with request: \(error)")
  135. }
  136. }
  137. /**
  138. Gets the device language.
  139. :return: Two letter language code of the device.
  140. */
  141. func getLanguage() -> String{
  142. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  143. if(pre == "es" || pre == "en" || pre == "eu"){
  144. return pre
  145. }
  146. else{
  147. return "es"
  148. }
  149. }
  150. }