ViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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: 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. func populate(){
  144. self.containerViewHome.populate()
  145. //self.containerLocation.populate()
  146. //self.containerLablanca.populate()
  147. //self.containerActivities.populate()
  148. //self.containerBlog.populate()
  149. //self.containerGallery.populate()
  150. }
  151. /**
  152. Executed when the view is actually shown.
  153. Performs the initial sync in the first run.
  154. It also generates a user id if none exists.
  155. :param: animated Wether the controller appearance must be animated or not.
  156. */
  157. override func viewDidAppear(_ animated: Bool) {
  158. super.viewDidAppear(animated)
  159. if UserDefaults.standard.object(forKey: "userId") == nil{
  160. let newUid = randomString(length: 16)
  161. let defaults = UserDefaults.standard
  162. defaults.set(newUid, forKey: "userId")
  163. initialSync(showScreen: true)
  164. }
  165. }
  166. /**
  167. Generates a random string to be used as a user identifier.
  168. :param: length The length of the generated string.
  169. :return: A random alphanumeric string with the indicated length.
  170. */
  171. func randomString(length: Int) -> String {
  172. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  173. let len = UInt32(letters.length)
  174. var randomString = ""
  175. for _ in 0 ..< length {
  176. let rand = arc4random_uniform(len)
  177. var nextChar = letters.character(at: Int(rand))
  178. randomString += NSString(characters: &nextChar, length: 1) as String
  179. }
  180. return randomString
  181. }
  182. /**
  183. Dispose of any resources that can be recreated.
  184. */
  185. override func didReceiveMemoryWarning() {
  186. super.didReceiveMemoryWarning()
  187. }
  188. //Menu items.
  189. let reuseIdentifier = "cell"
  190. var selected = 0
  191. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  192. // MARK: - UICollectionViewDataSource protocol
  193. //Tell the collection view how many cells to make
  194. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  195. return self.items.count
  196. }
  197. //Make a cell for each section
  198. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  199. //Get a reference to our storyboard cell
  200. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  201. //Set text
  202. cell.label.text = self.items[indexPath.item]
  203. //Mark the first cell as selected...
  204. if indexPath.item == selected{
  205. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  206. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  207. }
  208. //... and the others as unselected
  209. else{
  210. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  211. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  212. }
  213. return cell
  214. }
  215. // MARK: - UICollectionViewDelegate protocol
  216. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  217. sectionCollection = collectionView
  218. showComponent(selected: indexPath.item)
  219. }
  220. @IBAction func showComponent(selected: Int) {
  221. //Activate label
  222. var i = 0
  223. for cell in sectionCollection.visibleCells as! [MenuCollectionViewCell]{ //TODO: Not only visibles!
  224. if i == selected + 1{
  225. cell.bar.backgroundColor = UIColor(red: 90/255, green: 180/255, blue: 255/255, alpha: 1)
  226. cell.label.font = UIFont.boldSystemFont(ofSize: cell.label.font.pointSize)
  227. }
  228. else{
  229. cell.bar.backgroundColor = UIColor(red: 148/255, green: 209/255, blue: 255/255, alpha: 1)
  230. cell.label.font = UIFont.systemFont(ofSize: cell.label.font.pointSize)
  231. }
  232. i = i + 1
  233. }
  234. //Show the view
  235. if selected == 0 {
  236. UIView.animate(withDuration: 0.5, animations: {
  237. self.containerViewHome.alpha = 1
  238. self.containerViewLocation.alpha = 0
  239. self.containerViewLablanca.alpha = 0
  240. self.containerViewActivities.alpha = 0
  241. self.containerViewBlog.alpha = 0
  242. self.containerViewGallery.alpha = 0
  243. })
  244. }
  245. else if selected == 1 {
  246. UIView.animate(withDuration: 0.5, animations: {
  247. self.containerViewHome.alpha = 0
  248. self.containerViewLocation.alpha = 1
  249. self.containerViewLablanca.alpha = 0
  250. self.containerViewActivities.alpha = 0
  251. self.containerViewBlog.alpha = 0
  252. self.containerViewGallery.alpha = 0
  253. })
  254. }
  255. else if selected == 2 {
  256. UIView.animate(withDuration: 0.5, animations: {
  257. self.containerViewHome.alpha = 0
  258. self.containerViewLocation.alpha = 0
  259. self.containerViewLablanca.alpha = 1
  260. self.containerViewActivities.alpha = 0
  261. self.containerViewBlog.alpha = 0
  262. self.containerViewGallery.alpha = 0
  263. })
  264. }
  265. else if selected == 3 {
  266. UIView.animate(withDuration: 0.5, animations: {
  267. self.containerViewHome.alpha = 0
  268. self.containerViewLocation.alpha = 0
  269. self.containerViewLablanca.alpha = 0
  270. self.containerViewActivities.alpha = 1
  271. self.containerViewBlog.alpha = 0
  272. self.containerViewGallery.alpha = 0
  273. })
  274. }
  275. else if selected == 4 {
  276. UIView.animate(withDuration: 0.5, animations: {
  277. self.containerViewHome.alpha = 0
  278. self.containerViewLocation.alpha = 0
  279. self.containerViewLablanca.alpha = 0
  280. self.containerViewActivities.alpha = 0
  281. self.containerViewBlog.alpha = 1
  282. self.containerViewGallery.alpha = 0
  283. })
  284. }
  285. else if selected == 5 {
  286. UIView.animate(withDuration: 0.5, animations: {
  287. self.containerViewHome.alpha = 0
  288. self.containerViewLocation.alpha = 0
  289. self.containerViewLablanca.alpha = 0
  290. self.containerViewActivities.alpha = 0
  291. self.containerViewBlog.alpha = 0
  292. self.containerViewGallery.alpha = 1
  293. })
  294. }
  295. }
  296. }