ViewController.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate {
  26. var delegate: AppDelegate?
  27. //The menu collection.
  28. var sectionCollection: UICollectionView!
  29. //Each of the sections of the app.
  30. @IBOutlet var containerViewGallery: GalleryView!
  31. @IBOutlet var containerViewBlog: BlogView!
  32. @IBOutlet var containerViewActivities: UIView!
  33. @IBOutlet var containerViewLablanca: LablancaView!
  34. @IBOutlet var containerViewLocation: UIView!
  35. @IBOutlet var containerViewHome: HomeView!
  36. var passId: Int = -1
  37. func getContext () -> NSManagedObjectContext {
  38. //let appDelegate = UIApplication.shared.delegate as! AppDelegate
  39. //return appDelegate.persistentContainer.viewContext
  40. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  41. }
  42. func showPost(id: Int){
  43. print("CONTROLLER:DEBUG: Showing Post \(id)")
  44. self.passId = id
  45. performSegue(withIdentifier: "SeguePost", sender: nil)
  46. }
  47. func showAlbum(id: Int){
  48. NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
  49. self.passId = id
  50. // TODO: Uncomment when ready
  51. performSegue(withIdentifier: "SegueAlbum", sender: nil)
  52. }
  53. func showSchedule(margolari: Bool){
  54. NSLog(":CONTROLLER:DEBUG: Showing schedule. Margolari: \(margolari)")
  55. if margolari == true{
  56. self.passId = 1
  57. }
  58. else{
  59. self.passId = 0
  60. }
  61. // TODO: Uncomment when ready
  62. performSegue(withIdentifier: "SegueSchedule", sender: nil)
  63. }
  64. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  65. NSLog(":CONTROLLER:DEBUG: preparing for segue '\(segue.identifier)' with id \(self.passId)")
  66. if segue.identifier == "SeguePost"{
  67. (segue.destination as! PostViewController).id = passId
  68. }
  69. if segue.identifier == "SegueAlbum"{
  70. (segue.destination as! AlbumViewController).id = passId
  71. }
  72. if segue.identifier == "SegueSchedule"{
  73. if passId == 1{
  74. (segue.destination as! ScheduleViewController).margolari = true
  75. }
  76. else{
  77. (segue.destination as! ScheduleViewController).margolari = false
  78. }
  79. }
  80. }
  81. /**
  82. Run when the app loads.
  83. */
  84. override func viewDidLoad() {
  85. //Hide all sections, except for the first one
  86. self.containerViewLocation.alpha = 0
  87. self.containerViewLablanca.alpha = 0
  88. self.containerViewActivities.alpha = 0
  89. self.containerViewBlog.alpha = 0
  90. self.containerViewGallery.alpha = 0
  91. //self.containerViewBlog.isHidden = true
  92. print("CONTROLLER:LOG: viewDidLoad()")
  93. print("CONTROLLER:DEBUG: Skyp sync")
  94. //Sync()
  95. super.viewDidLoad()
  96. // Do any additional setup after loading the view, typically from a nib.
  97. //self.containerViewBlog.setController(controller: self as ViewController)
  98. delegate = UIApplication.shared.delegate as! AppDelegate
  99. delegate?.controller = self
  100. }
  101. /**
  102. Dispose of any resources that can be recreated.
  103. */
  104. override func didReceiveMemoryWarning() {
  105. super.didReceiveMemoryWarning()
  106. }
  107. //Menu items.
  108. let reuseIdentifier = "cell"
  109. var selected = 0
  110. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  111. // MARK: - UICollectionViewDataSource protocol
  112. //Tell the collection view how many cells to make
  113. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  114. return self.items.count
  115. }
  116. //Make a cell for each section
  117. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  118. //Get a reference to our storyboard cell
  119. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  120. //Set text
  121. cell.label.text = self.items[indexPath.item]
  122. //Mark the first cell as selected...
  123. if indexPath.item == selected{
  124. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  125. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  126. }
  127. //... and the others as unselected
  128. else{
  129. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  130. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  131. }
  132. return cell
  133. }
  134. // MARK: - UICollectionViewDelegate protocol
  135. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  136. sectionCollection = collectionView
  137. showComponent(selected: indexPath.item)
  138. }
  139. @IBAction func showComponent(selected: Int) {
  140. //Activate label
  141. var i = 0
  142. for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
  143. if i == selected + 1{
  144. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  145. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  146. }
  147. else{
  148. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  149. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  150. }
  151. i = i + 1
  152. }
  153. //Show the view
  154. if selected == 0 {
  155. UIView.animate(withDuration: 0.5, animations: {
  156. self.containerViewHome.alpha = 1
  157. self.containerViewLocation.alpha = 0
  158. self.containerViewLablanca.alpha = 0
  159. self.containerViewActivities.alpha = 0
  160. self.containerViewBlog.alpha = 0
  161. self.containerViewGallery.alpha = 0
  162. })
  163. }
  164. else if selected == 1 {
  165. UIView.animate(withDuration: 0.5, animations: {
  166. self.containerViewHome.alpha = 0
  167. self.containerViewLocation.alpha = 1
  168. self.containerViewLablanca.alpha = 0
  169. self.containerViewActivities.alpha = 0
  170. self.containerViewBlog.alpha = 0
  171. self.containerViewGallery.alpha = 0
  172. })
  173. }
  174. else if selected == 2 {
  175. UIView.animate(withDuration: 0.5, animations: {
  176. self.containerViewHome.alpha = 0
  177. self.containerViewLocation.alpha = 0
  178. self.containerViewLablanca.alpha = 1
  179. self.containerViewActivities.alpha = 0
  180. self.containerViewBlog.alpha = 0
  181. self.containerViewGallery.alpha = 0
  182. })
  183. }
  184. else if selected == 3 {
  185. UIView.animate(withDuration: 0.5, animations: {
  186. self.containerViewHome.alpha = 0
  187. self.containerViewLocation.alpha = 0
  188. self.containerViewLablanca.alpha = 0
  189. self.containerViewActivities.alpha = 1
  190. self.containerViewBlog.alpha = 0
  191. self.containerViewGallery.alpha = 0
  192. })
  193. }
  194. else if selected == 4 {
  195. UIView.animate(withDuration: 0.5, animations: {
  196. self.containerViewHome.alpha = 0
  197. self.containerViewLocation.alpha = 0
  198. self.containerViewLablanca.alpha = 0
  199. self.containerViewActivities.alpha = 0
  200. self.containerViewBlog.alpha = 1
  201. self.containerViewGallery.alpha = 0
  202. })
  203. }
  204. else if selected == 5 {
  205. UIView.animate(withDuration: 0.5, animations: {
  206. self.containerViewHome.alpha = 0
  207. self.containerViewLocation.alpha = 0
  208. self.containerViewLablanca.alpha = 0
  209. self.containerViewActivities.alpha = 0
  210. self.containerViewBlog.alpha = 0
  211. self.containerViewGallery.alpha = 1
  212. })
  213. }
  214. }
  215. }