From e9faaf69fd882e6d31763c1fe58d291c54a98482 Mon Sep 17 00:00:00 2001 From: Okechi Date: Wed, 3 Feb 2016 09:16:03 -0500 Subject: [PATCH] updated some storyboard elements for friend system, reworked friend request system due to trouble with CC in parse, friend system 95% done --- GetHip/AppDelegate.swift | 22 +++- GetHip/FriendDataSource.swift | 31 ++++- GetHip/FriendRequestViewController.swift | 148 ++++++++++++++++++++++- GetHip/FriendsListViewController.swift | 15 ++- GetHip/Main.storyboard | 48 ++++---- 5 files changed, 225 insertions(+), 39 deletions(-) diff --git a/GetHip/AppDelegate.swift b/GetHip/AppDelegate.swift index e4f46f8..13d81a4 100644 --- a/GetHip/AppDelegate.swift +++ b/GetHip/AppDelegate.swift @@ -8,15 +8,22 @@ import UIKit import CoreData +import CoreLocation @UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { +class AppDelegate: UIResponder, UIApplicationDelegate , CLLocationManagerDelegate{ var window: UIWindow? + // var locationStarted = false + // var locationManager: CLLocationManager = CLLocationManager() + // var app = UIApplication.sharedApplication() - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + //create new CLLocaationManager + /*var locationStarted = false + locationManager.delegate = self*/ + Parse.logLevel() //NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveWillSendURLRequestNotification:", name: PFNetworkWillSendURLRequestNotification, object: nil) @@ -25,8 +32,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Override point for customization after application launch. Parse.setApplicationId("OUFlGeqCzg03ZbstGcbskj7UNwp53Y0yad9Fi6I0", clientKey: "xaw0kbAFnBi3zLhT1uSnzrzju56m7CnQJ8hJHW1K") PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions); + /* let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) - print(paths[0]) + print(paths[0])*/ + return true } @@ -50,8 +59,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidEnterBackground(application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. + // locationManager.startUpdatingLocation() } func applicationWillEnterForeground(application: UIApplication) { @@ -68,7 +76,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Saves changes in the application's managed object context before the application terminates. self.saveContext() } - + + + // MARK: - Core Data stack lazy var applicationDocumentsDirectory: NSURL = { diff --git a/GetHip/FriendDataSource.swift b/GetHip/FriendDataSource.swift index 9fcd372..57ec2d5 100644 --- a/GetHip/FriendDataSource.swift +++ b/GetHip/FriendDataSource.swift @@ -17,10 +17,11 @@ class FriendDataSource{ var query = PFUser.query() var currentUser: String! = PFUser.currentUser()?.username - let predicate: NSPredicate = NSPredicate(format: "(((RequestStatus = %@) OR (RequestStatus = %@)) AND (username = %@))", argumentArray: ["accepted", "pending",currentUser]) - var friendsQuery: PFQuery = PFQuery(className: "FriendRequest", predicate: predicate) + //finds pending friend request from main FriendRequest table + let predicate: NSPredicate = NSPredicate(format: "((RequestStatus = %@) AND (username = %@))", argumentArray: ["pending",currentUser]) + var friendsPendingQuery: PFQuery = PFQuery(className: "FriendRequest", predicate: predicate) - friendsQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock { + friendsPendingQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock { (objects, error) -> Void in //print(error) @@ -50,6 +51,30 @@ class FriendDataSource{ } NSLog("%d", self.dataSource.count) + //dispatches to the main queue the task of getting users current friends from relational table in Parse + var friendsRelation: PFRelation! = PFUser.currentUser()?.relationForKey("FriendRequest") + var friendsQuery = friendsRelation.query().whereKey("RequestStatus", equalTo: "accepted") + dispatch_async(dispatch_get_main_queue(), { + + friendsQuery.includeKey("OtherUser").findObjectsInBackgroundWithBlock({ + (objects, error) -> Void in + + if (error == nil){ + for object in objects! { + + let userName = object.objectForKey("OtherUser")!.objectForKey("username") as! String + let requestStatus = object.objectForKey("RequestStatus")! as! String + + var newFriend: FriendData = FriendData(display: userName, status: requestStatus) + //print(userName) + self.dataSource.append(newFriend) + } + } + }) + + }) + + } NSNotificationCenter.defaultCenter().postNotificationName("refreshTableView", object: nil) diff --git a/GetHip/FriendRequestViewController.swift b/GetHip/FriendRequestViewController.swift index c3184b8..e706c75 100644 --- a/GetHip/FriendRequestViewController.swift +++ b/GetHip/FriendRequestViewController.swift @@ -8,19 +8,102 @@ import UIKit -class FriendRequestViewController: UIViewController { +class FriendRequestViewController: UIViewController{ + var searchedUsername: String! + var frnds: [String]! @IBOutlet var displayImage: UIImageView! @IBOutlet var foundName: UILabel! @IBOutlet var sendRequest: UIButton! - @IBOutlet var searchBar: UISearchBar! + @IBOutlet var searchBar: UITextField! + @IBAction func sendButtonClicked(sender: AnyObject){ + var query = PFQuery(className: "_User") + query.whereKey("username", equalTo: foundName.text!) + println(foundName.text!) + dispatch_async(dispatch_get_main_queue(),{ + query.getFirstObjectInBackgroundWithBlock({ + (object:PFObject?, error: NSError?) -> Void in + + //make friend request object for current user + var friendRequest:PFObject = PFObject(className: "FriendRequest") + friendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: (object!.objectId)!), forKey: "OtherUser") + friendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: PFUser.currentUser()?.objectId!), forKey: "FromUser") + friendRequest.setObject(PFUser.currentUser()?.username as String!, forKey: "username") + friendRequest.setObject(object!.objectForKey("username")!, forKey: "inRealtionTo") + friendRequest.setObject("Awaiting Response", forKey: "RequestStatus") + friendRequest.save() + + PFUser.currentUser()?.relationForKey("FriendRequest").addObject(friendRequest) + + //make friend request object for other user + var otherFriendRequest:PFObject = PFObject(className: "FriendRequest") + otherFriendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: (object!.objectId)!), forKey: "FromUser") + otherFriendRequest.setObject(PFObject(withoutDataWithClassName: "_User", objectId: PFUser.currentUser()?.objectId!), forKey: "OtherUser") + otherFriendRequest.setObject(object!.objectForKey("username")!, forKey: "username") + otherFriendRequest.setObject(PFUser.currentUser()?.username as String!, forKey: "inRealtionTo") + otherFriendRequest.setObject("pending", forKey: "RequestStatus") + otherFriendRequest.save() + + + + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + PFUser.currentUser()!.saveInBackgroundWithBlock({ + (succeeded, error) -> Void in + + if(succeeded){ + var alert = UIAlertController(title: "Request Sent!", message: "Your friend request was sent successfully!", preferredStyle: .Alert) + alert.addAction(UIAlertAction(title: "OK", style: .Default, handler:{(action: UIAlertAction!) in alert.dismissViewControllerAnimated(true, completion: nil)})) + + self.presentViewController(alert, animated: true, completion: nil) + + println(friendRequest.objectId!) + var params = NSMutableDictionary() + params.setObject(friendRequest.objectId! as String!, forKey: "friendRequest") + + //var params = NSMutableDictionary() + //params.s + //var param = ["friendRequest" : friendRequest.objectId!] + /* + PFCloud.callFunctionInBackground("addFriendToFriendRelation", withParameters: params as [NSObject : AnyObject]){ + (response, error) -> Void in + if(error == nil){ + println(response as! String) + }else{ + println(error) + } + + }*/ + /*PFCloud.callFunctionInBackground("hello", withParameters: nil){ + (response, error) -> Void in + println(response as! String) + }*/ + } + }) + }) + + }) + }) + + } + + @IBAction func buttonClicked(sender: AnyObject){ + searchBar.resignFirstResponder(); + + } + + func setData(friends: [String]){ + self.frnds = friends + } override func viewDidLoad() { super.viewDidLoad() + searchBar.delegate = self self.displayImage.layer.cornerRadius = self.displayImage.frame.size.width/2 self.sendRequest.enabled = false self.sendRequest.tintColor = UIColor.grayColor() + // Do any additional setup after loading the view. } @@ -41,3 +124,64 @@ class FriendRequestViewController: UIViewController { */ } + +extension FriendRequestViewController: UITextFieldDelegate{ + func textFieldDidBeginEditing(textField: UITextField) { + println("Textfield did begin editing method called") + } + + func textFieldDidEndEditing(textField: UITextField) { + self.searchedUsername = searchBar.text + var inFriendsList: Bool = false + + /*for name in self.frnds{ + if name == self.searchedUsername{ + inFriendsList = true + break + } + }*/ + if(inFriendsList == false){ + var query = PFQuery(className: "_User") + query.whereKey("username", equalTo: searchedUsername) + + dispatch_async(dispatch_get_main_queue(), {() -> Void in + query.getFirstObjectInBackgroundWithBlock({ + (object: PFObject?, error: NSError?) -> Void in + if(error == nil){ + self.foundName.text = object?.objectForKey("username") as? String + self.sendRequest.enabled = true + }else{ + self.foundName.text = "No Friend Found :(" + self.sendRequest.enabled = false + } + }) + }) + }else{ + let alert = UIAlertController(title: "Already Friends", message: "You are already friends with this user!", preferredStyle: .Alert) + self.presentViewController(alert, animated: true, completion: nil) + } + + } + + func textFieldShouldBeginEditing(textField: UITextField) -> Bool { + return true + } + + func textFieldShouldClear(textField: UITextField) -> Bool { + return true + } + + func textFieldShouldEndEditing(textField: UITextField) -> Bool { + return true + } + + func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { + return true + } + + func textFieldShouldReturn(textField: UITextField) -> Bool { + searchBar.resignFirstResponder() + return true + + } +} diff --git a/GetHip/FriendsListViewController.swift b/GetHip/FriendsListViewController.swift index 0464eb1..122c409 100644 --- a/GetHip/FriendsListViewController.swift +++ b/GetHip/FriendsListViewController.swift @@ -141,7 +141,20 @@ class FriendsListViewController: UIViewController, UITableViewDelegate, UITableV } if segue.identifier == "FriendRequestSegue" { - + /* var frndNames: [String] = [] + for i in 0...self.friends.count-1{ + var str:String! = self.friends[i].displayName as String! + frndNames.append(str) + } + + /*for name in self.friends{ + //bad instruction here, fix later + println(name.displayName as String!) + frndNames.append(name.displayName!) + }*/ + + let vc: FriendRequestViewController = (segue.destinationViewController as? FriendRequestViewController)! + vc.setData(frndNames)*/ } } diff --git a/GetHip/Main.storyboard b/GetHip/Main.storyboard index 0ad5f51..a432e3d 100644 --- a/GetHip/Main.storyboard +++ b/GetHip/Main.storyboard @@ -38,18 +38,14 @@ - - - - - - - @@ -61,6 +57,14 @@ + + + + + + + + @@ -68,21 +72,11 @@ - - + - - - - - - - - - @@ -185,32 +179,32 @@ - - + + - + - +