ViewController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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: ActivitiesView!
  35. @IBOutlet var containerViewLablanca: LablancaView!
  36. @IBOutlet var containerViewLocation: UIView!
  37. @IBOutlet var containerViewHome: HomeView!
  38. var passId: Int = -1
  39. /**
  40. Controller initializer.
  41. */
  42. required init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44. }
  45. /**
  46. Retrieves the application context.
  47. :return: The application context.
  48. */
  49. func getContext () -> NSManagedObjectContext {
  50. //let appDelegate = UIApplication.shared.delegate as! AppDelegate
  51. //return appDelegate.persistentContainer.viewContext
  52. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  53. }
  54. /**
  55. Shows a post by loading the controller.
  56. :param: id The post id.
  57. */
  58. func showPost(id: Int){
  59. NSLog(":CONTROLLER:DEBUG: Showing Post \(id)")
  60. self.passId = id
  61. performSegue(withIdentifier: "SeguePost", sender: nil)
  62. }
  63. /**
  64. Shows a post by loading the controller.
  65. :param: id The album id.
  66. */
  67. func showAlbum(id: Int){
  68. NSLog(":CONTROLLER:DEBUG: Showing album \(id)")
  69. self.passId = id
  70. performSegue(withIdentifier: "SegueAlbum", sender: nil)
  71. }
  72. /**
  73. Handles the initial sync process.
  74. It can start it, showing the sync screen, or finish it, hidding the screen.
  75. :param: showScreen True to start the sync, false to end it.
  76. */
  77. func initialSync(showScreen: Bool){
  78. if showScreen == true{
  79. NSLog(":CONTROLLER:DEBUG: Showing initial sync screen.")
  80. performSegue(withIdentifier: "SegueSync", sender: nil)
  81. Sync(synchronous: true)
  82. }
  83. else{
  84. NSLog(":CONTROLLER:DEBUG: Hidding initial sync screen.")
  85. syncSegue?.destination.dismiss(animated: true, completion: nil)
  86. }
  87. }
  88. /**
  89. Run before performing a segue.
  90. Assigns id if neccessary.
  91. :param: segue The segue to perform.
  92. :sender: The calling view.
  93. */
  94. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  95. NSLog(":CONTROLLER:DEBUG: preparing for segue '\(segue.identifier)' with id \(self.passId)")
  96. if segue.identifier == "SeguePost"{
  97. (segue.destination as! PostViewController).id = passId
  98. }
  99. if segue.identifier == "SegueAlbum"{
  100. (segue.destination as! AlbumViewController).id = passId
  101. }
  102. if segue.identifier == "SegueSync"{
  103. self.syncSegue = segue
  104. }
  105. }
  106. /**
  107. Run when the app loads.
  108. */
  109. override func viewDidLoad() {
  110. //Hide all sections, except for the first one
  111. self.containerViewLocation.alpha = 0
  112. self.containerViewLablanca.alpha = 0
  113. self.containerViewActivities.alpha = 0
  114. self.containerViewBlog.alpha = 0
  115. self.containerViewGallery.alpha = 0
  116. NSLog(":CONTROLLER:LOG: viewDidLoad()")
  117. NSLog(":CONTROLLER:DEBUG: Skyp sync")
  118. //Sync()
  119. super.viewDidLoad()
  120. //self.containerViewBlog.setController(controller: self as ViewController)
  121. delegate = UIApplication.shared.delegate as! AppDelegate
  122. delegate?.controller = self
  123. }
  124. /**
  125. Executed when the view is actually shown.
  126. Performs the initial sync in the first run.
  127. It also generates a user id if none exists.
  128. :param: animated Wether the controller appearance must be animated or not.
  129. */
  130. override func viewDidAppear(_ animated: Bool) {
  131. super.viewDidAppear(animated)
  132. if UserDefaults.standard.object(forKey: "userId") == nil{
  133. let newUid = randomString(length: 16)
  134. let defaults = UserDefaults.standard
  135. defaults.set(newUid, forKey: "userId")
  136. initialSync(showScreen: true)
  137. }
  138. }
  139. /**
  140. Generates a random string to be used as a user identifier.
  141. :param: length The length of the generated string.
  142. :return: A random alphanumeric string with the indicated length.
  143. */
  144. func randomString(length: Int) -> String {
  145. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  146. let len = UInt32(letters.length)
  147. var randomString = ""
  148. for _ in 0 ..< length {
  149. let rand = arc4random_uniform(len)
  150. var nextChar = letters.character(at: Int(rand))
  151. randomString += NSString(characters: &nextChar, length: 1) as String
  152. }
  153. return randomString
  154. }
  155. /**
  156. Dispose of any resources that can be recreated.
  157. */
  158. override func didReceiveMemoryWarning() {
  159. super.didReceiveMemoryWarning()
  160. }
  161. //Menu items.
  162. let reuseIdentifier = "cell"
  163. var selected = 0
  164. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  165. // MARK: - UICollectionViewDataSource protocol
  166. //Tell the collection view how many cells to make
  167. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  168. return self.items.count
  169. }
  170. //Make a cell for each section
  171. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  172. //Get a reference to our storyboard cell
  173. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  174. //Set text
  175. cell.label.text = self.items[indexPath.item]
  176. //Mark the first cell as selected...
  177. if indexPath.item == selected{
  178. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  179. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  180. }
  181. //... and the others as unselected
  182. else{
  183. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  184. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  185. }
  186. return cell
  187. }
  188. // MARK: - UICollectionViewDelegate protocol
  189. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  190. sectionCollection = collectionView
  191. showComponent(selected: indexPath.item)
  192. }
  193. @IBAction func showComponent(selected: Int) {
  194. //Activate label
  195. var i = 0
  196. for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
  197. if i == selected + 1{
  198. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  199. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  200. }
  201. else{
  202. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  203. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  204. }
  205. i = i + 1
  206. }
  207. //Show the view
  208. if selected == 0 {
  209. UIView.animate(withDuration: 0.5, animations: {
  210. self.containerViewHome.alpha = 1
  211. self.containerViewLocation.alpha = 0
  212. self.containerViewLablanca.alpha = 0
  213. self.containerViewActivities.alpha = 0
  214. self.containerViewBlog.alpha = 0
  215. self.containerViewGallery.alpha = 0
  216. })
  217. }
  218. else if selected == 1 {
  219. UIView.animate(withDuration: 0.5, animations: {
  220. self.containerViewHome.alpha = 0
  221. self.containerViewLocation.alpha = 1
  222. self.containerViewLablanca.alpha = 0
  223. self.containerViewActivities.alpha = 0
  224. self.containerViewBlog.alpha = 0
  225. self.containerViewGallery.alpha = 0
  226. })
  227. }
  228. else if selected == 2 {
  229. UIView.animate(withDuration: 0.5, animations: {
  230. self.containerViewHome.alpha = 0
  231. self.containerViewLocation.alpha = 0
  232. self.containerViewLablanca.alpha = 1
  233. self.containerViewActivities.alpha = 0
  234. self.containerViewBlog.alpha = 0
  235. self.containerViewGallery.alpha = 0
  236. })
  237. }
  238. else if selected == 3 {
  239. UIView.animate(withDuration: 0.5, animations: {
  240. self.containerViewHome.alpha = 0
  241. self.containerViewLocation.alpha = 0
  242. self.containerViewLablanca.alpha = 0
  243. self.containerViewActivities.alpha = 1
  244. self.containerViewBlog.alpha = 0
  245. self.containerViewGallery.alpha = 0
  246. })
  247. }
  248. else if selected == 4 {
  249. UIView.animate(withDuration: 0.5, animations: {
  250. self.containerViewHome.alpha = 0
  251. self.containerViewLocation.alpha = 0
  252. self.containerViewLablanca.alpha = 0
  253. self.containerViewActivities.alpha = 0
  254. self.containerViewBlog.alpha = 1
  255. self.containerViewGallery.alpha = 0
  256. })
  257. }
  258. else if selected == 5 {
  259. UIView.animate(withDuration: 0.5, animations: {
  260. self.containerViewHome.alpha = 0
  261. self.containerViewLocation.alpha = 0
  262. self.containerViewLablanca.alpha = 0
  263. self.containerViewActivities.alpha = 0
  264. self.containerViewBlog.alpha = 0
  265. self.containerViewGallery.alpha = 1
  266. })
  267. }
  268. }
  269. }