ViewController.swift 9.8 KB

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