ViewController.swift 10 KB

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