ViewController.swift 6.9 KB

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