ViewController.swift 11 KB

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