ViewController.swift 8.4 KB

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