ViewController.swift 6.6 KB

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