ScheduleViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Margolari schedule indicator
  28. var margolari: Bool = false
  29. // App delegate
  30. var delegate: AppDelegate?
  31. func getContext () -> NSManagedObjectContext {
  32. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  33. }
  34. /**
  35. Run when the app loads.
  36. */
  37. override func viewDidLoad() {
  38. NSLog(":SCHEDULECONTROLLER:LOG: Init album.")
  39. super.viewDidLoad()
  40. self.loadSchedule(margolari: margolari)
  41. // Set button action TODO
  42. //barButton.addTarget(self, action: #selector(self.back), for: .touchUpInside)
  43. }
  44. /**
  45. Returns to the main view controller.
  46. */
  47. func back() {
  48. NSLog(":SCHEDULECONTROLLER:DEBUG: Back")
  49. self.dismiss(animated: true, completion: nil)
  50. }
  51. /**
  52. Dispose of any resources that can be recreated.
  53. */
  54. override func didReceiveMemoryWarning() {
  55. super.didReceiveMemoryWarning()
  56. }
  57. /**
  58. Loads the photos of the album
  59. */
  60. public func loadSchedule(margolari: Bool){
  61. NSLog(":SCHEDULECONTROLLER:DEBUG: Loading schedule: Margolariak \(margolari)")
  62. /*
  63. var row : RowAlbum
  64. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  65. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  66. let lang : String = getLanguage()
  67. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  68. let fetchRequest: NSFetchRequest<Photo_album> = Photo_album.fetchRequest()
  69. fetchRequest.predicate = NSPredicate(format: "album = %i", id)
  70. var rowcount = 0
  71. do {
  72. // Get the photo list
  73. let searchResults = try context.fetch(fetchRequest)
  74. var image: String
  75. NSLog(":GALLERYCONTROLLER:DEBUG: Total photos: \(searchResults.count)")
  76. for i in (0..<searchResults.count) where i % 2 == 0 {
  77. var r = searchResults[i]
  78. var photoId = r.value(forKey: "photo")! as! Int
  79. row = RowAlbum.init(s: "rowAlbum\(i)", i: i)
  80. // Get photo info from Photo entity
  81. let imgFetchRequest: NSFetchRequest<Photo> = Photo.fetchRequest()
  82. let imgSortDescriptor = NSSortDescriptor(key: "uploaded", ascending: true)
  83. let imgSortDescriptors = [imgSortDescriptor]
  84. imgFetchRequest.sortDescriptors = imgSortDescriptors
  85. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  86. imgFetchRequest.fetchLimit = 1
  87. do{
  88. var imgSearchResults = try context.fetch(imgFetchRequest)
  89. var imgR = imgSearchResults[0]
  90. image = imgR.value(forKey: "file")! as! String
  91. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) left: \(image)")
  92. row.setImage(idx: 0, filename: image)
  93. if i + 1 < searchResults.count {
  94. r = searchResults[i + 1]
  95. photoId = r.value(forKey: "photo")! as! Int
  96. imgFetchRequest.predicate = NSPredicate(format: "id == %i", photoId)
  97. imgSearchResults = try context.fetch(imgFetchRequest)
  98. imgR = imgSearchResults[0]
  99. image = imgR.value(forKey: "file")! as! String
  100. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: \(image)")
  101. row.setImage(idx: 1, filename: image)
  102. }
  103. else{
  104. NSLog(":GALLERYCONTROLLER:LOG: Image Row \(i) right: none")
  105. }
  106. //TODO create row, add images (L+R), add row to container.
  107. //row.setImage(filename: image)
  108. } catch {
  109. print(":GALLERYCONTROLLER:ERROR: Error getting image for post \(id): \(error)")
  110. }
  111. NSLog(":GALLERYCONTROLLER:DEBUG: Adding row: height: \(row.frame.height)")
  112. albumContainer.addArrangedSubview(row)
  113. rowcount = rowcount + 1
  114. }
  115. } catch {
  116. print("POST:ERROR: Error with request: \(error)")
  117. }
  118. */
  119. }
  120. /**
  121. Gets the device language.
  122. :return: Two letter language code of the device.
  123. */
  124. func getLanguage() -> String{
  125. let pre = NSLocale.preferredLanguages[0].subStr(start: 0, end: 1)
  126. if(pre == "es" || pre == "en" || pre == "eu"){
  127. return pre
  128. }
  129. else{
  130. return "es"
  131. }
  132. }
  133. }