ViewController.swift 9.6 KB

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