ViewController.swift 6.3 KB

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