ViewController.swift 11 KB

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