ViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // Passed id to perform segues.
  40. var passId: Int = -1
  41. var passAlbum: Int = -1
  42. // Location-related variables.
  43. var locationManager = CLLocationManager()
  44. var didFindMyLocation = false
  45. var locationTimer: Timer? = nil
  46. /**
  47. Controller initializer.
  48. :param: coder Coder.
  49. */
  50. required init?(coder aDecoder: NSCoder) {
  51. NSLog(":CONTROLLER:DEBUG: Init.")
  52. super.init(coder: aDecoder)
  53. }
  54. func getLocation() -> CLLocationCoordinate2D {
  55. return (locationManager.location?.coordinate)!
  56. }
  57. /**
  58. Retrieves the application context.
  59. :return: The application context.
  60. */
  61. func getContext () -> NSManagedObjectContext {
  62. //let appDelegate = UIApplication.shared.delegate as! AppDelegate
  63. //return appDelegate.persistentContainer.viewContext
  64. return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
  65. }
  66. /**
  67. Shows a post.
  68. :param: id The post id.
  69. */
  70. func showPost(id: Int){
  71. self.passId = id
  72. performSegue(withIdentifier: "SeguePost", sender: nil)
  73. }
  74. /**
  75. Shows a past activity.
  76. :param: id The activity id.
  77. */
  78. func showPastActivity(id: Int){
  79. self.passId = id
  80. performSegue(withIdentifier: "SeguePastActivity", sender: nil)
  81. }
  82. /**
  83. Shows a future activity.
  84. :param: id The activity id.
  85. */
  86. func showFutureActivity(id: Int){
  87. self.passId = id
  88. performSegue(withIdentifier: "SegueFutureActivity", sender: nil)
  89. }
  90. /**
  91. Shows an album.
  92. :param: id The album id.
  93. */
  94. func showAlbum(id: Int){
  95. self.passId = id
  96. performSegue(withIdentifier: "SegueAlbum", sender: nil)
  97. }
  98. /**
  99. Shows a photo.
  100. :param: id The album id.
  101. */
  102. func showPhoto(album: Int, photo: Int){
  103. self.passAlbum = album
  104. self.passId = photo
  105. performSegue(withIdentifier: "SeguePhoto", sender: nil)
  106. }
  107. /**
  108. Shows a schedule.
  109. :param: margolari True for the margolari schedule, false for the city one.
  110. */
  111. func showSchedule(margolari: Bool){
  112. if margolari == true{
  113. self.passId = 1
  114. }
  115. else{
  116. self.passId = 0
  117. }
  118. performSegue(withIdentifier: "SegueSchedule", sender: nil)
  119. }
  120. /**
  121. Handles the initial sync process.
  122. It can start it, showing the sync screen, or finish it, hidding the screen.
  123. :param: showScreen True to start the sync, false to end it.
  124. */
  125. func initialSync(showScreen: Bool){
  126. if showScreen == true{
  127. Sync(synchronous: true)
  128. }
  129. else{
  130. syncSegue?.destination.dismiss(animated: true, completion: nil)
  131. }
  132. }
  133. /**
  134. Run before performing a segue.
  135. Assigns id if neccessary.
  136. :param: segue The segue to perform.
  137. :sender: The calling view.
  138. */
  139. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  140. if segue.identifier == "SeguePost"{
  141. (segue.destination as! PostViewController).id = passId
  142. }
  143. if segue.identifier == "SegueAlbum"{
  144. (segue.destination as! AlbumViewController).id = passId
  145. }
  146. if segue.identifier == "SeguePhoto"{
  147. (segue.destination as! PhotoViewController).photoId = passId
  148. (segue.destination as! PhotoViewController).albumId = passAlbum
  149. }
  150. if segue.identifier == "SegueSchedule"{
  151. if passId == 1{
  152. (segue.destination as! ScheduleViewController).margolari = true
  153. }
  154. else{
  155. (segue.destination as! ScheduleViewController).margolari = false
  156. }
  157. }
  158. if segue.identifier == "SegueSync"{
  159. self.syncSegue = segue
  160. }
  161. if segue.identifier == "SeguePastActivity"{
  162. (segue.destination as! PastActivityViewController).id = passId
  163. }
  164. if segue.identifier == "SegueFutureActivity"{
  165. (segue.destination as! FutureActivityViewController).id = passId
  166. }
  167. }
  168. override func viewWillAppear(_ animated: Bool) {
  169. super.viewWillAppear(animated)
  170. }
  171. /**
  172. Run when the app loads.
  173. */
  174. override func viewDidLoad() {
  175. // Ask for location permissions.
  176. locationManager.delegate = self
  177. locationManager.requestWhenInUseAuthorization()
  178. //Hide all sections, except for the first one
  179. self.containerViewLocation.alpha = 0
  180. self.containerViewLablanca.alpha = 0
  181. self.containerViewActivities.alpha = 0
  182. self.containerViewBlog.alpha = 0
  183. self.containerViewGallery.alpha = 0
  184. // Start the location schedule
  185. fetchLocation()
  186. self.locationTimer = Timer.scheduledTimer(timeInterval: 90, target: self, selector: #selector(fetchLocation), userInfo: nil, repeats: true)
  187. NSLog(":CONTROLLER:DEBUG: Don't skyp sync")
  188. //NSLog(":CONTROLLER:DEBUG: Skyp sync")
  189. //Notifications()
  190. //Sync()
  191. self.delegate = UIApplication.shared.delegate as? AppDelegate
  192. self.delegate?.controller = self
  193. //self.sectionCollection.visibleCells[0].isSelected = true
  194. super.viewDidLoad()
  195. }
  196. func fetchLocation(){
  197. FetchLocation()
  198. }
  199. /**
  200. Executed when the view is actually shown.
  201. Performs the initial sync in the first run.
  202. It also generates a user id if none exists.
  203. :param: animated Wether the controller appearance must be animated or not.
  204. */
  205. override func viewDidAppear(_ animated: Bool) {
  206. super.viewDidAppear(animated)
  207. /*if UserDefaults.standard.object(forKey: "userId") == nil{
  208. let newUid = randomString(length: 16)
  209. let defaults = UserDefaults.standard
  210. defaults.set(newUid, forKey: "userId")
  211. initialSync(showScreen: true)
  212. }*/
  213. }
  214. /**
  215. Generates a random string to be used as a user identifier.
  216. :param: length The length of the generated string.
  217. :return: A random alphanumeric string with the indicated length.
  218. */
  219. func randomString(length: Int) -> String {
  220. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  221. let len = UInt32(letters.length)
  222. var randomString = ""
  223. for _ in 0 ..< length {
  224. let rand = arc4random_uniform(len)
  225. var nextChar = letters.character(at: Int(rand))
  226. randomString += NSString(characters: &nextChar, length: 1) as String
  227. }
  228. return randomString
  229. }
  230. /**
  231. Dispose of any resources that can be recreated.
  232. */
  233. override func didReceiveMemoryWarning() {
  234. super.didReceiveMemoryWarning()
  235. }
  236. //Menu items.
  237. let reuseIdentifier = "cell"
  238. var selected = 0
  239. var items = ["Home", "Location", "La Blanca", "Actividades", "Blog", "Gallery"]
  240. // MARK: - UICollectionViewDataSource protocol
  241. /**
  242. Sets a collection of menu items.
  243. :param: numberOfItemsInSection The number of entries of the menu.
  244. :return: The number of entries of the menu.
  245. */
  246. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  247. return self.items.count
  248. }
  249. //Make a cell for each section
  250. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  251. //Get a reference to our storyboard cell
  252. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  253. //Set text
  254. cell.label.text = self.items[indexPath.item]
  255. // Mark the first one as selected.
  256. if indexPath.item == 0{
  257. cell.isSelected = true
  258. }
  259. return cell
  260. }
  261. // MARK: - UICollectionViewDelegate protocol
  262. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  263. sectionCollection = collectionView
  264. showComponent(selected: indexPath.item)
  265. }
  266. /**
  267. Caled when an item of the menu bar is selected.
  268. :param: selected Index of the selected item.
  269. */
  270. @IBAction func showComponent(selected: Int) {
  271. //Show the view
  272. if selected == 0 {
  273. UIView.animate(withDuration: 0.5, animations: {
  274. self.containerViewHome.alpha = 1
  275. self.containerViewLocation.alpha = 0
  276. self.containerViewLablanca.alpha = 0
  277. self.containerViewActivities.alpha = 0
  278. self.containerViewBlog.alpha = 0
  279. self.containerViewGallery.alpha = 0
  280. })
  281. }
  282. else if selected == 1 {
  283. UIView.animate(withDuration: 0.5, animations: {
  284. self.containerViewHome.alpha = 0
  285. self.containerViewLocation.alpha = 1
  286. self.containerViewLablanca.alpha = 0
  287. self.containerViewActivities.alpha = 0
  288. self.containerViewBlog.alpha = 0
  289. self.containerViewGallery.alpha = 0
  290. })
  291. }
  292. else if selected == 2 {
  293. UIView.animate(withDuration: 0.5, animations: {
  294. self.containerViewHome.alpha = 0
  295. self.containerViewLocation.alpha = 0
  296. self.containerViewLablanca.alpha = 1
  297. self.containerViewActivities.alpha = 0
  298. self.containerViewBlog.alpha = 0
  299. self.containerViewGallery.alpha = 0
  300. })
  301. }
  302. else if selected == 3 {
  303. UIView.animate(withDuration: 0.5, animations: {
  304. self.containerViewHome.alpha = 0
  305. self.containerViewLocation.alpha = 0
  306. self.containerViewLablanca.alpha = 0
  307. self.containerViewActivities.alpha = 1
  308. self.containerViewBlog.alpha = 0
  309. self.containerViewGallery.alpha = 0
  310. })
  311. }
  312. else if selected == 4 {
  313. UIView.animate(withDuration: 0.5, animations: {
  314. self.containerViewHome.alpha = 0
  315. self.containerViewLocation.alpha = 0
  316. self.containerViewLablanca.alpha = 0
  317. self.containerViewActivities.alpha = 0
  318. self.containerViewBlog.alpha = 1
  319. self.containerViewGallery.alpha = 0
  320. })
  321. }
  322. else if selected == 5 {
  323. UIView.animate(withDuration: 0.5, animations: {
  324. self.containerViewHome.alpha = 0
  325. self.containerViewLocation.alpha = 0
  326. self.containerViewLablanca.alpha = 0
  327. self.containerViewActivities.alpha = 0
  328. self.containerViewBlog.alpha = 0
  329. self.containerViewGallery.alpha = 1
  330. })
  331. }
  332. }
  333. }