|
|
@@ -22,7 +22,7 @@ import UIKit
|
|
|
import CoreData
|
|
|
|
|
|
/**
|
|
|
-The view controller of the app.
|
|
|
+The controller of the schedule view.
|
|
|
*/
|
|
|
class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
|
|
|
@@ -34,12 +34,21 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
@IBOutlet weak var lbDayName: UILabel!
|
|
|
@IBOutlet weak var svScheduleList: UIStackView!
|
|
|
|
|
|
+ var context: NSManagedObjectContext? = nil
|
|
|
+ var lang: String? = nil
|
|
|
+ var delegate: AppDelegate? = nil
|
|
|
+
|
|
|
// Margolari schedule indicator
|
|
|
var margolari: Bool = false
|
|
|
-
|
|
|
- // App delegate
|
|
|
- var delegate: AppDelegate?
|
|
|
-
|
|
|
+
|
|
|
+ // Day indicator
|
|
|
+ var days: [String] = [String]()
|
|
|
+ var selectedDay: Int = 0
|
|
|
+
|
|
|
+ /**
|
|
|
+ Gets the application context.
|
|
|
+ :return: The application context.
|
|
|
+ */
|
|
|
func getContext () -> NSManagedObjectContext {
|
|
|
return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
|
|
|
}
|
|
|
@@ -91,39 +100,91 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
var locationId: Int
|
|
|
var location: String
|
|
|
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: 1")
|
|
|
- let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
|
|
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
- let lang : String = getLanguage()
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: 2")
|
|
|
- context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
|
|
|
+ self.context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
|
|
+ self.delegate = UIApplication.shared.delegate as! AppDelegate
|
|
|
+ self.lang = getLanguage()
|
|
|
+ self.context?.persistentStoreCoordinator = self.delegate?.persistentStoreCoordinator
|
|
|
+
|
|
|
+ // Get days
|
|
|
+ let dayFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_event")
|
|
|
+ dayFetchRequest.propertiesToFetch = ["day"]
|
|
|
+ if self.margolari == true{
|
|
|
+ dayFetchRequest.predicate = NSPredicate(format: "gm = %i", 1)
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ dayFetchRequest.predicate = NSPredicate(format: "gm = %i", 0)
|
|
|
+ }
|
|
|
+
|
|
|
+ dayFetchRequest.returnsDistinctResults = true
|
|
|
+ dayFetchRequest.resultType = NSFetchRequestResultType.dictionaryResultType
|
|
|
+
|
|
|
+ do {
|
|
|
+ let results = try self.context?.fetch(dayFetchRequest)
|
|
|
+
|
|
|
+ let resultsDict = results as! [[String: String]]
|
|
|
+ for r in resultsDict {
|
|
|
+ self.days.append(r["name"]!)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch let err as NSError {
|
|
|
+ NSLog(":SCHEDULECONTROLLER:ERROR: Error getting day list: \(err)")
|
|
|
+ }
|
|
|
+
|
|
|
+ // Select the day to show initially.
|
|
|
+ // TODO: Check if any day corresponds to the current date.
|
|
|
+ self.selectedDay = 0
|
|
|
+
|
|
|
+ // Set listenerts for the arrow buttons
|
|
|
+ let tapRecognizerPrevDay = UITapGestureRecognizer(target: self, action: #selector(prevDay(_:)))
|
|
|
+ self.btPrev.isUserInteractionEnabled = true
|
|
|
+ self.btPrev.addGestureRecognizer(tapRecognizerPrevDay)
|
|
|
+ let tapRecognizerNextDay = UITapGestureRecognizer(target: self, action: #selector(nextDay(_:)))
|
|
|
+ self.btNext.isUserInteractionEnabled = true
|
|
|
+ self.btNext.addGestureRecognizer(tapRecognizerNextDay)
|
|
|
+
|
|
|
+ loadDay()
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ Populates the schedule list with tehe events of the currently selected days.
|
|
|
+ Usually there is no need to call this function manually.
|
|
|
+ */
|
|
|
+ func loadDay(){
|
|
|
+
|
|
|
+ // TODO: Clear list.
|
|
|
+ // TODO: Set toolbar elements.
|
|
|
+
|
|
|
+ var rowcount = 0
|
|
|
+ var row: RowSchedule
|
|
|
+ var start: NSDate
|
|
|
+ var title: String
|
|
|
+ var text: String
|
|
|
+ var locationId: Int
|
|
|
+ var location: String
|
|
|
+
|
|
|
let fetchRequest: NSFetchRequest<Festival_event> = Festival_event.fetchRequest()
|
|
|
let sortDescriptor = NSSortDescriptor(key: "start", ascending: false)
|
|
|
let sortDescriptors = [sortDescriptor]
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: 3")
|
|
|
fetchRequest.sortDescriptors = sortDescriptors
|
|
|
|
|
|
if margolari == true{
|
|
|
- fetchRequest.predicate = NSPredicate(format: "gm = %i", 1)
|
|
|
+ fetchRequest.predicate = NSPredicate(format: "(gm = %i) AND (day = @)", argumentArray: [1, days[selectedDay]])
|
|
|
}
|
|
|
else{
|
|
|
- fetchRequest.predicate = NSPredicate(format: "gm = %i", 0)
|
|
|
+ fetchRequest.predicate = NSPredicate(format: "(gm = %i) AND (day = @)", argumentArray: [0, days[selectedDay]])
|
|
|
}
|
|
|
-
|
|
|
- // TODO: filter by date, not by number
|
|
|
- fetchRequest.fetchLimit = 20
|
|
|
-
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: 4")
|
|
|
-
|
|
|
+
|
|
|
do {
|
|
|
|
|
|
// Get the result
|
|
|
- let searchResults = try context.fetch(fetchRequest)
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: 5")
|
|
|
+ let searchResults = try self.context?.fetch(fetchRequest)
|
|
|
|
|
|
- NSLog(":SCHEDULECONTROLLER:DEBUG: Total events: \(searchResults.count)")
|
|
|
+ NSLog(":SCHEDULECONTROLLER:DEBUG: Total events: \(searchResults?.count)")
|
|
|
|
|
|
- for r in searchResults as [NSManagedObject] {
|
|
|
+ for r in searchResults as! [NSManagedObject] {
|
|
|
|
|
|
title = r.value(forKey: "title_\(lang)") as! String
|
|
|
if let tx = r.value(forKey: "description_\(lang)"){
|
|
|
@@ -145,9 +206,9 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
locationFetchRequest.predicate = NSPredicate(format: "id == %i", locationId)
|
|
|
locationFetchRequest.fetchLimit = 1
|
|
|
do{
|
|
|
- var locationSearchResults = try context.fetch(locationFetchRequest)
|
|
|
- var locationR = locationSearchResults[0]
|
|
|
- location = locationR.value(forKey: "name_\(lang)")! as! String
|
|
|
+ var locationSearchResults = try self.context?.fetch(locationFetchRequest)
|
|
|
+ var locationR = locationSearchResults?[0]
|
|
|
+ location = locationR?.value(forKey: "name_\(lang)")! as! String
|
|
|
|
|
|
row.setLocation(text: location)
|
|
|
} catch {
|
|
|
@@ -156,8 +217,6 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
|
|
|
NSLog(":SCHEDULECONTROLLER:DEBUG: Adding row: height: \(row.frame.height)")
|
|
|
svScheduleList.addArrangedSubview(row)
|
|
|
- //row.setNeedsLayout()
|
|
|
- //row.layoutIfNeeded()
|
|
|
|
|
|
rowcount = rowcount + 1
|
|
|
}
|
|
|
@@ -170,6 +229,41 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ Shows the schedule of the next day
|
|
|
+ :param: sender Event trigger.
|
|
|
+ */
|
|
|
+ func nextDay(_ sender:UITapGestureRecognizer? = nil){
|
|
|
+ changeDay(increment: 1)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ Shows the schedule of the previous day.
|
|
|
+ :param: sender Event trigger.
|
|
|
+ */
|
|
|
+ func prevDay(_ sender:UITapGestureRecognizer? = nil){
|
|
|
+ changeDay(increment: -1)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ Changes the day by a fixed amount of days.
|
|
|
+ Usualli called to move one step forward or backward.
|
|
|
+ :param: increment Days to advance the schedule. Negative values to back it up.
|
|
|
+ */
|
|
|
+ func changeDay(increment: Int){
|
|
|
+ selectedDay = selectedDay + increment
|
|
|
+ if selectedDay <= -1{
|
|
|
+ selectedDay = 0
|
|
|
+ }
|
|
|
+ if self.selectedDay >= self.days.count{
|
|
|
+ selectedDay = (self.days.count - 1)
|
|
|
+ }
|
|
|
+ loadDay()
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
Gets the device language.
|
|
|
@@ -186,4 +280,3 @@ class ScheduleViewController: UIViewController, UIGestureRecognizerDelegate {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|