ViewController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. Sync()
  177. }
  178. /**
  179. Asynchronously gets a location report from the server.
  180. */
  181. @objc func fetchLocation(){
  182. FetchLocation()
  183. }
  184. /**
  185. Generates a random string to be used as a user identifier.
  186. :param: length The length of the generated string.
  187. :return: A random alphanumeric string with the indicated length.
  188. */
  189. func randomString(length: Int) -> String {
  190. let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  191. let len = UInt32(letters.length)
  192. var randomString = ""
  193. for _ in 0 ..< length {
  194. let rand = arc4random_uniform(len)
  195. var nextChar = letters.character(at: Int(rand))
  196. randomString += NSString(characters: &nextChar, length: 1) as String
  197. }
  198. return randomString
  199. }
  200. /**
  201. Dispose of any resources that can be recreated.
  202. */
  203. override func didReceiveMemoryWarning() {
  204. super.didReceiveMemoryWarning()
  205. }
  206. // MARK: - UICollectionViewDataSource protocol
  207. /**
  208. Sets a collection of menu items.
  209. :param: numberOfItemsInSection The number of entries of the menu.
  210. :return: The number of entries of the menu.
  211. */
  212. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  213. return self.items.count
  214. }
  215. /**
  216. Sets up the menu bar.
  217. :param: cellForItemAt indexPath One cell of the menu.
  218. */
  219. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  220. //Get a reference to our storyboard cell
  221. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuCollectionViewCell
  222. //Set text
  223. cell.label.text = self.items[indexPath.item]
  224. // Mark the first one as selected.
  225. if indexPath.item == 0{
  226. cell.isSelected = true
  227. }
  228. return cell
  229. }
  230. // MARK: - UICollectionViewDelegate protocol
  231. /**
  232. Inits items in the menu bar.
  233. :paramm: didSelectItemAt item index
  234. */
  235. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  236. sectionCollection = collectionView
  237. showComponent(selected: indexPath.item)
  238. }
  239. /**
  240. Caled when an item of the menu bar is selected.
  241. :param: selected Index of the selected item.
  242. */
  243. @IBAction func showComponent(selected: Int) {
  244. // Reverts the style of the first tab.
  245. if selected != 0{
  246. if self.sectionCollection != nil{
  247. self.sectionCollection.visibleCells[0].isSelected = false
  248. }
  249. }
  250. //Show the view
  251. if selected == 0 {
  252. UIView.animate(withDuration: 0.5, animations: {
  253. self.containerViewHome.alpha = 1
  254. self.containerViewLocation.alpha = 0
  255. self.containerViewLablanca.alpha = 0
  256. self.containerViewActivities.alpha = 0
  257. self.containerViewBlog.alpha = 0
  258. self.containerViewGallery.alpha = 0
  259. })
  260. }
  261. else if selected == 1 {
  262. UIView.animate(withDuration: 0.5, animations: {
  263. self.containerViewHome.alpha = 0
  264. self.containerViewLocation.alpha = 1
  265. self.containerViewLablanca.alpha = 0
  266. self.containerViewActivities.alpha = 0
  267. self.containerViewBlog.alpha = 0
  268. self.containerViewGallery.alpha = 0
  269. })
  270. }
  271. else if selected == 2 {
  272. UIView.animate(withDuration: 0.5, animations: {
  273. self.containerViewHome.alpha = 0
  274. self.containerViewLocation.alpha = 0
  275. self.containerViewLablanca.alpha = 1
  276. self.containerViewActivities.alpha = 0
  277. self.containerViewBlog.alpha = 0
  278. self.containerViewGallery.alpha = 0
  279. })
  280. }
  281. else if selected == 3 {
  282. UIView.animate(withDuration: 0.5, animations: {
  283. self.containerViewHome.alpha = 0
  284. self.containerViewLocation.alpha = 0
  285. self.containerViewLablanca.alpha = 0
  286. self.containerViewActivities.alpha = 1
  287. self.containerViewBlog.alpha = 0
  288. self.containerViewGallery.alpha = 0
  289. })
  290. }
  291. else if selected == 4 {
  292. UIView.animate(withDuration: 0.5, animations: {
  293. self.containerViewHome.alpha = 0
  294. self.containerViewLocation.alpha = 0
  295. self.containerViewLablanca.alpha = 0
  296. self.containerViewActivities.alpha = 0
  297. self.containerViewBlog.alpha = 1
  298. self.containerViewGallery.alpha = 0
  299. })
  300. }
  301. else if selected == 5 {
  302. UIView.animate(withDuration: 0.5, animations: {
  303. self.containerViewHome.alpha = 0
  304. self.containerViewLocation.alpha = 0
  305. self.containerViewLablanca.alpha = 0
  306. self.containerViewActivities.alpha = 0
  307. self.containerViewBlog.alpha = 0
  308. self.containerViewGallery.alpha = 1
  309. })
  310. }
  311. }
  312. }