Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.10 KB | None | 0 0
  1. //
  2. // TableViewController.swift
  3. // Yelpify
  4. //
  5. // Created by Ryan Yue on 4/9/16.
  6. // Copyright © 2016 Yelpify. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import ParseUI
  11. import Parse
  12.  
  13. struct playlist
  14. {
  15. static var playlistname: String!
  16. }
  17.  
  18. class TableViewController: UITableViewController, PFLogInViewControllerDelegate, PFSignUpViewControllerDelegate, CLLocationManagerDelegate {
  19.  
  20. override func didReceiveMemoryWarning() {
  21. super.didReceiveMemoryWarning()
  22. // Dispose of any resources that can be recreated.
  23. }
  24.  
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. locationManager.delegate = self
  28. locationManager.requestWhenInUseAuthorization()
  29. locationManager.requestLocation()
  30.  
  31. }
  32.  
  33. override func viewDidAppear(animated: Bool) {
  34. if (PFUser.currentUser() == nil) {
  35. let logInViewController = PFLogInViewController()
  36. logInViewController.delegate = self
  37.  
  38. let signUpViewController = PFSignUpViewController()
  39. signUpViewController.delegate = self
  40.  
  41. logInViewController.signUpController = signUpViewController
  42.  
  43. self.presentViewController(logInViewController, animated: true, completion: nil)
  44.  
  45.  
  46. }
  47. }
  48.  
  49. var locationManager = CLLocationManager()
  50. let client = YelpAPIClient()
  51. var parameters = ["ll": "", "category_filter": "pizza", "radius_filter": "3000", "sort": "0"]
  52. var playlists_location = []
  53. var playlists_user = []
  54.  
  55. var userlatitude: Double!
  56. var userlongitude: Double!
  57. var inputTextField: UITextField!
  58.  
  59. @IBAction func showPlaylistAlert(sender: UIBarButtonItem) {
  60. print("hello")
  61. let alertController = UIAlertController(title: "Create new playlist", message: "Enter name of playlist.", preferredStyle: UIAlertControllerStyle.Alert)
  62.  
  63. alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
  64. textField.placeholder = "Playlist Name"
  65. textField.secureTextEntry = false
  66. self.inputTextField = textField
  67. })
  68. let deleteAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Destructive, handler: {(alert :UIAlertAction!) in
  69. self.view.endEditing(true)
  70. print("Delete button tapped")
  71. })
  72. alertController.addAction(deleteAction)
  73. let okAction = UIAlertAction(title: "Enter", style: UIAlertActionStyle.Default, handler: {(alert :UIAlertAction!) in
  74. let query = PFQuery(className: "Playlists")
  75. query.whereKey("createdbyuser", equalTo: (PFUser.currentUser()?.username!)!)
  76. query.whereKey("playlistName", equalTo: self.inputTextField.text!)
  77. query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
  78. if ((error) == nil)
  79. {
  80. dispatch_async(dispatch_get_main_queue(), {
  81. if (objects!.count == 0)
  82. {
  83. let object = PFObject(className: "Playlists")
  84. object["playlistName"] = self.inputTextField.text!
  85. object["createdbyuser"] = PFUser.currentUser()?.username!
  86. object.saveInBackgroundWithBlock {(success, error) -> Void in
  87. if (error == nil)
  88. {
  89. playlist.playlistname = self.inputTextField.text!
  90. self.performSegueWithIdentifier("createPlaylist", sender: self)
  91. }
  92. else
  93. {
  94. print(error?.userInfo)
  95. }
  96. }
  97. }
  98. else
  99. {
  100. print("You have already created this playlist")
  101. }
  102. })
  103. }
  104. else
  105. {
  106. print(error?.description)
  107. }
  108. }
  109. })
  110. alertController.addAction(okAction)
  111. presentViewController(alertController, animated: true, completion: nil)
  112.  
  113. }
  114.  
  115. func fetchNearbyPlaylists()
  116. {
  117. let query:PFQuery = PFQuery(className: "Playlists")
  118. query.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: userlatitude, longitude: userlongitude), withinMiles: 1000000000.0)
  119. query.orderByAscending("location")
  120. query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
  121. if ((error) == nil)
  122. {
  123. dispatch_async(dispatch_get_main_queue(), {
  124. self.playlists_location = objects!
  125. })
  126. }
  127. else
  128. {
  129. print(error?.userInfo)
  130. }
  131. }
  132. }
  133.  
  134. func fetchUserPlaylists()
  135. {
  136. let query: PFQuery = PFQuery(className: "Playlists")
  137. query.whereKey("createdbyuser", equalTo: (PFUser.currentUser()?.username)!)
  138. query.orderByDescending("updatedAt")
  139. query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
  140. if ((error) == nil)
  141. {
  142. dispatch_async(dispatch_get_main_queue(), {
  143. self.playlists_user = objects!
  144. })
  145. }
  146. else
  147. {
  148. print(error?.userInfo)
  149. }
  150. }
  151. }
  152.  
  153. func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  154. let userLocation: CLLocation = locations[0]
  155.  
  156. let latitude = userLocation.coordinate.latitude
  157. let longitude = userLocation.coordinate.longitude
  158. print(userLocation.coordinate)
  159. userlatitude = latitude
  160. userlongitude = longitude
  161. fetchNearbyPlaylists()
  162. fetchUserPlaylists()
  163.  
  164. self.tableView.reloadData()
  165. parameters["ll"] = String(latitude) + "," + String(longitude)
  166. }
  167. func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
  168. print(error.description)
  169. }
  170.  
  171. func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
  172. if status == .AuthorizedWhenInUse
  173. {
  174. //print("Authorized")
  175. }
  176. }
  177.  
  178. func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {
  179. if (!username.isEmpty || !password.isEmpty)
  180. {
  181. return true;
  182. }
  183. else
  184. {
  185. return false;
  186. }
  187. }
  188. func logInViewController(logInController: PFLogInViewController, didLogInUser user: PFUser) {
  189. self.dismissViewControllerAnimated(true, completion: nil)
  190. }
  191. func logInViewController(logInController: PFLogInViewController, didFailToLogInWithError error: NSError?) {
  192. print("failed to login")
  193. }
  194. func signUpViewController(signUpController: PFSignUpViewController, shouldBeginSignUp info: [String : String]) -> Bool {
  195. if let password = info["password"]
  196. {
  197. return password.utf16.count >= 8
  198. }
  199. else
  200. {
  201. return false
  202. }
  203. }
  204. func signUpViewController(signUpController: PFSignUpViewController, didSignUpUser user: PFUser) {
  205. self.dismissViewControllerAnimated(true, completion: nil)
  206. }
  207. func signUpViewController(signUpController: PFSignUpViewController, didFailToSignUpWithError error: NSError?) {
  208. print("failed to signup")
  209. }
  210. func signUpViewControllerDidCancelSignUp(signUpController: PFSignUpViewController) {
  211. print("signup canceled")
  212. }
  213.  
  214. // MARK: - Table view data source
  215. var storedOffsets = [Int: CGFloat]()
  216.  
  217. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  218. // #warning Incomplete implementation, return the number of sections
  219. return 1
  220. }
  221.  
  222. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  223. // #warning Incomplete implementation, return the number of rows
  224. return 3
  225. }
  226.  
  227. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  228. let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  229. return cell
  230. }
  231. override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  232. guard let tableViewCell = cell as? TableViewCell else{return}
  233. tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
  234. tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
  235. }
  236. override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath:NSIndexPath) {
  237.  
  238. guard let tableViewCell = cell as? TableViewCell else { return }
  239.  
  240. storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
  241. }
  242. }
  243.  
  244. extension TableViewController: UICollectionViewDataSource, UICollectionViewDelegate
  245. {
  246. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  247.  
  248. return 2
  249. }
  250. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  251.  
  252. let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
  253.  
  254.  
  255. if(collectionView.tag == 0 && self.playlists_location.count != 0)
  256. {
  257. let templist = self.playlists_location[indexPath.row] as! PFObject
  258. cell.label.text = templist["playlistName"] as? String
  259. }
  260.  
  261. return cell
  262. }
  263. func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  264. print(collectionView.tag)
  265. print(indexPath.item)
  266. }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement